Skip to content

Commit eac936e

Browse files
authored
Merge branch 'releases/7.x' into sdk_v7_uc_query_v4
2 parents 1b96b65 + 0a5e6fe commit eac936e

File tree

6 files changed

+186
-3
lines changed

6 files changed

+186
-3
lines changed

QiniuSDK.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
05EFAFE5DD675C7ECAD063A4 /* libPods-QiniuSDK_iOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C1D8890E270C769A9E798A8E /* libPods-QiniuSDK_iOS.a */; };
1111
3115471D243476CF00D77B8B /* QNTransactionTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 3115471C243476CF00D77B8B /* QNTransactionTest.m */; };
1212
3115471E243476D600D77B8B /* QNTransactionTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 3115471C243476CF00D77B8B /* QNTransactionTest.m */; };
13+
31259E3324E3D02C00CF7531 /* NSData+QNGZip.m in Sources */ = {isa = PBXBuildFile; fileRef = 31259E3124E3D02C00CF7531 /* NSData+QNGZip.m */; };
14+
31259E3424E3D02C00CF7531 /* NSData+QNGZip.h in Headers */ = {isa = PBXBuildFile; fileRef = 31259E3224E3D02C00CF7531 /* NSData+QNGZip.h */; };
15+
31259E3624E3D32700CF7531 /* QNGZipTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 31259E3524E3D32700CF7531 /* QNGZipTest.m */; };
16+
31259E3724E3D32700CF7531 /* QNGZipTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 31259E3524E3D32700CF7531 /* QNGZipTest.m */; };
1317
3142419E2449553F00BD9A21 /* QNCFHttpClientTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 3142419B2449547B00BD9A21 /* QNCFHttpClientTest.m */; };
1418
3142419F2449554200BD9A21 /* QNCFHttpClientTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 3142419B2449547B00BD9A21 /* QNCFHttpClientTest.m */; };
1519
314944522446FED700386F16 /* NSObject+QNSwizzle.h in Headers */ = {isa = PBXBuildFile; fileRef = 314944502446FED700386F16 /* NSObject+QNSwizzle.h */; };
@@ -64,6 +68,7 @@
6468
31C2EEE5242DE86D00713A33 /* QNUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 31C2EEE2242DE86300713A33 /* QNUtils.m */; };
6569
31C2EEEC242DEF6A00713A33 /* QNUtilTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 31C2EEEB242DEF6A00713A33 /* QNUtilTest.m */; };
6670
31C2EEED242DEF6A00713A33 /* QNUtilTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 31C2EEEB242DEF6A00713A33 /* QNUtilTest.m */; };
71+
31C6E68824ECC75700633629 /* NSData+QNGZip.m in Sources */ = {isa = PBXBuildFile; fileRef = 31259E3124E3D02C00CF7531 /* NSData+QNGZip.m */; };
6772
31F553932456F2F3000B66AD /* QNUrlSafeBase64.m in Sources */ = {isa = PBXBuildFile; fileRef = 31F5537D2456F2F2000B66AD /* QNUrlSafeBase64.m */; };
6873
31F553942456F2F3000B66AD /* QNPHAssetResource.h in Headers */ = {isa = PBXBuildFile; fileRef = 31F5537E2456F2F2000B66AD /* QNPHAssetResource.h */; };
6974
31F553952456F2F3000B66AD /* QNEtag.h in Headers */ = {isa = PBXBuildFile; fileRef = 31F5537F2456F2F2000B66AD /* QNEtag.h */; };
@@ -204,6 +209,9 @@
204209
/* Begin PBXFileReference section */
205210
135958056D8FF4295F3D9AB0 /* Pods-QiniuSDK_iOSTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-QiniuSDK_iOSTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-QiniuSDK_iOSTests/Pods-QiniuSDK_iOSTests.release.xcconfig"; sourceTree = "<group>"; };
206211
3115471C243476CF00D77B8B /* QNTransactionTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QNTransactionTest.m; sourceTree = "<group>"; };
212+
31259E3124E3D02C00CF7531 /* NSData+QNGZip.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+QNGZip.m"; sourceTree = "<group>"; };
213+
31259E3224E3D02C00CF7531 /* NSData+QNGZip.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+QNGZip.h"; sourceTree = "<group>"; };
214+
31259E3524E3D32700CF7531 /* QNGZipTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QNGZipTest.m; sourceTree = "<group>"; };
207215
3142419B2449547B00BD9A21 /* QNCFHttpClientTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QNCFHttpClientTest.m; sourceTree = "<group>"; };
208216
314944502446FED700386F16 /* NSObject+QNSwizzle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+QNSwizzle.h"; sourceTree = "<group>"; };
209217
314944512446FED700386F16 /* NSObject+QNSwizzle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+QNSwizzle.m"; sourceTree = "<group>"; };
@@ -398,6 +406,8 @@
398406
3149446424484DE200386F16 /* Utils */ = {
399407
isa = PBXGroup;
400408
children = (
409+
31259E3224E3D02C00CF7531 /* NSData+QNGZip.h */,
410+
31259E3124E3D02C00CF7531 /* NSData+QNGZip.m */,
401411
31F5538E2456F2F3000B66AD /* QN_GTM_Base64.h */,
402412
31F553822456F2F2000B66AD /* QN_GTM_Base64.m */,
403413
31F553832456F2F2000B66AD /* QNALAssetFile.h */,
@@ -625,6 +635,7 @@
625635
CC2513C02455C141003F4C65 /* QNTempFile.m */,
626636
31A1840D246942E0001D6EEE /* QNComplexUploadSceneTest.m */,
627637
31A1841024694A3C001D6EEE /* QNUploadErrorTest.m */,
638+
31259E3524E3D32700CF7531 /* QNGZipTest.m */,
628639
);
629640
path = QiniuSDKTests;
630641
sourceTree = SOURCE_ROOT;
@@ -660,6 +671,7 @@
660671
CC251352244C026A003F4C65 /* QNHttpResponseInfo.h in Headers */,
661672
DF293C9E19DBC2AE00799011 /* QNUserAgent.h in Headers */,
662673
314944572446FF4700386F16 /* QNCFHttpClient.h in Headers */,
674+
31259E3424E3D02C00CF7531 /* NSData+QNGZip.h in Headers */,
663675
31C2EEE3242DE86300713A33 /* QNUtils.h in Headers */,
664676
31BAA27C243DB83700B7E883 /* QNURLProtocol.h in Headers */,
665677
31F553952456F2F3000B66AD /* QNEtag.h in Headers */,
@@ -947,6 +959,7 @@
947959
DF437CDA1B2429E10099587B /* QNUpToken.m in Sources */,
948960
CC676CC124357778006A7372 /* QNSystemTool.m in Sources */,
949961
CC2513B7245423C3003F4C65 /* QNConcurrentResumeUpload.m in Sources */,
962+
31259E3324E3D02C00CF7531 /* NSData+QNGZip.m in Sources */,
950963
31BAA277243DB83700B7E883 /* NSURLRequest+QNRequest.m in Sources */,
951964
31494477244852BA00386F16 /* QNFixedZone.m in Sources */,
952965
DFF525321A6235D100D02BA1 /* QNSessionManager.m in Sources */,
@@ -981,6 +994,7 @@
981994
DF2CDE7119DAE90300CE01FB /* QNBase64Test.m in Sources */,
982995
3115471D243476CF00D77B8B /* QNTransactionTest.m in Sources */,
983996
3149446224470C0500386F16 /* XCTestCase+QNTest.m in Sources */,
997+
31259E3624E3D32700CF7531 /* QNGZipTest.m in Sources */,
984998
);
985999
runOnlyForDeploymentPostprocessing = 0;
9861000
};
@@ -1000,6 +1014,7 @@
10001014
316A4B8A24346364007BF564 /* QNTransactionManager.m in Sources */,
10011015
DF609A081A58E39D00AC7297 /* QNFormUpload.m in Sources */,
10021016
31F553BD2457FF14000B66AD /* QNConfiguration.m in Sources */,
1017+
31C6E68824ECC75700633629 /* NSData+QNGZip.m in Sources */,
10031018
31C2EECF242CC52600713A33 /* QNDnsCacheInfo.m in Sources */,
10041019
CC251354244C026A003F4C65 /* QNHttpResponseInfo.m in Sources */,
10051020
CC0F85FA2447343C008A1ABA /* QNUploadInfoCollector.m in Sources */,
@@ -1055,6 +1070,7 @@
10551070
DF0A03301B3BAC6E00E3778C /* QNUpTokenTest.m in Sources */,
10561071
3115471E243476D600D77B8B /* QNTransactionTest.m in Sources */,
10571072
3149446324470C0600386F16 /* XCTestCase+QNTest.m in Sources */,
1073+
31259E3724E3D32700CF7531 /* QNGZipTest.m in Sources */,
10581074
);
10591075
runOnlyForDeploymentPostprocessing = 0;
10601076
};

