-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
从服务器获取签名的工具类
- Loading branch information
karisli
committed
Sep 21, 2018
1 parent
a521560
commit 3b47cb6
Showing
13 changed files
with
170 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# 5.5.6 | ||
从服务器获取签名的工具类 | ||
|
||
# 5.5.5 | ||
- bug修复 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
QCloudCore/Classes/QCLOUDRestNet/CoreRequest/COSXMLGetSignatureTool.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
129
QCloudCore/Classes/QCLOUDRestNet/CoreRequest/COSXMLGetSignatureTool.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters