From 4ab3e880ca7535e20b21e5cdfed279fc562a0ebd Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Tue, 8 Nov 2016 11:46:56 -0800 Subject: [PATCH] First pass at new HTTPRequest API --- lime/Assets.hx | 10 +- lime/_backend/html5/HTML5HTTPRequest.hx | 184 ++++++++ lime/_backend/native/NativeHTTPRequest.hx | 248 +++++++++++ lime/app/Event.hx | 5 + lime/app/Future.hx | 40 +- lime/app/Preloader.hx | 7 +- lime/audio/AudioBuffer.hx | 2 +- lime/net/HTTPRequest.hx | 505 +++++++++++++++------- lime/net/HTTPRequestHeader.hx | 19 + lime/net/HTTPRequestMethod.hx | 14 + lime/net/NetConnection.hx | 0 lime/net/NetConnectionManager.hx | 0 lime/net/URIParser.hx | 2 +- lime/system/BackgroundWorker.hx | 4 + lime/utils/Bytes.hx | 7 + templates/haxe/DefaultAssetLibrary.hx | 21 +- 16 files changed, 891 insertions(+), 177 deletions(-) create mode 100644 lime/_backend/html5/HTML5HTTPRequest.hx create mode 100644 lime/_backend/native/NativeHTTPRequest.hx create mode 100644 lime/net/HTTPRequestHeader.hx create mode 100644 lime/net/HTTPRequestMethod.hx delete mode 100644 lime/net/NetConnection.hx delete mode 100644 lime/net/NetConnectionManager.hx diff --git a/lime/Assets.hx b/lime/Assets.hx index c76ee7ee1..9a5ed2f33 100644 --- a/lime/Assets.hx +++ b/lime/Assets.hx @@ -938,28 +938,28 @@ class AssetLibrary { public function loadAudioBuffer (id:String):Future { - return new Future (function () return getAudioBuffer (id)); + return new Future (function () return getAudioBuffer (id), true); } public function loadBytes (id:String):Future { - return new Future (function () return getBytes (id)); + return new Future (function () return getBytes (id), true); } public function loadFont (id:String):Future { - return new Future (function () return getFont (id)); + return new Future (function () return getFont (id), true); } public function loadImage (id:String):Future { - return new Future (function () return getImage (id)); + return new Future (function () return getImage (id), true); } @@ -980,7 +980,7 @@ class AssetLibrary { } - }); + }, true); }); diff --git a/lime/_backend/html5/HTML5HTTPRequest.hx b/lime/_backend/html5/HTML5HTTPRequest.hx new file mode 100644 index 000000000..e2fb04c51 --- /dev/null +++ b/lime/_backend/html5/HTML5HTTPRequest.hx @@ -0,0 +1,184 @@ +package lime._backend.html5; + + +import js.html.Event; +import js.html.ProgressEvent; +import js.html.XMLHttpRequest; +import haxe.io.Bytes; +import lime.app.Future; +import lime.app.Promise; +import lime.net.HTTPRequest; + + +class HTML5HTTPRequest { + + + private var binary:Bool; + private var parent:IHTTPRequest; + private var request:XMLHttpRequest; + + + public function new () { + + + + } + + + public function cancel ():Void { + + if (request != null) { + + request.abort (); + + } + + } + + + public function init (parent:IHTTPRequest):Void { + + this.parent = parent; + + } + + + private function load (uri:String, progress:Dynamic, readyStateChange:Dynamic):Void { + + request = new XMLHttpRequest (); + request.addEventListener ("progress", progress, false); + request.onreadystatechange = readyStateChange; + + if (parent.timeout > 0) { + + request.timeout = parent.timeout; + + } + + var query = ""; + + for (key in parent.formData.keys ()) { + + if (query.length > 0) query += "&"; + query += StringTools.urlEncode (key) + "=" + StringTools.urlEncode (Std.string (parent.formData.get (key))); + + } + + if (parent.method == GET) { + + if (uri.indexOf ("?") > -1) { + + uri += "&" + query; + + } else { + + uri += "?" + query; + + } + + query = ""; + + } + + request.open (Std.string (parent.method), uri, true); + + if (binary) { + + request.responseType = ARRAYBUFFER; + + } + + for (header in parent.headers) { + + request.setRequestHeader (header.name, header.value); + + } + + request.send (query); + + } + + + public function loadData (uri:String):Future { + + var promise = new Promise (); + + var progress = function (event) { + + promise.progress (event.loaded / event.total); + + } + + var readyStateChange = function (event) { + + if (request.readyState != 4) return; + + if (request.status != null && request.status >= 200 && request.status <= 400) { + + var bytes; + + if (request.responseType == NONE) { + + bytes = Bytes.ofString (request.responseText); + + } + + bytes = Bytes.ofData (request.response); + + promise.complete (bytes); + + } else { + + promise.error (request.status); + + } + + request = null; + + } + + binary = true; + load (uri, progress, readyStateChange); + + return promise.future; + + } + + + public function loadText (uri:String):Future { + + var promise = new Promise (); + + var progress = function (event) { + + promise.progress (event.loaded / event.total); + + } + + var readyStateChange = function (event) { + + if (request.readyState != 4) return; + + if (request.status != null && request.status >= 200 && request.status <= 400) { + + promise.complete (request.responseText); + + } else { + + promise.error (request.status); + + } + + request = null; + + } + + binary = false; + load (uri, progress, readyStateChange); + + return promise.future; + + } + + +} \ No newline at end of file diff --git a/lime/_backend/native/NativeHTTPRequest.hx b/lime/_backend/native/NativeHTTPRequest.hx new file mode 100644 index 000000000..f928b9164 --- /dev/null +++ b/lime/_backend/native/NativeHTTPRequest.hx @@ -0,0 +1,248 @@ +package lime._backend.native; + + +import haxe.io.Bytes; +import haxe.Timer; +import lime.app.Future; +import lime.app.Promise; +import lime.net.curl.CURLCode; +import lime.net.curl.CURLEasy; +import lime.net.curl.CURL; +import lime.net.HTTPRequest; +import lime.net.HTTPRequestMethod; +import lime.system.BackgroundWorker; + + +class NativeHTTPRequest { + + + private var bytes:Bytes; + private var bytesLoaded:Int; + private var bytesTotal:Int; + private var curl:CURL; + private var parent:IHTTPRequest; + private var promise:Promise; + + + public function new () { + + curl = 0; + promise = new Promise (); + + } + + + public function cancel ():Void { + + if (curl != 0) { + + CURLEasy.reset (curl); + CURLEasy.perform (curl); + + } + + } + + + public function init (parent:IHTTPRequest):Void { + + this.parent = parent; + + } + + + public function loadData (uri:String):Future { + + if (uri.indexOf ("http://") == -1 && uri.indexOf ("https://") == -1) { + + loadFile (uri); + + } else { + + loadURL (uri); + + } + + return promise.future; + + } + + + private function loadFile (path:String):Void { + + var worker = new BackgroundWorker (); + worker.doWork.add (function (_) { + + var index = path.indexOf ("?"); + + if (index > -1) { + + path = path.substring (0, index); + + } + + bytes = lime.utils.Bytes.readFile (path); + promise.complete (bytes); + + }); + worker.run (); + + } + + + public function loadText (uri:String):Future { + + var promise = new Promise (); + var future = loadData (uri); + + future.onProgress (promise.progress); + future.onError (promise.error); + + future.onComplete (function (bytes) { + + if (bytes == null) { + + promise.complete (null); + + } else { + + promise.complete (bytes.getString (0, bytes.length)); + + } + + }); + + return promise.future; + + } + + + private function loadURL (url:String):Void { + + bytes = Bytes.alloc (0); + + bytesLoaded = 0; + bytesTotal = 0; + + if (curl == 0) { + + curl = CURLEasy.init (); + + } else { + + CURLEasy.reset (curl); + + } + + CURLEasy.setopt (curl, URL, url); + CURLEasy.setopt (curl, HTTPGET, parent.method == HTTPRequestMethod.GET); + + CURLEasy.setopt (curl, FOLLOWLOCATION, true); + CURLEasy.setopt (curl, AUTOREFERER, true); + + var headers = []; + headers.push ("Expect: "); + + for (header in cast (parent.headers, Array)) { + + headers.push ('${header.name}: ${header.value}'); + + } + + CURLEasy.setopt (curl, HTTPHEADER, headers); + + CURLEasy.setopt (curl, PROGRESSFUNCTION, curl_onProgress); + CURLEasy.setopt (curl, WRITEFUNCTION, curl_onWrite); + + CURLEasy.setopt (curl, SSL_VERIFYPEER, false); + CURLEasy.setopt (curl, SSL_VERIFYHOST, 0); + CURLEasy.setopt (curl, USERAGENT, "libcurl-agent/1.0"); + + //CURLEasy.setopt (curl, CONNECTTIMEOUT, 30); + CURLEasy.setopt (curl, NOSIGNAL, true); + + CURLEasy.setopt (curl, TRANSFERTEXT, 0); + + var worker = new BackgroundWorker (); + worker.doWork.add (function (_) { + + var result = CURLEasy.perform (curl); + + worker.sendComplete (result); + + }); + worker.onComplete.add (function (result) { + + var responseCode = CURLEasy.getinfo (curl, RESPONSE_CODE); + + if (result == CURLCode.OK) { + + promise.complete (bytes); + + } else { + + promise.error (result); + + } + + }); + worker.run (); + + if (parent.timeout > 0) { + + Timer.delay (function () { + + if (!worker.completed) { + + worker.cancel (); + cancel (); + + promise.error (CURLCode.OPERATION_TIMEDOUT); + + } + + }, parent.timeout); + + } + + } + + + + + // Event Handlers + + + + + private function curl_onProgress (dltotal:Float, dlnow:Float, uptotal:Float, upnow:Float):Int { + + if (upnow > bytesLoaded || dlnow > bytesLoaded || uptotal > bytesTotal || dltotal > bytesTotal) { + + if (upnow > bytesLoaded) bytesLoaded = Std.int (upnow); + if (dlnow > bytesLoaded) bytesLoaded = Std.int (dlnow); + if (uptotal > bytesTotal) bytesTotal = Std.int (uptotal); + if (dltotal > bytesTotal) bytesTotal = Std.int (dltotal); + + promise.progress (bytesLoaded / bytesTotal); + + } + + return 0; + + } + + + private function curl_onWrite (output:Bytes, size:Int, nmemb:Int):Int { + + var cacheBytes = bytes; + bytes = Bytes.alloc (bytes.length + output.length); + bytes.blit (0, cacheBytes, 0, cacheBytes.length); + bytes.blit (cacheBytes.length, output, 0, output.length); + + return size * nmemb; + + } + + +} \ No newline at end of file diff --git a/lime/app/Event.hx b/lime/app/Event.hx index c52ba5856..be7b4c0ec 100644 --- a/lime/app/Event.hx +++ b/lime/app/Event.hx @@ -71,7 +71,12 @@ class Event { typeArgs = args; typeResult = result; + + case TInst (localType, _): + Context.fatalError ("Invalid number of type parameters for " + localType.toString (), Context.currentPos ()); + return null; + default: throw false; diff --git a/lime/app/Future.hx b/lime/app/Future.hx index 245735501..72c271969 100644 --- a/lime/app/Future.hx +++ b/lime/app/Future.hx @@ -21,14 +21,24 @@ import lime.utils.Log; private var __progressListeners:ArrayVoid>; - public function new (work:Void->T = null) { + public function new (work:Void->T = null, async:Bool = false) { if (work != null) { - var promise = new Promise (); - promise.future = this; - - FutureWork.queue ({ promise: promise, work: work }); + if (async) { + + var promise = new Promise (); + promise.future = this; + + FutureWork.queue ({ promise: promise, work: work }); + + } else { + + trace (work); + value = work (); + isComplete = true; + + } } @@ -204,6 +214,26 @@ import lime.utils.Log; } + public static function withError (error:Dynamic):Future { + + var future = new Future (); + future.isError = true; + future.error = error; + return future; + + } + + + public static function withValue (value:T):Future { + + var future = new Future (); + future.isComplete = true; + future.value = value; + return future; + + } + + } diff --git a/lime/app/Preloader.hx b/lime/app/Preloader.hx index 6643479d3..eb305d9e9 100644 --- a/lime/app/Preloader.hx +++ b/lime/app/Preloader.hx @@ -1,6 +1,7 @@ package lime.app; +import haxe.io.Bytes; import haxe.io.Path; import lime.app.Event; import lime.Assets; @@ -29,7 +30,7 @@ class Preloader #if flash extends Sprite #end { #if (js && html5) public static var audioBuffers = new Map (); public static var images = new Map (); - public static var loaders = new Map (); + public static var loaders = new Map> (); private var loaded = 0; private var total = 0; #end @@ -94,7 +95,7 @@ class Preloader #if flash extends Sprite #end { if (!loaders.exists (url)) { - var loader = new HTTPRequest (); + var loader = new HTTPRequest (); loaders.set (url, loader); total++; @@ -104,7 +105,7 @@ class Preloader #if flash extends Sprite #end { if (!loaders.exists (url)) { - var loader = new HTTPRequest (); + var loader = new HTTPRequest (); loaders.set (url, loader); total++; diff --git a/lime/audio/AudioBuffer.hx b/lime/audio/AudioBuffer.hx index ba990041b..ff3e97a5f 100644 --- a/lime/audio/AudioBuffer.hx +++ b/lime/audio/AudioBuffer.hx @@ -374,7 +374,7 @@ class AudioBuffer { #else - promise.completeWith (new Future (function () return fromFiles (paths))); + promise.completeWith (new Future (function () return fromFiles (paths), true)); #end diff --git a/lime/net/HTTPRequest.hx b/lime/net/HTTPRequest.hx index 479de81fc..1f72beff2 100644 --- a/lime/net/HTTPRequest.hx +++ b/lime/net/HTTPRequest.hx @@ -1,192 +1,397 @@ -package lime.net; +package lime.net; #if !macro -import lime.app.Future; -import lime.app.Promise; -import lime.net.curl.CURLCode; -import lime.net.curl.CURLEasy; -import lime.system.BackgroundWorker; -import lime.system.CFFI; -import lime.utils.Bytes; - -#if (js && html5) -import js.html.XMLHttpRequest; +#if (!macro && !display) +@:genericBuild(lime.net.HTTPRequest.build()) #end +class HTTPRequest implements IHTTPRequest { + + + public var contentType:String; + public var data:haxe.io.Bytes; + public var enableResponseHeaders:Bool; + public var followRedirects:Bool; + public var formData:Map; + public var headers:Array; + public var method:HTTPRequestMethod; + public var responseData:T; + public var responseHeaders:Array; + public var responseStatus:Int; + public var timeout:Int; + public var uri:String; + public var userAgent:String; + + #if flash + private var backend = new lime._backend.flash.FlashHTTPRequest (); + #elseif (js && html5) + private var backend = new lime._backend.html5.HTML5HTTPRequest (); + #else + private var backend = new lime._backend.native.NativeHTTPRequest (); + #end + + + public function new (uri:String = null) { + + this.uri = uri; + + formData = new Map (); + headers = []; + method = GET; + timeout = 30000; + + backend.init (this); + + } + + + public function cancel ():Void { + + backend.cancel (); + + } + + + public function load (uri:String = null):lime.app.Future { + + return null; + + } + + +} + + +@:dox(hide) @:noCompletion interface IHTTPRequest { + + public var contentType:String; + public var data:haxe.io.Bytes; + public var enableResponseHeaders:Bool; + public var followRedirects:Bool; + public var formData:Map; + public var headers:Array; + public var method:HTTPRequestMethod; + //public var responseData:T; + public var responseHeaders:Array; + public var responseStatus:Int; + public var timeout:Int; + public var uri:String; + public var userAgent:String; + +} + + +#else + + +import haxe.macro.Context; +import haxe.macro.Expr; +import haxe.macro.Type; +using haxe.macro.Tools; + + class HTTPRequest { - public var bytes:Bytes; - - private var bytesLoaded:Int; - private var bytesTotal:Int; - private var promise:Promise; - - - public function new () { + private static function build () { - promise = new Promise (); + var paramType; + var type:BaseType, typeArgs; + var stringAbstract = false; + var bytesAbstract = false; - } - - - public function load (url:String):Future { - - bytesLoaded = 0; - bytesTotal = 0; - - #if flash - - - - #elseif (js && html5) - - var request = new XMLHttpRequest (); - request.addEventListener ("progress", request_onProgress, false); - request.onreadystatechange = function () { + switch (Context.follow (Context.getLocalType ())) { - if (request.readyState != 4) return; + case TInst (localType, [ t ]): + + paramType = t; + + switch (t) { + + case TInst (t, args): + + type = t.get (); + typeArgs = args; + + case TAbstract (t, args): + + type = t.get (); + typeArgs = args; + + stringAbstract = isStringAbstract (t.get ()); + if (!stringAbstract) bytesAbstract = isBytesAbstract (t.get ()); + + case TMono (_): + + Context.fatalError ("Invalid number of type parameters for " + localType.toString (), Context.currentPos ()); + return null; + + case TDynamic (_): + + switch (Context.getType ("haxe.io.Bytes")) { + + case TInst (t, args): + + type = t.get (); + typeArgs = args; + + default: + + throw false; + + } + + default: + + throw false; + + } + + default: + + throw false; - if (request.status != null && request.status >= 200 && request.status <= 400) { + } + + var typeString = type.module; + + if (type.name != type.module && !StringTools.endsWith (type.module, "." + type.name)) { + + typeString += "." + type.name; + + } + + var typeParamString = typeString; + + if (typeArgs.length > 0) { + + typeParamString += "<"; + + for (i in 0...typeArgs.length) { - bytes = Bytes.ofData (request.response); - promise.complete (bytes); - - } else { - - promise.error (request.status); + if (i > 0) typeParamString += ","; + typeParamString += typeArgs[i].toString (); } - }; - - request.open ("GET", url, true); - request.responseType = ARRAYBUFFER; - request.send (""); - - #else - - if (url.indexOf ("http://") == -1 && url.indexOf ("https://") == -1) { - - var worker = new BackgroundWorker (); - worker.doWork.add (function (_) { - - var path = url; - var index = path.indexOf ("?"); - - if (index > -1) { - - path = path.substring (0, index); - - } - - var bytes = Bytes.readFile (path); - promise.complete (bytes); - - }); - worker.run (); - - } else { - - bytes = Bytes.alloc (0); - - bytesLoaded = 0; - bytesTotal = 0; - - var curl = CURLEasy.init (); - - CURLEasy.setopt (curl, URL, url); - CURLEasy.setopt (curl, HTTPGET, true); - - CURLEasy.setopt (curl, FOLLOWLOCATION, true); - CURLEasy.setopt (curl, AUTOREFERER, true); - CURLEasy.setopt (curl, HTTPHEADER, [ "Expect: " ]); - - CURLEasy.setopt (curl, PROGRESSFUNCTION, curl_onProgress); - CURLEasy.setopt (curl, WRITEFUNCTION, curl_onWrite); - - CURLEasy.setopt (curl, SSL_VERIFYPEER, false); - CURLEasy.setopt (curl, SSL_VERIFYHOST, 0); - CURLEasy.setopt (curl, USERAGENT, "libcurl-agent/1.0"); - CURLEasy.setopt (curl, CONNECTTIMEOUT, 30); - CURLEasy.setopt (curl, TRANSFERTEXT, 0); - - var worker = new BackgroundWorker (); - worker.doWork.add (function (_) { - - var result = CURLEasy.perform (curl); - worker.sendComplete (result); - - }); - worker.onComplete.add (function (result) { - - var responseCode = CURLEasy.getinfo (curl, RESPONSE_CODE); - - if (result == CURLCode.OK) { - - promise.complete (bytes); - - } else { - - promise.error (result); - - } - - }); - worker.run (); + typeParamString += ">"; } - #end + var flattenedTypeString = typeParamString; - return promise.future; + flattenedTypeString = StringTools.replace (flattenedTypeString, "->", "_"); + flattenedTypeString = StringTools.replace (flattenedTypeString, ".", "_"); + flattenedTypeString = StringTools.replace (flattenedTypeString, "<", "_"); + flattenedTypeString = StringTools.replace (flattenedTypeString, ">", "_"); - } - - - - - // Event Handlers - - - - - private function curl_onProgress (dltotal:Float, dlnow:Float, uptotal:Float, upnow:Float):Int { + var name = "_HTTPRequest_" + flattenedTypeString; - if (upnow > bytesLoaded || dlnow > bytesLoaded || uptotal > bytesTotal || dltotal > bytesTotal) { + try { - if (upnow > bytesLoaded) bytesLoaded = Std.int (upnow); - if (dlnow > bytesLoaded) bytesLoaded = Std.int (dlnow); - if (uptotal > bytesTotal) bytesTotal = Std.int (uptotal); - if (dltotal > bytesTotal) bytesTotal = Std.int (dltotal); + Context.getType ("lime.net." + name); - promise.progress (bytesLoaded / bytesTotal); + } catch (e:Dynamic) { + + var pos = Context.currentPos (); + var fields = Context.getBuildFields (); + var load = null; + + for (field in fields) { + + if (field.name == "load") { + + load = field; + break; + + } + + } + + switch (load.kind) { + + case FFun (fun): + + if (typeString == "String") { + + fun.expr = macro { + + if (uri != null) { + + this.uri = uri; + + } + + return cast backend.loadText (this.uri); + + }; + + } else if (stringAbstract) { + + fun.expr = macro { + + if (uri != null) { + + this.uri = uri; + + } + + var promise = new lime.app.Promise (); + var future = backend.loadText (this.uri); + + future.onProgress (promise.progress); + future.onError (promise.error); + + future.onComplete (function (text) { + + responseData = cast text; + promise.complete (responseData); + + }); + + return promise.future; + + }; + + } else { + + fun.expr = macro { + + if (uri != null) { + + this.uri = uri; + + } + + var promise = new lime.app.Promise (); + var future = backend.loadData (this.uri); + + future.onProgress (promise.progress); + future.onError (promise.error); + + future.onComplete (function (bytes) { + + responseData = cast fromBytes (bytes); + promise.complete (responseData); + + }); + + return promise.future; + + }; + + var fromBytes = null; + + if (typeString == "haxe.io.Bytes" || bytesAbstract) { + + fromBytes = macro { return bytes; } + + } else { + + fromBytes = Context.parse ("return " + typeString + ".fromBytes (bytes)", pos); + + } + + fields.push ( { name: "fromBytes", access: [ APrivate, AInline ], kind: FFun ( { args: [ { name: "bytes", type: macro :haxe.io.Bytes } ], expr: fromBytes, params: [], ret: paramType.toComplexType () } ), pos: pos } ); + + } + + default: + + } + + Context.defineType ({ + + pos: pos, + pack: [ "lime", "net" ], + name: name, + kind: TDClass (null, [ { pack: [ "lime", "net" ], name: "HTTPRequest", sub: "IHTTPRequest", params: [] } ], null), + fields: fields, + params: [ { name: "T" } ], + meta: [ { name: ":dox", params: [ macro hide ], pos: pos }, { name: ":noCompletion", pos: pos } ] + + }); } - return 0; + return TPath ( { pack: [ "lime", "net" ], name: name, params: [ TPType (paramType.toComplexType ()) ] } ).toType (); } - private function curl_onWrite (output:Bytes, size:Int, nmemb:Int):Int { + private static function isBytesAbstract (type:AbstractType):Bool { - var cacheBytes = bytes; - bytes = Bytes.alloc (bytes.length + output.length); - bytes.blit (0, cacheBytes, 0, cacheBytes.length); - bytes.blit (cacheBytes.length, output, 0, output.length); + while (type != null) { + + switch (type.type) { + + case TInst (t, _): + + return isBytesType (t.get ()); + + case TAbstract (t, _): + + type = t.get (); + + default: + + return false; + + } + + } - return size * nmemb; + return false; } - private function request_onProgress (event:Dynamic):Void { + private static function isBytesType (type:ClassType):Bool { - promise.progress (event.loaded / event.total); + while (true) { + + if (type.module == "haxe.io.Bytes") return true; + if (type.superClass == null) break; + type = type.superClass.t.get (); + + } + + return false; } -} \ No newline at end of file + private static function isStringAbstract (type:AbstractType):Bool { + + while (type != null) { + + switch (type.type) { + + case TInst (t, _): + + return t.get ().module == "String"; + + case TAbstract (t, _): + + type = t.get (); + + default: + + return false; + + } + + } + + return false; + + } + + + + +} + + +#end \ No newline at end of file diff --git a/lime/net/HTTPRequestHeader.hx b/lime/net/HTTPRequestHeader.hx new file mode 100644 index 000000000..02f7c997d --- /dev/null +++ b/lime/net/HTTPRequestHeader.hx @@ -0,0 +1,19 @@ +package lime.net; + + +class HTTPRequestHeader { + + + public var name:String; + public var value:String; + + + public function new (name:String, value:String = "") { + + this.name = name; + this.value = value; + + } + + +} \ No newline at end of file diff --git a/lime/net/HTTPRequestMethod.hx b/lime/net/HTTPRequestMethod.hx new file mode 100644 index 000000000..1bf2962d2 --- /dev/null +++ b/lime/net/HTTPRequestMethod.hx @@ -0,0 +1,14 @@ +package lime.net; + + +enum HTTPRequestMethod { + + GET; + POST; + //HEAD; + //PUT; + //DELETE; + //OPTIONS; + //CONNECT; + +} \ No newline at end of file diff --git a/lime/net/NetConnection.hx b/lime/net/NetConnection.hx deleted file mode 100644 index e69de29bb..000000000 diff --git a/lime/net/NetConnectionManager.hx b/lime/net/NetConnectionManager.hx deleted file mode 100644 index e69de29bb..000000000 diff --git a/lime/net/URIParser.hx b/lime/net/URIParser.hx index 7cb756c17..e4638e49e 100644 --- a/lime/net/URIParser.hx +++ b/lime/net/URIParser.hx @@ -69,4 +69,4 @@ class URIParser { } -typedef KVPair = { k:String, v:String }; \ No newline at end of file +@:dox(hide) typedef KVPair = { k:String, v:String }; \ No newline at end of file diff --git a/lime/system/BackgroundWorker.hx b/lime/system/BackgroundWorker.hx index d1ffe3643..c0e6c4b3d 100644 --- a/lime/system/BackgroundWorker.hx +++ b/lime/system/BackgroundWorker.hx @@ -20,6 +20,7 @@ class BackgroundWorker { private static var MESSAGE_ERROR = "__ERROR__"; public var canceled (default, null):Bool; + public var completed (default, null):Bool; public var doWork = new EventVoid> (); public var onComplete = new EventVoid> (); public var onError = new EventVoid> (); @@ -56,6 +57,7 @@ class BackgroundWorker { public function run (message:Dynamic = null):Void { canceled = false; + completed = false; __runMessage = message; #if (cpp || neko) @@ -76,6 +78,8 @@ class BackgroundWorker { public function sendComplete (message:Dynamic = null):Void { + completed = true; + #if (cpp || neko) __messageQueue.add (MESSAGE_COMPLETE); diff --git a/lime/utils/Bytes.hx b/lime/utils/Bytes.hx index 4389b0322..59a15fe98 100644 --- a/lime/utils/Bytes.hx +++ b/lime/utils/Bytes.hx @@ -40,6 +40,13 @@ class Bytes extends HaxeBytes { } + public static function fromBytes (bytes:haxe.io.Bytes):Bytes { + + return new Bytes (bytes.length, bytes.getData ()); + + } + + public static function ofData (b:BytesData):Bytes { var bytes = HaxeBytes.ofData (b); diff --git a/templates/haxe/DefaultAssetLibrary.hx b/templates/haxe/DefaultAssetLibrary.hx index 12979ff66..013cea81d 100644 --- a/templates/haxe/DefaultAssetLibrary.hx +++ b/templates/haxe/DefaultAssetLibrary.hx @@ -242,7 +242,7 @@ class DefaultAssetLibrary extends AssetLibrary { } - var bytes = loader.bytes; + var bytes:Bytes = cast loader.responseData; if (bytes != null) { @@ -380,7 +380,7 @@ class DefaultAssetLibrary extends AssetLibrary { } - var bytes = loader.bytes; + var bytes:Bytes = cast loader.responseData; if (bytes != null) { @@ -475,7 +475,7 @@ class DefaultAssetLibrary extends AssetLibrary { if (Assets.isLocal (id)) { - promise.completeWith (new Future (function () return getAudioBuffer (id))); + promise.completeWith (new Future (function () return getAudioBuffer (id), true)); } else if (path.exists (id)) { @@ -534,7 +534,7 @@ class DefaultAssetLibrary extends AssetLibrary { if (path.exists (id)) { - var request = new HTTPRequest (); + var request = new HTTPRequest (); promise.completeWith (request.load (path.get (id) + "?" + Assets.cache.version)); } else { @@ -545,7 +545,7 @@ class DefaultAssetLibrary extends AssetLibrary { #else - promise.completeWith (new Future (function () return getBytes (id))); + promise.completeWith (new Future (function () return getBytes (id), true)); #end @@ -612,7 +612,7 @@ class DefaultAssetLibrary extends AssetLibrary { #else - promise.completeWith (new Future (function () return getImage (id))); + promise.completeWith (new Future (function () return getImage (id), true)); #end @@ -693,11 +693,8 @@ class DefaultAssetLibrary extends AssetLibrary { if (path.exists (id)) { - var request = new HTTPRequest (); - var future = request.load (path.get (id) + "?" + Assets.cache.version); - future.onProgress (function (progress) promise.progress (progress)); - future.onError (function (msg) promise.error (msg)); - future.onComplete (function (bytes) promise.complete (bytes.getString (0, bytes.length))); + var request = new HTTPRequest (); + promise.completeWith (request.load (path.get (id) + "?" + Assets.cache.version)); } else { @@ -721,7 +718,7 @@ class DefaultAssetLibrary extends AssetLibrary { } - }); + }, true); }));