Skip to content

Commit a2f58b4

Browse files
committed
add http manager delegate
1 parent e216686 commit a2f58b4

File tree

7 files changed

+221
-10
lines changed

7 files changed

+221
-10
lines changed

QiniuSDK.xcodeproj/project.pbxproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@
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 */; };
106112
DFFE0E6019E6575600D7A0FC /* QNFileRecorderTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFFE0E5F19E6575600D7A0FC /* QNFileRecorderTest.m */; };
107113
DFFE0E6119E6575600D7A0FC /* QNFileRecorderTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFFE0E5F19E6575600D7A0FC /* QNFileRecorderTest.m */; };
108114
EE76B381AF33E356C2C8FC56 /* libPods-QiniuSDK MacTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 0EB0B4B6BA4EEC2DEF421644 /* libPods-QiniuSDK MacTests.a */; };
@@ -181,6 +187,9 @@
181187
DFA9B65E19E391A100A15FD1 /* QNTestConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNTestConfig.h; sourceTree = "<group>"; };
182188
DFBC622419DE459800458C4B /* QNHttpTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNHttpTest.m; sourceTree = "<group>"; };
183189
DFBC622719DE578600458C4B /* QNConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNConfig.m; sourceTree = "<group>"; };
190+
DFF5252F1A6235D100D02BA1 /* QNSessionManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNSessionManager.h; sourceTree = "<group>"; };
191+
DFF525301A6235D100D02BA1 /* QNSessionManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNSessionManager.m; sourceTree = "<group>"; };
192+
DFF525361A626A3700D02BA1 /* QNhttpDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNhttpDelegate.h; sourceTree = "<group>"; };
184193
DFFE0E5F19E6575600D7A0FC /* QNFileRecorderTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNFileRecorderTest.m; sourceTree = "<group>"; };
185194
/* End PBXFileReference section */
186195

