Make cURL use Bytes instead of Strings

This commit is contained in:
Joshua Granick
2015-09-21 16:46:16 -07:00
parent 90ef7cedea
commit d287d64af6
3 changed files with 45 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
package lime.net; package lime.net;
import haxe.io.Bytes;
import lime.app.Event; import lime.app.Event;
import lime.utils.ByteArray; import lime.utils.ByteArray;
@@ -36,7 +37,7 @@ class URLLoader {
#if lime_curl #if lime_curl
private var __curl:CURL; private var __curl:CURL;
private var __data:String; private var __data:ByteArray;
#end #end
public function new (request:URLRequest = null) { public function new (request:URLRequest = null) {
@@ -46,7 +47,7 @@ class URLLoader {
dataFormat = URLLoaderDataFormat.TEXT; dataFormat = URLLoaderDataFormat.TEXT;
#if lime_curl #if lime_curl
__data = ""; __data = new ByteArray ();
__curl = CURLEasy.init(); __curl = CURLEasy.init();
#end #end
@@ -265,7 +266,7 @@ class URLLoader {
for (p in Reflect.fields (data)) { for (p in Reflect.fields (data)) {
if (tmp.length != 0) tmp += "&"; if (tmp.length != 0) tmp += "&";
tmp += StringTools.urlEncode (p) + "=" + StringTools.urlEncode (Reflect.field (data, p)); tmp += StringTools.urlEncode (p) + "=" + StringTools.urlEncode (Std.string (Reflect.field (data, p)));
} }
@@ -291,7 +292,7 @@ class URLLoader {
var uri = prepareData(data); var uri = prepareData(data);
uri.position = 0; uri.position = 0;
__data = ""; __data = new ByteArray ();
bytesLoaded = 0; bytesLoaded = 0;
bytesTotal = 0; bytesTotal = 0;
@@ -303,6 +304,7 @@ class URLLoader {
CURLEasy.setopt(__curl, NOBODY, true); CURLEasy.setopt(__curl, NOBODY, true);
case GET: case GET:
CURLEasy.setopt(__curl, HTTPGET, true); CURLEasy.setopt(__curl, HTTPGET, true);
if (uri.length > 0) CURLEasy.setopt (__curl, URL, url + "?" + uri.readUTFBytes (uri.length));
case POST: case POST:
CURLEasy.setopt(__curl, POST, true); CURLEasy.setopt(__curl, POST, true);
CURLEasy.setopt(__curl, READFUNCTION, readFunction.bind(_, uri)); CURLEasy.setopt(__curl, READFUNCTION, readFunction.bind(_, uri));
@@ -350,7 +352,9 @@ class URLLoader {
default: this.data = __data.asString(); default: this.data = __data.asString();
} }
*/ */
this.data = __data; //this.data = __data;
__data.position = 0;
this.data = __data.readUTFBytes (__data.length);
onHTTPStatus.dispatch (this, Std.parseInt(responseCode)); onHTTPStatus.dispatch (this, Std.parseInt(responseCode));
onComplete.dispatch (this); onComplete.dispatch (this);
} else { } else {
@@ -359,14 +363,14 @@ class URLLoader {
} }
private function writeFunction (output:String, size:Int, nmemb:Int):Int { private function writeFunction (output:Bytes, size:Int, nmemb:Int):Int {
__data += output; __data.readBytes (ByteArray.fromBytes (output));
return size * nmemb; return size * nmemb;
} }
private function headerFunction (output:String, size:Int, nmemb:Int):Int { private function headerFunction (output:Bytes, size:Int, nmemb:Int):Int {
// TODO // TODO
return size * nmemb; return size * nmemb;
@@ -385,8 +389,8 @@ class URLLoader {
return 0; return 0;
} }
private function readFunction(max:Int, input:ByteArray):String { private function readFunction(max:Int, input:ByteArray):Bytes {
return input == null ? "" : input.readUTFBytes(Std.int(Math.min(max, input.length - input.position))); return input;
} }
#end #end

View File

@@ -1,6 +1,7 @@
package lime.net.curl; package lime.net.curl;
import haxe.io.Bytes;
import lime.net.curl.CURL; import lime.net.curl.CURL;
#if !macro #if !macro
@@ -120,6 +121,11 @@ class CURLEasy {
public static function setopt (handle:CURL, option:CURLOption, parameter:Dynamic):CURLCode { public static function setopt (handle:CURL, option:CURLOption, parameter:Dynamic):CURLCode {
#if ((cpp || neko || nodejs) && lime_curl && !macro) #if ((cpp || neko || nodejs) && lime_curl && !macro)
if (option == CURLOption.WRITEFUNCTION || option == CURLOption.HEADERFUNCTION) {
parameter = __writeCallback.bind (parameter);
}
return cast lime_curl_easy_setopt (handle, cast (option, Int), parameter); return cast lime_curl_easy_setopt (handle, cast (option, Int), parameter);
#else #else
return cast 0; return cast 0;
@@ -150,6 +156,21 @@ class CURLEasy {
} }
private static function __writeCallback (callback:Dynamic, output:Dynamic, size:Int, nmemb:Int):Int {
var bytes:Bytes = null;
if (output != null) {
bytes = @:privateAccess new Bytes (output.length, output.b);
}
return callback (bytes, size, nmemb);
}
#if ((cpp || neko || nodejs) && lime_curl && !macro) #if ((cpp || neko || nodejs) && lime_curl && !macro)
@:cffi private static function lime_curl_easy_cleanup (handle:Float):Void; @:cffi private static function lime_curl_easy_cleanup (handle:Float):Void;
@:cffi private static function lime_curl_easy_duphandle (handle:Float):Float; @:cffi private static function lime_curl_easy_duphandle (handle:Float):Float;

View File

@@ -1,5 +1,6 @@
#include <curl/curl.h> #include <curl/curl.h>
#include <hx/CFFIPrime.h> #include <hx/CFFIPrime.h>
#include <utils/Bytes.h>
#include <string.h> #include <string.h>
@@ -173,8 +174,10 @@ namespace lime {
} }
value str = alloc_string_len ((const char*)ptr, size * nmemb); Bytes bytes = Bytes (size * nmemb);
return val_int (val_call3 (callback->get (), str, alloc_int (size), alloc_int (nmemb))); memcpy (bytes.Data (), ptr, size * nmemb);
return val_int (val_call3 (callback->get (), bytes.Value (), alloc_int (size), alloc_int (nmemb)));
} }
@@ -183,15 +186,14 @@ namespace lime {
AutoGCRoot* callback = (AutoGCRoot*)userp; AutoGCRoot* callback = (AutoGCRoot*)userp;
size_t bytes = size * nmemb; size_t length = size * nmemb;
const char *input = val_string (val_call1 (callback->get (), alloc_int (bytes))); Bytes bytes = Bytes (val_call1 (callback->get (), alloc_int (length)));
size_t length = strlen (input);
if (length <= bytes) bytes = length; if (bytes.Length () <= length) length = bytes.Length ();
memcpy (buffer, input, bytes); memcpy (buffer, bytes.Data (), length);
return bytes; return length;
} }