Skip to content

Commit 18b2196

Browse files
committed
Merge pull request #21 from longbai/iOS-7.x
use AFHttpSessionManager
2 parents 1473c5e + 4e6c401 commit 18b2196

File tree

15 files changed

+468
-45
lines changed

15 files changed

+468
-45
lines changed

Podfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ source 'https://github.com/CocoaPods/Specs.git'
22

33
target "QiniuSDK iOS" do
44
platform :ios, "6.0"
5-
pod 'AFNetworking', '~> 2.0'
5+
pod 'AFNetworking', '>= 2.4'
66
end
77

88
target "QiniuSDK iOSTests" do
@@ -12,7 +12,7 @@ end
1212

1313
target "QiniuSDK Mac" do
1414
platform :osx, "10.8"
15-
pod 'AFNetworking', '~> 2.0'
15+
pod 'AFNetworking', '>= 2.4'
1616
end
1717

1818
target "QiniuSDK MacTests" do

QiniuSDK.xcodeproj/project.pbxproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@
103103
DFBC622919DE578600458C4B /* QNConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = DFBC622719DE578600458C4B /* QNConfig.m */; };
104104
DFBC622A19DE5E2500458C4B /* QNConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = DFBC622719DE578600458C4B /* QNConfig.m */; };
105105
DFBC622B19DE5E3200458C4B /* QNConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = DFBC622719DE578600458C4B /* QNConfig.m */; };
106+
DFF525311A6235D100D02BA1 /* QNSessionManager.h in Headers */ = {isa = PBXBuildFile; fileRef = DFF5252F1A6235D100D02BA1 /* QNSessionManager.h */; };
107+
DFF525321A6235D100D02BA1 /* QNSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525301A6235D100D02BA1 /* QNSessionManager.m */; };
108+
DFF525331A6235D100D02BA1 /* QNSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525301A6235D100D02BA1 /* QNSessionManager.m */; };
109+
DFF525341A6235D100D02BA1 /* QNSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525301A6235D100D02BA1 /* QNSessionManager.m */; };
110+
DFF525351A6235D100D02BA1 /* QNSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525301A6235D100D02BA1 /* QNSessionManager.m */; };
111+
DFF525371A626A3700D02BA1 /* QNhttpDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = DFF525361A626A3700D02BA1 /* QNhttpDelegate.h */; };
112+
DFF525391A64079B00D02BA1 /* QNSessionTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525381A64079B00D02BA1 /* QNSessionTest.m */; };
113+
DFF5253A1A64079B00D02BA1 /* QNSessionTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525381A64079B00D02BA1 /* QNSessionTest.m */; };
106114
DFFE0E6019E6575600D7A0FC /* QNFileRecorderTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFFE0E5F19E6575600D7A0FC /* QNFileRecorderTest.m */; };
107115
DFFE0E6119E6575600D7A0FC /* QNFileRecorderTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFFE0E5F19E6575600D7A0FC /* QNFileRecorderTest.m */; };
108116
EE76B381AF33E356C2C8FC56 /* libPods-QiniuSDK MacTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 0EB0B4B6BA4EEC2DEF421644 /* libPods-QiniuSDK MacTests.a */; };
@@ -181,6 +189,10 @@
181189
DFA9B65E19E391A100A15FD1 /* QNTestConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNTestConfig.h; sourceTree = "<group>"; };
182190
DFBC622419DE459800458C4B /* QNHttpTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNHttpTest.m; sourceTree = "<group>"; };
183191
DFBC622719DE578600458C4B /* QNConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNConfig.m; sourceTree = "<group>"; };
192+
DFF5252F1A6235D100D02BA1 /* QNSessionManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNSessionManager.h; sourceTree = "<group>"; };
193+
DFF525301A6235D100D02BA1 /* QNSessionManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNSessionManager.m; sourceTree = "<group>"; };
194+
DFF525361A626A3700D02BA1 /* QNhttpDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNhttpDelegate.h; sourceTree = "<group>"; };
195+
DFF525381A64079B00D02BA1 /* QNSessionTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNSessionTest.m; sourceTree = "<group>"; };
184196
DFFE0E5F19E6575600D7A0FC /* QNFileRecorderTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNFileRecorderTest.m; sourceTree = "<group>"; };
185197
/* End PBXFileReference section */
186198

