Skip to content

Commit a762177

Browse files
committed
Merge pull request #13 from longbai/timeout
Timeout, empty body
2 parents d112a06 + ecb9279 commit a762177

File tree

8 files changed

+42
-9
lines changed

8 files changed

+42
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#Changelog
22

3+
## 7.0.6 (2014-10-31)
4+
5+
### 修正
6+
* 检查因网络断开服务器返回空body或者非七牛服务器的情况
7+
* 增加timeout
8+
39
## 7.0.5 (2014-10-28)
410

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

9-
1015
## 7.0.4 (2014-10-17)
1116

1217
### 增加

Qiniu.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'Qiniu'
3-
s.version = '7.0.5'
3+
s.version = '7.0.6'
44
s.summary = 'Qiniu Resource Storage SDK for iOS and Mac'
55
s.homepage = 'https://github.com/qiniu/objc-sdk'
66
s.social_media_url = 'http://weibo.com/qiniutek'

QiniuSDK/Common/QNConfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ extern const UInt32 kQNBlockSize;
1919
extern const UInt32 kQNRetryMax;
2020

2121
extern const UInt32 kQNPutThreshold;
22+
23+
extern const float kQNTimeoutInterval;

QiniuSDK/Common/QNConfig.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@
99
#import "QNConfig.h"
1010

1111
NSString *const kQNUpHost = @"upload.qiniu.com";
12+
1213
NSString *const kQNUpHostBackup = @"up.qiniu.com";
14+
1315
const UInt32 kQNChunkSize = 256 * 1024;
16+
1417
const UInt32 kQNBlockSize = 4 * 1024 * 1024;
18+
1519
const UInt32 kQNPutThreshold = 512 * 1024;
1620

1721
const UInt32 kQNRetryMax = 3;
22+
23+
const float kQNTimeoutInterval = 30.0;

QiniuSDK/Common/QNVersion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
#import <Foundation/Foundation.h>
1010

11-
static const NSString *kQiniuVersion = @"7.0.5";
11+
static const NSString *kQiniuVersion = @"7.0.6";

QiniuSDK/Http/QNHttpManager.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import <AFNetworking/AFNetworking.h>
1010

11+
#import "QNConfig.h"
1112
#import "QNHttpManager.h"
1213
#import "QNUserAgent.h"
1314
#import "QNResponseInfo.h"
@@ -74,6 +75,7 @@ - (void) sendRequest:(NSMutableURLRequest *)request
7475
progressBlock(totalBytesWritten, totalBytesExpectedToWrite);
7576
}];
7677
}
78+
[request setTimeoutInterval:kQNTimeoutInterval];
7779

7880
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
7981
[request setValue:nil forHTTPHeaderField:@"Accept-Language"];

QiniuSDK/Http/QNResponseInfo.m

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
static QNResponseInfo *cancelledInfo = nil;
1919

20+
static NSString *domain = @"qiniu.com";
21+
2022
@implementation QNResponseInfo
2123

2224
+ (instancetype)cancel {
@@ -50,7 +52,7 @@ - (instancetype)initWithStatus:(int)status
5052

5153
- (instancetype)initWithStatus:(int)status
5254
errorDescription:(NSString *)text {
53-
NSError *error = [[NSError alloc] initWithDomain:@"qiniu" code:status userInfo:@{ @"error":text }];
55+
NSError *error = [[NSError alloc] initWithDomain:domain code:status userInfo:@{ @"error":text }];
5456
return [self initWithStatus:status error:error];
5557
}
5658

@@ -64,17 +66,21 @@ - (instancetype)init:(int)status
6466
_xlog = [xlog copy];
6567
if (status != 200) {
6668
if (body == nil) {
67-
_error = [[NSError alloc] initWithDomain:@"qiniu" code:_statusCode userInfo:nil];
69+
_error = [[NSError alloc] initWithDomain:domain code:_statusCode userInfo:nil];
6870
}
6971
else {
7072
NSError *tmp;
7173
NSDictionary *uInfo = [NSJSONSerialization JSONObjectWithData:body options:NSJSONReadingMutableLeaves error:&tmp];
7274
if (tmp != nil) {
7375
uInfo = @{ @"error":[[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding] };
7476
}
75-
_error = [[NSError alloc] initWithDomain:@"qiniu" code:_statusCode userInfo:uInfo];
77+
_error = [[NSError alloc] initWithDomain:domain code:_statusCode userInfo:uInfo];
7678
}
7779
}
80+
else if (body == nil || body.length == 0) {
81+
NSDictionary *uInfo = @{ @"error":@"no response json" };
82+
_error = [[NSError alloc] initWithDomain:domain code:_statusCode userInfo:uInfo];
83+
}
7884
}
7985
return self;
8086
}
@@ -88,15 +94,16 @@ - (BOOL)isCancelled {
8894
}
8995

9096
- (BOOL)isOK {
91-
return _statusCode == 200;
97+
return _statusCode == 200 && _error == nil && _reqId != nil;
9298
}
9399

94100
- (BOOL)isConnectionBroken {
95-
return _statusCode == kQNNetworkError;
101+
// reqId is nill means the server is not qiniu
102+
return _statusCode == kQNNetworkError || _reqId == nil;
96103
}
97104

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

102109
@end

QiniuSDKTests/QNHttpTest.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ - (void)testPost {
6868
NSLog(@"%@", testInfo);
6969
XCTAssert(testInfo.statusCode == 418, @"Pass");
7070
XCTAssert(testInfo.error != nil, @"Pass");
71+
72+
testInfo = nil;
73+
[_httpManager post:@"http://httpbin.org/status/200" withData:nil withParams:nil withHeaders:nil withCompleteBlock: ^(QNResponseInfo *info, NSDictionary *resp) {
74+
testInfo = info;
75+
} withProgressBlock:nil withCancelBlock:nil];
76+
77+
AGWW_WAIT_WHILE(testInfo == nil, 100.0);
78+
NSLog(@"%@", testInfo);
79+
XCTAssert(testInfo.statusCode == 200, @"Pass");
80+
XCTAssert(!testInfo.isOK, @"Pass");
81+
XCTAssert(testInfo.error != nil, @"Pass");
7182
}
7283

7384
@end

0 commit comments

Comments
 (0)