AFHTTPSessionManager.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // AFHTTPSessionManager.m
  2. // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. #import "AFHTTPSessionManager.h"
  22. #if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000) || (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090) || TARGET_WATCH_OS
  23. #import "AFURLRequestSerialization.h"
  24. #import "AFURLResponseSerialization.h"
  25. #import <Availability.h>
  26. #import <Security/Security.h>
  27. #ifdef _SYSTEMCONFIGURATION_H
  28. #import <netinet/in.h>
  29. #import <netinet6/in6.h>
  30. #import <arpa/inet.h>
  31. #import <ifaddrs.h>
  32. #import <netdb.h>
  33. #endif
  34. #if TARGET_OS_IOS
  35. #import <UIKit/UIKit.h>
  36. #elif TARGET_OS_WATCH
  37. #import <WatchKit/WatchKit.h>
  38. #endif
  39. @interface AFHTTPSessionManager ()
  40. @property (readwrite, nonatomic, strong) NSURL *baseURL;
  41. @end
  42. @implementation AFHTTPSessionManager
  43. @dynamic responseSerializer;
  44. + (instancetype)manager {
  45. return [[[self class] alloc] initWithBaseURL:nil];
  46. }
  47. - (instancetype)init {
  48. return [self initWithBaseURL:nil];
  49. }
  50. - (instancetype)initWithBaseURL:(NSURL *)url {
  51. return [self initWithBaseURL:url sessionConfiguration:nil];
  52. }
  53. - (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration {
  54. return [self initWithBaseURL:nil sessionConfiguration:configuration];
  55. }
  56. - (instancetype)initWithBaseURL:(NSURL *)url
  57. sessionConfiguration:(NSURLSessionConfiguration *)configuration
  58. {
  59. self = [super initWithSessionConfiguration:configuration];
  60. if (!self) {
  61. return nil;
  62. }
  63. // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected
  64. if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {
  65. url = [url URLByAppendingPathComponent:@""];
  66. }
  67. self.baseURL = url;
  68. self.requestSerializer = [AFHTTPRequestSerializer serializer];
  69. self.responseSerializer = [AFJSONResponseSerializer serializer];
  70. return self;
  71. }
  72. #pragma mark -
  73. #ifdef _SYSTEMCONFIGURATION_H
  74. #endif
  75. - (void)setRequestSerializer:(AFHTTPRequestSerializer <AFURLRequestSerialization> *)requestSerializer {
  76. NSParameterAssert(requestSerializer);
  77. _requestSerializer = requestSerializer;
  78. }
  79. - (void)setResponseSerializer:(AFHTTPResponseSerializer <AFURLResponseSerialization> *)responseSerializer {
  80. NSParameterAssert(responseSerializer);
  81. [super setResponseSerializer:responseSerializer];
  82. }
  83. #pragma mark -
  84. - (NSURLSessionDataTask *)GET:(NSString *)URLString
  85. parameters:(id)parameters
  86. success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
  87. failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
  88. {
  89. NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"GET" URLString:URLString parameters:parameters success:success failure:failure];
  90. [dataTask resume];
  91. return dataTask;
  92. }
  93. - (NSURLSessionDataTask *)HEAD:(NSString *)URLString
  94. parameters:(id)parameters
  95. success:(void (^)(NSURLSessionDataTask *task))success
  96. failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
  97. {
  98. NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"HEAD" URLString:URLString parameters:parameters success:^(NSURLSessionDataTask *task, __unused id responseObject) {
  99. if (success) {
  100. success(task);
  101. }
  102. } failure:failure];
  103. [dataTask resume];
  104. return dataTask;
  105. }
  106. - (NSURLSessionDataTask *)POST:(NSString *)URLString
  107. parameters:(id)parameters
  108. success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
  109. failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
  110. {
  111. NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"POST" URLString:URLString parameters:parameters success:success failure:failure];
  112. [dataTask resume];
  113. return dataTask;
  114. }
  115. - (NSURLSessionDataTask *)POST:(NSString *)URLString
  116. parameters:(id)parameters
  117. constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
  118. success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
  119. failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
  120. {
  121. NSError *serializationError = nil;
  122. NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:&serializationError];
  123. if (serializationError) {
  124. if (failure) {
  125. #pragma clang diagnostic push
  126. #pragma clang diagnostic ignored "-Wgnu"
  127. dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
  128. failure(nil, serializationError);
  129. });
  130. #pragma clang diagnostic pop
  131. }
  132. return nil;
  133. }
  134. __block NSURLSessionDataTask *task = [self uploadTaskWithStreamedRequest:request progress:nil completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
  135. if (error) {
  136. if (failure) {
  137. failure(task, error);
  138. }
  139. } else {
  140. if (success) {
  141. success(task, responseObject);
  142. }
  143. }
  144. }];
  145. [task resume];
  146. return task;
  147. }
  148. - (NSURLSessionDataTask *)PUT:(NSString *)URLString
  149. parameters:(id)parameters
  150. success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
  151. failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
  152. {
  153. NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"PUT" URLString:URLString parameters:parameters success:success failure:failure];
  154. [dataTask resume];
  155. return dataTask;
  156. }
  157. - (NSURLSessionDataTask *)PATCH:(NSString *)URLString
  158. parameters:(id)parameters
  159. success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
  160. failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
  161. {
  162. NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"PATCH" URLString:URLString parameters:parameters success:success failure:failure];
  163. [dataTask resume];
  164. return dataTask;
  165. }
  166. - (NSURLSessionDataTask *)DELETE:(NSString *)URLString
  167. parameters:(id)parameters
  168. success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
  169. failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
  170. {
  171. NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"DELETE" URLString:URLString parameters:parameters success:success failure:failure];
  172. [dataTask resume];
  173. return dataTask;
  174. }
  175. - (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
  176. URLString:(NSString *)URLString
  177. parameters:(id)parameters
  178. success:(void (^)(NSURLSessionDataTask *, id))success
  179. failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
  180. {
  181. NSError *serializationError = nil;
  182. NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
  183. if (serializationError) {
  184. if (failure) {
  185. #pragma clang diagnostic push
  186. #pragma clang diagnostic ignored "-Wgnu"
  187. dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
  188. failure(nil, serializationError);
  189. });
  190. #pragma clang diagnostic pop
  191. }
  192. return nil;
  193. }
  194. __block NSURLSessionDataTask *dataTask = nil;
  195. dataTask = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
  196. if (error) {
  197. if (failure) {
  198. failure(dataTask, error);
  199. }
  200. } else {
  201. if (success) {
  202. success(dataTask, responseObject);
  203. }
  204. }
  205. }];
  206. return dataTask;
  207. }
  208. #pragma mark - NSObject
  209. - (NSString *)description {
  210. return [NSString stringWithFormat:@"<%@: %p, baseURL: %@, session: %@, operationQueue: %@>", NSStringFromClass([self class]), self, [self.baseURL absoluteString], self.session, self.operationQueue];
  211. }
  212. #pragma mark - NSSecureCoding
  213. + (BOOL)supportsSecureCoding {
  214. return YES;
  215. }
  216. - (id)initWithCoder:(NSCoder *)decoder {
  217. NSURL *baseURL = [decoder decodeObjectOfClass:[NSURL class] forKey:NSStringFromSelector(@selector(baseURL))];
  218. NSURLSessionConfiguration *configuration = [decoder decodeObjectOfClass:[NSURLSessionConfiguration class] forKey:@"sessionConfiguration"];
  219. if (!configuration) {
  220. NSString *configurationIdentifier = [decoder decodeObjectOfClass:[NSString class] forKey:@"identifier"];
  221. if (configurationIdentifier) {
  222. #if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1100)
  223. configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:configurationIdentifier];
  224. #else
  225. configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:configurationIdentifier];
  226. #endif
  227. }
  228. }
  229. self = [self initWithBaseURL:baseURL sessionConfiguration:configuration];
  230. if (!self) {
  231. return nil;
  232. }
  233. self.requestSerializer = [decoder decodeObjectOfClass:[AFHTTPRequestSerializer class] forKey:NSStringFromSelector(@selector(requestSerializer))];
  234. self.responseSerializer = [decoder decodeObjectOfClass:[AFHTTPResponseSerializer class] forKey:NSStringFromSelector(@selector(responseSerializer))];
  235. return self;
  236. }
  237. - (void)encodeWithCoder:(NSCoder *)coder {
  238. [super encodeWithCoder:coder];
  239. [coder encodeObject:self.baseURL forKey:NSStringFromSelector(@selector(baseURL))];
  240. if ([self.session.configuration conformsToProtocol:@protocol(NSCoding)]) {
  241. [coder encodeObject:self.session.configuration forKey:@"sessionConfiguration"];
  242. } else {
  243. [coder encodeObject:self.session.configuration.identifier forKey:@"identifier"];
  244. }
  245. [coder encodeObject:self.requestSerializer forKey:NSStringFromSelector(@selector(requestSerializer))];
  246. [coder encodeObject:self.responseSerializer forKey:NSStringFromSelector(@selector(responseSerializer))];
  247. }
  248. #pragma mark - NSCopying
  249. - (id)copyWithZone:(NSZone *)zone {
  250. AFHTTPSessionManager *HTTPClient = [[[self class] allocWithZone:zone] initWithBaseURL:self.baseURL sessionConfiguration:self.session.configuration];
  251. HTTPClient.requestSerializer = [self.requestSerializer copyWithZone:zone];
  252. HTTPClient.responseSerializer = [self.responseSerializer copyWithZone:zone];
  253. return HTTPClient;
  254. }
  255. @end
  256. #endif