QiniuSDK/Storage/QNUploadInfoReporter.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import "QNAsyncRun.h"
1616
#import "QNSystemTool.h"
1717
#import "QNVersion.h"
18+
#import "NSData+QNGZip.h"
1819
#import <objc/runtime.h>
1920

2021
@interface QNReportBaseItem ()
@@ -376,7 +377,7 @@ - (instancetype)init {
376377
if (self) {
377378
_reportEnable = YES;
378379
_interval = 10;
379-
_serverURL = @"https://uplog.qbox.me/log/4";
380+
_serverURL = @"https://uplog.qbox.me/log/4?compressed=gzip";
380381
_recordDirectory = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"com.qiniu.report"];
381382
_maxRecordFileSize = 2 * 1024 * 1024;
382383
_uploadThreshold = 4 * 1024;
@@ -504,13 +505,18 @@ - (void)innerReport:(NSString *)jsonString token:(NSString *)token {
504505
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:_config.serverURL]];
505506
[request setValue:[NSString stringWithFormat:@"UpToken %@", token] forHTTPHeaderField:@"Authorization"];
506507
[request setValue:[[QNUserAgent sharedInstance] getUserAgent:[QNUpToken parse:token].access] forHTTPHeaderField:@"User-Agent"];
508+
507509
if (self.X_Log_Client_Id) {
508510
[request setValue:self.X_Log_Client_Id forHTTPHeaderField:@"X-Log-Client-Id"];
509511
}
512+
510513
[request setHTTPMethod:@"POST"];
511514
[request setTimeoutInterval:_config.timeoutInterval];
515+
516+
NSData *reportData = [NSData dataWithContentsOfFile:_recorderFilePath];
517+
reportData = [NSData qn_gZip:reportData];
512518
__block NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
513-
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:[NSURL fileURLWithPath:_recorderFilePath] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
519+
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:reportData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
514520
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
515521
if (httpResponse.statusCode == 200) {
516522
self.lastReportTime = [[NSDate dateWithTimeIntervalSinceNow:0] timeIntervalSince1970];

QiniuSDK/Utils/NSData+QNGZip.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// NSData+QNGZip.h
3+
// GZipTest
4+
//
5+
// Created by yangsen on 2020/8/12.
6+
// Copyright © 2020 yangsen. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface NSData(QNGZip)
14+
15+
+ (NSData *)qn_gZip:(NSData *)data;
16+
17+
+ (NSData *)qn_gUnzip:(NSData *)data;
18+
19+
@end
20+
21+
NS_ASSUME_NONNULL_END

QiniuSDK/Utils/NSData+QNGZip.m

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//
2+
// NSData+QNGZip.m
3+
// GZipTest
4+
//
5+
// Created by yangsen on 2020/8/12.
6+
// Copyright © 2020 yangsen. All rights reserved.
7+
//
8+
9+
#import "NSData+QNGZip.h"
10+
#import <zlib.h>
11+
12+
#pragma clang diagnostic ignored "-Wcast-qual"
13+
14+
@implementation NSData(QNGZip)
15+
16+
+ (NSData *)qn_gZip:(NSData *)data{
17+
18+
if (data.length == 0 || [self qn_isGzippedData:data]){
19+
return data;
20+
}
21+
22+
z_stream stream;
23+
stream.opaque = Z_NULL;
24+
stream.zalloc = Z_NULL;
25+
stream.zfree = Z_NULL;
26+
stream.total_out = 0;
27+
stream.avail_out = 0;
28+
stream.avail_in = (uint)data.length;
29+
stream.next_in = (Bytef *)(void *)data.bytes;
30+
31+
static const NSUInteger chunkSize = 16384;
32+
33+
NSMutableData *gzippedData = nil;
34+
35+
if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY) == Z_OK) {
36+
gzippedData = [NSMutableData dataWithLength:chunkSize];
37+
while (stream.avail_out == 0) {
38+
if (stream.total_out >= gzippedData.length) {
39+
gzippedData.length += chunkSize;
40+
}
41+
stream.next_out = (uint8_t *)gzippedData.mutableBytes + stream.total_out;
42+
stream.avail_out = (uInt)(gzippedData.length - stream.total_out);
43+
deflate(&stream, Z_FINISH);
44+
}
45+
deflateEnd(&stream);
46+
gzippedData.length = stream.total_out;
47+
}
48+
49+
return gzippedData;
50+
}
51+
52+
+ (NSData *)qn_gUnzip:(NSData *)data{
53+
if (data.length == 0 || ![self qn_isGzippedData:data]){
54+
return data;
55+
}
56+
57+
z_stream stream;
58+
stream.zalloc = Z_NULL;
59+
stream.zfree = Z_NULL;
60+
stream.total_out = 0;
61+
stream.avail_out = 0;
62+
stream.avail_in = (uint)data.length;
63+
stream.next_in = (Bytef *)data.bytes;
64+
65+
NSMutableData *gunzippedData = nil;
66+
if (inflateInit2(&stream, 47) == Z_OK) {
67+
int status = Z_OK;
68+
gunzippedData = [NSMutableData dataWithCapacity:data.length * 2];
69+
while (status == Z_OK) {
70+
if (stream.total_out >= gunzippedData.length) {
71+
gunzippedData.length += data.length / 2;
72+
}
73+
stream.next_out = (uint8_t *)gunzippedData.mutableBytes + stream.total_out;
74+
stream.avail_out = (uInt)(gunzippedData.length - stream.total_out);
75+
status = inflate (&stream, Z_SYNC_FLUSH);
76+
}
77+
if (inflateEnd(&stream) == Z_OK) {
78+
if (status == Z_STREAM_END) {
79+
gunzippedData.length = stream.total_out;
80+
}
81+
}
82+
}
83+
84+
return gunzippedData;
85+
}
86+
87+
+ (BOOL)qn_isGzippedData:(NSData *)data{
88+
if (!data || data.length == 0) {
89+
return false;
90+
}
91+
const UInt8 *bytes = (const UInt8 *)data.bytes;
92+
return (data.length >= 2 && bytes[0] == 0x1f && bytes[1] == 0x8b);
93+
}
94+
95+
@end

