Browse files

Fixed http timeout problem. Made sure errors in http callbacks raise …

…lua errors.
  • Loading branch information...
1 parent 2e1a5c3 commit ea429447f588640c7ec02144311b140474611ab9 @probablycorey committed Jun 24, 2011
View
3 lib/extensions/HTTP/wax_http.m
@@ -91,8 +91,7 @@ static int request(lua_State *L) {
[urlRequest setCachePolicy:cachePolicy];
[urlRequest setAllHTTPHeaderFields:headerFields];
[urlRequest setHTTPBody:body];
- [urlRequest setTimeoutInterval:timeout]; // Apple makes has a mandatory 240 second timeout WTF? https://devforums.apple.com/thread/25282
-
+
wax_http_connection *connection;
connection = [[wax_http_connection alloc] initWithRequest:urlRequest luaState:L];
View
6 lib/extensions/HTTP/wax_http_connection.h
@@ -27,10 +27,12 @@ enum {
NSMutableData *_data;
NSHTTPURLResponse *_response;
NSURLRequest *_request;
+ NSTimer *_timeoutTimer;
+ NSError *_error;
+ NSTimeInterval _timeout;
int _format;
bool _finished;
- bool _error;
bool _canceled;
}
@@ -39,7 +41,7 @@ enum {
@property (nonatomic, assign) int format;
@property (nonatomic, readonly, getter=isFinished) bool finished;
-- (id)initWithRequest:(NSURLRequest *)urlRequest luaState:(lua_State *)luaState;
+- (id)initWithRequest:(NSURLRequest *)urlRequest timeout:(NSTimeInterval)timeout luaState:(lua_State *)luaState;
- (void)callRedirectCallback:(NSURLResponse *)redirectResponse;
- (BOOL)callLuaAuthCallback:(NSURLAuthenticationChallenge *)challenge;
- (void)callLuaProgressCallback;
View
40 lib/extensions/HTTP/wax_http_connection.m
@@ -27,32 +27,45 @@ @implementation wax_http_connection
- (void)dealloc {
[_data release];
+ [_error release];
[_response release];
[_request release];
+ [_timeoutTimer release];
[super dealloc];
}
-- (id)initWithRequest:(NSURLRequest *)urlRequest luaState:(lua_State *)luaState {
+- (id)initWithRequest:(NSURLRequest *)urlRequest timeout:(NSTimeInterval)timeout luaState:(lua_State *)luaState {
[super initWithRequest:urlRequest delegate:self startImmediately:NO];
[self scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
L = luaState;
_data = [[NSMutableData alloc] init];
_request = [urlRequest retain];
+ _timeout = timeout;
_format = WAX_HTTP_UNKNOWN;
- _error = NO;
_canceled = NO;
return self;
}
+- (void)timeoutHack {
+ NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorTimedOut userInfo:nil];
+ [self connection:self didFailWithError:error];
+ [self cancel];
+}
+
- (void)start {
+ // Apple makes has a mandatory 240 second minimum timeout WTF? https://devforums.apple.com/thread/25282
+ // Because of this stupidity by Apple, we are forced to setup our own timeout
+ // using a timer.
+ _timeoutTimer = [[NSTimer scheduledTimerWithTimeInterval:_timeout target:self selector:@selector(timeoutHack) userInfo:nil repeats:NO] retain];
wax_log(LOG_NETWORK, @"HTTP(%@) %@", [_request HTTPMethod], [_request URL]);
[super start];
}
- (void)cancel {
wax_log(LOG_NETWORK, @"CANCELING (%@) %@", [_request HTTPMethod], [_request URL]);
+ [_timeoutTimer invalidate];
_canceled = YES;
[super cancel];
}
@@ -84,9 +97,9 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if (!_canceled) {
- _error = YES;
+ _error = [error retain];
[_data release];
- _data = [[error localizedDescription] retain];
+ _data = nil;
[self callLuaCallback];
}
}
@@ -117,7 +130,7 @@ - (void)callRedirectCallback:(NSURLResponse *)redirectResponse {
if (hasCallback && wax_pcall(L, 1, 0)) {
const char* error_string = lua_tostring(L, -1);
- printf("Problem calling Lua function '%s' from wax_http.\n%s", WAX_HTTP_REDIRECT_CALLBACK_FUNCTION_NAME, error_string);
+ luaL_error(L, "Problem calling Lua function '%s' from wax_http.\n%s", WAX_HTTP_REDIRECT_CALLBACK_FUNCTION_NAME, error_string);
}
END_STACK_MODIFY(L, 0)
@@ -143,7 +156,7 @@ - (BOOL)callLuaAuthCallback:(NSURLAuthenticationChallenge *)challenge {
if (hasCallback && wax_pcall(L, 1, 0)) {
const char* error_string = lua_tostring(L, -1);
- printf("Problem calling Lua function '%s' from wax_http.\n%s", WAX_HTTP_AUTH_CALLBACK_FUNCTION_NAME, error_string);
+ luaL_error(L, "Problem calling Lua function '%s' from wax_http.\n%s", WAX_HTTP_AUTH_CALLBACK_FUNCTION_NAME, error_string);
}
END_STACK_MODIFY(L, 0)
@@ -180,7 +193,7 @@ - (void)callLuaProgressCallback {
if (wax_pcall(L, 2, 0)) {
const char* error_string = lua_tostring(L, -1);
- printf("Problem calling Lua function '%s' from wax_http.\n%s", WAX_HTTP_PROGRESS_CALLBACK_FUNCTION_NAME, error_string);
+ luaL_error(L, "Problem calling Lua function '%s' from wax_http.\n%s", WAX_HTTP_PROGRESS_CALLBACK_FUNCTION_NAME, error_string);
}
}
@@ -189,6 +202,8 @@ - (void)callLuaProgressCallback {
- (void)callLuaCallback {
+ [_timeoutTimer invalidate];
+
BEGIN_STACK_MODIFY(L)
if (_canceled) {
@@ -267,11 +282,18 @@ - (void)callLuaCallback {
}
wax_fromObjc(L, "@", &_response);
- wax_fromObjc(L, "@", &_data); // Send the raw data too (since oddly, the response doesn't contain it)
+ if (_error) {
+ wax_fromObjc(L, "@", &_error);
+ }
+ else {
+ lua_pushnil(L);
+ }
+
+
if (hasCallback && wax_pcall(L, 3, 0)) {
const char* error_string = lua_tostring(L, -1);
- printf("Problem calling Lua function '%s' from wax_http.\n%s", WAX_HTTP_CALLBACK_FUNCTION_NAME, error_string);
+ luaL_error(L, "Problem calling Lua function '%s' from wax_http.\n%s", WAX_HTTP_CALLBACK_FUNCTION_NAME, error_string);
}
_finished = YES;

0 comments on commit ea42944

Please sign in to comment.