@@ -263,6 +272,9 @@
263272
DF0D23CE19DCE6E500D6B68F /* QNResponseInfo.m */,
264273
DF6099F91A5676AA00AC7297 /* QNDns.h */,
265274
DF6099FB1A5676CA00AC7297 /* QNDns.m */,
275+
DFF5252F1A6235D100D02BA1 /* QNSessionManager.h */,
276+
DFF525301A6235D100D02BA1 /* QNSessionManager.m */,
277+
DFF525361A626A3700D02BA1 /* QNhttpDelegate.h */,
266278
);
267279
path = Http;
268280
sourceTree = "<group>";
@@ -377,11 +389,13 @@
377389
DF293CA419DC04C200799011 /* QNHttpManager.h in Headers */,
378390
DFA9B65819E0B53700A15FD1 /* QNFileRecorder.h in Headers */,
379391
DF609A051A58E39D00AC7297 /* QNFormUpload.h in Headers */,
392+
DFF525371A626A3700D02BA1 /* QNhttpDelegate.h in Headers */,
380393
DF2CDE6019DAC6A400CE01FB /* QNCrc32.h in Headers */,
381394
DFA9B63B19DF904000A15FD1 /* QNEtag.h in Headers */,
382395
DF2CDE5C19DAC6A400CE01FB /* QNUrlSafeBase64.h in Headers */,
383396
DFA9B64919E0018800A15FD1 /* QNUploadOption.h in Headers */,
384397
DFA9B65C19E0B58900A15FD1 /* QNRecorderDelegate.h in Headers */,
398+
DFF525311A6235D100D02BA1 /* QNSessionManager.h in Headers */,
385399
DF2CDE6819DAC6A400CE01FB /* QiniuSDK.h in Headers */,
386400
DF2CDE6919DAC6A400CE01FB /* QNUploadManager.h in Headers */,
387401
DF2CDE5F19DAC6A400CE01FB /* QNConfig.h in Headers */,
@@ -667,6 +681,7 @@
667681
DF2CDE6119DAC6A400CE01FB /* QNCrc32.m in Sources */,
668682
DF293CA619DC05B800799011 /* QNHttpManager.m in Sources */,
669683
DF293CAB19DC0E5300799011 /* QNResumeUpload.m in Sources */,
684+
DFF525321A6235D100D02BA1 /* QNSessionManager.m in Sources */,
670685
DFA39AA519F1272800A1A158 /* QNAsyncRun.m in Sources */,
671686
DFA9B64A19E0018800A15FD1 /* QNUploadOption.m in Sources */,
672687
DF2CDE6A19DAC6A400CE01FB /* QNUploadManager.m in Sources */,
@@ -697,6 +712,7 @@
697712
DF293C9719DB865800799011 /* QNCrc32Test.m in Sources */,
698713
DFA9B64519DFE43500A15FD1 /* QNTempFile.m in Sources */,
699714
DF609A071A58E39D00AC7297 /* QNFormUpload.m in Sources */,
715+
DFF525331A6235D100D02BA1 /* QNSessionManager.m in Sources */,
700716
DFA9B63F19DFD8C900A15FD1 /* QNEtagTest.m in Sources */,
701717
DF6099FD1A5676CA00AC7297 /* QNDns.m in Sources */,
702718
DF2CDE8A19DAF67F00CE01FB /* QNUploadManager.m in Sources */,
@@ -718,6 +734,7 @@
718734
DF2CDE6219DAC6A400CE01FB /* QNCrc32.m in Sources */,
719735
DF293CA719DC05B800799011 /* QNHttpManager.m in Sources */,
720736
DF293CAC19DC0E5300799011 /* QNResumeUpload.m in Sources */,
737+
DFF525341A6235D100D02BA1 /* QNSessionManager.m in Sources */,
721738
DFA39AA719F1272800A1A158 /* QNAsyncRun.m in Sources */,
722739
DFA9B64B19E0018800A15FD1 /* QNUploadOption.m in Sources */,
723740
DF2CDE6B19DAC6A400CE01FB /* QNUploadManager.m in Sources */,
@@ -748,6 +765,7 @@
748765
DF293C9819DB865800799011 /* QNCrc32Test.m in Sources */,
749766
DFA9B64619DFE43500A15FD1 /* QNTempFile.m in Sources */,
750767
DF609A091A58E39D00AC7297 /* QNFormUpload.m in Sources */,
768+
DFF525351A6235D100D02BA1 /* QNSessionManager.m in Sources */,
751769
DFA9B64019DFD8C900A15FD1 /* QNEtagTest.m in Sources */,
752770
DF6099FF1A5676CA00AC7297 /* QNDns.m in Sources */,
753771
DF2CDE7E19DAF65800CE01FB /* QNUploadManager.m in Sources */,

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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ + (QNResponseInfo *)buildResponseInfo:(AFHTTPRequestOperation *)operation
4141
withResponse:(id)responseObject {
4242
QNResponseInfo *info;
4343
NSString *host = operation.request.URL.host;
44-
// NSArray *hostIp = [QNDns getAddresses:host];
4544

4645
if (operation.response) {
4746
NSDictionary *headers = [operation.response allHeaderFields];

QiniuSDK/Http/QNSessionManager.h

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

QiniuSDK/Http/QNSessionManager.m

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
17+
@interface QNSessionManager ()
18+
@property (nonatomic) AFHTTPSessionManager *httpManager;
19+
@end
20+
21+
static NSString *userAgent = nil;
22+
23+
@implementation QNSessionManager
24+
25+
+ (void)initialize {
26+
userAgent = QNUserAgent();
27+
}
28+
29+
- (instancetype)initWithProxy:(NSDictionary *)proxyDict {
30+
if (self = [super init]) {
31+
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
32+
if (proxyDict != nil) {
33+
configuration.connectionProxyDictionary = proxyDict;
34+
}
35+
_httpManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
36+
_httpManager.responseSerializer = [AFJSONResponseSerializer serializer];
37+
}
38+
39+
return self;
40+
}
41+
42+
+ (QNResponseInfo *)buildResponseInfo:(NSHTTPURLResponse *)response
43+
withError:(NSError *)error
44+
withDuration:(double)duration
45+
withResponse:(id)responseObject
46+
withHost:(NSString *)host {
47+
QNResponseInfo *info;
48+
49+
if (response) {
50+
NSDictionary *headers = [response allHeaderFields];
51+
NSString *reqId = headers[@"X-Reqid"];
52+
NSString *xlog = headers[@"X-Log"];
53+
int status = (int)[response statusCode];
54+
info = [[QNResponseInfo alloc] init:status withReqId:reqId withXLog:xlog withHost:host withDuration:duration withBody:responseObject];
55+
}
56+
else {
57+
info = [QNResponseInfo responseInfoWithNetError:error host:host duration:duration];
58+
}
59+
return info;
60+
}
61+
62+
- (void) sendRequest:(NSMutableURLRequest *)request
63+
withCompleteBlock:(QNCompleteBlock)completeBlock
64+
withProgressBlock:(QNInternalProgressBlock)progressBlock {
65+
__block NSDate *startTime = [NSDate date];
66+
NSProgress *progress = nil;
67+
__block NSString *host = request.URL.host;
68+
NSURLSessionUploadTask *uploadTask = [_httpManager uploadTaskWithStreamedRequest:request progress:&progress completionHandler: ^(NSURLResponse *response, id responseObject, NSError *error) {
69+
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
70+
double duration = [[NSDate date] timeIntervalSinceDate:startTime];
71+
QNResponseInfo *info;
72+
NSDictionary *resp = nil;
73+
if (error == nil) {
74+
info = [QNSessionManager buildResponseInfo:httpResponse withError:nil withDuration:duration withResponse:responseObject withHost:host];
75+
if (info.isOK) {
76+
resp = responseObject;
77+
}
78+
}
79+
else {
80+
info = [QNSessionManager buildResponseInfo:httpResponse withError:error withDuration:duration withResponse:responseObject withHost:host];
81+
}
82+
completeBlock(info, resp);
83+
}];
84+
85+
[request setTimeoutInterval:kQNTimeoutInterval];
86+
87+
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
88+
[request setValue:nil forHTTPHeaderField:@"Accept-Language"];
89+
[uploadTask resume];
90+
}
91+
92+
- (void)multipartPost:(NSString *)url
93+
withData:(NSData *)data
94+
withParams:(NSDictionary *)params
95+
withFileName:(NSString *)key
96+
withMimeType:(NSString *)mime
97+
withCompleteBlock:(QNCompleteBlock)completeBlock
98+
withProgressBlock:(QNInternalProgressBlock)progressBlock
99+
withCancelBlock:(QNCancelBlock)cancelBlock {
100+
NSMutableURLRequest *request = [_httpManager.requestSerializer
101+
multipartFormRequestWithMethod:@"POST"
102+
URLString:url
103+
parameters:params
104+
constructingBodyWithBlock: ^(id < AFMultipartFormData > formData) {
105+
[formData appendPartWithFileData:data name:@"file" fileName:key mimeType:mime];
106+
}
107+
108+
error:nil];
109+
[self sendRequest:request
110+
withCompleteBlock:completeBlock
111+
withProgressBlock:progressBlock];
112+
}
113+
114+
- (void) post:(NSString *)url
115+
withData:(NSData *)data
116+
withParams:(NSDictionary *)params
117+
withHeaders:(NSDictionary *)headers
118+
withCompleteBlock:(QNCompleteBlock)completeBlock
119+
withProgressBlock:(QNInternalProgressBlock)progressBlock
120+
withCancelBlock:(QNCancelBlock)cancelBlock {
121+
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:url]];
122+
if (headers) {
123+
[request setAllHTTPHeaderFields:headers];
124+
}
125+
126+
[request setHTTPMethod:@"POST"];
127+
128+
if (params) {
129+
[request setValuesForKeysWithDictionary:params];
130+
}
131+
[request setHTTPBody:data];
132+
[self sendRequest:request
133+
withCompleteBlock:completeBlock
134+
withProgressBlock:progressBlock];
135+
}
136+
137+
@end

