Bez popisu

OSSModel.m 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. //
  2. // OSSModel.m
  3. // oss_ios_sdk
  4. //
  5. // Created by zhouzhuo on 8/16/15.
  6. // Copyright (c) 2015 aliyun.com. All rights reserved.
  7. //
  8. #import "OSSDefine.h"
  9. #import "OSSModel.h"
  10. #import "OSSBolts.h"
  11. #import "OSSUtil.h"
  12. #import "OSSNetworking.h"
  13. #import "OSSLog.h"
  14. #import "OSSXMLDictionary.h"
  15. #import "NSMutableData+OSS_CRC.h"
  16. #if TARGET_OS_IOS
  17. #import <UIKit/UIDevice.h>
  18. #endif
  19. #import "OSSAllRequestNeededMessage.h"
  20. @implementation NSDictionary (OSS)
  21. - (NSString *)base64JsonString {
  22. NSError * error;
  23. NSData * jsonData = [NSJSONSerialization dataWithJSONObject:self
  24. options:0
  25. error:&error];
  26. if (!jsonData) {
  27. return @"e30="; // base64("{}");
  28. } else {
  29. NSString * jsonStr = [[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding] stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
  30. NSLog(@"callback json - %@", jsonStr);
  31. return [[jsonStr dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
  32. }
  33. }
  34. @end
  35. @implementation NSDate (OSS)
  36. NSString * const serverReturnDateFormat = @"EEE, dd MMM yyyy HH:mm:ss z";
  37. static NSTimeInterval _clockSkew = 0.0;
  38. + (void)oss_setClockSkew:(NSTimeInterval)clockSkew {
  39. @synchronized(self) {
  40. _clockSkew = clockSkew;
  41. }
  42. }
  43. + (NSDate *)oss_clockSkewFixedDate {
  44. NSTimeInterval skew = 0.0;
  45. @synchronized(self) {
  46. skew = _clockSkew;
  47. }
  48. return [[NSDate date] dateByAddingTimeInterval:(-1 * skew)];
  49. }
  50. + (NSDate *)oss_dateFromString:(NSString *)string {
  51. NSDateFormatter *dateFormatter = [NSDateFormatter new];
  52. dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
  53. dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
  54. dateFormatter.dateFormat = serverReturnDateFormat;
  55. return [dateFormatter dateFromString:string];
  56. }
  57. - (NSString *)oss_asStringValue {
  58. NSDateFormatter *dateFormatter = [NSDateFormatter new];
  59. dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
  60. dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
  61. dateFormatter.dateFormat = serverReturnDateFormat;
  62. return [dateFormatter stringFromDate:self];
  63. }
  64. @end
  65. @implementation OSSSyncMutableDictionary
  66. - (instancetype)init {
  67. if (self = [super init]) {
  68. _dictionary = [NSMutableDictionary new];
  69. _dispatchQueue = dispatch_queue_create("com.aliyun.aliyunsycmutabledictionary", DISPATCH_QUEUE_SERIAL);
  70. }
  71. return self;
  72. }
  73. - (NSArray *)allKeys {
  74. __block NSArray *allKeys = nil;
  75. dispatch_sync(self.dispatchQueue, ^{
  76. allKeys = [self.dictionary allKeys];
  77. });
  78. return allKeys;
  79. }
  80. - (id)objectForKey:(id)aKey {
  81. __block id returnObject = nil;
  82. dispatch_sync(self.dispatchQueue, ^{
  83. returnObject = [self.dictionary objectForKey:aKey];
  84. });
  85. return returnObject;
  86. }
  87. - (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey {
  88. dispatch_sync(self.dispatchQueue, ^{
  89. [self.dictionary setObject:anObject forKey:aKey];
  90. });
  91. }
  92. - (void)removeObjectForKey:(id)aKey {
  93. dispatch_sync(self.dispatchQueue, ^{
  94. [self.dictionary removeObjectForKey:aKey];
  95. });
  96. }
  97. @end
  98. @implementation OSSFederationToken
  99. - (NSString *)description
  100. {
  101. return [NSString stringWithFormat:@"OSSFederationToken<%p>:{AccessKeyId: %@\nAccessKeySecret: %@\nSecurityToken: %@\nExpiration: %@}", self, _tAccessKey, _tSecretKey, _tToken, _expirationTimeInGMTFormat];
  102. }
  103. @end
  104. @implementation OSSPlainTextAKSKPairCredentialProvider
  105. - (instancetype)initWithPlainTextAccessKey:(nonnull NSString *)accessKey secretKey:(nonnull NSString *)secretKey {
  106. if (self = [super init]) {
  107. self.accessKey = [accessKey oss_trim];
  108. self.secretKey = [secretKey oss_trim];
  109. }
  110. return self;
  111. }
  112. - (nullable NSString *)sign:(NSString *)content error:(NSError **)error {
  113. if (![self.accessKey oss_isNotEmpty] || ![self.secretKey oss_isNotEmpty])
  114. {
  115. if (error != nil)
  116. {
  117. *error = [NSError errorWithDomain:OSSClientErrorDomain
  118. code:OSSClientErrorCodeSignFailed
  119. userInfo:@{OSSErrorMessageTOKEN: @"accessKey or secretKey can't be null"}];
  120. }
  121. return nil;
  122. }
  123. NSString * sign = [OSSUtil calBase64Sha1WithData:content withSecret:self.secretKey];
  124. return [NSString stringWithFormat:@"OSS %@:%@", self.accessKey, sign];
  125. }
  126. @end
  127. @implementation OSSCustomSignerCredentialProvider
  128. - (instancetype)initWithImplementedSigner:(OSSCustomSignContentBlock)signContent
  129. {
  130. NSParameterAssert(signContent);
  131. if (self = [super init])
  132. {
  133. _signContent = signContent;
  134. }
  135. return self;
  136. }
  137. - (NSString *)sign:(NSString *)content error:(NSError **)error
  138. {
  139. NSString * signature = @"";
  140. @synchronized(self) {
  141. signature = self.signContent(content, error);
  142. }
  143. if (*error) {
  144. *error = [NSError errorWithDomain:OSSClientErrorDomain
  145. code:OSSClientErrorCodeSignFailed
  146. userInfo:[[NSDictionary alloc] initWithDictionary:[*error userInfo]]];
  147. return nil;
  148. }
  149. return signature;
  150. }
  151. @end
  152. @implementation OSSFederationCredentialProvider
  153. - (instancetype)initWithFederationTokenGetter:(OSSGetFederationTokenBlock)federationTokenGetter {
  154. if (self = [super init]) {
  155. self.federationTokenGetter = federationTokenGetter;
  156. }
  157. return self;
  158. }
  159. - (nullable OSSFederationToken *)getToken:(NSError **)error {
  160. OSSFederationToken * validToken = nil;
  161. @synchronized(self) {
  162. if (self.cachedToken == nil) {
  163. self.cachedToken = self.federationTokenGetter();
  164. } else {
  165. if (self.cachedToken.expirationTimeInGMTFormat) {
  166. NSDateFormatter * fm = [NSDateFormatter new];
  167. [fm setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
  168. self.cachedToken.expirationTimeInMilliSecond = [[fm dateFromString:self.cachedToken.expirationTimeInGMTFormat] timeIntervalSince1970] * 1000;
  169. self.cachedToken.expirationTimeInGMTFormat = nil;
  170. OSSLogVerbose(@"Transform GMT date to expirationTimeInMilliSecond: %lld", self.cachedToken.expirationTimeInMilliSecond);
  171. }
  172. NSDate * expirationDate = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)(self.cachedToken.expirationTimeInMilliSecond / 1000)];
  173. NSTimeInterval interval = [expirationDate timeIntervalSinceDate:[NSDate oss_clockSkewFixedDate]];
  174. /* if this token will be expired after less than 2min, we abort it in case of when request arrived oss server,
  175. it's expired already. */
  176. if (interval < 5 * 60) {
  177. OSSLogDebug(@"get federation token, but after %lf second it would be expired", interval);
  178. self.cachedToken = self.federationTokenGetter();
  179. }
  180. }
  181. validToken = self.cachedToken;
  182. }
  183. if (!validToken)
  184. {
  185. if (error != nil)
  186. {
  187. *error = [NSError errorWithDomain:OSSClientErrorDomain
  188. code:OSSClientErrorCodeSignFailed
  189. userInfo:@{OSSErrorMessageTOKEN: @"Can't get a federation token"}];
  190. }
  191. return nil;
  192. }
  193. return validToken;
  194. }
  195. @end
  196. @implementation OSSStsTokenCredentialProvider
  197. - (OSSFederationToken *)getToken {
  198. OSSFederationToken * token = [OSSFederationToken new];
  199. token.tAccessKey = self.accessKeyId;
  200. token.tSecretKey = self.secretKeyId;
  201. token.tToken = self.securityToken;
  202. token.expirationTimeInMilliSecond = NSIntegerMax;
  203. return token;
  204. }
  205. - (instancetype)initWithAccessKeyId:(NSString *)accessKeyId secretKeyId:(NSString *)secretKeyId securityToken:(NSString *)securityToken {
  206. if (self = [super init]) {
  207. self.accessKeyId = [accessKeyId oss_trim];
  208. self.secretKeyId = [secretKeyId oss_trim];
  209. self.securityToken = [securityToken oss_trim];
  210. }
  211. return self;
  212. }
  213. - (NSString *)sign:(NSString *)content error:(NSError **)error {
  214. NSString * sign = [OSSUtil calBase64Sha1WithData:content withSecret:self.secretKeyId];
  215. return [NSString stringWithFormat:@"OSS %@:%@", self.accessKeyId, sign];
  216. }
  217. @end
  218. @implementation OSSAuthCredentialProvider
  219. - (instancetype)initWithAuthServerUrl:(NSString *)authServerUrl
  220. {
  221. return [self initWithAuthServerUrl:authServerUrl responseDecoder:nil];
  222. }
  223. - (instancetype)initWithAuthServerUrl:(NSString *)authServerUrl responseDecoder:(nullable OSSResponseDecoderBlock)decoder
  224. {
  225. self = [super initWithFederationTokenGetter:^OSSFederationToken * {
  226. NSURL * url = [NSURL URLWithString:self.authServerUrl];
  227. NSURLRequest * request = [NSURLRequest requestWithURL:url];
  228. OSSTaskCompletionSource * tcs = [OSSTaskCompletionSource taskCompletionSource];
  229. NSURLSession * session = [NSURLSession sharedSession];
  230. NSURLSessionTask * sessionTask = [session dataTaskWithRequest:request
  231. completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  232. if (error) {
  233. [tcs setError:error];
  234. return;
  235. }
  236. [tcs setResult:data];
  237. }];
  238. [sessionTask resume];
  239. [tcs.task waitUntilFinished];
  240. if (tcs.task.error) {
  241. return nil;
  242. } else {
  243. NSData* data = tcs.task.result;
  244. if(decoder){
  245. data = decoder(data);
  246. }
  247. NSDictionary * object = [NSJSONSerialization JSONObjectWithData:data
  248. options:kNilOptions
  249. error:nil];
  250. int statusCode = [[object objectForKey:@"StatusCode"] intValue];
  251. if (statusCode == 200) {
  252. OSSFederationToken * token = [OSSFederationToken new];
  253. // All the entries below are mandatory.
  254. token.tAccessKey = [object objectForKey:@"AccessKeyId"];
  255. token.tSecretKey = [object objectForKey:@"AccessKeySecret"];
  256. token.tToken = [object objectForKey:@"SecurityToken"];
  257. token.expirationTimeInGMTFormat = [object objectForKey:@"Expiration"];
  258. OSSLogDebug(@"token: %@ %@ %@ %@", token.tAccessKey, token.tSecretKey, token.tToken, [object objectForKey:@"Expiration"]);
  259. return token;
  260. }else{
  261. return nil;
  262. }
  263. }
  264. }];
  265. if(self){
  266. self.authServerUrl = authServerUrl;
  267. }
  268. return self;
  269. }
  270. @end
  271. NSString * const BACKGROUND_SESSION_IDENTIFIER = @"com.aliyun.oss.backgroundsession";
  272. @implementation OSSClientConfiguration
  273. - (instancetype)init {
  274. if (self = [super init]) {
  275. self.maxRetryCount = OSSDefaultRetryCount;
  276. self.maxConcurrentRequestCount = OSSDefaultMaxConcurrentNum;
  277. self.enableBackgroundTransmitService = NO;
  278. self.isHttpdnsEnable = YES;
  279. self.backgroundSesseionIdentifier = BACKGROUND_SESSION_IDENTIFIER;
  280. self.timeoutIntervalForRequest = OSSDefaultTimeoutForRequestInSecond;
  281. self.timeoutIntervalForResource = OSSDefaultTimeoutForResourceInSecond;
  282. }
  283. return self;
  284. }
  285. - (void)setCnameExcludeList:(NSArray *)cnameExcludeList {
  286. NSMutableArray * array = [NSMutableArray new];
  287. [cnameExcludeList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  288. NSString * host = [(NSString *)obj lowercaseString];
  289. if ([host containsString:@"://"]) {
  290. NSString * trimHost = [host substringFromIndex:[host rangeOfString:@"://"].location + 3];
  291. [array addObject:trimHost];
  292. } else {
  293. [array addObject:host];
  294. }
  295. }];
  296. _cnameExcludeList = array.copy;
  297. }
  298. @end
  299. @implementation OSSSignerInterceptor
  300. - (instancetype)initWithCredentialProvider:(id<OSSCredentialProvider>)credentialProvider {
  301. if (self = [super init]) {
  302. self.credentialProvider = credentialProvider;
  303. }
  304. return self;
  305. }
  306. - (OSSTask *)interceptRequestMessage:(OSSAllRequestNeededMessage *)requestMessage {
  307. OSSLogVerbose(@"signing intercepting - ");
  308. NSError * error = nil;
  309. /****************************************************************
  310. * define a constant array to contain all specified subresource */
  311. static NSArray * OSSSubResourceARRAY = nil;
  312. static dispatch_once_t onceToken;
  313. dispatch_once(&onceToken, ^{
  314. OSSSubResourceARRAY = @[@"acl", @"uploadId", @"partNumber", @"uploads", @"logging", @"website", @"location",
  315. @"lifecycle", @"referer", @"cors", @"delete", @"append", @"position", @"security-token", @"x-oss-process", @"sequential"];
  316. });
  317. /****************************************************************/
  318. /* initial each part of content to sign */
  319. NSString * method = requestMessage.httpMethod;
  320. NSString * contentType = @"";
  321. NSString * contentMd5 = @"";
  322. NSString * date = requestMessage.date;
  323. NSString * xossHeader = @"";
  324. NSString * resource = @"";
  325. OSSFederationToken * federationToken = nil;
  326. if (requestMessage.contentType) {
  327. contentType = requestMessage.contentType;
  328. }
  329. if (requestMessage.contentMd5) {
  330. contentMd5 = requestMessage.contentMd5;
  331. }
  332. /* if credential provider is a federation token provider, it need to specially handle */
  333. if ([self.credentialProvider isKindOfClass:[OSSFederationCredentialProvider class]]) {
  334. federationToken = [(OSSFederationCredentialProvider *)self.credentialProvider getToken:&error];
  335. if (error) {
  336. return [OSSTask taskWithError:error];
  337. }
  338. [requestMessage.headerParams setObject:federationToken.tToken forKey:@"x-oss-security-token"];
  339. } else if ([self.credentialProvider isKindOfClass:[OSSStsTokenCredentialProvider class]]) {
  340. federationToken = [(OSSStsTokenCredentialProvider *)self.credentialProvider getToken];
  341. [requestMessage.headerParams setObject:federationToken.tToken forKey:@"x-oss-security-token"];
  342. }
  343. if (requestMessage.contentSHA1) {
  344. [requestMessage.headerParams setObject:requestMessage.contentSHA1 forKey:OSSHttpHeaderHashSHA1];
  345. }
  346. /* construct CanonicalizedOSSHeaders */
  347. if (requestMessage.headerParams) {
  348. NSMutableArray * params = [[NSMutableArray alloc] init];
  349. NSArray * sortedKey = [[requestMessage.headerParams allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  350. return [obj1 compare:obj2];
  351. }];
  352. for (NSString * key in sortedKey) {
  353. if ([key hasPrefix:@"x-oss-"]) {
  354. [params addObject:[NSString stringWithFormat:@"%@:%@", key, [requestMessage.headerParams objectForKey:key]]];
  355. }
  356. }
  357. if ([params count]) {
  358. xossHeader = [NSString stringWithFormat:@"%@\n", [params componentsJoinedByString:@"\n"]];
  359. }
  360. }
  361. /* construct CanonicalizedResource */
  362. resource = @"/";
  363. if (requestMessage.bucketName) {
  364. resource = [NSString stringWithFormat:@"/%@/", requestMessage.bucketName];
  365. }
  366. if (requestMessage.objectKey) {
  367. resource = [resource oss_stringByAppendingPathComponentForURL:requestMessage.objectKey];
  368. }
  369. if (requestMessage.querys) {
  370. NSMutableArray * querys = [[NSMutableArray alloc] init];
  371. NSArray * sortedKey = [[requestMessage.querys allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  372. return [obj1 compare:obj2];
  373. }];
  374. for (NSString * key in sortedKey) {
  375. NSString * value = [requestMessage.querys objectForKey:key];
  376. if (![OSSSubResourceARRAY containsObject:key]) { // notice it's based on content compare
  377. continue;
  378. }
  379. if ([value isEqualToString:@""]) {
  380. [querys addObject:[NSString stringWithFormat:@"%@", key]];
  381. } else {
  382. [querys addObject:[NSString stringWithFormat:@"%@=%@", key, value]];
  383. }
  384. }
  385. if ([querys count]) {
  386. resource = [resource stringByAppendingString:[NSString stringWithFormat:@"?%@",[querys componentsJoinedByString:@"&"]]];
  387. }
  388. }
  389. /* now, join every part of content and sign */
  390. NSString * stringToSign = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@%@", method, contentMd5, contentType, date, xossHeader, resource];
  391. OSSLogDebug(@"string to sign: %@", stringToSign);
  392. if ([self.credentialProvider isKindOfClass:[OSSFederationCredentialProvider class]]
  393. || [self.credentialProvider isKindOfClass:[OSSStsTokenCredentialProvider class]])
  394. {
  395. NSString * signature = [OSSUtil sign:stringToSign withToken:federationToken];
  396. [requestMessage.headerParams setObject:signature forKey:@"Authorization"];
  397. }else if ([self.credentialProvider isKindOfClass:[OSSCustomSignerCredentialProvider class]])
  398. {
  399. OSSCustomSignerCredentialProvider *provider = (OSSCustomSignerCredentialProvider *)self.credentialProvider;
  400. NSError *customSignError;
  401. NSString * signature = [provider sign:stringToSign error:&customSignError];
  402. if (customSignError) {
  403. OSSLogError(@"OSSCustomSignerError: %@",customSignError);
  404. }
  405. [requestMessage.headerParams setObject:signature forKey:@"Authorization"];
  406. }else
  407. {
  408. NSString * signature = [self.credentialProvider sign:stringToSign error:&error];
  409. if (error) {
  410. return [OSSTask taskWithError:error];
  411. }
  412. [requestMessage.headerParams setObject:signature forKey:@"Authorization"];
  413. }
  414. return [OSSTask taskWithResult:nil];
  415. }
  416. @end
  417. @implementation OSSUASettingInterceptor
  418. - (instancetype)initWithClientConfiguration:(OSSClientConfiguration *)clientConfiguration{
  419. if (self = [super init]) {
  420. self.clientConfiguration = clientConfiguration;
  421. }
  422. return self;
  423. }
  424. - (OSSTask *)interceptRequestMessage:(OSSAllRequestNeededMessage *)request {
  425. NSString * userAgent = [self getUserAgent:self.clientConfiguration.userAgentMark];
  426. [request.headerParams setObject:userAgent forKey:@"User-Agent"];
  427. return [OSSTask taskWithResult:nil];
  428. }
  429. - (NSString *)getUserAgent:(NSString *)customUserAgent {
  430. static NSString * userAgent = nil;
  431. static dispatch_once_t once;
  432. NSString * tempUserAgent = nil;
  433. dispatch_once(&once, ^{
  434. NSString *localeIdentifier = [[NSLocale currentLocale] localeIdentifier];
  435. #if TARGET_OS_IOS
  436. NSString *systemName = [[[UIDevice currentDevice] systemName] stringByReplacingOccurrencesOfString:@" " withString:@"-"];
  437. NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
  438. userAgent = [NSString stringWithFormat:@"%@/%@(/%@/%@/%@)", OSSUAPrefix, OSSSDKVersion, systemName, systemVersion, localeIdentifier];
  439. #elif TARGET_OS_OSX
  440. userAgent = [NSString stringWithFormat:@"%@/%@(/%@/%@/%@)", OSSUAPrefix, OSSSDKVersion, @"OSX", [NSProcessInfo processInfo].operatingSystemVersionString, localeIdentifier];
  441. #endif
  442. });
  443. if(customUserAgent){
  444. if(userAgent){
  445. tempUserAgent = [[userAgent stringByAppendingString:@"/"] stringByAppendingString:customUserAgent];
  446. }else{
  447. tempUserAgent = customUserAgent;
  448. }
  449. }else{
  450. tempUserAgent = userAgent;
  451. }
  452. return tempUserAgent;
  453. }
  454. @end
  455. @implementation OSSTimeSkewedFixingInterceptor
  456. - (OSSTask *)interceptRequestMessage:(OSSAllRequestNeededMessage *)request {
  457. request.date = [[NSDate oss_clockSkewFixedDate] oss_asStringValue];
  458. return [OSSTask taskWithResult:nil];
  459. }
  460. @end
  461. @implementation OSSRange
  462. - (instancetype)initWithStart:(int64_t)start withEnd:(int64_t)end {
  463. if (self = [super init]) {
  464. self.startPosition = start;
  465. self.endPosition = end;
  466. }
  467. return self;
  468. }
  469. - (NSString *)toHeaderString {
  470. NSString * rangeString = nil;
  471. if (self.startPosition < 0 && self.endPosition < 0) {
  472. rangeString = [NSString stringWithFormat:@"bytes=%lld-%lld", self.startPosition, self.endPosition];
  473. } else if (self.startPosition < 0) {
  474. rangeString = [NSString stringWithFormat:@"bytes=-%lld", self.endPosition];
  475. } else if (self.endPosition < 0) {
  476. rangeString = [NSString stringWithFormat:@"bytes=%lld-", self.startPosition];
  477. } else {
  478. rangeString = [NSString stringWithFormat:@"bytes=%lld-%lld", self.startPosition, self.endPosition];
  479. }
  480. return rangeString;
  481. }
  482. - (NSString *)description {
  483. return [NSString stringWithFormat:@"Range: %@", [self toHeaderString]];
  484. }
  485. @end
  486. #pragma mark request and result objects
  487. @implementation OSSGetServiceRequest
  488. - (NSDictionary *)getQueryDict {
  489. NSMutableDictionary * querys = [NSMutableDictionary new];
  490. if (self.prefix) {
  491. [querys setObject:self.prefix forKey:@"prefix"];
  492. }
  493. if (self.marker) {
  494. [querys setObject:self.marker forKey:@"marker"];
  495. }
  496. if (self.maxKeys) {
  497. [querys setObject:[@(self.maxKeys) stringValue] forKey:@"max-keys"];
  498. }
  499. return querys;
  500. }
  501. @end
  502. @implementation OSSGetServiceResult
  503. @end
  504. @implementation OSSCreateBucketRequest
  505. @end
  506. @implementation OSSCreateBucketResult
  507. @end
  508. @implementation OSSDeleteBucketRequest
  509. @end
  510. @implementation OSSDeleteBucketResult
  511. @end
  512. @implementation OSSGetBucketRequest
  513. - (NSDictionary *)getQueryDict {
  514. NSMutableDictionary * querys = [NSMutableDictionary new];
  515. if (self.delimiter) {
  516. [querys setObject:self.delimiter forKey:@"delimiter"];
  517. }
  518. if (self.prefix) {
  519. [querys setObject:self.prefix forKey:@"prefix"];
  520. }
  521. if (self.marker) {
  522. [querys setObject:self.marker forKey:@"marker"];
  523. }
  524. if (self.maxKeys) {
  525. [querys setObject:[@(self.maxKeys) stringValue] forKey:@"max-keys"];
  526. }
  527. return querys;
  528. }
  529. @end
  530. @implementation OSSListMultipartUploadsRequest
  531. - (NSDictionary *)getQueryDict {
  532. NSMutableDictionary * querys = [NSMutableDictionary new];
  533. if (self.delimiter) {
  534. [querys setObject:self.delimiter forKey:@"delimiter"];
  535. }
  536. if (self.prefix) {
  537. [querys setObject:self.prefix forKey:@"prefix"];
  538. }
  539. if (self.keyMarker) {
  540. [querys setObject:self.keyMarker forKey:@"key-marker"];
  541. }
  542. if (self.maxUploads) {
  543. [querys setObject:[@(self.maxUploads) stringValue] forKey:@"max-uploads"];
  544. }
  545. if (self.uploadIdMarker) {
  546. [querys setObject:self.uploadIdMarker forKey:@"upload-id-marker"];
  547. }
  548. if (self.encodingType) {
  549. [querys setObject:self.encodingType forKey:@"encoding-type"];
  550. }
  551. return querys;
  552. }
  553. @end
  554. @implementation OSSListMultipartUploadsResult
  555. @end
  556. @implementation OSSGetBucketResult
  557. @end
  558. @implementation OSSGetBucketACLRequest
  559. @end
  560. @implementation OSSGetBucketACLResult
  561. @end
  562. @implementation OSSHeadObjectRequest
  563. @end
  564. @implementation OSSHeadObjectResult
  565. @end
  566. @implementation OSSGetObjectRequest
  567. @end
  568. @implementation OSSGetObjectResult
  569. @end
  570. @implementation OSSPutObjectACLRequest
  571. @end
  572. @implementation OSSPutObjectACLResult
  573. @end
  574. @implementation OSSPutObjectRequest
  575. - (instancetype)init {
  576. if (self = [super init]) {
  577. self.objectMeta = [NSDictionary new];
  578. }
  579. return self;
  580. }
  581. @end
  582. @implementation OSSPutObjectResult
  583. @end
  584. @implementation OSSAppendObjectRequest
  585. - (instancetype)init {
  586. if (self = [super init]) {
  587. self.objectMeta = [NSDictionary new];
  588. }
  589. return self;
  590. }
  591. @end
  592. @implementation OSSAppendObjectResult
  593. @end
  594. @implementation OSSDeleteObjectRequest
  595. @end
  596. @implementation OSSDeleteObjectResult
  597. @end
  598. @implementation OSSCopyObjectRequest
  599. - (instancetype)init {
  600. if (self = [super init]) {
  601. self.objectMeta = [NSDictionary new];
  602. }
  603. return self;
  604. }
  605. @end
  606. @implementation OSSCopyObjectResult
  607. @end
  608. @implementation OSSInitMultipartUploadRequest
  609. - (instancetype)init {
  610. if (self = [super init]) {
  611. self.objectMeta = [NSDictionary new];
  612. }
  613. return self;
  614. }
  615. @end
  616. @implementation OSSInitMultipartUploadResult
  617. @end
  618. @implementation OSSUploadPartRequest
  619. @end
  620. @implementation OSSUploadPartResult
  621. @end
  622. @implementation OSSPartInfo
  623. + (instancetype)partInfoWithPartNum:(int32_t)partNum
  624. eTag:(NSString *)eTag
  625. size:(int64_t)size {
  626. return [self partInfoWithPartNum:partNum
  627. eTag:eTag
  628. size:size
  629. crc64:0];
  630. }
  631. + (instancetype)partInfoWithPartNum:(int32_t)partNum eTag:(NSString *)eTag size:(int64_t)size crc64:(uint64_t)crc64
  632. {
  633. OSSPartInfo *parInfo = [OSSPartInfo new];
  634. parInfo.partNum = partNum;
  635. parInfo.eTag = eTag;
  636. parInfo.size = size;
  637. parInfo.crc64 = crc64;
  638. return parInfo;
  639. }
  640. - (nonnull NSDictionary *)entityToDictionary
  641. {
  642. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  643. [dict setValue:@(_partNum) forKey:@"partNum"];
  644. if (_eTag)
  645. {
  646. [dict setValue:_eTag forKey:@"eTag"];
  647. }
  648. [dict setValue:@(_size) forKey:@"size"];
  649. [dict setValue:@(_crc64) forKey:@"crc64"];
  650. return [dict copy];
  651. }
  652. - (NSString *)description
  653. {
  654. return [NSString stringWithFormat:@"OSSPartInfo<%p>:{partNum: %d,eTag: %@,partSize: %lld,crc64: %llu}",self,self.partNum,self.eTag,self.size,self.crc64];
  655. }
  656. #pragma marks - Protocol Methods
  657. - (id)copyWithZone:(nullable NSZone *)zone
  658. {
  659. OSSPartInfo *instance = [[[self class] allocWithZone:zone] init];
  660. instance.partNum = self.partNum;
  661. instance.eTag = self.eTag;
  662. instance.size = self.size;
  663. instance.crc64 = self.crc64;
  664. return instance;
  665. }
  666. @end
  667. @implementation OSSCompleteMultipartUploadRequest
  668. @end
  669. @implementation OSSCompleteMultipartUploadResult
  670. @end
  671. @implementation OSSAbortMultipartUploadRequest
  672. @end
  673. @implementation OSSAbortMultipartUploadResult
  674. @end
  675. @implementation OSSListPartsRequest
  676. @end
  677. @implementation OSSListPartsResult
  678. @end
  679. @implementation OSSMultipartUploadRequest
  680. - (instancetype)init {
  681. if (self = [super init]) {
  682. self.partSize = 256 * 1024;
  683. }
  684. return self;
  685. }
  686. - (void)cancel {
  687. [super cancel];
  688. }
  689. @end
  690. @implementation OSSResumableUploadRequest
  691. - (instancetype)init {
  692. if (self = [super init]) {
  693. self.deleteUploadIdOnCancelling = YES;
  694. self.partSize = 256 * 1024;
  695. }
  696. return self;
  697. }
  698. - (void)cancel {
  699. [super cancel];
  700. if(_runningChildrenRequest){
  701. [_runningChildrenRequest cancel];
  702. }
  703. }
  704. @end
  705. @implementation OSSResumableUploadResult
  706. @end
  707. @implementation OSSCallBackRequest
  708. @end
  709. @implementation OSSCallBackResult
  710. @end
  711. @implementation OSSImagePersistRequest
  712. @end
  713. @implementation OSSImagePersistResult
  714. @end