forked from aws-amplify/aws-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAWSS3TransferUtility+HeaderHelper.m
152 lines (132 loc) · 7.35 KB
/
AWSS3TransferUtility+HeaderHelper.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// A copy of the License is located at
//
// http://aws.amazon.com/apache2.0
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
//
#import "AWSS3TransferUtility+HeaderHelper.h"
#import "AWSS3TransferUtilityTasks.h"
@interface AWSS3CreateMultipartUploadRequest()
+ (NSValueTransformer *)ACLJSONTransformer;
+ (NSValueTransformer *)storageClassJSONTransformer;
+ (NSValueTransformer *)serverSideEncryptionJSONTransformer;
+ (NSValueTransformer *)requestPayerJSONTransformer;
+ (NSValueTransformer *)expiresJSONTransformer;
@end
@interface AWSS3TransferUtilityExpression()
@property (strong, nonatomic) NSMutableDictionary<NSString *, NSString *> *internalRequestHeaders;
@property (strong, nonatomic) NSMutableDictionary<NSString *, NSString *> *internalRequestParameters;
- (void)assignRequestParameters:(AWSS3GetPreSignedURLRequest *)getPreSignedURLRequest;
- (void)assignRequestHeaders:(AWSS3GetPreSignedURLRequest *)getPreSignedURLRequest;
@end
@interface AWSS3TransferUtilityUploadExpression()
@property (copy, atomic) AWSS3TransferUtilityUploadCompletionHandlerBlock completionHandler;
@end
@interface AWSS3TransferUtilityMultiPartUploadExpression()
@property (strong, nonatomic) NSMutableDictionary<NSString *, NSString *> *internalRequestHeaders;
@property (strong, nonatomic) NSMutableDictionary<NSString *, NSString *> *internalRequestParameters;
- (void)assignRequestParameters:(AWSS3GetPreSignedURLRequest *)getPreSignedURLRequest;
@property (copy, atomic) AWSS3TransferUtilityMultiPartUploadCompletionHandlerBlock completionHandler;
@end
@implementation AWSS3TransferUtility (HeaderHelper)
-(void) propagateHeaderInformation: (AWSS3CreateMultipartUploadRequest *) uploadRequest
expression: (AWSS3TransferUtilityMultiPartUploadExpression *) expression {
//Propagate header info and add custom metadata
NSMutableDictionary<NSString *, NSString *> *metadata = [NSMutableDictionary new];
for (NSString *key in expression.requestHeaders) {
NSString *lKey = [key lowercaseString];
if ([lKey hasPrefix:@"x-amz-meta"]) {
[metadata setValue:expression.requestHeaders[key] forKey:[key stringByReplacingOccurrencesOfString:@"x-amz-meta-" withString:@""]];
}
else if ([lKey isEqualToString:@"x-amz-acl"]) {
NSValueTransformer *transformer = [AWSS3CreateMultipartUploadRequest ACLJSONTransformer];
uploadRequest.ACL = (AWSS3ObjectCannedACL)[[transformer transformedValue:expression.requestHeaders[key]] integerValue];
}
else if ([lKey isEqualToString:@"x-amz-grant-read" ]) {
uploadRequest.grantRead = expression.requestHeaders[key];
}
else if ([lKey isEqualToString:@"x-amz-grant-read-acp" ]) {
uploadRequest.grantReadACP = expression.requestHeaders[key];
}
else if ([lKey isEqualToString:@"x-amz-grant-read-acp" ]) {
uploadRequest.grantReadACP = expression.requestHeaders[key];
}
else if ([lKey isEqualToString:@"x-amz-grant-write-acp" ]) {
uploadRequest.grantWriteACP = expression.requestHeaders[key];
}
else if ([lKey isEqualToString:@"x-amz-grant-full-control" ]) {
uploadRequest.grantFullControl = expression.requestHeaders[key];
}
else if ([lKey isEqualToString:@"x-amz-server-side-encryption" ]) {
NSValueTransformer *transformer = [AWSS3CreateMultipartUploadRequest serverSideEncryptionJSONTransformer];
uploadRequest.serverSideEncryption = (AWSS3ServerSideEncryption)[[transformer transformedValue:expression.requestHeaders[key]] integerValue];
}
else if ([lKey isEqualToString:@"x-amz-server-side-encryption-aws-kms-key-id" ]) {
uploadRequest.SSEKMSKeyId = expression.requestHeaders[key];
}
else if ([lKey isEqualToString:@"x-amz-server-side-encryption-customer-algorithm" ]) {
uploadRequest.SSECustomerAlgorithm = expression.requestHeaders[key];
}
else if ([lKey isEqualToString:@"x-amz-server-side-encryption-customer-key" ]) {
uploadRequest.SSECustomerKey = expression.requestHeaders[key];
}
else if ([lKey isEqualToString:@"x-amz-server-side-encryption-customer-key-MD5" ]) {
uploadRequest.SSECustomerKeyMD5 = expression.requestHeaders[key];
}
else if ([lKey isEqualToString:@"content-encoding" ]) {
uploadRequest.contentEncoding = expression.requestHeaders[key];
}
else if ([lKey isEqualToString:@"content-type" ]) {
uploadRequest.contentType = expression.requestHeaders[key];
}
else if([lKey isEqualToString:@"cache-control"]) {
uploadRequest.cacheControl = expression.requestHeaders[key];
}
else if ([lKey isEqualToString:@"x-amz-request-payer" ]) {
NSValueTransformer *transformer = [AWSS3CreateMultipartUploadRequest requestPayerJSONTransformer];
uploadRequest.requestPayer = (AWSS3RequestPayer)[[transformer transformedValue:expression.requestHeaders[key]] integerValue];
}
else if ([lKey isEqualToString:@"expires" ]) {
NSValueTransformer *transformer = [AWSS3CreateMultipartUploadRequest expiresJSONTransformer];
uploadRequest.expires = [transformer transformedValue:expression.requestHeaders[key]];
}
else if ([lKey isEqualToString:@"x-amz-storage-class" ]) {
NSValueTransformer *transformer = [AWSS3CreateMultipartUploadRequest storageClassJSONTransformer];
uploadRequest.storageClass = (AWSS3StorageClass)[[transformer transformedValue:expression.requestHeaders[key]] integerValue];
}
else if ([lKey isEqualToString:@"x-amz-website-redirect-location" ]) {
uploadRequest.websiteRedirectLocation = expression.requestHeaders[key];
}
else if ([lKey isEqualToString:@"x-amz-tagging" ]) {
uploadRequest.tagging = expression.requestHeaders[key];
}
}
uploadRequest.metadata = metadata;
}
-(void) filterAndAssignHeaders:(NSDictionary<NSString *, NSString *> *) requestHeaders
getPresignedURLRequest:(AWSS3GetPreSignedURLRequest *) getPresignedURLRequest
URLRequest: (NSMutableURLRequest *) URLRequest {
NSSet *disallowedHeaders = [[NSSet alloc] initWithArray:
@[@"x-amz-acl", @"x-amz-tagging", @"x-amz-storage-class", @"x-amz-server-side-encryption"]];
for (NSString *key in requestHeaders) {
//Do not include custom metadata or custom grants
NSString *lKey = [key lowercaseString];
if ([ lKey hasPrefix:@"x-amz-meta"] || [lKey hasPrefix:@"x-amz-grant"]) {
continue;
}
if ([disallowedHeaders containsObject:lKey]) {
continue;
}
[getPresignedURLRequest setValue:requestHeaders[key] forRequestHeader:key];
[URLRequest setValue:requestHeaders[key] forHTTPHeaderField:key];
}
}
@end