New: added timeout and cache feature in CPURLRequest

This PR adds the possibility to set the timeout and the cache policy of a CPURLRequest.
For that, there are new things in the framework :

- CFHTTPRequest has now the functions setTimeout(), getTimeout() and isTimeoutRequest
- CPURLRequest has now the methods +requestWithURL:cachePolicy:timeoutInterval: and -initWithURL:cachePolicy:timeoutInterval:
- CPURLConnection does not call the delegate didReceiveData: when a request has timed out. I will call the delegate didFailWithError:
- Cache is now possible for a CPURLRequest, only CPURLRequestReturnCacheDataElseLoad, CPURLRequestReturnCacheDataDontLoad and CPURLRequestReloadIgnoringLocalCacheData are supported. By default CPURLRequestReloadIgnoringLocalCacheData.
- Default timeout has been set to 60sec as in cocoa.
- A request is considered as timeout when there isn't any response (text/xml/type) and when the status of the response is 0.
This commit is contained in:
Alexandre Wilhelm committed 2015-08-13 17:08:59 -07:00
1 parent 3d79799450
commit bd0bd2e087
3 files changed
+132 -14

No files matched your search

+34 -4
View File
@@ -105,6 +105,7 @@ GLOBAL(CFHTTPRequest) = function()
// by default, all requests will assume that credentials should not be sent.
this._nativeRequest.withCredentials = false;
this._nativeRequest.timeout = 60000;
var self = this;
this._stateChangeHandler = function()
@@ -112,7 +113,13 @@ GLOBAL(CFHTTPRequest) = function()
determineAndDispatchHTTPRequestEvents(self);
};
this._timeoutHandler = function()
{
dispatchTimeoutHTTPRequestEvents(self);
};
this._nativeRequest.onreadystatechange = this._stateChangeHandler;
this._nativeRequest.ontimeout = this._timeoutHandler;
if (CFHTTPRequest.AuthenticationDelegate !== nil)
this._eventDispatcher.addEventListener("HTTP403", function()
@@ -209,6 +216,16 @@ CFHTTPRequest.prototype.getResponseHeader = function(/*String*/ aHeader)
return this._nativeRequest.getResponseHeader(aHeader);
};
CFHTTPRequest.prototype.setTimeout = function(/*int*/ aTimeout)
{
this._nativeRequest.timeout = aTimeout;
};
CFHTTPRequest.prototype.getTimeout = function(/*int*/ aTimeout)
{
return this._nativeRequest.timeout
};
CFHTTPRequest.prototype.getAllResponseHeaders = function()
{
return this._nativeRequest.getAllResponseHeaders();
@@ -235,7 +252,10 @@ CFHTTPRequest.prototype.send = function(/*Object*/ aBody)
if (!this._isOpen)
{
delete this._nativeRequest.onreadystatechange;
delete this._nativeRequest.ontimeout;
this._nativeRequest.open(this._method, this._URL, this._async, this._user, this._password);
this._nativeRequest.ontimeout = this._timeoutHandler;
this._nativeRequest.onreadystatechange = this._stateChangeHandler;
}
@@ -287,15 +307,25 @@ CFHTTPRequest.prototype.withCredentials = function()
return this._nativeRequest.withCredentials;
};
CFHTTPRequest.prototype.isTimeoutRequest = function()
{
// Can we consider that as a timeout ?
return !this.success() && !this._nativeRequest.response && !this._nativeRequest.responseText && !this._nativeRequest.responseType && !this._nativeRequest.responseURL && !this._nativeRequest.responseXML;
};
function dispatchTimeoutHTTPRequestEvents(/*CFHTTPRequest*/ aRequest)
{
aRequest._eventDispatcher.dispatchEvent({ type:"timeout", request:aRequest});
}
function determineAndDispatchHTTPRequestEvents(/*CFHTTPRequest*/ aRequest)
{
var eventDispatcher = aRequest._eventDispatcher;
var eventDispatcher = aRequest._eventDispatcher,
nativeRequest = aRequest._nativeRequest,
readyStates = ["uninitialized", "loading", "loaded", "interactive", "complete"];
eventDispatcher.dispatchEvent({ type:"readystatechange", request:aRequest});
var nativeRequest = aRequest._nativeRequest,
readyStates = ["uninitialized", "loading", "loaded", "interactive", "complete"];
if (readyStates[aRequest.readyState()] === "complete")
{
var status = "HTTP" + aRequest.status();