Skip to content

Commit

Permalink
Update some parts of hs.http to use LuaSkin methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cmsj committed Apr 5, 2018
1 parent 8c09321 commit 611be77
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions extensions/http/internal.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 611be77

Please sign in to comment.