Skip to content

Commit 658733d

Browse files
committed
test pass
1 parent 0c34fb1 commit 658733d

File tree

5 files changed

+87
-76
lines changed

5 files changed

+87
-76
lines changed

QiniuSDK/Common/QNAsyncRun.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
void QNAsyncRun(QNRun run) {
1313
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
14-
run();
14+
run();
1515
});
1616
}

QiniuSDK/Http/QNHttpManager.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ + (QNResponseInfo *)buildResponseInfo:(AFHTTPRequestOperation *)operation
3737
NSString *host = operation.request.URL.host;
3838

3939
if (operation.response) {
40+
int status = (int)[operation.response statusCode];
4041
NSDictionary *headers = [operation.response allHeaderFields];
4142
NSString *reqId = headers[@"X-Reqid"];
4243
NSString *xlog = headers[@"X-Log"];
4344
NSString *xvia = headers[@"X-Via"];
4445
if (xvia == nil) {
4546
xvia = headers[@"X-Px"];
4647
}
47-
int status = (int)[operation.response statusCode];
4848
info = [[QNResponseInfo alloc] init:status withReqId:reqId withXLog:xlog withXVia:xvia withHost:host withDuration:duration withBody:responseObject];
4949
}
5050
else {

QiniuSDK/Http/QNSessionManager.m

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,34 @@
2020
@interface QNProgessDelegate : NSObject
2121
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
2222
@property (nonatomic, strong) QNInternalProgressBlock progressBlock;
23-
- (instancetype) initWithProgress:(QNInternalProgressBlock)progressBlock;
23+
@property (nonatomic, strong) NSProgress *progress;
24+
- (instancetype)initWithProgress:(QNInternalProgressBlock)progressBlock;
2425
@end
2526

2627
@implementation QNProgessDelegate
27-
- (instancetype) initWithProgress:(QNInternalProgressBlock)progressBlock{
28-
if (self = [super init]) {
29-
_progressBlock = progressBlock;
30-
}
31-
32-
return self;
28+
- (instancetype)initWithProgress:(QNInternalProgressBlock)progressBlock {
29+
if (self = [super init]) {
30+
_progressBlock = progressBlock;
31+
_progress = nil;
32+
}
33+
34+
return self;
3335
}
3436

3537
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context; {
36-
NSLog(@"log %@ %@, %@, %p", keyPath, object, change, context);
37-
if (context == nil || object == nil) {
38-
return;
39-
}
40-
41-
NSProgress *progress = (NSProgress *)object;
42-
43-
void *p = (__bridge void *)(self);
44-
if (p != context) {
45-
return;
46-
}
47-
_progressBlock(progress.completedUnitCount, progress.totalUnitCount);
38+
if (context == nil || object == nil) {
39+
return;
40+
}
41+
42+
NSProgress *progress = (NSProgress *)object;
43+
44+
void *p = (__bridge void *)(self);
45+
if (p == context) {
46+
_progressBlock(progress.completedUnitCount, progress.totalUnitCount);
47+
}
48+
else {
49+
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
50+
}
4851
}
4952

5053
@end
@@ -82,11 +85,18 @@ + (QNResponseInfo *)buildResponseInfo:(NSHTTPURLResponse *)response
8285
QNResponseInfo *info;
8386

8487
if (response) {
88+
int status = (int)[response statusCode];
8589
NSDictionary *headers = [response allHeaderFields];
8690
NSString *reqId = headers[@"X-Reqid"];
8791
NSString *xlog = headers[@"X-Log"];
88-
int status = (int)[response statusCode];
89-
info = [[QNResponseInfo alloc] init:status withReqId:reqId withXLog:xlog withHost:host withDuration:duration withBody:body];
92+
NSString *xvia = headers[@"X-Via"];
93+
if (xvia == nil) {
94+
xvia = headers[@"X-Px"];
95+
}
96+
if (xvia == nil) {
97+
xvia = headers[@"Fw-Via"];
98+
}
99+
info = [[QNResponseInfo alloc] init:status withReqId:reqId withXLog:xlog withXVia:xvia withHost:host withDuration:duration withBody:body];
90100
}
91101
else {
92102
info = [QNResponseInfo responseInfoWithNetError:error host:host duration:duration];
@@ -100,15 +110,14 @@ - (void) sendRequest:(NSMutableURLRequest *)request
100110
__block NSDate *startTime = [NSDate date];
101111
NSProgress *progress = nil;
102112
__block NSString *host = request.URL.host;
103-
104-
__block QNProgessDelegate *delegate = nil;
105-
if (progressBlock == nil) {
106-
progressBlock = ^(long long totalBytesWritten, long long totalBytesExpectedToWrite) {
107-
};
108-
}
109-
delegate = [[QNProgessDelegate alloc] initWithProgress:progressBlock];
110-
111-
NSURLSessionUploadTask *uploadTask = [_httpManager uploadTaskWithStreamedRequest:request progress:&progress completionHandler: ^(NSURLResponse *response, id responseObject, NSError *error) {
113+
114+
if (progressBlock == nil) {
115+
progressBlock = ^(long long totalBytesWritten, long long totalBytesExpectedToWrite) {
116+
};
117+
}
118+
__block QNProgessDelegate *delegate = [[QNProgessDelegate alloc] initWithProgress:progressBlock];
119+
120+
NSURLSessionUploadTask *uploadTask = [_httpManager uploadTaskWithStreamedRequest:request progress:&progress completionHandler: ^(NSURLResponse *response, id responseObject, NSError *error) {
112121
NSData *data = responseObject;
113122
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
114123
double duration = [[NSDate date] timeIntervalSinceDate:startTime];
@@ -124,11 +133,17 @@ - (void) sendRequest:(NSMutableURLRequest *)request
124133
else {
125134
info = [QNSessionManager buildResponseInfo:httpResponse withError:error withDuration:duration withResponse:data withHost:host];
126135
}
127-
NSLog(@"finish %p %p", progress, delegate);
128-
[progress removeObserver:delegate forKeyPath:@"fractionCompleted" context:(__bridge void *)(delegate)];
129-
completeBlock(info, resp);
136+
137+
if (delegate.progress != nil) {
138+
[delegate.progress removeObserver:delegate forKeyPath:@"fractionCompleted" context:(__bridge void *)(delegate)];
139+
delegate.progress = nil;
140+
}
141+
completeBlock(info, resp);
130142
}];
131-
[progress addObserver:delegate forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:(__bridge void *)(progressBlock)];
143+
if (progress != nil) {
144+
[progress addObserver:delegate forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:(__bridge void *)delegate];
145+
delegate.progress = progress;
146+
}
132147

133148
[request setTimeoutInterval:kQNTimeoutInterval];
134149

@@ -177,17 +192,13 @@ - (void) post:(NSString *)url
177192
[request setValuesForKeysWithDictionary:params];
178193
}
179194
[request setHTTPBody:data];
180-
QNAsyncRun(^{
181-
[self sendRequest:request
182-
withCompleteBlock:completeBlock
183-
withProgressBlock:progressBlock];
184-
});
185-
}
186-
187-
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context; {
195+
QNAsyncRun( ^{
196+
[self sendRequest:request
197+
withCompleteBlock:completeBlock
198+
withProgressBlock:progressBlock];
199+
});
188200
}
189201

190-
191202
@end
192203

193204
#endif

QiniuSDK/Storage/QNUploadManager.m

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ + (instancetype)sharedInstanceWithRecorder:(id <QNRecorderDelegate> )recorder
7979

8080
static dispatch_once_t onceToken;
8181
dispatch_once(&onceToken, ^{
82-
sharedInstance = [[self alloc] initWithRecorder:recorder recorderKeyGenerator:recorderKeyGenerator];
82+
sharedInstance = [[self alloc] initWithRecorder:recorder recorderKeyGenerator:recorderKeyGenerator];
8383
});
8484

8585
return sharedInstance;
@@ -103,8 +103,8 @@ + (BOOL)checkAndNotifyError:(NSString *)key
103103
desc = @"no token";
104104
}
105105
if (desc != nil) {
106-
QNAsyncRun ( ^{
107-
completionHandler([QNResponseInfo responseInfoWithInvalidArgument:desc], key, nil);
106+
QNAsyncRun( ^{
107+
completionHandler([QNResponseInfo responseInfoWithInvalidArgument:desc], key, nil);
108108
});
109109
return YES;
110110
}
@@ -121,13 +121,13 @@ - (void)putData:(NSData *)data
121121
}
122122
QNFormUpload *up = [[QNFormUpload alloc]
123123
initWithData:data
124-
withKey:key
125-
withToken:token
126-
withCompletionHandler:completionHandler
127-
withOption:option
128-
withHttpManager:_httpManager];
129-
QNAsyncRun ( ^{
130-
[up put];
124+
withKey:key
125+
withToken:token
126+
withCompletionHandler:completionHandler
127+
withOption:option
128+
withHttpManager:_httpManager];
129+
QNAsyncRun( ^{
130+
[up put];
131131
});
132132
}
133133

@@ -145,9 +145,9 @@ - (void)putFile:(NSString *)filePath
145145
NSDictionary *fileAttr = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error];
146146

147147
if (error) {
148-
QNAsyncRun ( ^{
149-
QNResponseInfo *info = [QNResponseInfo responseInfoWithFileError:error];
150-
completionHandler(info, key, nil);
148+
QNAsyncRun( ^{
149+
QNResponseInfo *info = [QNResponseInfo responseInfoWithFileError:error];
150+
completionHandler(info, key, nil);
151151
});
152152
return;
153153
}
@@ -156,9 +156,9 @@ - (void)putFile:(NSString *)filePath
156156
UInt32 fileSize = [fileSizeNumber intValue];
157157
NSData *data = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];
158158
if (error) {
159-
QNAsyncRun ( ^{
160-
QNResponseInfo *info = [QNResponseInfo responseInfoWithFileError:error];
161-
completionHandler(info, key, nil);
159+
QNAsyncRun( ^{
160+
QNResponseInfo *info = [QNResponseInfo responseInfoWithFileError:error];
161+
completionHandler(info, key, nil);
162162
});
163163
return;
164164
}
@@ -180,17 +180,17 @@ - (void)putFile:(NSString *)filePath
180180

181181
QNResumeUpload *up = [[QNResumeUpload alloc]
182182
initWithData:data
183-
withSize:fileSize
184-
withKey:key
185-
withToken:token
186-
withCompletionHandler:complete
187-
withOption:option
188-
withModifyTime:modifyTime
189-
withRecorder:_recorder
190-
withRecorderKey:recorderKey
191-
withHttpManager:_httpManager];
192-
QNAsyncRun ( ^{
193-
[up run];
183+
withSize:fileSize
184+
withKey:key
185+
withToken:token
186+
withCompletionHandler:complete
187+
withOption:option
188+
withModifyTime:modifyTime
189+
withRecorder:_recorder
190+
withRecorderKey:recorderKey
191+
withHttpManager:_httpManager];
192+
QNAsyncRun( ^{
193+
[up run];
194194
});
195195
}
196196
}

QiniuSDKTests/QNSessionTest.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ - (void)testPost {
4343

4444
testInfo = nil;
4545

46-
[_httpManager post:@"http://up.qiniu.com" withData:nil withParams:nil withHeaders:nil withCompleteBlock: ^(QNResponseInfo *info, NSDictionary *resp) {
46+
[_httpManager post:@"http://up.qiniu.com" withData:data withParams:nil withHeaders:nil withCompleteBlock: ^(QNResponseInfo *info, NSDictionary *resp) {
4747
testInfo = info;
4848
} withProgressBlock:nil withCancelBlock:nil];
4949

@@ -52,7 +52,7 @@ - (void)testPost {
5252
XCTAssert(testInfo.reqId, @"Pass");
5353

5454
testInfo = nil;
55-
[_httpManager post:@"http://httpbin.org/status/500" withData:nil withParams:nil withHeaders:nil withCompleteBlock: ^(QNResponseInfo *info, NSDictionary *resp) {
55+
[_httpManager post:@"http://httpbin.org/status/500" withData:data withParams:nil withHeaders:nil withCompleteBlock: ^(QNResponseInfo *info, NSDictionary *resp) {
5656
testInfo = info;
5757
} withProgressBlock:nil withCancelBlock:nil];
5858

@@ -62,7 +62,7 @@ - (void)testPost {
6262
XCTAssert(testInfo.error != nil, @"Pass");
6363

6464
testInfo = nil;
65-
[_httpManager post:@"http://httpbin.org/status/418" withData:nil withParams:nil withHeaders:nil withCompleteBlock: ^(QNResponseInfo *info, NSDictionary *resp) {
65+
[_httpManager post:@"http://httpbin.org/status/418" withData:data withParams:nil withHeaders:nil withCompleteBlock: ^(QNResponseInfo *info, NSDictionary *resp) {
6666
testInfo = info;
6767
} withProgressBlock:nil withCancelBlock:nil];
6868

@@ -72,7 +72,7 @@ - (void)testPost {
7272
XCTAssert(testInfo.error != nil, @"Pass");
7373

7474
testInfo = nil;
75-
[_httpManager post:@"http://httpbin.org/status/200" withData:nil withParams:nil withHeaders:nil withCompleteBlock: ^(QNResponseInfo *info, NSDictionary *resp) {
75+
[_httpManager post:@"http://httpbin.org/status/200" withData:data withParams:nil withHeaders:nil withCompleteBlock: ^(QNResponseInfo *info, NSDictionary *resp) {
7676
testInfo = info;
7777
} withProgressBlock:nil withCancelBlock:nil];
7878

0 commit comments

Comments
 (0)