320 lines
12 KiB
Objective-C
320 lines
12 KiB
Objective-C
// AFHTTPSessionManager.m
|
|
//
|
|
// Copyright (c) 2013 AFNetworking (http://afnetworking.com)
|
|
//
|
|
// 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)
|
|
|
|
#import "AFHTTPRequestOperation.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 defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
|
|
#import <UIKit/UIKit.h>
|
|
#endif
|
|
|
|
@interface AFHTTPSessionManager ()
|
|
@property (readwrite, nonatomic, strong) NSURL *baseURL;
|
|
@property (readwrite, nonatomic, strong) AFNetworkReachabilityManager *reachabilityManager;
|
|
@end
|
|
|
|
@implementation AFHTTPSessionManager
|
|
|
|
+ (instancetype)manager {
|
|
return [[AFHTTPSessionManager alloc] initWithBaseURL:nil];
|
|
}
|
|
|
|
- (instancetype)initWithBaseURL:(NSURL *)url {
|
|
return [self initWithBaseURL:url sessionConfiguration:nil];
|
|
}
|
|
|
|
- (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];
|
|
|
|
self.securityPolicy = [AFSecurityPolicy defaultPolicy];
|
|
|
|
if (self.baseURL.host) {
|
|
self.reachabilityManager = [AFNetworkReachabilityManager managerForDomain:self.baseURL.host];
|
|
} else {
|
|
self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (NSString *)description {
|
|
return [NSString stringWithFormat:@"<%@: %p, baseURL: %@, session: %@, operationQueue: %@>", NSStringFromClass([self class]), self, [self.baseURL absoluteString], self.session, self.operationQueue];
|
|
}
|
|
|
|
#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:(NSDictionary *)parameters
|
|
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
|
|
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
|
|
{
|
|
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters];
|
|
|
|
__block NSURLSessionDataTask *task = [self dataTaskWithRequest:request 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 *)HEAD:(NSString *)URLString
|
|
parameters:(NSDictionary *)parameters
|
|
success:(void (^)(NSURLSessionDataTask *task))success
|
|
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
|
|
{
|
|
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"HEAD" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters];
|
|
|
|
__block NSURLSessionDataTask *task = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse * __unused response, id __unused responseObject, NSError *error) {
|
|
if (error) {
|
|
if (failure) {
|
|
failure(task, error);
|
|
}
|
|
} else {
|
|
if (success) {
|
|
success(task);
|
|
}
|
|
}
|
|
}];
|
|
|
|
[task resume];
|
|
|
|
return task;
|
|
}
|
|
|
|
- (NSURLSessionDataTask *)POST:(NSString *)URLString
|
|
parameters:(NSDictionary *)parameters
|
|
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
|
|
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
|
|
{
|
|
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters];
|
|
|
|
__block NSURLSessionDataTask *task = [self dataTaskWithRequest:request 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 *)POST:(NSString *)URLString
|
|
parameters:(NSDictionary *)parameters
|
|
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
|
|
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
|
|
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
|
|
{
|
|
NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block];
|
|
|
|
__block NSURLSessionDataTask *task = [self dataTaskWithRequest:request 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:(NSDictionary *)parameters
|
|
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
|
|
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
|
|
{
|
|
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"PUT" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters];
|
|
|
|
__block NSURLSessionDataTask *task = [self dataTaskWithRequest:request 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 *)PATCH:(NSString *)URLString
|
|
parameters:(NSDictionary *)parameters
|
|
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
|
|
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
|
|
{
|
|
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"PATCH" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters];
|
|
|
|
__block NSURLSessionDataTask *task = [self dataTaskWithRequest:request 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 *)DELETE:(NSString *)URLString
|
|
parameters:(NSDictionary *)parameters
|
|
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
|
|
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
|
|
{
|
|
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"DELETE" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters];
|
|
|
|
__block NSURLSessionDataTask *task = [self dataTaskWithRequest:request 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;
|
|
}
|
|
|
|
#pragma mark - NSCoding
|
|
|
|
- (id)initWithCoder:(NSCoder *)decoder {
|
|
NSURL *baseURL = [decoder decodeObjectForKey:NSStringFromSelector(@selector(baseURL))];
|
|
NSURLSessionConfiguration *configuration = [decoder decodeObjectForKey:@"sessionConfiguration"];
|
|
|
|
self = [self initWithBaseURL:baseURL sessionConfiguration:configuration];
|
|
if (!self) {
|
|
return nil;
|
|
}
|
|
|
|
self.requestSerializer = [decoder decodeObjectForKey:NSStringFromSelector(@selector(requestSerializer))];
|
|
self.responseSerializer = [decoder decodeObjectForKey:NSStringFromSelector(@selector(responseSerializer))];
|
|
|
|
return self;
|
|
}
|
|
|
|
- (void)encodeWithCoder:(NSCoder *)coder {
|
|
[super encodeWithCoder:coder];
|
|
|
|
[coder encodeObject:self.baseURL forKey:NSStringFromSelector(@selector(baseURL))];
|
|
[coder encodeObject:self.session.configuration forKey:@"sessionConfiguration"];
|
|
[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
|