Add simple HTTPRequest class to help fill holes from URLLoader and make a few small fixes
This commit is contained in:
@@ -93,12 +93,6 @@ import lime.net.oauth.OAuthVersion;
|
||||
//import lime.net.NetConnection;
|
||||
//import lime.net.NetConnectionManager;
|
||||
import lime.net.URIParser;
|
||||
import lime.net.URLLoader;
|
||||
import lime.net.URLLoaderDataFormat;
|
||||
import lime.net.URLRequest;
|
||||
import lime.net.URLRequestHeader;
|
||||
import lime.net.URLRequestMethod;
|
||||
import lime.net.URLVariables;
|
||||
#if (windows || mac || linux || neko)
|
||||
import lime.project.ApplicationData;
|
||||
import lime.project.Architecture;
|
||||
|
||||
@@ -8,8 +8,7 @@ import lime.Assets;
|
||||
import js.html.Image;
|
||||
import js.html.SpanElement;
|
||||
import js.Browser;
|
||||
import lime.net.URLLoader;
|
||||
import lime.net.URLRequest;
|
||||
import lime.net.HTTPRequest;
|
||||
#elseif flash
|
||||
import flash.display.LoaderInfo;
|
||||
import flash.display.Sprite;
|
||||
@@ -27,7 +26,7 @@ class Preloader #if flash extends Sprite #end {
|
||||
|
||||
#if (js && html5)
|
||||
public static var images = new Map<String, Image> ();
|
||||
public static var loaders = new Map<String, URLLoader> ();
|
||||
public static var loaders = new Map<String, HTTPRequest> ();
|
||||
private var loaded = 0;
|
||||
private var total = 0;
|
||||
#end
|
||||
@@ -91,9 +90,7 @@ class Preloader #if flash extends Sprite #end {
|
||||
|
||||
if (!loaders.exists (url)) {
|
||||
|
||||
var loader = new URLLoader ();
|
||||
loader.dataFormat = BINARY;
|
||||
loaders.set (url, loader);
|
||||
var loader = new HTTPRequest ();
|
||||
total++;
|
||||
|
||||
}
|
||||
@@ -102,8 +99,7 @@ class Preloader #if flash extends Sprite #end {
|
||||
|
||||
if (!loaders.exists (url)) {
|
||||
|
||||
var loader = new URLLoader ();
|
||||
loaders.set (url, loader);
|
||||
var loader = new HTTPRequest ();
|
||||
total++;
|
||||
|
||||
}
|
||||
@@ -122,8 +118,8 @@ class Preloader #if flash extends Sprite #end {
|
||||
for (url in loaders.keys ()) {
|
||||
|
||||
var loader = loaders.get (url);
|
||||
loader.onComplete.add (loader_onComplete);
|
||||
loader.load (new URLRequest (url + "?" + cacheVersion));
|
||||
var future = loader.load (url + "?" + cacheVersion);
|
||||
future.onComplete (loader_onComplete);
|
||||
|
||||
}
|
||||
|
||||
@@ -276,7 +272,7 @@ class Preloader #if flash extends Sprite #end {
|
||||
}
|
||||
|
||||
|
||||
private function loader_onComplete (loader:URLLoader):Void {
|
||||
private function loader_onComplete (_):Void {
|
||||
|
||||
loaded++;
|
||||
|
||||
|
||||
@@ -891,6 +891,7 @@ class Image {
|
||||
|
||||
#if flash
|
||||
rect.offset (offsetX, offsetY);
|
||||
var byteArray = new ByteArray ();
|
||||
|
||||
switch (format) {
|
||||
|
||||
@@ -898,7 +899,7 @@ class Image {
|
||||
case BGRA32:
|
||||
|
||||
var srcData:ByteArray = bytes.getData ();
|
||||
var byteArray = new ByteArray ();
|
||||
byteArray = new ByteArray ();
|
||||
#if flash
|
||||
@:privateAccess byteArray.length = srcData.length;
|
||||
#end
|
||||
@@ -918,7 +919,7 @@ class Image {
|
||||
|
||||
default:
|
||||
|
||||
var srcData = byteArray;
|
||||
var srcData = bytes.getData ();
|
||||
byteArray = new ByteArray ();
|
||||
#if flash
|
||||
@:privateAccess byteArray.length = srcData.length;
|
||||
|
||||
@@ -98,7 +98,7 @@ class JPEG {
|
||||
|
||||
for (i in 0...buffer.length) {
|
||||
|
||||
bytes[i] = buffer.charCodeAt (i);
|
||||
bytes.set (i, buffer.charCodeAt (i));
|
||||
|
||||
}
|
||||
|
||||
|
||||
208
lime/net/HTTPRequest.hx
Normal file
208
lime/net/HTTPRequest.hx
Normal file
@@ -0,0 +1,208 @@
|
||||
package lime.net;
|
||||
|
||||
|
||||
import flash.utils.ByteArray;
|
||||
import haxe.io.Bytes;
|
||||
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;
|
||||
|
||||
#if (js && html5)
|
||||
import js.html.XMLHttpRequest;
|
||||
#end
|
||||
|
||||
|
||||
class HTTPRequest {
|
||||
|
||||
|
||||
public var bytes:Bytes;
|
||||
|
||||
private var bytesLoaded:Int;
|
||||
private var bytesTotal:Int;
|
||||
private var promise:Promise<Bytes>;
|
||||
|
||||
|
||||
public function new () {
|
||||
|
||||
promise = new Promise<Bytes> ();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function load (url:String):Future<Bytes> {
|
||||
|
||||
bytesLoaded = 0;
|
||||
bytesTotal = 0;
|
||||
|
||||
#if flash
|
||||
|
||||
|
||||
|
||||
#elseif (js && html5)
|
||||
|
||||
var request = new XMLHttpRequest ();
|
||||
request.addEventListener ("progress", request_onProgress, false);
|
||||
request.onreadystatechange = function () {
|
||||
|
||||
if (request.readyState != 4) return;
|
||||
|
||||
if (request.status != null && request.status >= 200 && request.status <= 400) {
|
||||
|
||||
promise.complete (Bytes.ofData (request.response));
|
||||
|
||||
} else {
|
||||
|
||||
promise.error (request.status);
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
request.open ("GET", url, true);
|
||||
request.responseType = ARRAYBUFFER;
|
||||
request.send (null);
|
||||
|
||||
#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 = 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 ();
|
||||
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
return promise.future;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function readFile (path:String):Bytes {
|
||||
|
||||
#if (!flash && !html5 && !macro)
|
||||
var data:Dynamic = lime_bytes_read_file (path);
|
||||
if (data != null) return @:privateAccess new Bytes (data.length, data.b);
|
||||
#end
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function request_onProgress (event:Dynamic):Void {
|
||||
|
||||
promise.progress (event.loaded / event.total);
|
||||
|
||||
}
|
||||
|
||||
|
||||
#if (!flash && !html5 && !macro)
|
||||
private static var lime_bytes_read_file = CFFI.load ("lime", "lime_bytes_read_file", 1);
|
||||
#end
|
||||
|
||||
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import lime.audio.AudioSource;
|
||||
import lime.audio.openal.AL;
|
||||
import lime.audio.AudioBuffer;
|
||||
import lime.graphics.Image;
|
||||
import lime.net.HTTPRequest;
|
||||
import lime.system.CFFI;
|
||||
import lime.text.Font;
|
||||
import lime.utils.UInt8Array;
|
||||
@@ -20,10 +21,7 @@ import lime.Assets;
|
||||
import sys.FileSystem;
|
||||
#end
|
||||
|
||||
#if (js && html5)
|
||||
//import lime.net.URLLoader;
|
||||
//import lime.net.URLRequest;
|
||||
#elseif flash
|
||||
#if flash
|
||||
import flash.display.Bitmap;
|
||||
import flash.display.BitmapData;
|
||||
import flash.display.Loader;
|
||||
@@ -225,7 +223,6 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
|
||||
#elseif html5
|
||||
|
||||
var bytes:Bytes = null;
|
||||
var loader = Preloader.loaders.get (path.get (id));
|
||||
|
||||
if (loader == null) {
|
||||
@@ -234,25 +231,10 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
|
||||
}
|
||||
|
||||
var data = loader.data;
|
||||
|
||||
if (Std.is (data, String)) {
|
||||
|
||||
bytes = Bytes.ofString (data);
|
||||
|
||||
} else if (Std.is (data, Bytes)) {
|
||||
|
||||
bytes = data;
|
||||
|
||||
} else {
|
||||
|
||||
bytes = null;
|
||||
|
||||
}
|
||||
var bytes = loader.bytes;
|
||||
|
||||
if (bytes != null) {
|
||||
|
||||
bytes.position = 0;
|
||||
return bytes;
|
||||
|
||||
} else {
|
||||
@@ -379,7 +361,6 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
|
||||
#if html5
|
||||
|
||||
var bytes:Bytes = null;
|
||||
var loader = Preloader.loaders.get (path.get (id));
|
||||
|
||||
if (loader == null) {
|
||||
@@ -388,21 +369,7 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
|
||||
}
|
||||
|
||||
var data = loader.data;
|
||||
|
||||
if (Std.is (data, String)) {
|
||||
|
||||
return cast data;
|
||||
|
||||
} else if (Std.is (data, Bytes)) {
|
||||
|
||||
bytes = cast data;
|
||||
|
||||
} else {
|
||||
|
||||
bytes = null;
|
||||
|
||||
}
|
||||
var bytes = loader.bytes;
|
||||
|
||||
if (bytes != null) {
|
||||
|
||||
@@ -559,40 +526,16 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
|
||||
#elseif html5
|
||||
|
||||
//if (path.exists (id)) {
|
||||
//
|
||||
//var loader = new URLLoader ();
|
||||
//loader.dataFormat = BINARY;
|
||||
//loader.onComplete.add (function (_):Void {
|
||||
//
|
||||
//promise.complete (loader.data);
|
||||
//
|
||||
//});
|
||||
//loader.onProgress.add (function (_, loaded, total) {
|
||||
//
|
||||
//if (total == 0) {
|
||||
//
|
||||
//promise.progress (0);
|
||||
//
|
||||
//} else {
|
||||
//
|
||||
//promise.progress (loaded / total);
|
||||
//
|
||||
//}
|
||||
//
|
||||
//});
|
||||
//loader.onIOError.add (function (_, e) {
|
||||
//
|
||||
//promise.error (e);
|
||||
//
|
||||
//});
|
||||
//loader.load (new URLRequest (path.get (id) + "?" + Assets.cache.version));
|
||||
//
|
||||
//} else {
|
||||
//
|
||||
//promise.complete (getBytes (id));
|
||||
//
|
||||
//}
|
||||
if (path.exists (id)) {
|
||||
|
||||
var request = new HTTPRequest ();
|
||||
promise.completeWith (request.load (path.get (id) + "?" + Assets.cache.version));
|
||||
|
||||
} else {
|
||||
|
||||
promise.complete (getBytes (id));
|
||||
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
@@ -742,35 +685,19 @@ class DefaultAssetLibrary extends AssetLibrary {
|
||||
|
||||
#if html5
|
||||
|
||||
//if (path.exists (id)) {
|
||||
//
|
||||
//var loader = new URLLoader ();
|
||||
//loader.onComplete.add (function (_):Void {
|
||||
//
|
||||
//promise.complete (loader.data);
|
||||
//
|
||||
//});
|
||||
//loader.onProgress.add (function (_, loaded, total) {
|
||||
//
|
||||
//if (total == 0) {
|
||||
//
|
||||
//promise.progress (0);
|
||||
//
|
||||
//} else {
|
||||
//
|
||||
//promise.progress (loaded / total);
|
||||
//
|
||||
//}
|
||||
//
|
||||
//});
|
||||
//loader.onIOError.add (function (_, msg) promise.error (msg));
|
||||
//loader.load (new URLRequest (path.get (id) + "?" + Assets.cache.version));
|
||||
//
|
||||
//} else {
|
||||
//
|
||||
//promise.complete (getText (id));
|
||||
//
|
||||
//}
|
||||
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)));
|
||||
|
||||
} else {
|
||||
|
||||
promise.complete (getText (id));
|
||||
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
|
||||
Reference in New Issue
Block a user