123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- // AFHTTPSessionManager.m
- // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- #import "AFHTTPSessionManager.h"
- #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
- #import "AFURLRequestSerialization.h"
- #import "AFURLResponseSerialization.h"
- #import <Availability.h>
- #import <Security/Security.h>
- #ifdef _SYSTEMCONFIGURATION_H
- #import <netinet/in.h>
- #import <netinet6/in6.h>
- #import <arpa/inet.h>
- #import <ifaddrs.h>
- #import <netdb.h>
- #endif
- #if TARGET_OS_IOS
- #import <UIKit/UIKit.h>
- #elif TARGET_OS_WATCH
- #import <WatchKit/WatchKit.h>
- #endif
- @interface AFHTTPSessionManager ()
- @property (readwrite, nonatomic, strong) NSURL *baseURL;
- @end
- @implementation AFHTTPSessionManager
- @dynamic responseSerializer;
- + (instancetype)manager {
- return [[[self class] alloc] initWithBaseURL:nil];
- }
- - (instancetype)init {
- return [self initWithBaseURL:nil];
- }
- - (instancetype)initWithBaseURL:(NSURL *)url {
- return [self initWithBaseURL:url sessionConfiguration:nil];
- }
- - (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration {
- return [self initWithBaseURL:nil sessionConfiguration:configuration];
- }
- - (instancetype)initWithBaseURL:(NSURL *)url
- sessionConfiguration:(NSURLSessionConfiguration *)configuration
- {
- self = [super initWithSessionConfiguration:configuration];
- if (!self) {
- return nil;
- }
- // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected
- if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {
- url = [url URLByAppendingPathComponent:@""];
- }
- self.baseURL = url;
- self.requestSerializer = [AFHTTPRequestSerializer serializer];
- self.responseSerializer = [AFJSONResponseSerializer serializer];
- return self;
- }
- #pragma mark -
- #ifdef _SYSTEMCONFIGURATION_H
- #endif
- - (void)setRequestSerializer:(AFHTTPRequestSerializer <AFURLRequestSerialization> *)requestSerializer {
- NSParameterAssert(requestSerializer);
- _requestSerializer = requestSerializer;
- }
- - (void)setResponseSerializer:(AFHTTPResponseSerializer <AFURLResponseSerialization> *)responseSerializer {
- NSParameterAssert(responseSerializer);
- [super setResponseSerializer:responseSerializer];
- }
- #pragma mark -
- - (NSURLSessionDataTask *)GET:(NSString *)URLString
- parameters:(id)parameters
- success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
- failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
- {
- NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"GET" URLString:URLString parameters:parameters success:success failure:failure];
- [dataTask resume];
- return dataTask;
- }
- - (NSURLSessionDataTask *)HEAD:(NSString *)URLString
- parameters:(id)parameters
- success:(void (^)(NSURLSessionDataTask *task))success
- failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
- {
- NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"HEAD" URLString:URLString parameters:parameters success:^(NSURLSessionDataTask *task, __unused id responseObject) {
- if (success) {
- success(task);
- }
- } failure:failure];
- [dataTask resume];
- return dataTask;
- }
- - (NSURLSessionDataTask *)POST:(NSString *)URLString
- parameters:(id)parameters
- success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
- failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
- {
- NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"POST" URLString:URLString parameters:parameters success:success failure:failure];
- [dataTask resume];
- return dataTask;
- }
- - (NSURLSessionDataTask *)POST:(NSString *)URLString
- parameters:(id)parameters
- constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
- success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
- failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
- {
- NSError *serializationError = nil;
- NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:&serializationError];
- if (serializationError) {
- if (failure) {
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wgnu"
- dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
- failure(nil, serializationError);
- });
- #pragma clang diagnostic pop
- }
- return nil;
- }
- __block NSURLSessionDataTask *task = [self uploadTaskWithStreamedRequest:request progress:nil completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
- if (error) {
- if (failure) {
- failure(task, error);
- }
- } else {
- if (success) {
- success(task, responseObject);
- }
- }
- }];
- [task resume];
- return task;
- }
- - (NSURLSessionDataTask *)PUT:(NSString *)URLString
- parameters:(id)parameters
- success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
- failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
- {
- NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"PUT" URLString:URLString parameters:parameters success:success failure:failure];
- [dataTask resume];
- return dataTask;
- }
- - (NSURLSessionDataTask *)PATCH:(NSString *)URLString
- parameters:(id)parameters
- success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
- failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
- {
- NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"PATCH" URLString:URLString parameters:parameters success:success failure:failure];
- [dataTask resume];
- return dataTask;
- }
- - (NSURLSessionDataTask *)DELETE:(NSString *)URLString
- parameters:(id)parameters
- success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
- failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
- {
- NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"DELETE" URLString:URLString parameters:parameters success:success failure:failure];
- [dataTask resume];
- return dataTask;
- }
- - (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
- URLString:(NSString *)URLString
- parameters:(id)parameters
- success:(void (^)(NSURLSessionDataTask *, id))success
- failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
- {
- NSError *serializationError = nil;
- NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
- if (serializationError) {
- if (failure) {
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wgnu"
- dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
- failure(nil, serializationError);
- });
- #pragma clang diagnostic pop
- }
- return nil;
- }
- __block NSURLSessionDataTask *dataTask = nil;
- dataTask = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
- if (error) {
- if (failure) {
- failure(dataTask, error);
- }
- } else {
- if (success) {
- success(dataTask, responseObject);
- }
- }
- }];
- return dataTask;
- }
- #pragma mark - NSObject
- - (NSString *)description {
- return [NSString stringWithFormat:@"<%@: %p, baseURL: %@, session: %@, operationQueue: %@>", NSStringFromClass([self class]), self, [self.baseURL absoluteString], self.session, self.operationQueue];
- }
- #pragma mark - NSSecureCoding
- + (BOOL)supportsSecureCoding {
- return YES;
- }
- - (id)initWithCoder:(NSCoder *)decoder {
- NSURL *baseURL = [decoder decodeObjectOfClass:[NSURL class] forKey:NSStringFromSelector(@selector(baseURL))];
- NSURLSessionConfiguration *configuration = [decoder decodeObjectOfClass:[NSURLSessionConfiguration class] forKey:@"sessionConfiguration"];
- if (!configuration) {
- NSString *configurationIdentifier = [decoder decodeObjectOfClass:[NSString class] forKey:@"identifier"];
- if (configurationIdentifier) {
- #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)
- configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:configurationIdentifier];
- #else
- configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:configurationIdentifier];
- #endif
- }
- }
- self = [self initWithBaseURL:baseURL sessionConfiguration:configuration];
- if (!self) {
- return nil;
- }
- self.requestSerializer = [decoder decodeObjectOfClass:[AFHTTPRequestSerializer class] forKey:NSStringFromSelector(@selector(requestSerializer))];
- self.responseSerializer = [decoder decodeObjectOfClass:[AFHTTPResponseSerializer class] forKey:NSStringFromSelector(@selector(responseSerializer))];
- return self;
- }
- - (void)encodeWithCoder:(NSCoder *)coder {
- [super encodeWithCoder:coder];
- [coder encodeObject:self.baseURL forKey:NSStringFromSelector(@selector(baseURL))];
- if ([self.session.configuration conformsToProtocol:@protocol(NSCoding)]) {
- [coder encodeObject:self.session.configuration forKey:@"sessionConfiguration"];
- } else {
- [coder encodeObject:self.session.configuration.identifier forKey:@"identifier"];
- }
- [coder encodeObject:self.requestSerializer forKey:NSStringFromSelector(@selector(requestSerializer))];
- [coder encodeObject:self.responseSerializer forKey:NSStringFromSelector(@selector(responseSerializer))];
- }
- #pragma mark - NSCopying
- - (id)copyWithZone:(NSZone *)zone {
- AFHTTPSessionManager *HTTPClient = [[[self class] allocWithZone:zone] initWithBaseURL:self.baseURL sessionConfiguration:self.session.configuration];
- HTTPClient.requestSerializer = [self.requestSerializer copyWithZone:zone];
- HTTPClient.responseSerializer = [self.responseSerializer copyWithZone:zone];
- return HTTPClient;
- }
- @end
- #endif
|