AFHTTPSessionManager.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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)
  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 defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
  35. #import <UIKit/UIKit.h>
  36. #endif
  37. @interface AFHTTPSessionManager ()
  38. @property (readwrite, nonatomic, strong) NSURL *baseURL;
  39. @end
  40. @implementation AFHTTPSessionManager
  41. @dynamic responseSerializer;
  42. + (instancetype)manager {
  43. return [[[self class] alloc] initWithBaseURL:nil];
  44. }
  45. - (instancetype)init {
  46. return [self initWithBaseURL:nil];
  47. }
  48. - (instancetype)initWithBaseURL:(NSURL *)url {
  49. return [self initWithBaseURL:url sessionConfiguration:nil];
  50. }
  51. - (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration {
  52. return [self initWithBaseURL:nil sessionConfiguration:configuration];
  53. }
  54. - (instancetype)initWithBaseURL:(NSURL *)url
  55. sessionConfiguration:(NSURLSessionConfiguration *)configuration
  56. {
  57. self = [super initWithSessionConfiguration:configuration];
  58. if (!self) {
  59. return nil;
  60. }
  61. // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected
  62. if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {
  63. url = [url URLByAppendingPathComponent:@""];
  64. }
  65. self.baseURL = url;
  66. self.requestSerializer = [AFHTTPRequestSerializer serializer];
  67. self.responseSerializer = [AFJSONResponseSerializer serializer];
  68. return self;
  69. }
  70. #pragma mark -
  71. #ifdef _SYSTEMCONFIGURATION_H
  72. #endif
  73. - (void)setRequestSerializer:(AFHTTPRequestSerializer <AFURLRequestSerialization> *)requestSerializer {
  74. NSParameterAssert(requestSerializer);
  75. _requestSerializer = requestSerializer;
  76. }
  77. - (void)setResponseSerializer:(AFHTTPResponseSerializer <AFURLResponseSerialization> *)responseSerializer {
  78. NSParameterAssert(responseSerializer);
  79. [super setResponseSerializer:responseSerializer];
  80. }
  81. #pragma mark -
  82. - (NSURLSessionDataTask *)GET:(NSString *)URLString
  83. parameters:(id)parameters
  84. success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
  85. failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
  86. {
  87. NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"GET" URLString:URLString parameters:parameters success:success failure:failure];
  88. [dataTask resume];
  89. return dataTask;
  90. }
  91. - (NSURLSessionDataTask *)HEAD:(NSString *)URLString
  92. parameters:(id)parameters
  93. success:(void (^)(NSURLSessionDataTask *task))success
  94. failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
  95. {
  96. NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"HEAD" URLString:URLString parameters:parameters success:^(NSURLSessionDataTask *task, __unused id responseObject) {
  97. if (success) {
  98. success(task);
  99. }
  100. } failure:failure];
  101. [dataTask resume];
  102. return dataTask;
  103. }
  104. - (NSURLSessionDataTask *)POST:(NSString *)URLString
  105. parameters:(id)parameters
  106. success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
  107. failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
  108. {
  109. NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"POST" URLString:URLString parameters:parameters success:success failure:failure];
  110. [dataTask resume];
  111. return dataTask;
  112. }
  113. - (NSURLSessionDataTask *)POST:(NSString *)URLString
  114. parameters:(id)parameters
  115. constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
  116. success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
  117. failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
  118. {
  119. NSError *serializationError = nil;
  120. NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:&serializationError];
  121. if (serializationError) {
  122. if (failure) {
  123. #pragma clang diagnostic push
  124. #pragma clang diagnostic ignored "-Wgnu"
  125. dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
  126. failure(nil, serializationError);
  127. });
  128. #pragma clang diagnostic pop
  129. }
  130. return nil;
  131. }
  132. __block NSURLSessionDataTask *task = [self uploadTaskWithStreamedRequest:request progress:nil completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
  133. if (error) {
  134. if (failure) {
  135. failure(task, error);
  136. }
  137. } else {
  138. if (success) {
  139. success(task, responseObject);
  140. }
  141. }
  142. }];
  143. [task resume];
  144. return task;
  145. }
  146. - (NSURLSessionDataTask *)PUT:(NSString *)URLString
  147. parameters:(id)parameters
  148. success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
  149. failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
  150. {
  151. NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"PUT" URLString:URLString parameters:parameters success:success failure:failure];
  152. [dataTask resume];
  153. return dataTask;
  154. }
  155. - (NSURLSessionDataTask *)PATCH:(NSString *)URLString
  156. parameters:(id)parameters
  157. success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
  158. failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
  159. {
  160. NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"PATCH" URLString:URLString parameters:parameters success:success failure:failure];
  161. [dataTask resume];
  162. return dataTask;
  163. }
  164. - (NSURLSessionDataTask *)DELETE:(NSString *)URLString
  165. parameters:(id)parameters
  166. success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
  167. failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
  168. {
  169. NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"DELETE" URLString:URLString parameters:parameters success:success failure:failure];
  170. [dataTask resume];
  171. return dataTask;
  172. }
  173. - (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
  174. URLString:(NSString *)URLString
  175. parameters:(id)parameters
  176. success:(void (^)(NSURLSessionDataTask *, id))success
  177. failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
  178. {
  179. NSError *serializationError = nil;
  180. NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
  181. if (serializationError) {
  182. if (failure) {
  183. #pragma clang diagnostic push
  184. #pragma clang diagnostic ignored "-Wgnu"
  185. dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
  186. failure(nil, serializationError);
  187. });
  188. #pragma clang diagnostic pop
  189. }
  190. return nil;
  191. }
  192. __block NSURLSessionDataTask *dataTask = nil;
  193. dataTask = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
  194. if (error) {
  195. if (failure) {
  196. failure(dataTask, error);
  197. }
  198. } else {
  199. if (success) {
  200. success(dataTask, responseObject);
  201. }
  202. }
  203. }];
  204. return dataTask;
  205. }
  206. #pragma mark - NSObject
  207. - (NSString *)description {
  208. return [NSString stringWithFormat:@"<%@: %p, baseURL: %@, session: %@, operationQueue: %@>", NSStringFromClass([self class]), self, [self.baseURL absoluteString], self.session, self.operationQueue];
  209. }
  210. #pragma mark - NSSecureCoding
  211. + (BOOL)supportsSecureCoding {
  212. return YES;
  213. }
  214. - (id)initWithCoder:(NSCoder *)decoder {
  215. NSURL *baseURL = [decoder decodeObjectOfClass:[NSURL class] forKey:NSStringFromSelector(@selector(baseURL))];
  216. NSURLSessionConfiguration *configuration = [decoder decodeObjectOfClass:[NSURLSessionConfiguration class] forKey:@"sessionConfiguration"];
  217. if (!configuration) {
  218. NSString *configurationIdentifier = [decoder decodeObjectOfClass:[NSString class] forKey:@"identifier"];
  219. if (configurationIdentifier) {
  220. #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)
  221. configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:configurationIdentifier];
  222. #else
  223. configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:configurationIdentifier];
  224. #endif
  225. }
  226. }
  227. self = [self initWithBaseURL:baseURL sessionConfiguration:configuration];
  228. if (!self) {
  229. return nil;
  230. }
  231. self.requestSerializer = [decoder decodeObjectOfClass:[AFHTTPRequestSerializer class] forKey:NSStringFromSelector(@selector(requestSerializer))];
  232. self.responseSerializer = [decoder decodeObjectOfClass:[AFHTTPResponseSerializer class] forKey:NSStringFromSelector(@selector(responseSerializer))];
  233. return self;
  234. }
  235. - (void)encodeWithCoder:(NSCoder *)coder {
  236. [super encodeWithCoder:coder];
  237. [coder encodeObject:self.baseURL forKey:NSStringFromSelector(@selector(baseURL))];
  238. if ([self.session.configuration conformsToProtocol:@protocol(NSCoding)]) {
  239. [coder encodeObject:self.session.configuration forKey:@"sessionConfiguration"];
  240. } else {
  241. [coder encodeObject:self.session.configuration.identifier forKey:@"identifier"];
  242. }
  243. [coder encodeObject:self.requestSerializer forKey:NSStringFromSelector(@selector(requestSerializer))];
  244. [coder encodeObject:self.responseSerializer forKey:NSStringFromSelector(@selector(responseSerializer))];
  245. }
  246. #pragma mark - NSCopying
  247. - (id)copyWithZone:(NSZone *)zone {
  248. AFHTTPSessionManager *HTTPClient = [[[self class] allocWithZone:zone] initWithBaseURL:self.baseURL sessionConfiguration:self.session.configuration];
  249. HTTPClient.requestSerializer = [self.requestSerializer copyWithZone:zone];
  250. HTTPClient.responseSerializer = [self.responseSerializer copyWithZone:zone];
  251. return HTTPClient;
  252. }
  253. @end
  254. #endif