@@ -40,7 +40,45 @@ - (NSDictionary*)filteHeaders; {
4040}
4141@end
4242
43+ @implementation NSURL (QCloudExtension)
4344
45+ /* *
46+ 返回 COS 签名中用到的 path , 。如果没有path时,为 /
47+
48+ 例如
49+ 1. URL 为: http://test-123456.cos.ap-shanghai.myqcloud.com?delimiter=%2F&max-keys=1000&prefix=test%2F
50+
51+ path为 /
52+
53+ 2. URL为: http://test-123456.cos.ap-shanghai.myqcloud.com/test
54+ path 为 test
55+
56+ 3. URL为: http://test-123456.cos.ap-shanghai.myqcloud.com/test/
57+ path 为 test/
58+
59+
60+ @return COS签名中定义的 path
61+ */
62+
63+ - (NSString *)qcloud_path {
64+ NSString * path = self.path ;
65+ NSRange pathRange = [self .absoluteString rangeOfString: path];
66+ NSUInteger URLLength = self.absoluteString .length ;
67+ if ( pathRange.location == NSNotFound ) {
68+ return path;
69+ }
70+ NSUInteger pathLocation = pathRange.location + pathRange.length ;
71+ if (pathLocation >= URLLength) {
72+ return path;
73+ }
74+ if ( [self .absoluteString characterAtIndex: (pathLocation)] == ' /' ) {
75+ path = [path stringByAppendingString: @" /" ];
76+ return path;
77+ }
78+ return path;
79+ }
80+
81+ @end
4482
4583@implementation QCloudAuthentationV5Creator
4684
@@ -92,7 +130,7 @@ - (QCloudSignature*) signatureForData:(NSMutableURLRequest *)urlrequest
92130 };
93131
94132 AppendFormatString (urlrequest.HTTPMethod .lowercaseString );
95- NSString * path = urlrequest.URL .path ;
133+ NSString * path = urlrequest.URL .qcloud_path ;
96134 if (path.length == 0 ) {
97135 path = @" /" ;
98136 }
@@ -143,3 +181,4 @@ - (QCloudSignature*) signatureForData:(NSMutableURLRequest *)urlrequest
143181
144182
145183@end
184+
0 commit comments