Skip to content

Commit 3b47cb6

Browse files
author
karisli
committed
# 5.5.6
从服务器获取签名的工具类
1 parent a521560 commit 3b47cb6

File tree

13 files changed

+170
-13
lines changed

13 files changed

+170
-13
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 5.5.6
2+
从服务器获取签名的工具类
3+
14
# 5.5.5
25
- bug修复
36

QCloudCOSXML.podspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "QCloudCOSXML"
3-
s.version = "5.5.5"
3+
s.version = "5.5.6"
44
s.summary = "QCloudCOSXML 腾讯云iOS-SDK组件"
55

66
s.homepage = "https://cloud.tencent.com/"
@@ -9,12 +9,12 @@ s.version = "5.5.5"
99
s.source = { :git => "https://github.com/tencentyun/qcloud-sdk-ios.git", :tag => s.version.to_s }
1010
s.ios.deployment_target = '8.0'
1111
s.source_files = 'QCloudCOSXML/Classes/**/*'
12-
s.dependency "QCloudCore",'5.5.5'
12+
s.dependency "QCloudCore",'5.5.6'
1313
s.static_framework = true
1414

1515
s.subspec 'Transfer' do |sbt|
1616
sbt.source_files = 'QCloudCOSXML/Classes/Transfer/**/*','QCloudCOSXML/Classes/Base/*'
17-
sbt.dependency "QCloudCore",'5.5.5'
17+
sbt.dependency "QCloudCore",'5.5.6'
1818
# sbt.static_framework=true
1919
end
2020
end

QCloudCOSXML/Classes/QCloudCOSXMLVersion.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
#ifndef QCloudCOSXMLModuleVersion_h
88
#define QCloudCOSXMLModuleVersion_h
9-
#define QCloudCOSXMLModuleVersionNumber 505005
9+
#define QCloudCOSXMLModuleVersionNumber 505006
1010

1111
//dependency
12-
#if QCloudCoreModuleVersionNumber != 505005
13-
#error "库QCloudCOSXML依赖QCloudCore最小版本号为5.5.5,当前引入的QCloudCore版本号过低,请及时升级后使用"
12+
#if QCloudCoreModuleVersionNumber != 505006
13+
#error "库QCloudCOSXML依赖QCloudCore最小版本号为5.5.6,当前引入的QCloudCore版本号过低,请及时升级后使用"
1414
#endif
1515

1616
//

QCloudCOSXML/Classes/QCloudCOSXMLVersion.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#import "QCloudCOSXMLVersion.h"
2-
NSString * const QCloudCOSXMLModuleVersion = @"5.5.5";
2+
NSString * const QCloudCOSXMLModuleVersion = @"5.5.6";
33
NSString * const QCloudCOSXMLModuleName = @"QCloudCOSXML";
44
@interface QCloudQCloudCOSXMLLoad : NSObject
55
@end

QCloudCOSXML/Classes/Transfer/request/QCloudCOSXMLUploadObjectRequest.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,8 @@ - (void) cancel
519519
}
520520

