Skip to content

Commit 1681950

Browse files
committed
Merge pull request #4 from longbai/add_crc
crc32 check
2 parents 0470894 + 06ab35a commit 1681950

File tree

10 files changed

+46
-18
lines changed

10 files changed

+46
-18
lines changed

Qiniu.podspec

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ Pod::Spec.new do |s|
55
s.homepage = 'https://github.com/qiniu/objc-sdk'
66
s.social_media_url = 'http://weibo.com/qiniutek'
77
s.author = 'Qiniu => sdk@qiniu.com'
8-
s.documentation_url = 'http://developer.qiniu.com/docs/v6/sdk/ios-sdk.html'
9-
s.source = {:git => 'https://github.com/qiniu/objc-sdk.git', :tag => 'v7.0.2'}
8+
s.source = {:git => 'https://github.com/qiniu/objc-sdk.git', :tag => s.version}
109

1110
s.ios.deployment_target = '6.0'
1211
s.osx.deployment_target = '10.8'

QiniuSDK/Common/QNCrc32.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import <zlib.h>
1010

1111
#import "QNCrc32.h"
12+
#import "QNConfig.h"
1213

1314
@implementation QNCrc32
1415

@@ -26,7 +27,18 @@ + (UInt32)file:(NSString *)filePath
2627
if (*error != nil) {
2728
return 0;
2829
}
29-
return [QNCrc32 data:data];
30+
31+
int len = (int)[data length];
32+
int count = (len + kQNBlockSize - 1) / kQNBlockSize;
33+
34+
uLong crc = crc32(0L, Z_NULL, 0);
35+
for (int i = 0; i < count; i++) {
36+
int offset = i * kQNBlockSize;
37+
int size = (len - offset) > kQNBlockSize ? kQNBlockSize : (len - offset);
38+
NSData *d = [data subdataWithRange:NSMakeRange(offset, (unsigned int)size)];
39+
crc = crc32(crc, [d bytes], (uInt)[d length]);
40+
}
41+
return (UInt32)crc;
3042
}
3143
}
3244

QiniuSDK/Common/QNUrlSafeBase64.m

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ + (NSString *)encodeString:(NSString *)sourceString {
1818
return [self encodeData:data];
1919
}
2020

21-
+ (NSError *)decodeError:(int)pos {
22-
// NSString *s = [[NSString alloc] initWithFormat:@"illegal url safe base64 data at input byte %d", pos];
23-
return nil;
24-
}
25-
2621
+ (NSString *)encodeData:(NSData *)data {
2722
NSUInteger length = [data length];
2823
NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4];

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.0";
11+
static const NSString *kQiniuVersion = @"7.0.3";

QiniuSDK/Http/QNResponseInfo.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ extern const int kQNNetworkError;
2020
@property (nonatomic, readonly, getter = isCancelled) BOOL canceled;
2121
@property (nonatomic, readonly, getter = isOK) BOOL ok;
2222
@property (nonatomic, readonly, getter = isConnectionBroken) BOOL broken;
23+
@property (nonatomic, readonly) BOOL couldRetry;
2324

2425
+ (instancetype)cancel;
2526

2627
- (instancetype)initWithError:(NSError *)error;
2728

2829
- (instancetype)initWithCancelled;
2930

30-
@property (nonatomic, readonly) BOOL couldRetry;
31-
3231
- (instancetype)init:(int)status
3332
withReqId:(NSString *)reqId
3433
withXLog:(NSString *)xlog

QiniuSDK/Http/QNResponseInfo.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ - (BOOL)isConnectionBroken {
4949
}
5050

5151
- (BOOL)couldRetry {
52-
return (_statusCode >= 500 && _statusCode < 600 && _statusCode != 579) || _statusCode == kQNNetworkError || _statusCode == 996;
52+
return (_statusCode >= 500 && _statusCode < 600 && _statusCode != 579) || _statusCode == kQNNetworkError || _statusCode == 996 || _statusCode == 406;
5353
}
5454

5555
- (instancetype)init:(int)status

QiniuSDK/Storage/QNResumeUpload.m

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#import "QNHttpManager.h"
1515
#import "QNUploadOption+Private.h"
1616
#import "QNRecorderDelegate.h"
17+
#import "QNCrc32.h"
1718

1819
typedef void (^task)(void);
1920

@@ -34,6 +35,7 @@ @interface QNResumeUpload ()
3435
@property UInt64 modifyTime;
3536
@property (nonatomic, weak) id <QNRecorderDelegate> recorder;
3637

38+
@property UInt32 chunkCrc;
3739

