diff options
| author | Arseny Smirnov <arseny30@gmail.com> | 2021-03-26 18:04:01 +0300 |
|---|---|---|
| committer | Arseny Smirnov <arseny30@gmail.com> | 2021-03-26 18:04:25 +0300 |
| commit | 5c7b7d4c85d1966b248e86c1769fdee4fc151f06 (patch) | |
| tree | 292f499926884f1d040f8f522b18ae01eceb4c23 /tdnet | |
| parent | f8667294151d336ac77b99e47a7a01d4c3cc8b6e (diff) | |
TD_EXPERIMENTAL_WATCH_OS cmake option
Diffstat (limited to 'tdnet')
| -rw-r--r-- | tdnet/CMakeLists.txt | 15 | ||||
| -rw-r--r-- | tdnet/td/net/DarwinHttp.h | 13 | ||||
| -rw-r--r-- | tdnet/td/net/DarwinHttp.mm | 57 |
3 files changed, 85 insertions, 0 deletions
diff --git a/tdnet/CMakeLists.txt b/tdnet/CMakeLists.txt index fb0319855..bc2b0f6ef 100644 --- a/tdnet/CMakeLists.txt +++ b/tdnet/CMakeLists.txt @@ -44,8 +44,20 @@ set(TDNET_SOURCE td/net/TcpListener.h td/net/TransparentProxy.h td/net/Wget.h + + td/net/DarwinHttp.mm + td/net/DarwinHttp.h ) +if (TD_EXPERIMENTAL_WATCH_OS) +set (TDNET_SOURCE + ${TDNET_SOURCE} + td/net/DarwinHttp.mm + td/net/DarwinHttp.h +) +set_source_files_properties(td/net/DarwinHttp.mm PROPERTIES COMPILE_FLAGS -fobjc-arc) +endif() + #RULES #LIBRARIES @@ -66,6 +78,9 @@ if (WIN32) endif() endif() +find_library(FOUNDATION_LIBRARY Foundation REQUIRED) +target_link_libraries(tdnet PRIVATE ${FOUNDATION_LIBRARY}) + install(TARGETS tdnet EXPORT TdTargets LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" diff --git a/tdnet/td/net/DarwinHttp.h b/tdnet/td/net/DarwinHttp.h new file mode 100644 index 000000000..7dae980eb --- /dev/null +++ b/tdnet/td/net/DarwinHttp.h @@ -0,0 +1,13 @@ +#pragma once + +#include "td/actor/PromiseFuture.h" + +#include "td/utils/buffer.h" + +namespace td { +class DarwinHttp { + public: + static void get(CSlice url, Promise<BufferSlice> promise); + static void post(CSlice url, Slice data, Promise<BufferSlice> promise); +}; +} // namespace td diff --git a/tdnet/td/net/DarwinHttp.mm b/tdnet/td/net/DarwinHttp.mm new file mode 100644 index 000000000..df545661a --- /dev/null +++ b/tdnet/td/net/DarwinHttp.mm @@ -0,0 +1,57 @@ +#include "td/net/DarwinHttp.h" + +#import <Foundation/Foundation.h> + +namespace td { +namespace { +NSString *to_ns_string(CSlice slice) { + return [NSString stringWithUTF8String:slice.c_str()]; +} + +NSData *to_ns_data(Slice data) { + return [NSData dataWithBytes:static_cast<const void*>(data.data()) length:data.size()]; +} + +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; +} + +void http_send(NSURLRequest *request, Promise<BufferSlice> promise) { + __block auto callback = std::move(promise); + NSURLSessionDataTask* dataTask = + [NSURLSession.sharedSession + dataTaskWithRequest:request + completionHandler: + ^(NSData *data, NSURLResponse *response, NSError *error) { + if(error == nil) { + callback(BufferSlice(Slice((const char *)([data bytes]), [data length]))); + } else { + callback(Status::Error(static_cast<int32>([error code]))); + } + }]; + [dataTask resume]; +} +} + +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)); +} +} |
