Skip to content

Commit fbafd33

Browse files
committed
gzip report text
1 parent d66bce1 commit fbafd33

File tree

5 files changed

+179
-1
lines changed

5 files changed

+179
-1
lines changed

QiniuSDK.xcodeproj/project.pbxproj

Lines changed: 14 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 */; };
@@ -204,6 +208,9 @@
204208
/* Begin PBXFileReference section */
205209
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>"; };
206210
3115471C243476CF00D77B8B /* QNTransactionTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QNTransactionTest.m; sourceTree = "<group>"; };
211+
31259E3124E3D02C00CF7531 /* NSData+QNGZip.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+QNGZip.m"; sourceTree = "<group>"; };
212+
31259E3224E3D02C00CF7531 /* NSData+QNGZip.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+QNGZip.h"; sourceTree = "<group>"; };
213+
31259E3524E3D32700CF7531 /* QNGZipTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QNGZipTest.m; sourceTree = "<group>"; };
207214
3142419B2449547B00BD9A21 /* QNCFHttpClientTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QNCFHttpClientTest.m; sourceTree = "<group>"; };
208215
314944502446FED700386F16 /* NSObject+QNSwizzle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+QNSwizzle.h"; sourceTree = "<group>"; };
209216
314944512446FED700386F16 /* NSObject+QNSwizzle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+QNSwizzle.m"; sourceTree = "<group>"; };
@@ -398,6 +405,8 @@
398405
3149446424484DE200386F16 /* Utils */ = {
399406
isa = PBXGroup;
400407
children = (
408+
31259E3224E3D02C00CF7531 /* NSData+QNGZip.h */,
409+
31259E3124E3D02C00CF7531 /* NSData+QNGZip.m */,
401410
31F5538E2456F2F3000B66AD /* QN_GTM_Base64.h */,
402411
31F553822456F2F2000B66AD /* QN_GTM_Base64.m */,
403412
31F553832456F2F2000B66AD /* QNALAssetFile.h */,
@@ -625,6 +634,7 @@
625634
CC2513C02455C141003F4C65 /* QNTempFile.m */,
626635
31A1840D246942E0001D6EEE /* QNComplexUploadSceneTest.m */,
627636
31A1841024694A3C001D6EEE /* QNUploadErrorTest.m */,
637+
31259E3524E3D32700CF7531 /* QNGZipTest.m */,
628638
);
629639
path = QiniuSDKTests;
630640
sourceTree = SOURCE_ROOT;
@@ -660,6 +670,7 @@
660670
CC251352244C026A003F4C65 /* QNHttpResponseInfo.h in Headers */,
661671
DF293C9E19DBC2AE00799011 /* QNUserAgent.h in Headers */,
662672
314944572446FF4700386F16 /* QNCFHttpClient.h in Headers */,
673+
31259E3424E3D02C00CF7531 /* NSData+QNGZip.h in Headers */,
663674
31C2EEE3242DE86300713A33 /* QNUtils.h in Headers */,
664675
31BAA27C243DB83700B7E883 /* QNURLProtocol.h in Headers */,
665676
31F553952456F2F3000B66AD /* QNEtag.h in Headers */,
@@ -947,6 +958,7 @@
947958
DF437CDA1B2429E10099587B /* QNUpToken.m in Sources */,
948959
CC676CC124357778006A7372 /* QNSystemTool.m in Sources */,
949960
CC2513B7245423C3003F4C65 /* QNConcurrentResumeUpload.m in Sources */,
961+
31259E3324E3D02C00CF7531 /* NSData+QNGZip.m in Sources */,
950962
31BAA277243DB83700B7E883 /* NSURLRequest+QNRequest.m in Sources */,
951963
31494477244852BA00386F16 /* QNFixedZone.m in Sources */,
952964
DFF525321A6235D100D02BA1 /* QNSessionManager.m in Sources */,
@@ -981,6 +993,7 @@
981993
DF2CDE7119DAE90300CE01FB /* QNBase64Test.m in Sources */,
982994
3115471D243476CF00D77B8B /* QNTransactionTest.m in Sources */,
983995
3149446224470C0500386F16 /* XCTestCase+QNTest.m in Sources */,
996+
31259E3624E3D32700CF7531 /* QNGZipTest.m in Sources */,
984997
);
985998
runOnlyForDeploymentPostprocessing = 0;
986999
};
@@ -1055,6 +1068,7 @@
10551068
DF0A03301B3BAC6E00E3778C /* QNUpTokenTest.m in Sources */,
10561069
3115471E243476D600D77B8B /* QNTransactionTest.m in Sources */,
10571070
3149446324470C0600386F16 /* XCTestCase+QNTest.m in Sources */,
1071+
31259E3724E3D32700CF7531 /* QNGZipTest.m in Sources */,
10581072
);
10591073
runOnlyForDeploymentPostprocessing = 0;
10601074
};

