From 611be77b088a5d21635a2fad7acaa897124b61e1 Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Thu, 5 Apr 2018 19:29:36 +0100 Subject: [PATCH] Update some parts of hs.http to use LuaSkin methods --- extensions/http/internal.m | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/extensions/http/internal.m b/extensions/http/internal.m index d225a38e8..b7e1870d6 100644 --- a/extensions/http/internal.m +++ b/extensions/http/internal.m @@ -17,17 +17,6 @@ static int refTable; static NSMutableArray* delegates; -// Create a new Lua table and add all response header keys and values from the response -static void createResponseHeaderTable(lua_State* L, NSHTTPURLResponse* httpResponse){ - NSDictionary *responseHeaders = [httpResponse allHeaderFields]; - lua_newtable(L); - for (id key in responseHeaders) { - NSString *value = [responseHeaders objectForKey:key]; - lua_pushstring(L, [value UTF8String]); - lua_setfield(L, -2, [key UTF8String]); - } -} - // Convert a response body to data we can send to Lua static id responseBodyToId(NSHTTPURLResponse *httpResponse, NSData *bodyData) { NSString *contentType = [httpResponse.allHeaderFields objectForKey:@"Content-Type"]; @@ -100,7 +89,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection * __unused)connection { [skin pushLuaRef:refTable ref:self.fn]; lua_pushinteger(L, (int)self.httpResponse.statusCode); [skin pushNSObject:responseBodyToId(self.httpResponse, self.receivedData)]; - createResponseHeaderTable(L, self.httpResponse); + [skin pushNSObject:self.httpResponse.allHeaderFields]; if (![skin protectedCallAndTraceback:3 nresults:0]) { const char *errorMsg = lua_tostring(L, -1); @@ -121,7 +110,7 @@ - (void)connection:(NSURLConnection * __unused)connection didFailWithError:(NSEr NSString* errorMessage = [NSString stringWithFormat:@"Connection failed: %@ - %@", [error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]]; [skin pushLuaRef:refTable ref:self.fn]; lua_pushinteger(self.L, -1); - lua_pushstring(self.L, [errorMessage UTF8String]); + [skin pushNSObject:errorMessage]; lua_pcall(self.L, 2, 0, 0); remove_delegate(self.L, self); } @@ -153,7 +142,7 @@ - (void)webSocket:(PSWebSocket *)webSocket didReceiveMessage:(id)message { LuaSkin *skin = [LuaSkin shared]; [skin pushLuaRef:refTable ref:self.fn]; - lua_pushstring(skin.L, [message UTF8String]); + [skin pushNSObject:message]; if (![skin protectedCallAndTraceback:1 nresults:0]) { const char *errorMsg = lua_tostring(skin.L, -1); @@ -305,7 +294,7 @@ static int http_doRequest(lua_State* L) { lua_pushinteger(L, (int)httpResponse.statusCode); [skin pushNSObject:responseBodyToId(httpResponse, dataReply)]; - createResponseHeaderTable(L, httpResponse); + [skin pushNSObject:httpResponse.allHeaderFields]; return 3; }