Authored by 朱小军

添加请求支持gzip压缩 review by 尹诚

... ... @@ -20,6 +20,7 @@
// THE SOFTWARE.
#import "AFURLRequestSerialization.h"
#import "NSData+YOHOGZIP.h"
#if __IPHONE_OS_VERSION_MIN_REQUIRED
#import <MobileCoreServices/MobileCoreServices.h>
... ... @@ -514,6 +515,12 @@ forHTTPHeaderField:(NSString *)field
[mutableRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
}
[mutableRequest setHTTPBody:[query dataUsingEncoding:self.stringEncoding]];
if ([mutableRequest.allHTTPHeaderFields[@"Content-Encoding"] isEqualToString:@"gzip"]) {
mutableRequest.HTTPBody = [mutableRequest.HTTPBody YHgzippedData];
[mutableRequest setValue:(@(mutableRequest.HTTPBody.length)).description forHTTPHeaderField:@"Content-Length"];
}
}
}
... ... @@ -1266,11 +1273,12 @@ typedef enum {
}
[mutableRequest setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:self.writingOptions error:error]];
}
}
return mutableRequest;
}
#pragma mark - NSSecureCoding
- (id)initWithCoder:(NSCoder *)decoder {
... ...
//
// GZIP.h
//
// Version 1.1.1
//
// Created by Nick Lockwood on 03/06/2012.
// Copyright (C) 2012 Charcoal Design
//
// Distributed under the permissive zlib License
// Get the latest version from here:
//
// https://github.com/nicklockwood/GZIP
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
#import <Foundation/Foundation.h>
@interface NSData (YOHOGZIP)
- (nullable NSData *)YHgzippedDataWithCompressionLevel:(float)level;
- (nullable NSData *)YHgzippedData;
- (nullable NSData *)YHgunzippedData;
- (BOOL)YHisGzippedData;
@end
... ...
//
// GZIP.m
//
// Version 1.1.1
//
// Created by Nick Lockwood on 03/06/2012.
// Copyright (C) 2012 Charcoal Design
//
// Distributed under the permissive zlib License
// Get the latest version from here:
//
// https://github.com/nicklockwood/GZIP
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
#import "NSData+YOHOGZIP.h"
#import <zlib.h>
#import <dlfcn.h>
#pragma clang diagnostic ignored "-Wcast-qual"
@implementation NSData (YOHOGZIP)
static void *libzOpen()
{
static void *libz;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
libz = dlopen("/usr/lib/libz.dylib", RTLD_LAZY);
});
return libz;
}
- (NSData *)YHgzippedDataWithCompressionLevel:(float)level
{
if (self.length == 0 || [self YHisGzippedData])
{
return self;
}
void *libz = libzOpen();
int (*deflateInit2_)(z_streamp, int, int, int, int, int, const char *, int) =
(int (*)(z_streamp, int, int, int, int, int, const char *, int))dlsym(libz, "deflateInit2_");
int (*deflate)(z_streamp, int) = (int (*)(z_streamp, int))dlsym(libz, "deflate");
int (*deflateEnd)(z_streamp) = (int (*)(z_streamp))dlsym(libz, "deflateEnd");
z_stream stream;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
stream.avail_in = (uint)self.length;
stream.next_in = (Bytef *)(void *)self.bytes;
stream.total_out = 0;
stream.avail_out = 0;
static const NSUInteger ChunkSize = 16384;
NSMutableData *output = nil;
int compression = (level < 0.0f)? Z_DEFAULT_COMPRESSION: (int)(roundf(level * 9));
if (deflateInit2(&stream, compression, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY) == Z_OK)
{
output = [NSMutableData dataWithLength:ChunkSize];
while (stream.avail_out == 0)
{
if (stream.total_out >= output.length)
{
output.length += ChunkSize;
}
stream.next_out = (uint8_t *)output.mutableBytes + stream.total_out;
stream.avail_out = (uInt)(output.length - stream.total_out);
deflate(&stream, Z_FINISH);
}
deflateEnd(&stream);
output.length = stream.total_out;
}
return output;
}
- (NSData *)YHgzippedData
{
return [self YHgzippedDataWithCompressionLevel:-1.0f];
}
- (NSData *)YHgunzippedData
{
if (self.length == 0 || ![self YHisGzippedData])
{
return self;
}
void *libz = libzOpen();
int (*inflateInit2_)(z_streamp, int, const char *, int) =
(int (*)(z_streamp, int, const char *, int))dlsym(libz, "inflateInit2_");
int (*inflate)(z_streamp, int) = (int (*)(z_streamp, int))dlsym(libz, "inflate");
int (*inflateEnd)(z_streamp) = (int (*)(z_streamp))dlsym(libz, "inflateEnd");
z_stream stream;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.avail_in = (uint)self.length;
stream.next_in = (Bytef *)self.bytes;
stream.total_out = 0;
stream.avail_out = 0;
NSMutableData *output = nil;
if (inflateInit2(&stream, 47) == Z_OK)
{
int status = Z_OK;
output = [NSMutableData dataWithCapacity:self.length * 2];
while (status == Z_OK)
{
if (stream.total_out >= output.length)
{
output.length += self.length / 2;
}
stream.next_out = (uint8_t *)output.mutableBytes + stream.total_out;
stream.avail_out = (uInt)(output.length - stream.total_out);
status = inflate (&stream, Z_SYNC_FLUSH);
}
if (inflateEnd(&stream) == Z_OK)
{
if (status == Z_STREAM_END)
{
output.length = stream.total_out;
}
}
}
return output;
}
- (BOOL)YHisGzippedData
{
const UInt8 *bytes = (const UInt8 *)self.bytes;
return (self.length >= 2 && bytes[0] == 0x1f && bytes[1] == 0x8b);
}
@end
... ...
Pod::Spec.new do |s|
s.name = "YH_AFNetworking"
s.version = "1.0.1"
s.version = "1.0.2"
s.summary = "YH_AFNetworking"
s.description = <<-DESC
YH_AFNetworking
DESC
s.homepage = "http://git.yoho.cn/ios/yh_afnetworking"
s.license = 'MIT'
s.author = { "wangqj" => "arthur.wang@yoho.cn" }
s.author = { "iOSdev" => "iosdev@yoho.cn" }
s.source = { :git => "http://git.yoho.cn/ios/yh_afnetworking.git",
:branch => 'master',
:tag => s.version.to_s }
... ... @@ -28,6 +28,10 @@ Pod::Spec.new do |s|
s.subspec 'Aspects' do |ss|
ss.source_files = 'Aspects/*.{h,m}'
ss.requires_arc = true
s.subspec 'Util' do |ss|
ss.source_files = 'Util/*.{h,m}'
ss.requires_arc = true
end
s.frameworks = 'Foundation', 'CoreGraphics', 'UIKit'
... ...