Skip to content

Commit a6d37f4

Browse files
committed
macOS: Use NSURLSession API instead of NSURLConnection since the latter is deprecated. Move autoreleasepool to inside the request implementation.
1 parent 7fe0406 commit a6d37f4

2 files changed

Lines changed: 57 additions & 54 deletions

File tree

src/lua/main.cpp

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
# include "windows/SChannelConnection.h"
1515
#endif
1616
#ifdef USE_NSURL_BACKEND
17-
# import <Foundation/Foundation.h>
1817
# include "macos/NSURLClient.h"
1918
#endif
2019

@@ -122,48 +121,38 @@ static int w_request(lua_State *L)
122121
lua_pop(L, 1);
123122
}
124123

125-
#if defined(USE_NSURL_BACKEND) && defined(__APPLE__)
126-
@autoreleasepool
127-
#elif defined(USE_NSURL_BACKEND) && defined(GNUSTEP)
128-
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
129-
#endif
130-
{
131-
for (size_t i = 0; clients[i]; ++i)
132-
{
133-
HTTPSClient &client = *clients[i];
134-
if (!client.valid())
135-
continue;
136-
137-
auto reply = client.request(req);
138-
lua_pushinteger(L, reply.responseCode);
139-
w_pushstring(L, reply.body);
140-
141-
if (advanced)
142-
{
143-
lua_newtable(L);
144-
for (auto header : reply.headers)
145-
{
146-
w_pushstring(L, header.first);
147-
w_pushstring(L, header.second);
148-
lua_settable(L, -3);
149-
}
150-
}
151-
152-
foundClient = true;
153-
break;
154-
}
155-
156-
if (!foundClient)
157-
{
158-
lua_pushnil(L);
159-
lua_pushstring(L, "No applicable implementation found");
160-
if (advanced)
161-
lua_pushnil(L);
162-
}
163-
}
164-
#if defined(USE_NSURL_BACKEND) && defined(GNUSTEP)
165-
[pool release];
166-
#endif
124+
for (size_t i = 0; clients[i]; ++i)
125+
{
126+
HTTPSClient &client = *clients[i];
127+
if (!client.valid())
128+
continue;
129+
130+
auto reply = client.request(req);
131+
lua_pushinteger(L, reply.responseCode);
132+
w_pushstring(L, reply.body);
133+
134+
if (advanced)
135+
{
136+
lua_newtable(L);
137+
for (const auto &header : reply.headers)
138+
{
139+
w_pushstring(L, header.first);
140+
w_pushstring(L, header.second);
141+
lua_settable(L, -3);
142+
}
143+
}
144+
145+
foundClient = true;
146+
break;
147+
}
148+
149+
if (!foundClient)
150+
{
151+
lua_pushnil(L);
152+
lua_pushstring(L, "No applicable implementation found");
153+
if (advanced)
154+
lua_pushnil(L);
155+
}
167156

168157
return advanced ? 3 : 2;
169158
}

src/macos/NSURLClient.mm

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
static std::string toCppString(NSData *data)
1616
{
17-
return std::string((const char*) [data bytes], (size_t) [data length]);
17+
return std::string((const char*) data.bytes, (size_t) data.length);
1818
}
1919

2020
static std::string toCppString(NSString *str)
@@ -23,27 +23,41 @@
2323
}
2424

2525
HTTPSClient::Reply NSURLClient::request(const HTTPSClient::Request &req)
26-
{
27-
NSURL *url = [NSURL URLWithString: toNSString(req.url)];
28-
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url];
26+
{ @autoreleasepool {
27+
NSURL *url = [NSURL URLWithString:toNSString(req.url)];
28+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
2929

3030
switch(req.method)
3131
{
3232
case Request::GET:
33-
[request setHTTPMethod: @"GET"];
33+
[request setHTTPMethod:@"GET"];
3434
break;
3535
case Request::POST:
36-
[request setHTTPMethod: @"POST"];
37-
[request setHTTPBody: [NSData dataWithBytesNoCopy: (void*) req.postdata.data() length: req.postdata.size()]];
36+
[request setHTTPMethod:@"POST"];
37+
[request setHTTPBody:[NSData dataWithBytesNoCopy:(void*) req.postdata.data() length:req.postdata.size()]];
3838
break;
3939
}
4040

4141
for (auto &header : req.headers)
4242
[request setValue: toNSString(header.second) forHTTPHeaderField: toNSString(header.first)];
4343

44-
NSHTTPURLResponse *response = nil;
45-
NSError *error = nil;
46-
NSData *body = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];
44+
__block NSHTTPURLResponse *response = nil;
45+
__block NSError *error = nil;
46+
__block NSData *body = nil;
47+
48+
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
49+
50+
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request
51+
completionHandler:^(NSData *data, NSURLResponse *resp, NSError *err) {
52+
body = data;
53+
response = (NSHTTPURLResponse *)resp;
54+
error = err;
55+
dispatch_semaphore_signal(sem);
56+
}];
57+
58+
[task resume];
59+
60+
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
4761

4862
HTTPSClient::Reply reply;
4963
reply.responseCode = 400;
@@ -66,4 +80,4 @@
6680
}
6781

6882
return reply;
69-
}
83+
} }

0 commit comments

Comments
 (0)