QiniuSDK/Storage/QNUploadInfoReporter.m

Lines changed: 7 additions & 1 deletion
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 ()
@@ -508,13 +509,18 @@ - (void)innerReport:(NSString *)jsonString token:(NSString *)token {
508509
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:_config.serverURL]];
509510
[request setValue:[NSString stringWithFormat:@"UpToken %@", token] forHTTPHeaderField:@"Authorization"];
510511
[request setValue:[[QNUserAgent sharedInstance] getUserAgent:[QNUpToken parse:token].access] forHTTPHeaderField:@"User-Agent"];
512+
[request setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
513+
511514
if (self.X_Log_Client_Id) {
512515
[request setValue:self.X_Log_Client_Id forHTTPHeaderField:@"X-Log-Client-Id"];
513516
}
517+
514518
[request setHTTPMethod:@"POST"];
515519
[request setTimeoutInterval:_config.timeoutInterval];
520+
521+
NSData *reportData = [[NSData dataWithContentsOfFile:_recorderFilePath] qn_gZip];
516522
__block NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
517-
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:[NSURL fileURLWithPath:_recorderFilePath] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
523+
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:reportData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
518524
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
519525
if (httpResponse.statusCode == 200) {
520526
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;
16+
17+
- (NSData *)qn_gUnzip;
18+
19+
@end
20+
21+
NS_ASSUME_NONNULL_END

QiniuSDK/Utils/NSData+QNGZip.m

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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{
17+
18+
if (self.length == 0 || [self qn_isGzippedData]){
19+
return self;
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)self.length;
29+
stream.next_in = (Bytef *)(void *)self.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{
53+
if (self.length == 0 || ![self qn_isGzippedData]){
54+
return self;
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)self.length;
63+
stream.next_in = (Bytef *)self.bytes;
64+
65+
NSMutableData *gunzippedData = nil;
66+
if (inflateInit2(&stream, 47) == Z_OK) {
67+
int status = Z_OK;
68+
gunzippedData = [NSMutableData dataWithCapacity:self.length * 2];
69+
while (status == Z_OK) {
70+
if (stream.total_out >= gunzippedData.length) {
71+
gunzippedData.length += self.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{
88+
const UInt8 *bytes = (const UInt8 *)self.bytes;
89+
return (self.length >= 2 && bytes[0] == 0x1f && bytes[1] == 0x8b);
90+
}
91+
92+
@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 = [data qn_gZip];
30+
XCTAssertTrue([gzip isEqualToData:gzip], "pass");
31+
32+
NSString *string = @"ABCDEFG";
33+
data = [string dataUsingEncoding:NSUTF8StringEncoding];
34+
gzip = [data qn_gZip];
35+
36+
NSData *gUnzip = [gzip qn_gUnzip];
37+
NSString *stringGUnzip = [[NSString alloc] initWithData:gUnzip encoding:NSUTF8StringEncoding];
38+
XCTAssertTrue([string isEqualToString:stringGUnzip], "pass");
39+
40+
NSData *reGUnzip = [gzip qn_gZip];
41+
XCTAssertTrue([gzip isEqualToData:reGUnzip], "pass");
42+
}
43+
44+
45+
@end

0 commit comments

Comments
 (0)