Skip to content

Commit

Permalink
Merge pull request #13 from longbai/timeout
Browse files Browse the repository at this point in the history
Timeout, empty body
  • Loading branch information
longbai committed Oct 31, 2014
2 parents d112a06 + ecb9279 commit a762177
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 9 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#Changelog

## 7.0.6 (2014-10-31)

### 修正
* 检查因网络断开服务器返回空body或者非七牛服务器的情况
* 增加timeout

## 7.0.5 (2014-10-28)

### 修正
* ResumeUpload中httpManager weak引用造成nil
* 重构代码,更符合objc 现代方式


## 7.0.4 (2014-10-17)

### 增加
Expand Down
2 changes: 1 addition & 1 deletion Qiniu.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Qiniu'
s.version = '7.0.5'
s.version = '7.0.6'
s.summary = 'Qiniu Resource Storage SDK for iOS and Mac'
s.homepage = 'https://github.com/qiniu/objc-sdk'
s.social_media_url = 'http://weibo.com/qiniutek'
Expand Down
2 changes: 2 additions & 0 deletions QiniuSDK/Common/QNConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ extern const UInt32 kQNBlockSize;
extern const UInt32 kQNRetryMax;

extern const UInt32 kQNPutThreshold;

extern const float kQNTimeoutInterval;
6 changes: 6 additions & 0 deletions QiniuSDK/Common/QNConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@
#import "QNConfig.h"

NSString *const kQNUpHost = @"upload.qiniu.com";

NSString *const kQNUpHostBackup = @"up.qiniu.com";

const UInt32 kQNChunkSize = 256 * 1024;

const UInt32 kQNBlockSize = 4 * 1024 * 1024;

const UInt32 kQNPutThreshold = 512 * 1024;

const UInt32 kQNRetryMax = 3;

const float kQNTimeoutInterval = 30.0;
2 changes: 1 addition & 1 deletion QiniuSDK/Common/QNVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

#import <Foundation/Foundation.h>

static const NSString *kQiniuVersion = @"7.0.5";
static const NSString *kQiniuVersion = @"7.0.6";
2 changes: 2 additions & 0 deletions QiniuSDK/Http/QNHttpManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import <AFNetworking/AFNetworking.h>

#import "QNConfig.h"
#import "QNHttpManager.h"
#import "QNUserAgent.h"
#import "QNResponseInfo.h"
Expand Down Expand Up @@ -74,6 +75,7 @@ - (void) sendRequest:(NSMutableURLRequest *)request
progressBlock(totalBytesWritten, totalBytesExpectedToWrite);
}];
}
[request setTimeoutInterval:kQNTimeoutInterval];

[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
[request setValue:nil forHTTPHeaderField:@"Accept-Language"];
Expand Down
19 changes: 13 additions & 6 deletions QiniuSDK/Http/QNResponseInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

static QNResponseInfo *cancelledInfo = nil;

static NSString *domain = @"qiniu.com";

@implementation QNResponseInfo

+ (instancetype)cancel {
Expand Down Expand Up @@ -50,7 +52,7 @@ - (instancetype)initWithStatus:(int)status

- (instancetype)initWithStatus:(int)status
errorDescription:(NSString *)text {
NSError *error = [[NSError alloc] initWithDomain:@"qiniu" code:status userInfo:@{ @"error":text }];
NSError *error = [[NSError alloc] initWithDomain:domain code:status userInfo:@{ @"error":text }];
return [self initWithStatus:status error:error];
}

Expand All @@ -64,17 +66,21 @@ - (instancetype)init:(int)status
_xlog = [xlog copy];
if (status != 200) {
if (body == nil) {
_error = [[NSError alloc] initWithDomain:@"qiniu" code:_statusCode userInfo:nil];
_error = [[NSError alloc] initWithDomain:domain code:_statusCode userInfo:nil];
}
else {
NSError *tmp;
NSDictionary *uInfo = [NSJSONSerialization JSONObjectWithData:body options:NSJSONReadingMutableLeaves error:&tmp];
if (tmp != nil) {
uInfo = @{ @"error":[[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding] };
}
_error = [[NSError alloc] initWithDomain:@"qiniu" code:_statusCode userInfo:uInfo];
_error = [[NSError alloc] initWithDomain:domain code:_statusCode userInfo:uInfo];
}
}
else if (body == nil || body.length == 0) {
NSDictionary *uInfo = @{ @"error":@"no response json" };
_error = [[NSError alloc] initWithDomain:domain code:_statusCode userInfo:uInfo];
}
}
return self;
}
Expand All @@ -88,15 +94,16 @@ - (BOOL)isCancelled {
}

- (BOOL)isOK {
return _statusCode == 200;
return _statusCode == 200 && _error == nil && _reqId != nil;
}

- (BOOL)isConnectionBroken {
return _statusCode == kQNNetworkError;
// reqId is nill means the server is not qiniu
return _statusCode == kQNNetworkError || _reqId == nil;
}

- (BOOL)couldRetry {
return (_statusCode >= 500 && _statusCode < 600 && _statusCode != 579) || _statusCode == kQNNetworkError || _statusCode == 996 || _statusCode == 406;
return (_statusCode >= 500 && _statusCode < 600 && _statusCode != 579) || _statusCode == kQNNetworkError || _statusCode == 996 || _statusCode == 406 || (_statusCode == 200 && _error != nil);
}

@end
11 changes: 11 additions & 0 deletions QiniuSDKTests/QNHttpTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ - (void)testPost {
NSLog(@"%@", testInfo);
XCTAssert(testInfo.statusCode == 418, @"Pass");
XCTAssert(testInfo.error != nil, @"Pass");

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

AGWW_WAIT_WHILE(testInfo == nil, 100.0);
NSLog(@"%@", testInfo);
XCTAssert(testInfo.statusCode == 200, @"Pass");
XCTAssert(!testInfo.isOK, @"Pass");
XCTAssert(testInfo.error != nil, @"Pass");
}

@end

0 comments on commit a762177

Please sign in to comment.