2021-03-26 16:52:45 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2021-03-26 16:52:45 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
2021-03-26 16:04:01 +01:00
|
|
|
#include "td/net/DarwinHttp.h"
|
|
|
|
|
2021-03-27 03:19:22 +01:00
|
|
|
#include "td/utils/logging.h"
|
2022-06-30 15:02:07 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2021-03-27 03:19:22 +01:00
|
|
|
|
2021-03-26 16:04:01 +01:00
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
|
|
|
namespace td {
|
2021-03-26 16:52:45 +01:00
|
|
|
|
2021-03-26 16:04:01 +01:00
|
|
|
namespace {
|
2023-07-21 12:25:32 +02:00
|
|
|
|
|
|
|
NSURLSession *getSession() {
|
|
|
|
static NSURLSession *urlSession = [] {
|
|
|
|
auto configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
|
|
|
|
configuration.networkServiceType = NSURLNetworkServiceTypeResponsiveData;
|
|
|
|
configuration.timeoutIntervalForResource = 90;
|
|
|
|
configuration.waitsForConnectivity = true;
|
|
|
|
return [NSURLSession sessionWithConfiguration:configuration];
|
|
|
|
}();
|
|
|
|
return urlSession;
|
|
|
|
}
|
|
|
|
|
2021-03-26 16:04:01 +01:00
|
|
|
NSString *to_ns_string(CSlice slice) {
|
|
|
|
return [NSString stringWithUTF8String:slice.c_str()];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSData *to_ns_data(Slice data) {
|
2021-03-27 03:19:22 +01:00
|
|
|
return [NSData dataWithBytes:static_cast<const void *>(data.data()) length:data.size()];
|
2021-03-26 16:04:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto http_get(CSlice url) {
|
|
|
|
auto nsurl = [NSURL URLWithString:to_ns_string(url)];
|
|
|
|
auto request = [NSURLRequest requestWithURL:nsurl];
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto http_post(CSlice url, Slice data) {
|
|
|
|
auto nsurl = [NSURL URLWithString:to_ns_string(url)];
|
|
|
|
auto request = [NSMutableURLRequest requestWithURL:nsurl];
|
|
|
|
[request setHTTPMethod:@"POST"];
|
|
|
|
[request setHTTPBody:to_ns_data(data)];
|
|
|
|
[request setValue:@"keep-alive" forHTTPHeaderField:@"Connection"];
|
|
|
|
[request setValue:@"" forHTTPHeaderField:@"Host"];
|
|
|
|
[request setValue:to_ns_string(PSLICE() << data.size()) forHTTPHeaderField:@"Content-Length"];
|
|
|
|
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
2021-03-27 03:19:22 +01:00
|
|
|
void http_send(NSURLRequest *request, Promise<BufferSlice> promise) {
|
2021-03-26 16:04:01 +01:00
|
|
|
__block auto callback = std::move(promise);
|
2021-03-27 03:19:22 +01:00
|
|
|
NSURLSessionDataTask *dataTask =
|
2023-07-21 12:25:32 +02:00
|
|
|
[getSession()
|
2021-03-26 16:04:01 +01:00
|
|
|
dataTaskWithRequest:request
|
|
|
|
completionHandler:
|
|
|
|
^(NSData *data, NSURLResponse *response, NSError *error) {
|
2021-03-27 03:19:22 +01:00
|
|
|
if (error == nil) {
|
2022-06-30 15:02:07 +02:00
|
|
|
callback.set_value(BufferSlice(Slice((const char *)([data bytes]), [data length])));
|
2021-03-26 16:04:01 +01:00
|
|
|
} else {
|
2022-06-30 15:02:07 +02:00
|
|
|
callback.set_error(Status::Error(static_cast<int32>([error code]), "HTTP request failed"));
|
2021-03-26 16:04:01 +01:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
[dataTask resume];
|
|
|
|
}
|
2021-03-26 16:52:45 +01:00
|
|
|
} // namespace
|
2021-03-26 16:04:01 +01:00
|
|
|
|
|
|
|
void DarwinHttp::get(CSlice url, Promise<BufferSlice> promise) {
|
|
|
|
return http_send(http_get(url), std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DarwinHttp::post(CSlice url, Slice data, Promise<BufferSlice> promise) {
|
|
|
|
return http_send(http_post(url, data), std::move(promise));
|
|
|
|
}
|
2021-03-26 16:52:45 +01:00
|
|
|
|
|
|
|
} // namespace td
|