QiniuSDKTests/QNGZipTest.m

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// QNGZipTest.m
3+
// QiniuSDK
4+
//
5+
// Created by yangsen on 2020/8/12.
6+
// Copyright © 2020 Qiniu. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "NSData+QNGZip.h"
11+
12+
@interface QNGZipTest : XCTestCase
13+
14+
@end
15+
16+
@implementation QNGZipTest
17+
18+
- (void)setUp {
19+
// Put setup code here. This method is called before the invocation of each test method in the class.
20+
}
21+
22+
- (void)tearDown {
23+
// Put teardown code here. This method is called after the invocation of each test method in the class.
24+
}
25+
26+
- (void)testGZip {
27+
28+
NSData *data = [NSData data];
29+
NSData *gzip = [NSData qn_gZip:data];
30+
XCTAssertTrue([data isEqualToData:gzip], "pass");
31+
32+
NSString *string = @"ABCDEFG";
33+
data = [string dataUsingEncoding:NSUTF8StringEncoding];
34+
gzip = [NSData qn_gZip:data];
35+
36+
NSData *gUnzip = [NSData qn_gUnzip:gzip];
37+
NSString *stringGUnzip = [[NSString alloc] initWithData:gUnzip encoding:NSUTF8StringEncoding];
38+
XCTAssertTrue([string isEqualToString:stringGUnzip], "pass");
39+
40+
NSData *reGUnzip = [NSData qn_gUnzip:gUnzip];
41+
XCTAssertTrue([gUnzip isEqualToData:reGUnzip], "pass");
42+
}
43+
44+
45+
@end

QiniuSDKTests/QNTestConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
// Copyright (c) 2014年 Qiniu. All rights reserved.
77
//
88

9-
109
static NSString *const g_token = @"jH983zIUFIP1OVumiBVGeAfiLYJvwrF45S-t22eu:6SxDNsbfE8Womrv09M5ORkcTDKk=:eyJzY29wZSI6InpvbmUwLXNwYWNlIiwiZGVhZGxpbmUiOjE2MDI5MDA0MDAsICJyZXR1cm5Cb2R5Ijoie1wiZm9vXCI6JCh4OmZvbyksIFwiYmFyXCI6JCh4OmJhciksIFwibWltZVR5cGVcIjokKG1pbWVUeXBlKSwgXCJoYXNoXCI6JChldGFnKSwgXCJrZXlcIjokKGtleSksIFwiZm5hbWVcIjokKGZuYW1lKX0ifQ==";
10+

0 commit comments

Comments
 (0)