521521
NSMutableArray* cancelledRequestIDs = [NSMutableArray array];
522-
for (QCloudHTTPRequest* request in self.requestCacheArray) {
522+
NSArray *tmpRequestCacheArray = [self.requestCacheArray copy];
523+
for (QCloudHTTPRequest* request in tmpRequestCacheArray) {
523524
if (request != nil) {
524525
[cancelledRequestIDs addObject:[NSNumber numberWithLongLong:request.requestID]];
525526
}

QCloudCore.podspec

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

99
Pod::Spec.new do |s|
1010
s.name = "QCloudCore"
11-
s.version = "5.5.5"
11+
s.version = "5.5.6"
1212
s.summary = "QCloudCore--腾讯云iOS-SDK Foundation"
1313

1414
# This description is used to generate tags and improve search results.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// COSXMLCSPGetSignatureHelper.h
3+
// QCloudCSPDemo
4+
//
5+
// Created by karisli(李雪) on 2018/9/20.
6+
// Copyright © 2018年 karisli(李雪). All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
@class QCloudHTTPRequest;
11+
NS_ASSUME_NONNULL_BEGIN
12+
//成功后回调的block :参数: 1. id: object(如果是 JSON ,那么直接解析   成OC中的数组或者字典.如果不是JSON ,直接返回 NSData) 2. NSURLResponse: 响应头信息,主要是对服务器端的描述
13+
typedef void(^SuccessBlock)(NSString * sign);
14+
//失败后回调的block:参数: 1.error:错误信息,如果请求失败,则error有值
15+
typedef void(^failBlock)(NSError *error);
16+
17+
@interface COSXMLGetSignatureTool : NSObject
18+
+(instancetype)sharedNewtWorkTool;
19+
-(void)PutRequestWithUrl:(NSString *)urlString request:(NSMutableURLRequest* )urlRequest successBlock:(SuccessBlock)success;
20+
@end
21+
22+
NS_ASSUME_NONNULL_END
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
//
2+
// COSXMLCSPGetSignatureHelper.m
3+
// QCloudCSPDemo
4+
//
5+
// Created by karisli(李雪) on 2018/9/20.
6+
// Copyright © 2018年 karisli(李雪). All rights reserved.
7+
//
8+
9+
#import "COSXMLGetSignatureTool.h"
10+
11+
#import "QCloudRequestSerializer.h"
12+
#import "QCloudLogger.h"
13+
#import "QCloudURLHelper.h"
14+
#import <QCloudCore/QCloudError.h>
15+
@implementation NSDictionary(HeaderFilter)
16+
- (NSDictionary*)filteHeaders; {
17+
NSMutableDictionary* signedHeaders = [[NSMutableDictionary alloc] init];
18+
__block const NSMutableArray* shouldSignedHeaderList = @[ @"Content-Length", @"Content-MD5"];
19+
[self enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
20+
//签名的Headers列表:x开头的(x-cos-之类的),content-length,content-MD5
21+
BOOL shouldSigned = NO;
22+
for (NSString* header in shouldSignedHeaderList) {
23+
if ([header isEqualToString:((NSString*)key)]) {
24+
shouldSigned = YES;
25+
}
26+
}
27+
NSArray* headerSeperatedArray = [key componentsSeparatedByString:@"-"];
28+
if ([headerSeperatedArray firstObject] && [headerSeperatedArray.firstObject isEqualToString:@"x"]) {
29+
shouldSigned = YES;
30+
}
31+
if (shouldSigned) {
32+
signedHeaders[key]=obj;
33+
}
34+
}];
35+
return [signedHeaders copy];
36+
}
37+
@end
38+
@implementation NSURL(QCloudCSPExtension)
39+
40+
/**
41+
返回 COS 签名中用到的 path , 。如果没有path时,为 /
42+
43+
例如
44+
1. URL 为: http://test-123456.cos.ap-shanghai.myqcloud.com?delimiter=%2F&max-keys=1000&prefix=test%2F
45+
46+
path为 /
47+
48+
2. URL为: http://test-123456.cos.ap-shanghai.myqcloud.com/test
49+
path 为 test
50+
51+
3. URL为: http://test-123456.cos.ap-shanghai.myqcloud.com/test/
52+
path 为 test/
53+
54+
55+
@return COS签名中定义的 path
56+
*/
57+
58+
- (NSString*)qcloud_csp_path {
59+
NSString* path = QCloudPercentEscapedStringFromString(self.path);
60+
//absoluteString in NSURL is URLEncoded
61+
NSRange pathRange = [self.absoluteString rangeOfString:path];
62+
NSUInteger URLLength = self.absoluteString.length;
63+
if ( pathRange.location == NSNotFound ) {
64+
return self.path;
65+
}
66+
NSUInteger pathLocation = pathRange.location + pathRange.length;
67+
if (pathLocation >= URLLength) {
68+
return self.path;
69+
}
70+
if ( [self.absoluteString characterAtIndex:(pathLocation)] == '/' ) {
71+
path = [self.path stringByAppendingString:@"/"];
72+
return path;
73+
}
74+
75+
return self.path;
76+
}
77+
78+
@end
79+
@implementation COSXMLGetSignatureTool
80+
+(instancetype)sharedNewtWorkTool
81+
{
82+
static id _instance;
83+
static dispatch_once_t onceToken;
84+
dispatch_once(&onceToken, ^{
85+
_instance = [[self alloc] init];
86+
});
87+
return _instance;
88+
}
89+
90+
-(void)PutRequestWithUrl:(NSString *)urlString request:(NSMutableURLRequest* )urlRequest successBlock:(SuccessBlock)success;
91+
{
92+
//取出参数
93+
NSDictionary* headers = [[urlRequest allHTTPHeaderFields] filteHeaders];
94+
NSDictionary* paramas = QCloudURLReadQuery(urlRequest.URL);
95+
NSMutableDictionary *paramaters = [NSMutableDictionary dictionary];
96+
paramaters[@"path"] = urlRequest.URL.qcloud_csp_path;
97+
paramaters[@"method"] = urlRequest.HTTPMethod;
98+
paramaters[@"host"] = urlRequest.URL.host;
99+
paramaters[@"headers"] = headers;
100+
paramaters[@"params"] = paramas;
101+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
102+
request.HTTPMethod = @"PUT";
103+
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
104+
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:paramaters options:NSJSONWritingPrettyPrinted error:nil]];
105+
106+
NSLog(@"request Body: %@",[[NSString alloc]initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]);
107+
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
108+
QCloudLogInfo(@"response data:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
109+
if (data && !error) {
110+
id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
111+
if (!obj) {
112+
@throw [NSException exceptionWithName:QCloudErrorDomain reason:@"返回的不是json数据" userInfo:nil];
113+
}
114+
NSDictionary *dic = (NSDictionary *)obj;
115+
QCloudLogInfo(@"%@ 的签名%@",urlRequest.URL,dic[@"sign"]);
116+
dispatch_async(dispatch_get_main_queue(), ^{
117+
if (success) {
118+
success(obj[@"sign"]);
119+
}
120+
});
121+
}else
122+
{
123+
@throw [NSException exceptionWithName:QCloudErrorDomain reason:@"获取签名错误" userInfo:nil];
124+
}
125+
126+
}] resume];
127+
128+
}
129+
@end

