Skip to content
Permalink
Browse files

*CL* BugFix - if body to http post is string, treat as raw data rathe…

…r then force specific string encoding type

*CL* Cleanup - update to module import style
  • Loading branch information...
asmagill authored and cmsj committed Mar 28, 2019
1 parent 7440731 commit 9e6a0c8f7c9ae959cb82e192e2199f82d0fc85f0
Showing with 19 additions and 11 deletions.
  1. +19 −11 extensions/http/internal.m
@@ -1,8 +1,8 @@
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
#import <LuaSkin/LuaSkin.h>
#import <WebKit/WebKit.h>
@import Foundation;
@import Cocoa;
@import Carbon;
@import LuaSkin;
@import WebKit;
#import "PocketSocket/PSWebSocket.h"

// Websocket userdata struct
@@ -161,12 +161,20 @@ - (void)webSocket:(PSWebSocket *)webSocket didCloseWithCode:(NSInteger)code reas
// add it to the request and add the content length header field
static void getBodyFromStack(lua_State* L, int index, NSMutableURLRequest* request){
if (!lua_isnoneornil(L, index)) {
NSString* body = [NSString stringWithCString:lua_tostring(L, 3) encoding:NSASCIIStringEncoding];
NSData *postData = [body dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", [postData length]];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSData *postData ;
if (lua_type(L, index) == LUA_TSTRING) {
postData = [[LuaSkin shared] toNSObjectAtIndex:index withOptions:LS_NSLuaStringAsDataOnly] ;
} else {
NSString* body = [NSString stringWithCString:lua_tostring(L, index) encoding:NSASCIIStringEncoding];
postData = [body dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
}
if (postData) {
NSString *postLength = [NSString stringWithFormat:@"%lu", [postData length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
} else {
[LuaSkin logError:[NSString stringWithFormat:@"%s - getBodyFromStack - non-nil entry at stack index %u but unable to convert to NSData", WS_USERDATA_TAG, index]] ;
}
}
}

0 comments on commit 9e6a0c8

Please sign in to comment.
You can’t perform that action at this time.