Skip to content

Commit

Permalink
HTTPRequest: fix missing response data for HTTP status codes (closes #…
Browse files Browse the repository at this point in the history
…1699)

Backends now return error and response data, but public API has not changed.

This allows OpenFL to expose URLLoader.data on IOErrorEvent.IO_ERROR to match the behavior of Flash
  • Loading branch information
joshtynjala committed Aug 11, 2023
1 parent c16f278 commit b6cfc7d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
12 changes: 7 additions & 5 deletions src/lime/_internal/backend/flash/FlashHTTPRequest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,13 @@ class FlashHTTPRequest

urlLoader.addEventListener(IOErrorEvent.IO_ERROR, function(event)
{
promise.error(event.errorID);
var bytes = Bytes.ofData(cast(urlLoader.data, ByteArray));
promise.error(new _HTTPRequestErrorResponse(event.errorID, bytes));
});

urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function(event)
{
promise.error(403);
promise.error(new _HTTPRequestErrorResponse(403, null));
});

urlLoader.addEventListener(Event.COMPLETE, function(event)
Expand Down Expand Up @@ -156,12 +157,13 @@ class FlashHTTPRequest

urlLoader.addEventListener(IOErrorEvent.IO_ERROR, function(event)
{
promise.error(event.errorID);
var responseData = cast(urlLoader.data, String);
promise.error(new _HTTPRequestErrorResponse(event.errorID, responseData));
});

urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function(event)
{
promise.error(403);
promise.error(new _HTTPRequestErrorResponse(403, null));
});

urlLoader.addEventListener(Event.COMPLETE, function(event)
Expand All @@ -172,4 +174,4 @@ class FlashHTTPRequest
urlLoader.load(urlRequest);
return promise.future;
}
}
}
31 changes: 15 additions & 16 deletions src/lime/_internal/backend/html5/HTML5HTTPRequest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -407,29 +407,28 @@ class HTML5HTTPRequest
{
if (request.readyState != 4) return;

if (request.status != null && ((request.status >= 200 && request.status < 400) || (validStatus0 && request.status == 0)))
var bytes = null;
if (request.responseType == NONE)
{
var bytes = null;

if (request.responseType == NONE)
{
if (request.responseText != null)
{
bytes = Bytes.ofString(request.responseText);
}
}
else if (request.response != null)
if (request.responseText != null)
{
bytes = Bytes.ofData(request.response);
bytes = Bytes.ofString(request.responseText);
}
}
else if (request.response != null)
{
bytes = Bytes.ofData(request.response);
}

if (request.status != null && ((request.status >= 200 && request.status < 400) || (validStatus0 && request.status == 0)))
{
processResponse();
promise.complete(bytes);
}
else
{
processResponse();
promise.error(request.status);
promise.error(new _HTTPRequestErrorResponse(request.status, bytes));
}

request = null;
Expand Down Expand Up @@ -482,7 +481,7 @@ class HTML5HTTPRequest
activeRequests--;
processQueue();

promise.error(event.detail);
promise.error(new _HTTPRequestErrorResponse(event.detail, null));
}, false);

image.src = uri;
Expand All @@ -505,7 +504,7 @@ class HTML5HTTPRequest

request.onerror = function(event:ErrorEvent)
{
promise.error(event.message);
promise.error(new _HTTPRequestErrorResponse(event.message, null));
}

request.onprogress = function(event:ProgressEvent)
Expand Down Expand Up @@ -542,7 +541,7 @@ class HTML5HTTPRequest
else
{
processResponse();
promise.error(request.status);
promise.error(new _HTTPRequestErrorResponse(request.status, request.responseText));
}

request = null;
Expand Down
13 changes: 9 additions & 4 deletions src/lime/_internal/backend/native/NativeHTTPRequest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ class NativeHTTPRequest
private static function localThreadPool_onError(state:{instance:NativeHTTPRequest, promise:Promise<Bytes>, error:String}):Void
{
var promise:Promise<Bytes> = state.promise;
promise.error(state.error);
promise.error(new _HTTPRequestErrorResponse(state.error, null));

var instance = state.instance;

Expand Down Expand Up @@ -576,16 +576,21 @@ class NativeHTTPRequest
}
else if (instance.bytes != null)
{
instance.promise.error(instance.bytes.getString(0, instance.bytes.length));
var error = instance.bytes.getString(0, instance.bytes.length);
var responseData = instance.buildBuffer();
instance.promise.error(new _HTTPRequestErrorResponse(error, responseData));
}
else
{
instance.promise.error('Status ${state.status}');
var error = 'Status ${state.status}';
var responseData = instance.buildBuffer();
instance.promise.error(new _HTTPRequestErrorResponse(error, responseData));
}
}
else
{
instance.promise.error(CURL.strerror(state.result));
var error = CURL.strerror(state.result);
instance.promise.error(new _HTTPRequestErrorResponse(error, null));
}

if (instance.timeout != null)
Expand Down
21 changes: 19 additions & 2 deletions src/lime/net/HTTPRequest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ public function load(uri:String = null):Future<T>
var future = __backend.loadData(this.uri);

future.onProgress(promise.progress);
future.onError(promise.error);
future.onError(function(errorResponse:_HTTPRequestErrorResponse<T>)
{
responseData = errorResponse.responseData;
promise.error(errorResponse.error);
});

future.onComplete(function(bytes)
{
Expand Down Expand Up @@ -140,7 +144,11 @@ public function load(uri:String = null):Future<T>
var future = __backend.loadText(this.uri);

future.onProgress(promise.progress);
future.onError(promise.error);
future.onError(function(errorResponse:_HTTPRequestErrorResponse<T>)
{
responseData = errorResponse.responseData;
promise.error(errorResponse.error);
});

future.onComplete(function(text)
{
Expand All @@ -152,6 +160,15 @@ public function load(uri:String = null):Future<T>
}
}

@:noCompletion class _HTTPRequestErrorResponse<T> {
public var error:Dynamic;
public var responseData:T;
public function new(error:Dynamic, responseData:T) {
this.error = error;
this.responseData = responseData;
}
}

@:noCompletion interface _IHTTPRequest
{
public var contentType:String;
Expand Down

0 comments on commit b6cfc7d

Please sign in to comment.