QCloudCore/Classes/QCLOUDRestNet/CoreRequest/QCloudHTTPRequest.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#import "NSError+QCloudNetworking.h"
2323
#import "NSObject+HTTPHeadersContainer.h"
2424
#import "QCloudService.h"
25+
2526
@interface QCloudHTTPRequest ()
2627
{
2728
BOOL _requesting;
@@ -76,6 +77,7 @@ - (void) notifySuccess:(id)object
7677

7778
- (void) loadConfigureBlock
7879
{
80+
7981
[self setConfigureBlock:^(QCloudRequestSerializer *requestSerializer, QCloudResponseSerializer *responseSerializer) {
8082

8183
requestSerializer.HTTPMethod = HTTPMethodGET;

QCloudCore/Classes/QCLOUDRestNet/SessionSupport/QCloudHTTPSessionManager.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ - (int) performRequest:(QCloudHTTPRequest *)httpRequst withFinishBlock:(QCloudRe
190190
return [self performRequest:httpRequst];
191191
}
192192

193-
- (int) performRequest:(QCloudHTTPRequest *)request
193+
- (int) performRequest:(QCloudHTTPRequest *)request
194194
{
195195
QCloudHTTPRequestOperation* operation = [[QCloudHTTPRequestOperation alloc] initWithRequest:request];
196196
operation.sessionManager = self;

0 commit comments

Comments
 (0)