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
4 changes: 2 additions & 2 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source 'https://github.com/CocoaPods/Specs.git'

target "QiniuSDK iOS" do
platform :ios, "6.0"
pod 'AFNetworking', '~> 2.0'
pod 'AFNetworking', '>= 2.4'
end

target "QiniuSDK iOSTests" do
Expand All @@ -12,7 +12,7 @@ end

target "QiniuSDK Mac" do
platform :osx, "10.8"
pod 'AFNetworking', '~> 2.0'
pod 'AFNetworking', '>= 2.4'
end

target "QiniuSDK MacTests" do
Expand Down
24 changes: 24 additions & 0 deletions QiniuSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@
DFBC622919DE578600458C4B /* QNConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = DFBC622719DE578600458C4B /* QNConfig.m */; };
DFBC622A19DE5E2500458C4B /* QNConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = DFBC622719DE578600458C4B /* QNConfig.m */; };
DFBC622B19DE5E3200458C4B /* QNConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = DFBC622719DE578600458C4B /* QNConfig.m */; };
DFF525311A6235D100D02BA1 /* QNSessionManager.h in Headers */ = {isa = PBXBuildFile; fileRef = DFF5252F1A6235D100D02BA1 /* QNSessionManager.h */; };
DFF525321A6235D100D02BA1 /* QNSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525301A6235D100D02BA1 /* QNSessionManager.m */; };
DFF525331A6235D100D02BA1 /* QNSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525301A6235D100D02BA1 /* QNSessionManager.m */; };
DFF525341A6235D100D02BA1 /* QNSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525301A6235D100D02BA1 /* QNSessionManager.m */; };
DFF525351A6235D100D02BA1 /* QNSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525301A6235D100D02BA1 /* QNSessionManager.m */; };
DFF525371A626A3700D02BA1 /* QNhttpDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = DFF525361A626A3700D02BA1 /* QNhttpDelegate.h */; };
DFF525391A64079B00D02BA1 /* QNSessionTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525381A64079B00D02BA1 /* QNSessionTest.m */; };
DFF5253A1A64079B00D02BA1 /* QNSessionTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525381A64079B00D02BA1 /* QNSessionTest.m */; };
DFFE0E6019E6575600D7A0FC /* QNFileRecorderTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFFE0E5F19E6575600D7A0FC /* QNFileRecorderTest.m */; };
DFFE0E6119E6575600D7A0FC /* QNFileRecorderTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFFE0E5F19E6575600D7A0FC /* QNFileRecorderTest.m */; };
EE76B381AF33E356C2C8FC56 /* libPods-QiniuSDK MacTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 0EB0B4B6BA4EEC2DEF421644 /* libPods-QiniuSDK MacTests.a */; };
Expand Down Expand Up @@ -181,6 +189,10 @@
DFA9B65E19E391A100A15FD1 /* QNTestConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNTestConfig.h; sourceTree = "<group>"; };
DFBC622419DE459800458C4B /* QNHttpTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNHttpTest.m; sourceTree = "<group>"; };
DFBC622719DE578600458C4B /* QNConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNConfig.m; sourceTree = "<group>"; };
DFF5252F1A6235D100D02BA1 /* QNSessionManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNSessionManager.h; sourceTree = "<group>"; };
DFF525301A6235D100D02BA1 /* QNSessionManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNSessionManager.m; sourceTree = "<group>"; };
DFF525361A626A3700D02BA1 /* QNhttpDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNhttpDelegate.h; sourceTree = "<group>"; };
DFF525381A64079B00D02BA1 /* QNSessionTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNSessionTest.m; sourceTree = "<group>"; };
DFFE0E5F19E6575600D7A0FC /* QNFileRecorderTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNFileRecorderTest.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -263,6 +275,9 @@
DF0D23CE19DCE6E500D6B68F /* QNResponseInfo.m */,
DF6099F91A5676AA00AC7297 /* QNDns.h */,
DF6099FB1A5676CA00AC7297 /* QNDns.m */,
DFF5252F1A6235D100D02BA1 /* QNSessionManager.h */,
DFF525301A6235D100D02BA1 /* QNSessionManager.m */,
DFF525361A626A3700D02BA1 /* QNhttpDelegate.h */,
);
path = Http;
sourceTree = "<group>";
Expand Down Expand Up @@ -349,6 +364,7 @@
DFA9B65E19E391A100A15FD1 /* QNTestConfig.h */,
DFFE0E5F19E6575600D7A0FC /* QNFileRecorderTest.m */,
DF609A001A5839B400AC7297 /* QNDnsTest.m */,
DFF525381A64079B00D02BA1 /* QNSessionTest.m */,
);
path = QiniuSDKTests;
sourceTree = SOURCE_ROOT;
Expand Down Expand Up @@ -377,11 +393,13 @@
DF293CA419DC04C200799011 /* QNHttpManager.h in Headers */,
DFA9B65819E0B53700A15FD1 /* QNFileRecorder.h in Headers */,
DF609A051A58E39D00AC7297 /* QNFormUpload.h in Headers */,
DFF525371A626A3700D02BA1 /* QNhttpDelegate.h in Headers */,
DF2CDE6019DAC6A400CE01FB /* QNCrc32.h in Headers */,
DFA9B63B19DF904000A15FD1 /* QNEtag.h in Headers */,
DF2CDE5C19DAC6A400CE01FB /* QNUrlSafeBase64.h in Headers */,
DFA9B64919E0018800A15FD1 /* QNUploadOption.h in Headers */,
DFA9B65C19E0B58900A15FD1 /* QNRecorderDelegate.h in Headers */,
DFF525311A6235D100D02BA1 /* QNSessionManager.h in Headers */,
DF2CDE6819DAC6A400CE01FB /* QiniuSDK.h in Headers */,
DF2CDE6919DAC6A400CE01FB /* QNUploadManager.h in Headers */,
DF2CDE5F19DAC6A400CE01FB /* QNConfig.h in Headers */,
Expand Down Expand Up @@ -667,6 +685,7 @@
DF2CDE6119DAC6A400CE01FB /* QNCrc32.m in Sources */,
DF293CA619DC05B800799011 /* QNHttpManager.m in Sources */,
DF293CAB19DC0E5300799011 /* QNResumeUpload.m in Sources */,
DFF525321A6235D100D02BA1 /* QNSessionManager.m in Sources */,
DFA39AA519F1272800A1A158 /* QNAsyncRun.m in Sources */,
DFA9B64A19E0018800A15FD1 /* QNUploadOption.m in Sources */,
DF2CDE6A19DAC6A400CE01FB /* QNUploadManager.m in Sources */,
Expand Down Expand Up @@ -696,7 +715,9 @@
DF2CDE8319DAF67F00CE01FB /* QNCrc32.m in Sources */,
DF293C9719DB865800799011 /* QNCrc32Test.m in Sources */,
DFA9B64519DFE43500A15FD1 /* QNTempFile.m in Sources */,
DFF525391A64079B00D02BA1 /* QNSessionTest.m in Sources */,
DF609A071A58E39D00AC7297 /* QNFormUpload.m in Sources */,
DFF525331A6235D100D02BA1 /* QNSessionManager.m in Sources */,
DFA9B63F19DFD8C900A15FD1 /* QNEtagTest.m in Sources */,
DF6099FD1A5676CA00AC7297 /* QNDns.m in Sources */,
DF2CDE8A19DAF67F00CE01FB /* QNUploadManager.m in Sources */,
Expand All @@ -718,6 +739,7 @@
DF2CDE6219DAC6A400CE01FB /* QNCrc32.m in Sources */,
DF293CA719DC05B800799011 /* QNHttpManager.m in Sources */,
DF293CAC19DC0E5300799011 /* QNResumeUpload.m in Sources */,
DFF525341A6235D100D02BA1 /* QNSessionManager.m in Sources */,
DFA39AA719F1272800A1A158 /* QNAsyncRun.m in Sources */,
DFA9B64B19E0018800A15FD1 /* QNUploadOption.m in Sources */,
DF2CDE6B19DAC6A400CE01FB /* QNUploadManager.m in Sources */,
Expand Down Expand Up @@ -747,7 +769,9 @@
DF2CDE7719DAF65800CE01FB /* QNCrc32.m in Sources */,
DF293C9819DB865800799011 /* QNCrc32Test.m in Sources */,
DFA9B64619DFE43500A15FD1 /* QNTempFile.m in Sources */,
DFF5253A1A64079B00D02BA1 /* QNSessionTest.m in Sources */,
DF609A091A58E39D00AC7297 /* QNFormUpload.m in Sources */,
DFF525351A6235D100D02BA1 /* QNSessionManager.m in Sources */,
DFA9B64019DFD8C900A15FD1 /* QNEtagTest.m in Sources */,
DF6099FF1A5676CA00AC7297 /* QNDns.m in Sources */,
DF2CDE7E19DAF65800CE01FB /* QNUploadManager.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion QiniuSDK/Common/QNAsyncRun.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

void QNAsyncRun(QNRun run) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
run();
run();
});
}
9 changes: 2 additions & 7 deletions QiniuSDK/Http/QNHttpManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
//

#import <Foundation/Foundation.h>
#import "QNhttpDelegate.h"

@class QNResponseInfo;

typedef void (^QNInternalProgressBlock)(long long totalBytesWritten, long long totalBytesExpectedToWrite);
typedef void (^QNCompleteBlock)(QNResponseInfo *info, NSDictionary *resp);
typedef BOOL (^QNCancelBlock)(void);

@interface QNHttpManager : NSObject
@interface QNHttpManager : NSObject <QNHttpDelegate>

- (void)multipartPost:(NSString *)url
withData:(NSData *)data
Expand Down
2 changes: 1 addition & 1 deletion QiniuSDK/Http/QNHttpManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ + (QNResponseInfo *)buildResponseInfo:(AFHTTPRequestOperation *)operation
NSString *host = operation.request.URL.host;

if (operation.response) {
int status = (int)[operation.response statusCode];
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 withXVia:xvia withHost:host withDuration:duration withBody:responseObject];
}
else {
Expand Down
29 changes: 29 additions & 0 deletions QiniuSDK/Http/QNSessionManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#import <Foundation/Foundation.h>
#import "QNHttpDelegate.h"

#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)

@interface QNSessionManager : NSObject <QNHttpDelegate>

- (instancetype)initWithProxy:(NSDictionary *)proxyDict;

- (void)multipartPost:(NSString *)url
withData:(NSData *)data
withParams:(NSDictionary *)params
withFileName:(NSString *)key
withMimeType:(NSString *)mime
withCompleteBlock:(QNCompleteBlock)completeBlock
withProgressBlock:(QNInternalProgressBlock)progressBlock
withCancelBlock:(QNCancelBlock)cancelBlock;

- (void) post:(NSString *)url
withData:(NSData *)data
withParams:(NSDictionary *)params
withHeaders:(NSDictionary *)headers
withCompleteBlock:(QNCompleteBlock)completeBlock
withProgressBlock:(QNInternalProgressBlock)progressBlock
withCancelBlock:(QNCancelBlock)cancelBlock;

@end

#endif
204 changes: 204 additions & 0 deletions QiniuSDK/Http/QNSessionManager.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
//
// QNHttpManager.m
// QiniuSDK
//
// Created by bailong on 14/10/1.
// Copyright (c) 2014年 Qiniu. All rights reserved.
//

#import <AFNetworking/AFNetworking.h>

#import "QNConfig.h"
#import "QNSessionManager.h"
#import "QNUserAgent.h"
#import "QNResponseInfo.h"
#import "QNDns.h"
#import "QNAsyncRun.h"

#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)

@interface QNProgessDelegate : NSObject
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
@property (nonatomic, strong) QNInternalProgressBlock progressBlock;
@property (nonatomic, strong) NSProgress *progress;
- (instancetype)initWithProgress:(QNInternalProgressBlock)progressBlock;
@end

@implementation QNProgessDelegate
- (instancetype)initWithProgress:(QNInternalProgressBlock)progressBlock {
if (self = [super init]) {
_progressBlock = progressBlock;
_progress = nil;
}

return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context; {
if (context == nil || object == nil) {
return;
}

NSProgress *progress = (NSProgress *)object;

void *p = (__bridge void *)(self);
if (p == context) {
_progressBlock(progress.completedUnitCount, progress.totalUnitCount);
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}

@end

@interface QNSessionManager ()
@property (nonatomic) AFHTTPSessionManager *httpManager;
@end

static NSString *userAgent = nil;

@implementation QNSessionManager

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

- (instancetype)initWithProxy:(NSDictionary *)proxyDict {
if (self = [super init]) {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
if (proxyDict != nil) {
configuration.connectionProxyDictionary = proxyDict;
}
_httpManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
_httpManager.responseSerializer = [AFHTTPResponseSerializer serializer];
}

return self;
}

+ (QNResponseInfo *)buildResponseInfo:(NSHTTPURLResponse *)response
withError:(NSError *)error
withDuration:(double)duration
withResponse:(NSData *)body
withHost:(NSString *)host {
QNResponseInfo *info;

if (response) {
int status = (int)[response statusCode];
NSDictionary *headers = [response allHeaderFields];
NSString *reqId = headers[@"X-Reqid"];
NSString *xlog = headers[@"X-Log"];
NSString *xvia = headers[@"X-Via"];
if (xvia == nil) {
xvia = headers[@"X-Px"];
}
if (xvia == nil) {
xvia = headers[@"Fw-Via"];
}
info = [[QNResponseInfo alloc] init:status withReqId:reqId withXLog:xlog withXVia:xvia withHost:host withDuration:duration withBody:body];
}
else {
info = [QNResponseInfo responseInfoWithNetError:error host:host duration:duration];
}
return info;
}

- (void) sendRequest:(NSMutableURLRequest *)request
withCompleteBlock:(QNCompleteBlock)completeBlock
withProgressBlock:(QNInternalProgressBlock)progressBlock {
__block NSDate *startTime = [NSDate date];
NSProgress *progress = nil;
__block NSString *host = request.URL.host;

if (progressBlock == nil) {
progressBlock = ^(long long totalBytesWritten, long long totalBytesExpectedToWrite) {
};
}
__block QNProgessDelegate *delegate = [[QNProgessDelegate alloc] initWithProgress:progressBlock];

NSURLSessionUploadTask *uploadTask = [_httpManager uploadTaskWithStreamedRequest:request progress:&progress completionHandler: ^(NSURLResponse *response, id responseObject, NSError *error) {
NSData *data = responseObject;
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
double duration = [[NSDate date] timeIntervalSinceDate:startTime];
QNResponseInfo *info;
NSDictionary *resp = nil;
if (error == nil) {
info = [QNSessionManager buildResponseInfo:httpResponse withError:nil withDuration:duration withResponse:data withHost:host];
if (info.isOK) {
NSError *tmp;
resp = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&tmp];
}
}
else {
info = [QNSessionManager buildResponseInfo:httpResponse withError:error withDuration:duration withResponse:data withHost:host];
}

if (delegate.progress != nil) {
[delegate.progress removeObserver:delegate forKeyPath:@"fractionCompleted" context:(__bridge void *)(delegate)];
delegate.progress = nil;
}
completeBlock(info, resp);
}];
if (progress != nil) {
[progress addObserver:delegate forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:(__bridge void *)delegate];
delegate.progress = progress;
}

[request setTimeoutInterval:kQNTimeoutInterval];

[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
[request setValue:nil forHTTPHeaderField:@"Accept-Language"];
[uploadTask resume];
}

- (void)multipartPost:(NSString *)url
withData:(NSData *)data
withParams:(NSDictionary *)params
withFileName:(NSString *)key
withMimeType:(NSString *)mime
withCompleteBlock:(QNCompleteBlock)completeBlock
withProgressBlock:(QNInternalProgressBlock)progressBlock
withCancelBlock:(QNCancelBlock)cancelBlock {
NSMutableURLRequest *request = [_httpManager.requestSerializer
multipartFormRequestWithMethod:@"POST"
URLString:url
parameters:params
constructingBodyWithBlock: ^(id < AFMultipartFormData > formData) {
[formData appendPartWithFileData:data name:@"file" fileName:key mimeType:mime];
}

error:nil];
[self sendRequest:request
withCompleteBlock:completeBlock
withProgressBlock:progressBlock];
}

- (void) post:(NSString *)url
withData:(NSData *)data
withParams:(NSDictionary *)params
withHeaders:(NSDictionary *)headers
withCompleteBlock:(QNCompleteBlock)completeBlock
withProgressBlock:(QNInternalProgressBlock)progressBlock
withCancelBlock:(QNCancelBlock)cancelBlock {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:url]];
if (headers) {
[request setAllHTTPHeaderFields:headers];
}

[request setHTTPMethod:@"POST"];

if (params) {
[request setValuesForKeysWithDictionary:params];
}
[request setHTTPBody:data];
QNAsyncRun( ^{
[self sendRequest:request
withCompleteBlock:completeBlock
withProgressBlock:progressBlock];
});
}

@end

#endif
Loading