瀏覽代碼

macOS: Use NSURLSession API instead of NSURLConnection since the latter is deprecated. Move autoreleasepool to inside the request implementation.

Alex Szpakowski 6 年之前
父節點
當前提交
a6d37f4453
共有 2 個文件被更改,包括 57 次插入54 次删除
  1. 32 43
      src/lua/main.cpp
  2. 25 11
      src/macos/NSURLClient.mm

+ 32 - 43
src/lua/main.cpp

@@ -14,7 +14,6 @@
 #	include "windows/SChannelConnection.h"
 #endif
 #ifdef USE_NSURL_BACKEND
-#	import <Foundation/Foundation.h>
 #	include "macos/NSURLClient.h"
 #endif
 
@@ -122,48 +121,38 @@ static int w_request(lua_State *L)
 		lua_pop(L, 1);
 	}
 
-#if defined(USE_NSURL_BACKEND) && defined(__APPLE__)
-	@autoreleasepool
-#elif defined(USE_NSURL_BACKEND) && defined(GNUSTEP)
-	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-#endif
-	{
-		for (size_t i = 0; clients[i]; ++i)
-		{
-			HTTPSClient &client = *clients[i];
-			if (!client.valid())
-				continue;
-
-			auto reply = client.request(req);
-			lua_pushinteger(L, reply.responseCode);
-			w_pushstring(L, reply.body);
-
-			if (advanced)
-			{
-				lua_newtable(L);
-				for (auto header : reply.headers)
-				{
-					w_pushstring(L, header.first);
-					w_pushstring(L, header.second);
-					lua_settable(L, -3);
-				}
-			}
-
-			foundClient = true;
-			break;
-		}
-
-		if (!foundClient)
-		{
-			lua_pushnil(L);
-			lua_pushstring(L, "No applicable implementation found");
-			if (advanced)
-				lua_pushnil(L);
-		}
-	}
-#if defined(USE_NSURL_BACKEND) && defined(GNUSTEP)
-	[pool release];
-#endif
+    for (size_t i = 0; clients[i]; ++i)
+    {
+        HTTPSClient &client = *clients[i];
+        if (!client.valid())
+            continue;
+
+        auto reply = client.request(req);
+        lua_pushinteger(L, reply.responseCode);
+        w_pushstring(L, reply.body);
+
+        if (advanced)
+        {
+            lua_newtable(L);
+            for (const auto &header : reply.headers)
+            {
+                w_pushstring(L, header.first);
+                w_pushstring(L, header.second);
+                lua_settable(L, -3);
+            }
+        }
+
+        foundClient = true;
+        break;
+    }
+
+    if (!foundClient)
+    {
+        lua_pushnil(L);
+        lua_pushstring(L, "No applicable implementation found");
+        if (advanced)
+            lua_pushnil(L);
+    }
 
 	return advanced ? 3 : 2;
 }

+ 25 - 11
src/macos/NSURLClient.mm

@@ -14,7 +14,7 @@ static NSString *toNSString(const std::string &str)
 
 static std::string toCppString(NSData *data)
 {
-	return std::string((const char*) [data bytes], (size_t) [data length]);
+	return std::string((const char*) data.bytes, (size_t) data.length);
 }
 
 static std::string toCppString(NSString *str)
@@ -23,27 +23,41 @@ static std::string toCppString(NSString *str)
 }
 
 HTTPSClient::Reply NSURLClient::request(const HTTPSClient::Request &req)
-{
-	NSURL *url = [NSURL URLWithString: toNSString(req.url)];
-	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url];
+{ @autoreleasepool {
+	NSURL *url = [NSURL URLWithString:toNSString(req.url)];
+	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
 
 	switch(req.method)
 	{
 	case Request::GET:
-		[request setHTTPMethod: @"GET"];
+		[request setHTTPMethod:@"GET"];
 		break;
 	case Request::POST:
-		[request setHTTPMethod: @"POST"];
-		[request setHTTPBody: [NSData dataWithBytesNoCopy: (void*) req.postdata.data() length: req.postdata.size()]];
+		[request setHTTPMethod:@"POST"];
+		[request setHTTPBody:[NSData dataWithBytesNoCopy:(void*) req.postdata.data() length:req.postdata.size()]];
 		break;
 	}
 
 	for (auto &header : req.headers)
 		[request setValue: toNSString(header.second) forHTTPHeaderField: toNSString(header.first)];
 
-	NSHTTPURLResponse *response = nil;
-	NSError *error = nil;
-	NSData *body = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];
+	__block NSHTTPURLResponse *response = nil;
+	__block NSError *error = nil;
+    __block NSData *body = nil;
+
+    dispatch_semaphore_t sem = dispatch_semaphore_create(0);
+
+    NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request
+        completionHandler:^(NSData *data, NSURLResponse *resp, NSError *err) {
+            body = data;
+            response = (NSHTTPURLResponse *)resp;
+            error = err;
+            dispatch_semaphore_signal(sem);
+    }];
+
+    [task resume];
+
+    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
 
 	HTTPSClient::Reply reply;
 	reply.responseCode = 400;
@@ -66,4 +80,4 @@ HTTPSClient::Reply NSURLClient::request(const HTTPSClient::Request &req)
 	}
 
 	return reply;
-}
+} }