Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions QiniuSDK/Http/QNHttpManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,8 @@ @interface QNHttpManager ()
@property (nonatomic) AFHTTPRequestOperationManager *httpManager;
@end

static NSString *userAgent = nil;

@implementation QNHttpManager

+ (void)initialize {
userAgent = QNUserAgent();
}

- (instancetype)init {
if (self = [super init]) {
_httpManager = [[AFHTTPRequestOperationManager alloc] init];
Expand All @@ -41,14 +35,17 @@ + (QNResponseInfo *)buildResponseInfo:(AFHTTPRequestOperation *)operation
withResponse:(id)responseObject {
QNResponseInfo *info;
NSString *host = operation.request.URL.host;
// NSArray *hostIp = [QNDns getAddresses:host];

if (operation.response) {
NSDictionary *headers = [operation.response allHeaderFields];
NSString *reqId = headers[@"X-Reqid"];
NSString *xlog = headers[@"X-Log"];
NSString *xvia = headers[@"X-Via"];
if (xvia == nil) {
xvia = headers[@"X-Px"];
}
int status = (int)[operation.response statusCode];
info = [[QNResponseInfo alloc] init:status withReqId:reqId withXLog:xlog withHost:host withDuration:duration withBody:responseObject];
info = [[QNResponseInfo alloc] init:status withReqId:reqId withXLog:xlog withXVia:xvia withHost:host withDuration:duration withBody:responseObject];
}
else {
info = [QNResponseInfo responseInfoWithNetError:error host:host duration:duration];
Expand All @@ -69,10 +66,12 @@ - (void) sendRequest:(NSMutableURLRequest *)request
if (info.isOK) {
resp = responseObject;
}
NSLog(@"success %@", info);
completeBlock(info, resp);
} failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
double duration = [[NSDate date] timeIntervalSinceDate:startTime];
QNResponseInfo *info = [QNHttpManager buildResponseInfo:operation withError:error withDuration:duration withResponse:operation.responseData];
NSLog(@"failure %@", info);
completeBlock(info, nil);
}

Expand All @@ -85,7 +84,7 @@ - (void) sendRequest:(NSMutableURLRequest *)request
}
[request setTimeoutInterval:kQNTimeoutInterval];

[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
[request setValue:QNUserAgent() forHTTPHeaderField:@"User-Agent"];
[request setValue:nil forHTTPHeaderField:@"Accept-Language"];
[_httpManager.operationQueue addOperation:operation];
}
Expand Down
10 changes: 8 additions & 2 deletions QiniuSDK/Http/QNResponseInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ extern const int kQNFileError;
*/
@property (nonatomic, copy, readonly) NSString *xlog;

/**
* cdn服务器内部跟踪记录
*/
@property (nonatomic, copy, readonly) NSString *xvia;

/**
* 错误信息,出错时请反馈此记录
*/
Expand All @@ -69,9 +74,9 @@ extern const int kQNFileError;
@property (nonatomic, readonly) NSString *serverIp;

/**
* 服务器IP
* 网络类型
*/
@property (nonatomic, readonly) NSString *networkType;
//@property (nonatomic, readonly) NSString *networkType;

/**
* 是否取消
Expand Down Expand Up @@ -152,6 +157,7 @@ extern const int kQNFileError;
- (instancetype)init:(int)status
withReqId:(NSString *)reqId
withXLog:(NSString *)xlog
withXVia:(NSString *)xvia
withHost:(NSString *)host
withDuration:(double)duration
withBody:(NSData *)body;
Expand Down
4 changes: 3 additions & 1 deletion QiniuSDK/Http/QNResponseInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ - (instancetype)initWithStatus:(int)status
- (instancetype)init:(int)status
withReqId:(NSString *)reqId
withXLog:(NSString *)xlog
withXVia:(NSString *)xvia
withHost:(NSString *)host
withDuration:(double)duration
withBody:(NSData *)body {
if (self = [super init]) {
_statusCode = status;
_reqId = [reqId copy];
_xlog = [xlog copy];
_xvia = [xvia copy];
_host = [host copy];
_duration = duration;
if (status != 200) {
Expand All @@ -105,7 +107,7 @@ - (instancetype)init:(int)status
}

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

- (BOOL)isCancelled {
Expand Down