@@ -263,6 +275,9 @@
263275
DF0D23CE19DCE6E500D6B68F /* QNResponseInfo.m */,
264276
DF6099F91A5676AA00AC7297 /* QNDns.h */,
265277
DF6099FB1A5676CA00AC7297 /* QNDns.m */,
278+
DFF5252F1A6235D100D02BA1 /* QNSessionManager.h */,
279+
DFF525301A6235D100D02BA1 /* QNSessionManager.m */,
280+
DFF525361A626A3700D02BA1 /* QNhttpDelegate.h */,
266281
);
267282
path = Http;
268283
sourceTree = "<group>";
@@ -349,6 +364,7 @@
349364
DFA9B65E19E391A100A15FD1 /* QNTestConfig.h */,
350365
DFFE0E5F19E6575600D7A0FC /* QNFileRecorderTest.m */,
351366
DF609A001A5839B400AC7297 /* QNDnsTest.m */,
367+
DFF525381A64079B00D02BA1 /* QNSessionTest.m */,
352368
);
353369
path = QiniuSDKTests;
354370
sourceTree = SOURCE_ROOT;
@@ -377,11 +393,13 @@
377393
DF293CA419DC04C200799011 /* QNHttpManager.h in Headers */,
378394
DFA9B65819E0B53700A15FD1 /* QNFileRecorder.h in Headers */,
379395
DF609A051A58E39D00AC7297 /* QNFormUpload.h in Headers */,
396+
DFF525371A626A3700D02BA1 /* QNhttpDelegate.h in Headers */,
380397
DF2CDE6019DAC6A400CE01FB /* QNCrc32.h in Headers */,
381398
DFA9B63B19DF904000A15FD1 /* QNEtag.h in Headers */,
382399
DF2CDE5C19DAC6A400CE01FB /* QNUrlSafeBase64.h in Headers */,
383400
DFA9B64919E0018800A15FD1 /* QNUploadOption.h in Headers */,
384401
DFA9B65C19E0B58900A15FD1 /* QNRecorderDelegate.h in Headers */,
402+
DFF525311A6235D100D02BA1 /* QNSessionManager.h in Headers */,
385403
DF2CDE6819DAC6A400CE01FB /* QiniuSDK.h in Headers */,
386404
DF2CDE6919DAC6A400CE01FB /* QNUploadManager.h in Headers */,
387405
DF2CDE5F19DAC6A400CE01FB /* QNConfig.h in Headers */,
@@ -667,6 +685,7 @@
667685
DF2CDE6119DAC6A400CE01FB /* QNCrc32.m in Sources */,
668686
DF293CA619DC05B800799011 /* QNHttpManager.m in Sources */,
669687
DF293CAB19DC0E5300799011 /* QNResumeUpload.m in Sources */,
688+
DFF525321A6235D100D02BA1 /* QNSessionManager.m in Sources */,
670689
DFA39AA519F1272800A1A158 /* QNAsyncRun.m in Sources */,
671690
DFA9B64A19E0018800A15FD1 /* QNUploadOption.m in Sources */,
672691
DF2CDE6A19DAC6A400CE01FB /* QNUploadManager.m in Sources */,
@@ -696,7 +715,9 @@
696715
DF2CDE8319DAF67F00CE01FB /* QNCrc32.m in Sources */,
697716
DF293C9719DB865800799011 /* QNCrc32Test.m in Sources */,
698717
DFA9B64519DFE43500A15FD1 /* QNTempFile.m in Sources */,
718+
DFF525391A64079B00D02BA1 /* QNSessionTest.m in Sources */,
699719
DF609A071A58E39D00AC7297 /* QNFormUpload.m in Sources */,
720+
DFF525331A6235D100D02BA1 /* QNSessionManager.m in Sources */,
700721
DFA9B63F19DFD8C900A15FD1 /* QNEtagTest.m in Sources */,
701722
DF6099FD1A5676CA00AC7297 /* QNDns.m in Sources */,
702723
DF2CDE8A19DAF67F00CE01FB /* QNUploadManager.m in Sources */,
@@ -718,6 +739,7 @@
718739
DF2CDE6219DAC6A400CE01FB /* QNCrc32.m in Sources */,
719740
DF293CA719DC05B800799011 /* QNHttpManager.m in Sources */,
720741
DF293CAC19DC0E5300799011 /* QNResumeUpload.m in Sources */,
742+
DFF525341A6235D100D02BA1 /* QNSessionManager.m in Sources */,
721743
DFA39AA719F1272800A1A158 /* QNAsyncRun.m in Sources */,
722744
DFA9B64B19E0018800A15FD1 /* QNUploadOption.m in Sources */,
723745
DF2CDE6B19DAC6A400CE01FB /* QNUploadManager.m in Sources */,
@@ -747,7 +769,9 @@
747769
DF2CDE7719DAF65800CE01FB /* QNCrc32.m in Sources */,
748770
DF293C9819DB865800799011 /* QNCrc32Test.m in Sources */,
749771
DFA9B64619DFE43500A15FD1 /* QNTempFile.m in Sources */,
772+
DFF5253A1A64079B00D02BA1 /* QNSessionTest.m in Sources */,
750773
DF609A091A58E39D00AC7297 /* QNFormUpload.m in Sources */,
774+
DFF525351A6235D100D02BA1 /* QNSessionManager.m in Sources */,
751775
DFA9B64019DFD8C900A15FD1 /* QNEtagTest.m in Sources */,
752776
DF6099FF1A5676CA00AC7297 /* QNDns.m in Sources */,
753777
DF2CDE7E19DAF65800CE01FB /* QNUploadManager.m in Sources */,

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.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,9 @@
77
//
88

