Skip to content

Commit 245033e

Browse files
committed
Merge pull request #42 from longbai/ip_backup
ip_backup
2 parents 057b7f8 + fd27380 commit 245033e

File tree

14 files changed

+148
-39
lines changed

14 files changed

+148
-39
lines changed

QiniuSDK.xcodeproj/xcshareddata/xcschemes/QiniuSDK Mac.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0510"
3+
LastUpgradeVersion = "0630"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

QiniuSDK.xcodeproj/xcshareddata/xcschemes/QiniuSDK iOS.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0510"
3+
LastUpgradeVersion = "0630"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

QiniuSDK/Http/QNDns.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414

1515
+ (NSString *)getAddressesString:(NSString *)hostName;
1616

17+
+ (NSString *)getAddress:(NSString *)hostName;
18+
1719
@end

QiniuSDK/Http/QNDns.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ + (NSArray *)getAddresses:(NSString *)hostName {
5454
return ret;
5555
}
5656

57+
+ (NSString *)getAddress:(NSString *)hostName {
58+
NSArray *result = [QNDns getAddresses:hostName];
59+
if (result == nil || result.count == 0) {
60+
return @"";
61+
}
62+
return result[0];
63+
}
64+
5765
+ (NSString *)getAddressesString:(NSString *)hostName {
5866
NSArray *result = [QNDns getAddresses:hostName];
5967
if (result == nil || result.count == 0) {

QiniuSDK/Http/QNHttpManager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
@interface QNHttpManager : NSObject <QNHttpDelegate>
1515

1616
- (instancetype)initWithTimeout:(UInt32)timeout
17-
urlConverter:(QNUrlConvert)converter;
17+
urlConverter:(QNUrlConvert)converter
18+
backupIp:(NSString *)ip;
1819

1920
- (void)multipartPost:(NSString *)url
2021
withData:(NSData *)data

QiniuSDK/Http/QNHttpManager.m

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,40 @@
1212
#import "QNHttpManager.h"
1313
#import "QNUserAgent.h"
1414
#import "QNResponseInfo.h"
15+
#import "QNDns.h"
1516

1617
@interface QNHttpManager ()
1718
@property (nonatomic) AFHTTPRequestOperationManager *httpManager;
1819
@property UInt32 timeout;
1920
@property (nonatomic, strong) QNUrlConvert converter;
21+
@property (nonatomic) NSString *backupIp;
2022
@end
2123

2224
@implementation QNHttpManager
2325

2426
- (instancetype)initWithTimeout:(UInt32)timeout
25-
urlConverter:(QNUrlConvert)converter {
27+
urlConverter:(QNUrlConvert)converter
28+
backupIp:(NSString *)ip {
2629
if (self = [super init]) {
2730
_httpManager = [[AFHTTPRequestOperationManager alloc] init];
2831
_httpManager.responseSerializer = [AFJSONResponseSerializer serializer];
2932
_timeout = timeout;
3033
_converter = converter;
34+
_backupIp = ip;
3135
}
3236

3337
return self;
3438
}
3539

3640
- (instancetype)init {
37-
return [self initWithTimeout:60 urlConverter:nil];
41+
return [self initWithTimeout:60 urlConverter:nil backupIp:nil];
3842
}
3943

4044
+ (QNResponseInfo *)buildResponseInfo:(AFHTTPRequestOperation *)operation
4145
withError:(NSError *)error
4246
withDuration:(double)duration
43-
withResponse:(id)responseObject {
47+
withResponse:(id)responseObject
48+
withIp:(NSString *)ip {
4449
QNResponseInfo *info;
4550
NSString *host = operation.request.URL.host;
4651

@@ -53,7 +58,7 @@ + (QNResponseInfo *)buildResponseInfo:(AFHTTPRequestOperation *)operation
5358
if (xvia == nil) {
5459
xvia = headers[@"X-Px"];
5560
}
56-
info = [[QNResponseInfo alloc] init:status withReqId:reqId withXLog:xlog withXVia:xvia withHost:host withDuration:duration withBody:responseObject];
61+
info = [[QNResponseInfo alloc] init:status withReqId:reqId withXLog:xlog withXVia:xvia withHost:host withIp:ip withDuration:duration withBody:responseObject];
5762
}
5863
else {
5964
info = [QNResponseInfo responseInfoWithNetError:error host:host duration:duration];
@@ -64,12 +69,35 @@ + (QNResponseInfo *)buildResponseInfo:(AFHTTPRequestOperation *)operation
6469
- (void) sendRequest:(NSMutableURLRequest *)request
6570
withCompleteBlock:(QNCompleteBlock)completeBlock
6671
withProgressBlock:(QNInternalProgressBlock)progressBlock {
72+
NSString *u = request.URL.absoluteString;
73+
NSURL *url = request.URL;
74+
__block NSString *ip = nil;
75+
if (_converter != nil) {
76+
url = [[NSURL alloc] initWithString:_converter(u)];
77+
}
78+
else {
79+
if (_backupIp != nil && ![_backupIp isEqualToString:@""]) {
80+
NSString *host = url.host;
81+
ip = [QNDns getAddress:host];
82+
if ([ip isEqualToString:@""]) {
83+
ip = _backupIp;
84+
}
85+
NSString *path = url.path;
86+
if (path == nil || [@"" isEqualToString:path]) {
87+
path = @"/";
88+
}
89+
url = [[NSURL alloc] initWithScheme:url.scheme host:ip path:path];
90+
[request setValue:host forHTTPHeaderField:@"Host"];
91+
}
92+
}
93+
request.URL = url;
94+
6795
__block NSDate *startTime = [NSDate date];
6896
AFHTTPRequestOperation *operation = [_httpManager
6997
HTTPRequestOperationWithRequest:request
7098
success: ^(AFHTTPRequestOperation *operation, id responseObject) {
7199
double duration = [[NSDate date] timeIntervalSinceDate:startTime];
72-
QNResponseInfo *info = [QNHttpManager buildResponseInfo:operation withError:nil withDuration:duration withResponse:operation.responseData];
100+
QNResponseInfo *info = [QNHttpManager buildResponseInfo:operation withError:nil withDuration:duration withResponse:operation.responseData withIp:ip];
73101
NSDictionary *resp = nil;
74102
if (info.isOK) {
75103
resp = responseObject;
@@ -78,11 +106,10 @@ - (void) sendRequest:(NSMutableURLRequest *)request
78106
completeBlock(info, resp);
79107
} failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
80108
double duration = [[NSDate date] timeIntervalSinceDate:startTime];
81-
QNResponseInfo *info = [QNHttpManager buildResponseInfo:operation withError:error withDuration:duration withResponse:operation.responseData];
109+
QNResponseInfo *info = [QNHttpManager buildResponseInfo:operation withError:error withDuration:duration withResponse:operation.responseData withIp:ip];
82110
NSLog(@"failure %@", info);
83111
completeBlock(info, nil);
84112
}
85-
86113
];
87114

88115
if (progressBlock) {
@@ -105,9 +132,6 @@ - (void)multipartPost:(NSString *)url
105132
withCompleteBlock:(QNCompleteBlock)completeBlock
106133
withProgressBlock:(QNInternalProgressBlock)progressBlock
107134
withCancelBlock:(QNCancelBlock)cancelBlock {
108-
if (_converter != nil) {
109-
url = _converter(url);
110-
}
111135
NSMutableURLRequest *request = [_httpManager.requestSerializer
112136
multipartFormRequestWithMethod:@"POST"
113137
URLString:url
@@ -129,10 +153,6 @@ - (void) post:(NSString *)url
129153
withCompleteBlock:(QNCompleteBlock)completeBlock
130154
withProgressBlock:(QNInternalProgressBlock)progressBlock
131155
withCancelBlock:(QNCancelBlock)cancelBlock {
132-
if (_converter != nil) {
133-
url = _converter(url);
134-
}
135-
136156
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:url]];
137157
if (headers) {
138158
[request setAllHTTPHeaderFields:headers];

QiniuSDK/Http/QNResponseInfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ extern const int kQNFileError;
159159
withXLog:(NSString *)xlog
160160
withXVia:(NSString *)xvia
161161
withHost:(NSString *)host
162+
withIp:(NSString *)ip
162163
withDuration:(double)duration
163164
withBody:(NSData *)body;
164165

QiniuSDK/Http/QNResponseInfo.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ - (instancetype)init:(int)status
122122
withXLog:(NSString *)xlog
123123
withXVia:(NSString *)xvia
124124
withHost:(NSString *)host
125+
withIp:(NSString *)ip
125126
withDuration:(double)duration
126127
withBody:(NSData *)body {
127128
if (self = [super init]) {
@@ -131,6 +132,7 @@ - (instancetype)init:(int)status
131132
_xvia = [xvia copy];
132133
_host = [host copy];
133134
_duration = duration;
135+
_serverIp = ip;
134136
if (status != 200) {
135137
if (body == nil) {
136138
_error = [[NSError alloc] initWithDomain:domain code:_statusCode userInfo:nil];
@@ -158,7 +160,7 @@ - (instancetype)init:(int)status
158160
}
159161

160162
- (NSString *)description {
161-
return [NSString stringWithFormat:@"<%@: %p, status: %d, requestId: %@, xlog: %@, xvia: %@, host: %@ duration:%f s error: %@>", NSStringFromClass([self class]), self, _statusCode, _reqId, _xlog, _xvia, _host, _duration, _error];
163+
return [NSString stringWithFormat:@"<%@: %p, status: %d, requestId: %@, xlog: %@, xvia: %@, host: %@ ip: %@ duration:%f s error: %@>", NSStringFromClass([self class]), self, _statusCode, _reqId, _xlog, _xvia, _host, _serverIp, _duration, _error];
162164
}
163165

164166
- (BOOL)isCancelled {

QiniuSDK/Http/QNSessionManager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
- (instancetype)initWithProxy:(NSDictionary *)proxyDict
1111
timeout:(UInt32)timeout
12-
urlConverter:(QNUrlConvert)converter;
12+
urlConverter:(QNUrlConvert)converter
13+
backupIp:(NSString *)ip;
1314

1415
- (void)multipartPost:(NSString *)url
1516
withData:(NSData *)data

QiniuSDK/Http/QNSessionManager.m

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#import "QNUserAgent.h"
1414
#import "QNResponseInfo.h"
1515
#import "QNAsyncRun.h"
16+
#import "QNDns.h"
1617

1718
#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)
1819

@@ -55,36 +56,45 @@ @interface QNSessionManager ()
5556
@property (nonatomic) AFHTTPSessionManager *httpManager;
5657
@property UInt32 timeout;
5758
@property (nonatomic, strong) QNUrlConvert converter;
59+
@property (nonatomic) NSString *backupIp;
60+
@property bool noProxy;
5861
@end
5962

6063
@implementation QNSessionManager
6164

6265
- (instancetype)initWithProxy:(NSDictionary *)proxyDict
6366
timeout:(UInt32)timeout
64-
urlConverter:(QNUrlConvert)converter {
67+
urlConverter:(QNUrlConvert)converter
68+
backupIp:(NSString *)ip {
6569
if (self = [super init]) {
6670
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
6771
if (proxyDict != nil) {
6872
configuration.connectionProxyDictionary = proxyDict;
73+
_noProxy = NO;
74+
}
75+
else {
76+
_noProxy = YES;
6977
}
7078
_httpManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
7179
_httpManager.responseSerializer = [AFHTTPResponseSerializer serializer];
7280
_timeout = timeout;
7381
_converter = converter;
82+
_backupIp = ip;
7483
}
7584

7685
return self;
7786
}
7887

7988
- (instancetype)init {
80-
return [self initWithProxy:nil timeout:60 urlConverter:nil];
89+
return [self initWithProxy:nil timeout:60 urlConverter:nil backupIp:nil];
8190
}
8291

8392
+ (QNResponseInfo *)buildResponseInfo:(NSHTTPURLResponse *)response
8493
withError:(NSError *)error
8594
withDuration:(double)duration
8695
withResponse:(NSData *)body
87-
withHost:(NSString *)host {
96+
withHost:(NSString *)host
97+
withIp:(NSString *)ip {
8898
QNResponseInfo *info;
8999

90100
if (response) {
@@ -99,7 +109,7 @@ + (QNResponseInfo *)buildResponseInfo:(NSHTTPURLResponse *)response
99109
if (xvia == nil) {
100110
xvia = headers[@"Fw-Via"];
101111
}
102-
info = [[QNResponseInfo alloc] init:status withReqId:reqId withXLog:xlog withXVia:xvia withHost:host withDuration:duration withBody:body];
112+
info = [[QNResponseInfo alloc] init:status withReqId:reqId withXLog:xlog withXVia:xvia withHost:host withIp:ip withDuration:duration withBody:body];
103113
}
104114
else {
105115
info = [QNResponseInfo responseInfoWithNetError:error host:host duration:duration];
@@ -113,6 +123,30 @@ - (void) sendRequest:(NSMutableURLRequest *)request
113123
__block NSDate *startTime = [NSDate date];
114124
NSProgress *progress = nil;
115125
__block NSString *host = request.URL.host;
126+
__block NSString *ip = nil;
127+
NSString *u = request.URL.absoluteString;
128+
NSURL *url = request.URL;
129+
if (_converter != nil) {
130+
url = [[NSURL alloc] initWithString:_converter(u)];
131+
host = url.host;
132+
}
133+
else {
134+
if (_noProxy && _backupIp != nil && ![_backupIp isEqualToString:@""]) {
135+
NSString *host = url.host;
136+
ip = [QNDns getAddress:host];
137+
if ([ip isEqualToString:@""]) {
138+
ip = _backupIp;
139+
}
140+
NSString *path = url.path;
141+
if (path == nil || [@"" isEqualToString:path]) {
142+
path = @"/";
143+
}
144+
url = [[NSURL alloc] initWithScheme:url.scheme host:ip path:path];
145+
NSLog(@"%@", url);
146+
[request setValue:host forHTTPHeaderField:@"Host"];
147+
}
148+
}
149+
request.URL = url;
116150

117151
if (progressBlock == nil) {
118152
progressBlock = ^(long long totalBytesWritten, long long totalBytesExpectedToWrite) {
@@ -127,14 +161,14 @@ - (void) sendRequest:(NSMutableURLRequest *)request
127161
QNResponseInfo *info;
128162
NSDictionary *resp = nil;
129163
if (error == nil) {
130-
info = [QNSessionManager buildResponseInfo:httpResponse withError:nil withDuration:duration withResponse:data withHost:host];
164+
info = [QNSessionManager buildResponseInfo:httpResponse withError:nil withDuration:duration withResponse:data withHost:host withIp:ip];
131165
if (info.isOK) {
132166
NSError *tmp;
133167
resp = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&tmp];
134168
}
135169
}
136170
else {
137-
info = [QNSessionManager buildResponseInfo:httpResponse withError:error withDuration:duration withResponse:data withHost:host];
171+
info = [QNSessionManager buildResponseInfo:httpResponse withError:error withDuration:duration withResponse:data withHost:host withIp:ip];
138172
}
139173

140174
if (delegate.progress != nil) {
@@ -163,10 +197,6 @@ - (void)multipartPost:(NSString *)url
163197
withCompleteBlock:(QNCompleteBlock)completeBlock
164198
withProgressBlock:(QNInternalProgressBlock)progressBlock
165199
withCancelBlock:(QNCancelBlock)cancelBlock {
166-
if (_converter != nil) {
167-
url = _converter(url);
168-
}
169-
170200
NSMutableURLRequest *request = [_httpManager.requestSerializer
171201
multipartFormRequestWithMethod:@"POST"
172202
URLString:url
@@ -188,10 +218,6 @@ - (void) post:(NSString *)url
188218
withCompleteBlock:(QNCompleteBlock)completeBlock
189219
withProgressBlock:(QNInternalProgressBlock)progressBlock
190220
withCancelBlock:(QNCancelBlock)cancelBlock {
191-
if (_converter != nil) {
192-
url = _converter(url);
193-
}
194-
195221
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:url]];
196222
if (headers) {
197223
[request setAllHTTPHeaderFields:headers];

0 commit comments

Comments
 (0)