Skip to content

Commit

Permalink
# 5.5.6
Browse files Browse the repository at this point in the history
从服务器获取签名的工具类
  • Loading branch information
karisli committed Sep 21, 2018
1 parent a521560 commit 3b47cb6
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 5.5.6
从服务器获取签名的工具类

# 5.5.5
- bug修复

Expand Down
6 changes: 3 additions & 3 deletions QCloudCOSXML.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "QCloudCOSXML"
s.version = "5.5.5"
s.version = "5.5.6"
s.summary = "QCloudCOSXML 腾讯云iOS-SDK组件"

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

s.subspec 'Transfer' do |sbt|
sbt.source_files = 'QCloudCOSXML/Classes/Transfer/**/*','QCloudCOSXML/Classes/Base/*'
sbt.dependency "QCloudCore",'5.5.5'
sbt.dependency "QCloudCore",'5.5.6'
# sbt.static_framework=true
end
end
6 changes: 3 additions & 3 deletions QCloudCOSXML/Classes/QCloudCOSXMLVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

#ifndef QCloudCOSXMLModuleVersion_h
#define QCloudCOSXMLModuleVersion_h
#define QCloudCOSXMLModuleVersionNumber 505005
#define QCloudCOSXMLModuleVersionNumber 505006

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

//
Expand Down
2 changes: 1 addition & 1 deletion QCloudCOSXML/Classes/QCloudCOSXMLVersion.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "QCloudCOSXMLVersion.h"
NSString * const QCloudCOSXMLModuleVersion = @"5.5.5";
NSString * const QCloudCOSXMLModuleVersion = @"5.5.6";
NSString * const QCloudCOSXMLModuleName = @"QCloudCOSXML";
@interface QCloudQCloudCOSXMLLoad : NSObject
@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,8 @@ - (void) cancel
}

NSMutableArray* cancelledRequestIDs = [NSMutableArray array];
for (QCloudHTTPRequest* request in self.requestCacheArray) {
NSArray *tmpRequestCacheArray = [self.requestCacheArray copy];
for (QCloudHTTPRequest* request in tmpRequestCacheArray) {
if (request != nil) {
[cancelledRequestIDs addObject:[NSNumber numberWithLongLong:request.requestID]];
}
Expand Down
2 changes: 1 addition & 1 deletion QCloudCore.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = "QCloudCore"
s.version = "5.5.5"
s.version = "5.5.6"
s.summary = "QCloudCore--腾讯云iOS-SDK Foundation"

# This description is used to generate tags and improve search results.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// COSXMLCSPGetSignatureHelper.h
// QCloudCSPDemo
//
// Created by karisli(李雪) on 2018/9/20.
// Copyright © 2018年 karisli(李雪). All rights reserved.
//

#import <Foundation/Foundation.h>
@class QCloudHTTPRequest;
NS_ASSUME_NONNULL_BEGIN
//成功后回调的block :参数: 1. id: object(如果是 JSON ,那么直接解析   成OC中的数组或者字典.如果不是JSON ,直接返回 NSData) 2. NSURLResponse: 响应头信息,主要是对服务器端的描述
typedef void(^SuccessBlock)(NSString * sign);
//失败后回调的block:参数: 1.error:错误信息,如果请求失败,则error有值
typedef void(^failBlock)(NSError *error);

@interface COSXMLGetSignatureTool : NSObject
+(instancetype)sharedNewtWorkTool;
-(void)PutRequestWithUrl:(NSString *)urlString request:(NSMutableURLRequest* )urlRequest successBlock:(SuccessBlock)success;
@end

NS_ASSUME_NONNULL_END
129 changes: 129 additions & 0 deletions QCloudCore/Classes/QCLOUDRestNet/CoreRequest/COSXMLGetSignatureTool.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//
// COSXMLCSPGetSignatureHelper.m
// QCloudCSPDemo
//
// Created by karisli(李雪) on 2018/9/20.
// Copyright © 2018年 karisli(李雪). All rights reserved.
//

#import "COSXMLGetSignatureTool.h"

#import "QCloudRequestSerializer.h"
#import "QCloudLogger.h"
#import "QCloudURLHelper.h"
#import <QCloudCore/QCloudError.h>
@implementation NSDictionary(HeaderFilter)
- (NSDictionary*)filteHeaders; {
NSMutableDictionary* signedHeaders = [[NSMutableDictionary alloc] init];
__block const NSMutableArray* shouldSignedHeaderList = @[ @"Content-Length", @"Content-MD5"];
[self enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
//签名的Headers列表:x开头的(x-cos-之类的),content-length,content-MD5
BOOL shouldSigned = NO;
for (NSString* header in shouldSignedHeaderList) {
if ([header isEqualToString:((NSString*)key)]) {
shouldSigned = YES;
}
}
NSArray* headerSeperatedArray = [key componentsSeparatedByString:@"-"];
if ([headerSeperatedArray firstObject] && [headerSeperatedArray.firstObject isEqualToString:@"x"]) {
shouldSigned = YES;
}
if (shouldSigned) {
signedHeaders[key]=obj;
}
}];
return [signedHeaders copy];
}
@end
@implementation NSURL(QCloudCSPExtension)

/**
返回 COS 签名中用到的 path , 。如果没有path时,为 /
例如
1. URL 为: http://test-123456.cos.ap-shanghai.myqcloud.com?delimiter=%2F&max-keys=1000&prefix=test%2F
path为 /
2. URL为: http://test-123456.cos.ap-shanghai.myqcloud.com/test
path 为 test
3. URL为: http://test-123456.cos.ap-shanghai.myqcloud.com/test/
path 为 test/
@return COS签名中定义的 path
*/

- (NSString*)qcloud_csp_path {
NSString* path = QCloudPercentEscapedStringFromString(self.path);
//absoluteString in NSURL is URLEncoded
NSRange pathRange = [self.absoluteString rangeOfString:path];
NSUInteger URLLength = self.absoluteString.length;
if ( pathRange.location == NSNotFound ) {
return self.path;
}
NSUInteger pathLocation = pathRange.location + pathRange.length;
if (pathLocation >= URLLength) {
return self.path;
}
if ( [self.absoluteString characterAtIndex:(pathLocation)] == '/' ) {
path = [self.path stringByAppendingString:@"/"];
return path;
}

return self.path;
}

@end
@implementation COSXMLGetSignatureTool
+(instancetype)sharedNewtWorkTool
{
static id _instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init];
});
return _instance;
}