99
#import <Foundation/Foundation.h>
10+
#import "QNhttpDelegate.h"
1011

11-
@class QNResponseInfo;
12-
13-
typedef void (^QNInternalProgressBlock)(long long totalBytesWritten, long long totalBytesExpectedToWrite);
14-
typedef void (^QNCompleteBlock)(QNResponseInfo *info, NSDictionary *resp);
15-
typedef BOOL (^QNCancelBlock)(void);
16-
17-
@interface QNHttpManager : NSObject
12+
@interface QNHttpManager : NSObject <QNHttpDelegate>
1813

1914
- (void)multipartPost:(NSString *)url
2015
withData:(NSData *)data

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.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#import <Foundation/Foundation.h>
2+
#import "QNHttpDelegate.h"
3+
4+
#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)
5+
6+
@interface QNSessionManager : NSObject <QNHttpDelegate>
7+
8+
- (instancetype)initWithProxy:(NSDictionary *)proxyDict;
9+
10+
- (void)multipartPost:(NSString *)url
11+
withData:(NSData *)data
12+
withParams:(NSDictionary *)params
13+
withFileName:(NSString *)key
14+
withMimeType:(NSString *)mime
15+
withCompleteBlock:(QNCompleteBlock)completeBlock
16+
withProgressBlock:(QNInternalProgressBlock)progressBlock
17+
withCancelBlock:(QNCancelBlock)cancelBlock;
18+
19+
- (void) post:(NSString *)url
20+
withData:(NSData *)data
21+
withParams:(NSDictionary *)params
22+
withHeaders:(NSDictionary *)headers
23+
withCompleteBlock:(QNCompleteBlock)completeBlock
24+
withProgressBlock:(QNInternalProgressBlock)progressBlock
25+
withCancelBlock:(QNCancelBlock)cancelBlock;
26+
27+
@end
28+
29+
#endif