3840
- (void)makeBlock:(NSString *)uphost
3941
offset:(UInt32)offset
@@ -216,8 +218,15 @@ - (void)nextTask:(UInt32)offset retriedTimes:(int)retried host:(NSString *)host
216218
[self nextTask:offset retriedTimes:retried + 1 host:nextHost];
217219
return;
218220
}
221+
222+
if (resp == nil) {
223+
[self nextTask:offset retriedTimes:retried host:host];
224+
return;
225+
}
226+
219227
NSString *ctx = resp[@"ctx"];
220-
if (ctx == nil) {
228+
NSNumber *crc = resp[@"crc32"];
229+
if (ctx == nil || crc == nil || [crc unsignedLongValue] != _chunkCrc) {
221230
[self nextTask:offset retriedTimes:retried host:host];
222231
return;
223232
}
@@ -252,6 +261,7 @@ - (void)makeBlock:(NSString *)uphost
252261
complete:(QNCompleteBlock)complete {
253262
NSData *data = [self.data subdataWithRange:NSMakeRange(offset, (unsigned int)chunkSize)];
254263
NSString *url = [[NSString alloc] initWithFormat:@"http://%@/mkblk/%u", uphost, (unsigned int)blockSize];
264+
_chunkCrc = [QNCrc32 data:data];
255265
[self post:url withData:data withCompleteBlock:complete withProgressBlock:progressBlock];
256266
}
257267

@@ -264,8 +274,7 @@ - (void)putChunk:(NSString *)uphost
264274
NSData *data = [self.data subdataWithRange:NSMakeRange(offset, (unsigned int)size)];
265275
UInt32 chunkOffset = offset % kQNBlockSize;
266276
NSString *url = [[NSString alloc] initWithFormat:@"http://%@/bput/%@/%u", uphost, context, (unsigned int)chunkOffset];
267-
268-
// Todo: check crc
277+
_chunkCrc = [QNCrc32 data:data];
269278
[self post:url withData:data withCompleteBlock:complete withProgressBlock:progressBlock];
270279
}
271280

QiniuSDK/Storage/QNUploadManager.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#import "QNConfig.h"
1212
#import "QNHttpManager.h"
1313
#import "QNResponseInfo.h"
14-
14+
#import "QNCrc32.h"
1515
#import "QNUploadManager.h"
1616
#import "QNResumeUpload.h"
1717
#import "QNUploadOption+Private.h"
@@ -71,7 +71,7 @@ - (void)putData:(NSData *)data
7171

7272
parameters[@"token"] = token;
7373

74-
if (option.params) {
74+
if (option && option.params) {
7575
[parameters addEntriesFromDictionary:[option p_convertToPostParams]];
7676
}
7777

@@ -81,6 +81,10 @@ - (void)putData:(NSData *)data
8181
mimeType = @"application/octet-stream";
8282
}
8383

84+
if (option && option.checkCrc) {
85+
parameters[@"crc32"] = [NSNumber numberWithUnsignedLong:[QNCrc32 data:data]];
86+
}
87+
8488
QNInternalProgressBlock p = nil;
8589

8690
if (option && option.progressHandler) {

QiniuSDKTests/QNCrc32Test.m

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

99
#import <XCTest/XCTest.h>
1010
#import "QNCrc32.h"
11+
#import "QNTempFile.h"
1112

1213
@interface QNCrc32Test : XCTestCase
1314

@@ -22,4 +23,13 @@ - (void)testData {
2223
XCTAssert(crc == 3964322768, @"Pass");
2324
}
2425

26+
- (void)testFile {
27+
NSError *error;
28+
NSURL *file = [QNTempFile createTempfileWithSize:5 * 1024 * 1024];
29+
UInt32 u = [QNCrc32 file:[file relativePath] error:&error];
30+
31+
XCTAssert(u == 3376132981, @"Pass");
32+
[QNTempFile removeTempfile:file];
33+
}
34+
2535
@end

QiniuSDKTests/QNFormUploadTest.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ - (void)testUp {
3535
__block QNResponseInfo *testInfo = nil;
3636
__block NSDictionary *testResp = nil;
3737

38-
QNUploadOption *opt = [[QNUploadOption alloc] initWithMime:@"text/plain" progressHandler:nil params:@{ @"foo":@"bar" } checkCrc:NO cancellationSignal:nil];
38+
QNUploadOption *opt = [[QNUploadOption alloc] initWithMime:@"text/plain" progressHandler:nil params:@{ @"x:foo":@"bar" } checkCrc:YES cancellationSignal:nil];
3939
NSData *data = [@"Hello, World!" dataUsingEncoding : NSUTF8StringEncoding];
4040
[self.upManager putData:data key:@"你好" token:g_token complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
4141
testInfo = info;

0 commit comments

Comments
 (0)