-(void)PutRequestWithUrl:(NSString *)urlString request:(NSMutableURLRequest* )urlRequest successBlock:(SuccessBlock)success;
{
//取出参数
NSDictionary* headers = [[urlRequest allHTTPHeaderFields] filteHeaders];
NSDictionary* paramas = QCloudURLReadQuery(urlRequest.URL);
NSMutableDictionary *paramaters = [NSMutableDictionary dictionary];
paramaters[@"path"] = urlRequest.URL.qcloud_csp_path;
paramaters[@"method"] = urlRequest.HTTPMethod;
paramaters[@"host"] = urlRequest.URL.host;
paramaters[@"headers"] = headers;
paramaters[@"params"] = paramas;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
request.HTTPMethod = @"PUT";
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:paramaters options:NSJSONWritingPrettyPrinted error:nil]];

NSLog(@"request Body: %@",[[NSString alloc]initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]);
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
QCloudLogInfo(@"response data:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
if (data && !error) {
id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
if (!obj) {
@throw [NSException exceptionWithName:QCloudErrorDomain reason:@"返回的不是json数据" userInfo:nil];
}
NSDictionary *dic = (NSDictionary *)obj;
QCloudLogInfo(@"%@ 的签名%@",urlRequest.URL,dic[@"sign"]);
dispatch_async(dispatch_get_main_queue(), ^{
if (success) {
success(obj[@"sign"]);
}
});
}else
{
@throw [NSException exceptionWithName:QCloudErrorDomain reason:@"获取签名错误" userInfo:nil];
}

}] resume];

}
@end
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#import "NSError+QCloudNetworking.h"
#import "NSObject+HTTPHeadersContainer.h"
#import "QCloudService.h"

@interface QCloudHTTPRequest ()
{
BOOL _requesting;
Expand Down Expand Up @@ -76,6 +77,7 @@ - (void) notifySuccess:(id)object

- (void) loadConfigureBlock
{

[self setConfigureBlock:^(QCloudRequestSerializer *requestSerializer, QCloudResponseSerializer *responseSerializer) {

requestSerializer.HTTPMethod = HTTPMethodGET;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ - (int) performRequest:(QCloudHTTPRequest *)httpRequst withFinishBlock:(QCloudRe
return [self performRequest:httpRequst];
}

- (int) performRequest:(QCloudHTTPRequest *)request
- (int) performRequest:(QCloudHTTPRequest *)request
{
QCloudHTTPRequestOperation* operation = [[QCloudHTTPRequestOperation alloc] initWithRequest:request];
operation.sessionManager = self;
Expand Down
2 changes: 1 addition & 1 deletion QCloudCore/Classes/QCloudCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@
#import "QCloudAuthentationV4Creator.h"
#import "QCloudCredentailFenceQueue.h"
#import "QCloudMultiDelegateProxy.h"

#import "COSXMLGetSignatureTool.h"
#endif /* QCloudCore_h */
2 changes: 1 addition & 1 deletion QCloudCore/Classes/QCloudCoreVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#ifndef QCloudCoreModuleVersion_h
#define QCloudCoreModuleVersion_h
#define QCloudCoreModuleVersionNumber 505005
#define QCloudCoreModuleVersionNumber 505006

//dependency

Expand Down
2 changes: 1 addition & 1 deletion QCloudCore/Classes/QCloudCoreVersion.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "QCloudCoreVersion.h"
NSString * const QCloudCoreModuleVersion = @"5.5.5";
NSString * const QCloudCoreModuleVersion = @"5.5.6";
NSString * const QCloudCoreModuleName = @"QCloudCore";
@interface QCloudQCloudCoreLoad : NSObject
@end
Expand Down

0 comments on commit 3b47cb6

Please sign in to comment.