QiniuSDK/Http/QNSessionManager.m

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
//
2+
// QNHttpManager.m
3+
// QiniuSDK
4+
//
5+
// Created by bailong on 14/10/1.
6+
// Copyright (c) 2014年 Qiniu. All rights reserved.
7+
//
8+
9+
#import <AFNetworking/AFNetworking.h>
10+
11+
#import "QNConfig.h"
12+
#import "QNSessionManager.h"
13+
#import "QNUserAgent.h"
14+
#import "QNResponseInfo.h"
15+
#import "QNDns.h"
16+
#import "QNAsyncRun.h"
17+
18+
#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)
19+
20+
@interface QNProgessDelegate : NSObject
21+
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
22+
@property (nonatomic, strong) QNInternalProgressBlock progressBlock;
23+
@property (nonatomic, strong) NSProgress *progress;
24+
- (instancetype)initWithProgress:(QNInternalProgressBlock)progressBlock;
25+
@end
26+
27+
@implementation QNProgessDelegate
28+
- (instancetype)initWithProgress:(QNInternalProgressBlock)progressBlock {
29+
if (self = [super init]) {
30+
_progressBlock = progressBlock;
31+
_progress = nil;
32+
}
33+
34+
return self;
35+
}
36+
37+
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context; {
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+
}
51+
}
52+
53+
@end
54+
55+
@interface QNSessionManager ()
56+
@property (nonatomic) AFHTTPSessionManager *httpManager;
57+
@end
58+
59+
static NSString *userAgent = nil;
60+
61+
@implementation QNSessionManager
62+
63+
+ (void)initialize {
64+
userAgent = QNUserAgent();
65+
}
66+
67+
- (instancetype)initWithProxy:(NSDictionary *)proxyDict {
68+
if (self = [super init]) {
69+
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
70+
if (proxyDict != nil) {
71+
configuration.connectionProxyDictionary = proxyDict;
72+
}
73+
_httpManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
74+
_httpManager.responseSerializer = [AFHTTPResponseSerializer serializer];
75+
}
76+
77+
return self;
78+
}
79+
80+
+ (QNResponseInfo *)buildResponseInfo:(NSHTTPURLResponse *)response
81+
withError:(NSError *)error
82+
withDuration:(double)duration
83+
withResponse:(NSData *)body
84+
withHost:(NSString *)host {
85+
QNResponseInfo *info;
86+
87+
if (response) {
88+
int status = (int)[response statusCode];
89+
NSDictionary *headers = [response allHeaderFields];
90+
NSString *reqId = headers[@"X-Reqid"];
91+
NSString *xlog = headers[@"X-Log"];
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];
100+
}
101+
else {
102+
info = [QNResponseInfo responseInfoWithNetError:error host:host duration:duration];
103+
}
104+
return info;
105+
}
106+
107+
- (void) sendRequest:(NSMutableURLRequest *)request
108+
withCompleteBlock:(QNCompleteBlock)completeBlock
109+
withProgressBlock:(QNInternalProgressBlock)progressBlock {
110+
__block NSDate *startTime = [NSDate date];
111+
NSProgress *progress = nil;
112+
__block NSString *host = request.URL.host;
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) {
121+
NSData *data = responseObject;
122+
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
123+
double duration = [[NSDate date] timeIntervalSinceDate:startTime];
124+
QNResponseInfo *info;
125+
NSDictionary *resp = nil;
126+
if (error == nil) {
127+
info = [QNSessionManager buildResponseInfo:httpResponse withError:nil withDuration:duration withResponse:data withHost:host];
128+
if (info.isOK) {
129+
NSError *tmp;
130+
resp = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&tmp];
131+
}
132+
}
133+
else {
134+
info = [QNSessionManager buildResponseInfo:httpResponse withError:error withDuration:duration withResponse:data withHost:host];
135+
}
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);
142+
}];
143+
if (progress != nil) {
144+
[progress addObserver:delegate forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:(__bridge void *)delegate];
145+
delegate.progress = progress;
146+
}
147+
148+
[request setTimeoutInterval:kQNTimeoutInterval];
149+
150+
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
151+
[request setValue:nil forHTTPHeaderField:@"Accept-Language"];
152+
[uploadTask resume];
153+
}
154+
155+
- (void)multipartPost:(NSString *)url
156+
withData:(NSData *)data
157+
withParams:(NSDictionary *)params
158+
withFileName:(NSString *)key
159+
withMimeType:(NSString *)mime
160+
withCompleteBlock:(QNCompleteBlock)completeBlock
161+
withProgressBlock:(QNInternalProgressBlock)progressBlock
162+
withCancelBlock:(QNCancelBlock)cancelBlock {
163+
NSMutableURLRequest *request = [_httpManager.requestSerializer
164+
multipartFormRequestWithMethod:@"POST"
165+
URLString:url
166+
parameters:params
167+
constructingBodyWithBlock: ^(id < AFMultipartFormData > formData) {
168+
[formData appendPartWithFileData:data name:@"file" fileName:key mimeType:mime];
169+
}
170+
171+
error:nil];
172+
[self sendRequest:request
173+
withCompleteBlock:completeBlock
174+
withProgressBlock:progressBlock];
175+
}
176+
177+
- (void) post:(NSString *)url
178+
withData:(NSData *)data
179+
withParams:(NSDictionary *)params
180+
withHeaders:(NSDictionary *)headers
181+
withCompleteBlock:(QNCompleteBlock)completeBlock
182+
withProgressBlock:(QNInternalProgressBlock)progressBlock
183+
withCancelBlock:(QNCancelBlock)cancelBlock {
184+
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:url]];
185+
if (headers) {
186+
[request setAllHTTPHeaderFields:headers];
187+
}
188+
189+
[request setHTTPMethod:@"POST"];
190+
191+
if (params) {
192+
[request setValuesForKeysWithDictionary:params];
193+
}
194+
[request setHTTPBody:data];
195+
QNAsyncRun( ^{
196+
[self sendRequest:request
197+
withCompleteBlock:completeBlock
198+
withProgressBlock:progressBlock];
199+
});
200+
}
201+
202+
@end
203+
204+
#endif

0 commit comments

Comments
 (0)