|
@@ -22,6 +22,9 @@
|
|
|
|
|
|
package sys;
|
|
package sys;
|
|
|
|
|
|
|
|
+import haxe.io.BytesOutput;
|
|
|
|
+import haxe.io.Bytes;
|
|
|
|
+import haxe.io.Input;
|
|
import sys.net.Host;
|
|
import sys.net.Host;
|
|
import sys.net.Socket;
|
|
import sys.net.Socket;
|
|
|
|
|
|
@@ -55,23 +58,17 @@ class Http extends haxe.http.HttpBase {
|
|
var old = onError;
|
|
var old = onError;
|
|
var err = false;
|
|
var err = false;
|
|
onError = function(e) {
|
|
onError = function(e) {
|
|
- #if neko
|
|
|
|
- responseData = neko.Lib.stringReference(output.getBytes());
|
|
|
|
- #else
|
|
|
|
- responseData = output.getBytes().toString();
|
|
|
|
- #end
|
|
|
|
|
|
+ responseBytes = output.getBytes();
|
|
err = true;
|
|
err = true;
|
|
// Resetting back onError before calling it allows for a second "retry" request to be sent without onError being wrapped twice
|
|
// Resetting back onError before calling it allows for a second "retry" request to be sent without onError being wrapped twice
|
|
onError = old;
|
|
onError = old;
|
|
onError(e);
|
|
onError(e);
|
|
}
|
|
}
|
|
|
|
+ post = post || postBytes != null || postData != null;
|
|
customRequest(post, output);
|
|
customRequest(post, output);
|
|
- if (!err)
|
|
|
|
- #if neko
|
|
|
|
- onData(responseData = neko.Lib.stringReference(output.getBytes()));
|
|
|
|
- #else
|
|
|
|
- onData(responseData = output.getBytes().toString());
|
|
|
|
- #end
|
|
|
|
|
|
+ if (!err) {
|
|
|
|
+ success(output.getBytes());
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
@:noCompletion
|
|
@:noCompletion
|
|
@@ -91,7 +88,8 @@ class Http extends haxe.http.HttpBase {
|
|
}
|
|
}
|
|
|
|
|
|
public function customRequest(post:Bool, api:haxe.io.Output, ?sock:sys.net.Socket, ?method:String) {
|
|
public function customRequest(post:Bool, api:haxe.io.Output, ?sock:sys.net.Socket, ?method:String) {
|
|
- this.responseData = null;
|
|
|
|
|
|
+ this.responseAsString = null;
|
|
|
|
+ this.responseBytes = null;
|
|
var url_regexp = ~/^(https?:\/\/)?([a-zA-Z\.0-9_-]+)(:[0-9]+)?(.*)$/;
|
|
var url_regexp = ~/^(https?:\/\/)?([a-zA-Z\.0-9_-]+)(:[0-9]+)?(.*)$/;
|
|
if (!url_regexp.match(url)) {
|
|
if (!url_regexp.match(url)) {
|
|
onError("Invalid URL");
|
|
onError("Invalid URL");
|
|
@@ -175,72 +173,76 @@ class Http extends haxe.http.HttpBase {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- var b = new StringBuf();
|
|
|
|
|
|
+ var b = new BytesOutput();
|
|
if (method != null) {
|
|
if (method != null) {
|
|
- b.add(method);
|
|
|
|
- b.add(" ");
|
|
|
|
|
|
+ b.writeString(method);
|
|
|
|
+ b.writeString(" ");
|
|
} else if (post)
|
|
} else if (post)
|
|
- b.add("POST ");
|
|
|
|
|
|
+ b.writeString("POST ");
|
|
else
|
|
else
|
|
- b.add("GET ");
|
|
|
|
|
|
+ b.writeString("GET ");
|
|
|
|
|
|
if (Http.PROXY != null) {
|
|
if (Http.PROXY != null) {
|
|
- b.add("http://");
|
|
|
|
- b.add(host);
|
|
|
|
|
|
+ b.writeString("http://");
|
|
|
|
+ b.writeString(host);
|
|
if (port != 80) {
|
|
if (port != 80) {
|
|
- b.add(":");
|
|
|
|
- b.add(port);
|
|
|
|
|
|
+ b.writeString(":");
|
|
|
|
+ b.writeString('$port');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- b.add(request);
|
|
|
|
|
|
+ b.writeString(request);
|
|
|
|
|
|
if (!post && uri != null) {
|
|
if (!post && uri != null) {
|
|
if (request.indexOf("?", 0) >= 0)
|
|
if (request.indexOf("?", 0) >= 0)
|
|
- b.add("&");
|
|
|
|
|
|
+ b.writeString("&");
|
|
else
|
|
else
|
|
- b.add("?");
|
|
|
|
- b.add(uri);
|
|
|
|
|
|
+ b.writeString("?");
|
|
|
|
+ b.writeString(uri);
|
|
|
|
+ }
|
|
|
|
+ b.writeString(" HTTP/1.1\r\nHost: " + host + "\r\n");
|
|
|
|
+ if (postData != null) {
|
|
|
|
+ postBytes = Bytes.ofString(postData);
|
|
|
|
+ postData = null;
|
|
}
|
|
}
|
|
- b.add(" HTTP/1.1\r\nHost: " + host + "\r\n");
|
|
|
|
- if (postData != null)
|
|
|
|
- b.add("Content-Length: " + postData.length + "\r\n");
|
|
|
|
|
|
+ if (postBytes != null)
|
|
|
|
+ b.writeString("Content-Length: " + postBytes.length + "\r\n");
|
|
else if (post && uri != null) {
|
|
else if (post && uri != null) {
|
|
if (multipart || !Lambda.exists(headers, function(h) return h.name == "Content-Type")) {
|
|
if (multipart || !Lambda.exists(headers, function(h) return h.name == "Content-Type")) {
|
|
- b.add("Content-Type: ");
|
|
|
|
|
|
+ b.writeString("Content-Type: ");
|
|
if (multipart) {
|
|
if (multipart) {
|
|
- b.add("multipart/form-data");
|
|
|
|
- b.add("; boundary=");
|
|
|
|
- b.add(boundary);
|
|
|
|
|
|
+ b.writeString("multipart/form-data");
|
|
|
|
+ b.writeString("; boundary=");
|
|
|
|
+ b.writeString(boundary);
|
|
} else
|
|
} else
|
|
- b.add("application/x-www-form-urlencoded");
|
|
|
|
- b.add("\r\n");
|
|
|
|
|
|
+ b.writeString("application/x-www-form-urlencoded");
|
|
|
|
+ b.writeString("\r\n");
|
|
}
|
|
}
|
|
if (multipart)
|
|
if (multipart)
|
|
- b.add("Content-Length: " + (uri.length + file.size + boundary.length + 6) + "\r\n");
|
|
|
|
|
|
+ b.writeString("Content-Length: " + (uri.length + file.size + boundary.length + 6) + "\r\n");
|
|
else
|
|
else
|
|
- b.add("Content-Length: " + uri.length + "\r\n");
|
|
|
|
|
|
+ b.writeString("Content-Length: " + uri.length + "\r\n");
|
|
}
|
|
}
|
|
- b.add("Connection: close\r\n");
|
|
|
|
|
|
+ b.writeString("Connection: close\r\n");
|
|
for (h in headers) {
|
|
for (h in headers) {
|
|
- b.add(h.name);
|
|
|
|
- b.add(": ");
|
|
|
|
- b.add(h.value);
|
|
|
|
- b.add("\r\n");
|
|
|
|
|
|
+ b.writeString(h.name);
|
|
|
|
+ b.writeString(": ");
|
|
|
|
+ b.writeString(h.value);
|
|
|
|
+ b.writeString("\r\n");
|
|
}
|
|
}
|
|
- b.add("\r\n");
|
|
|
|
- if (postData != null)
|
|
|
|
- b.add(postData);
|
|
|
|
|
|
+ b.writeString("\r\n");
|
|
|
|
+ if (postBytes != null)
|
|
|
|
+ b.writeFullBytes(postBytes, 0, postBytes.length);
|
|
else if (post && uri != null)
|
|
else if (post && uri != null)
|
|
- b.add(uri);
|
|
|
|
|
|
+ b.writeString(uri);
|
|
try {
|
|
try {
|
|
if (Http.PROXY != null)
|
|
if (Http.PROXY != null)
|
|
sock.connect(new Host(Http.PROXY.host), Http.PROXY.port);
|
|
sock.connect(new Host(Http.PROXY.host), Http.PROXY.port);
|
|
else
|
|
else
|
|
sock.connect(new Host(host), port);
|
|
sock.connect(new Host(host), port);
|
|
if (multipart)
|
|
if (multipart)
|
|
- writeBody(b.toString(),file.io,file.size,boundary,sock);
|
|
|
|
|
|
+ writeBody(b,file.io,file.size,boundary,sock)
|
|
else
|
|
else
|
|
- writeBody(b.toString(),null,0,null,sock);
|
|
|
|
|
|
+ writeBody(b,null,0,null,sock);
|
|
readHttpResponse(api, sock);
|
|
readHttpResponse(api, sock);
|
|
sock.close();
|
|
sock.close();
|
|
} catch (e:Dynamic) {
|
|
} catch (e:Dynamic) {
|
|
@@ -251,10 +253,12 @@ class Http extends haxe.http.HttpBase {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- function writeBody(body:String, fileInput:haxe.io.Input, fileSize:Int, boundary:String, sock:sys.net.Socket) {
|
|
|
|
- if (body!=null && body.length>0)
|
|
|
|
- sock.write(body);
|
|
|
|
- if (boundary!=null) {
|
|
|
|
|
|
+ function writeBody(body:Null<BytesOutput>, fileInput:Null<Input>, fileSize:Int, boundary:Null<String>, sock:Socket) {
|
|
|
|
+ if (body != null) {
|
|
|
|
+ var bytes = body.getBytes();
|
|
|
|
+ sock.output.writeFullBytes(bytes, 0, bytes.length);
|
|
|
|
+ }
|
|
|
|
+ if (boundary != null) {
|
|
var bufsize = 4096;
|
|
var bufsize = 4096;
|
|
var buf = haxe.io.Bytes.alloc(bufsize);
|
|
var buf = haxe.io.Bytes.alloc(bufsize);
|
|
while (fileSize > 0) {
|
|
while (fileSize > 0) {
|
|
@@ -267,10 +271,10 @@ class Http extends haxe.http.HttpBase {
|
|
sock.output.writeFullBytes(buf, 0, len);
|
|
sock.output.writeFullBytes(buf, 0, len);
|
|
fileSize -= len;
|
|
fileSize -= len;
|
|
}
|
|
}
|
|
- sock.write("\r\n");
|
|
|
|
- sock.write("--");
|
|
|
|
- sock.write(boundary);
|
|
|
|
- sock.write("--");
|
|
|
|
|
|
+ sock.output.writeString("\r\n");
|
|
|
|
+ sock.output.writeString("--");
|
|
|
|
+ sock.output.writeString(boundary);
|
|
|
|
+ sock.output.writeString("--");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|