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];
0 commit comments