AFHTTPRequestOperationManager.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // AFHTTPRequestOperationManager.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 <Foundation/Foundation.h>
  22. #import "AFHTTPRequestOperationManager.h"
  23. #import "AFHTTPRequestOperation.h"
  24. #import <Availability.h>
  25. #import <Security/Security.h>
  26. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
  27. #import <UIKit/UIKit.h>
  28. #endif
  29. @interface AFHTTPRequestOperationManager ()
  30. @property (readwrite, nonatomic, strong) NSURL *baseURL;
  31. @end
  32. @implementation AFHTTPRequestOperationManager
  33. + (instancetype)manager {
  34. return [[self alloc] initWithBaseURL:nil];
  35. }
  36. - (instancetype)init {
  37. return [self initWithBaseURL:nil];
  38. }
  39. - (instancetype)initWithBaseURL:(NSURL *)url {
  40. self = [super init];
  41. if (!self) {
  42. return nil;
  43. }
  44. // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected
  45. if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {
  46. url = [url URLByAppendingPathComponent:@""];
  47. }
  48. self.baseURL = url;
  49. self.requestSerializer = [AFHTTPRequestSerializer serializer];
  50. self.responseSerializer = [AFJSONResponseSerializer serializer];
  51. self.securityPolicy = [AFSecurityPolicy defaultPolicy];
  52. self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];
  53. self.operationQueue = [[NSOperationQueue alloc] init];
  54. self.shouldUseCredentialStorage = YES;
  55. return self;
  56. }
  57. #pragma mark -
  58. #ifdef _SYSTEMCONFIGURATION_H
  59. #endif
  60. - (void)setRequestSerializer:(AFHTTPRequestSerializer <AFURLRequestSerialization> *)requestSerializer {
  61. NSParameterAssert(requestSerializer);
  62. _requestSerializer = requestSerializer;
  63. }
  64. - (void)setResponseSerializer:(AFHTTPResponseSerializer <AFURLResponseSerialization> *)responseSerializer {
  65. NSParameterAssert(responseSerializer);
  66. _responseSerializer = responseSerializer;
  67. }
  68. #pragma mark -
  69. - (AFHTTPRequestOperation *)HTTPRequestOperationWithHTTPMethod:(NSString *)method
  70. URLString:(NSString *)URLString
  71. parameters:(id)parameters
  72. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  73. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  74. {
  75. NSError *serializationError = nil;
  76. NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
  77. if (serializationError) {
  78. if (failure) {
  79. #pragma clang diagnostic push
  80. #pragma clang diagnostic ignored "-Wgnu"
  81. dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
  82. failure(nil, serializationError);
  83. });
  84. #pragma clang diagnostic pop
  85. }
  86. return nil;
  87. }
  88. return [self HTTPRequestOperationWithRequest:request success:success failure:failure];
  89. }
  90. - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request
  91. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  92. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  93. {
  94. AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  95. operation.responseSerializer = self.responseSerializer;
  96. operation.shouldUseCredentialStorage = self.shouldUseCredentialStorage;
  97. operation.credential = self.credential;
  98. operation.securityPolicy = self.securityPolicy;
  99. [operation setCompletionBlockWithSuccess:success failure:failure];
  100. operation.completionQueue = self.completionQueue;
  101. operation.completionGroup = self.completionGroup;
  102. return operation;
  103. }
  104. #pragma mark -
  105. - (AFHTTPRequestOperation *)GET:(NSString *)URLString
  106. parameters:(id)parameters
  107. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  108. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  109. {
  110. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"GET" URLString:URLString parameters:parameters success:success failure:failure];
  111. [self.operationQueue addOperation:operation];
  112. return operation;
  113. }
  114. - (AFHTTPRequestOperation *)HEAD:(NSString *)URLString
  115. parameters:(id)parameters
  116. success:(void (^)(AFHTTPRequestOperation *operation))success
  117. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  118. {
  119. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"HEAD" URLString:URLString parameters:parameters success:^(AFHTTPRequestOperation *requestOperation, __unused id responseObject) {
  120. if (success) {
  121. success(requestOperation);
  122. }
  123. } failure:failure];
  124. [self.operationQueue addOperation:operation];
  125. return operation;
  126. }
  127. - (AFHTTPRequestOperation *)POST:(NSString *)URLString
  128. parameters:(id)parameters
  129. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  130. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  131. {
  132. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"POST" URLString:URLString parameters:parameters success:success failure:failure];
  133. [self.operationQueue addOperation:operation];
  134. return operation;
  135. }
  136. - (AFHTTPRequestOperation *)POST:(NSString *)URLString
  137. parameters:(id)parameters
  138. constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
  139. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  140. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  141. {
  142. NSError *serializationError = nil;
  143. NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:&serializationError];
  144. if (serializationError) {
  145. if (failure) {
  146. #pragma clang diagnostic push
  147. #pragma clang diagnostic ignored "-Wgnu"
  148. dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
  149. failure(nil, serializationError);
  150. });
  151. #pragma clang diagnostic pop
  152. }
  153. return nil;
  154. }
  155. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
  156. [self.operationQueue addOperation:operation];
  157. return operation;
  158. }
  159. - (AFHTTPRequestOperation *)PUT:(NSString *)URLString
  160. parameters:(id)parameters
  161. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  162. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  163. {
  164. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"PUT" URLString:URLString parameters:parameters success:success failure:failure];
  165. [self.operationQueue addOperation:operation];
  166. return operation;
  167. }
  168. - (AFHTTPRequestOperation *)PATCH:(NSString *)URLString
  169. parameters:(id)parameters
  170. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  171. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  172. {
  173. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"PATCH" URLString:URLString parameters:parameters success:success failure:failure];
  174. [self.operationQueue addOperation:operation];
  175. return operation;
  176. }
  177. - (AFHTTPRequestOperation *)DELETE:(NSString *)URLString
  178. parameters:(id)parameters
  179. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  180. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  181. {
  182. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"DELETE" URLString:URLString parameters:parameters success:success failure:failure];
  183. [self.operationQueue addOperation:operation];
  184. return operation;
  185. }
  186. #pragma mark - NSObject
  187. - (NSString *)description {
  188. return [NSString stringWithFormat:@"<%@: %p, baseURL: %@, operationQueue: %@>", NSStringFromClass([self class]), self, [self.baseURL absoluteString], self.operationQueue];
  189. }
  190. #pragma mark - NSSecureCoding
  191. + (BOOL)supportsSecureCoding {
  192. return YES;
  193. }
  194. - (id)initWithCoder:(NSCoder *)decoder {
  195. NSURL *baseURL = [decoder decodeObjectForKey:NSStringFromSelector(@selector(baseURL))];
  196. self = [self initWithBaseURL:baseURL];
  197. if (!self) {
  198. return nil;
  199. }
  200. self.requestSerializer = [decoder decodeObjectOfClass:[AFHTTPRequestSerializer class] forKey:NSStringFromSelector(@selector(requestSerializer))];
  201. self.responseSerializer = [decoder decodeObjectOfClass:[AFHTTPResponseSerializer class] forKey:NSStringFromSelector(@selector(responseSerializer))];
  202. return self;
  203. }
  204. - (void)encodeWithCoder:(NSCoder *)coder {
  205. [coder encodeObject:self.baseURL forKey:NSStringFromSelector(@selector(baseURL))];
  206. [coder encodeObject:self.requestSerializer forKey:NSStringFromSelector(@selector(requestSerializer))];
  207. [coder encodeObject:self.responseSerializer forKey:NSStringFromSelector(@selector(responseSerializer))];
  208. }
  209. #pragma mark - NSCopying
  210. - (id)copyWithZone:(NSZone *)zone {
  211. AFHTTPRequestOperationManager *HTTPClient = [[[self class] allocWithZone:zone] initWithBaseURL:self.baseURL];
  212. HTTPClient.requestSerializer = [self.requestSerializer copyWithZone:zone];
  213. HTTPClient.responseSerializer = [self.responseSerializer copyWithZone:zone];
  214. return HTTPClient;
  215. }
  216. @end