QiniuSDK/Http/QNhttpDelegate.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#import <Foundation/Foundation.h>
2+
3+
@class QNResponseInfo;
4+
5+
typedef void (^QNInternalProgressBlock)(long long totalBytesWritten, long long totalBytesExpectedToWrite);
6+
typedef void (^QNCompleteBlock)(QNResponseInfo *info, NSDictionary *resp);
7+
typedef BOOL (^QNCancelBlock)(void);
8+
9+
/**
10+
* Http 客户端接口
11+
*/
12+
@protocol QNHttpDelegate <NSObject>
13+
14+
- (void)multipartPost:(NSString *)url
15+
withData:(NSData *)data
16+
withParams:(NSDictionary *)params
17+
withFileName:(NSString *)key
18+
withMimeType:(NSString *)mime
19+
withCompleteBlock:(QNCompleteBlock)completeBlock
20+
withProgressBlock:(QNInternalProgressBlock)progressBlock
21+
withCancelBlock:(QNCancelBlock)cancelBlock;
22+
23+
- (void) post:(NSString *)url
24+
withData:(NSData *)data
25+
withParams:(NSDictionary *)params
26+
withHeaders:(NSDictionary *)headers
27+
withCompleteBlock:(QNCompleteBlock)completeBlock
28+
withProgressBlock:(QNInternalProgressBlock)progressBlock
29+
withCancelBlock:(QNCancelBlock)cancelBlock;
30+
31+
@end

QiniuSDK/Storage/QNResumeUpload.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
@interface QNResumeUpload ()
2222

2323
@property (nonatomic, strong) NSData *data;
24-
@property (nonatomic, strong) QNHttpManager *httpManager;
24+
@property (nonatomic, strong) id <QNHttpDelegate> httpManager;
2525
@property UInt32 size;
2626
@property (nonatomic) int retryTimes;
2727
@property (nonatomic, strong) NSString *key;
@@ -77,7 +77,7 @@ - (instancetype)initWithData:(NSData *)data
7777
_complete = block;
7878
_headers = @{ @"Authorization":tok, @"Content-Type":@"application/octet-stream" };
7979
_recorder = recorder;
80-
_httpManager = http;
80+
_httpManager = [[QNHttpManager alloc] init];
8181
if (time != nil) {
8282
_modifyTime = [time timeIntervalSince1970];
8383
}

0 commit comments

Comments
 (0)