@@ -100,6 +100,60 @@ local function derive_signing_key(kSecret, date, region, service)
100
100
end
101
101
102
102
103
+ local function get_host (config )
104
+ local host = config .endpoint
105
+ do
106
+ local s , e = host :find (" ://" )
107
+ if s then
108
+ -- the "globalSSL" one from the region_config_data file
109
+ local scheme = host :sub (1 , s - 1 ):lower ()
110
+ host = host :sub (e + 1 , - 1 )
111
+ if config .tls == nil then
112
+ config .tls = scheme == " https"
113
+ end
114
+ end
115
+ end
116
+
117
+ local tls = config .tls
118
+ local port = config .port or (tls and 443 or 80 )
119
+
120
+ local host_header do -- If the "standard" port is not in use, the port should be added to the Host header
121
+ local with_port
122
+ if tls then
123
+ with_port = port ~= 443
124
+ else
125
+ with_port = port ~= 80
126
+ end
127
+ if with_port then
128
+ host_header = string.format (" %s:%d" , host , port )
129
+ else
130
+ host_header = host
131
+ end
132
+ end
133
+
134
+ return host_header
135
+ end
136
+
137
+
138
+ local function s3_patch (request , bucket )
139
+ if not bucket then
140
+ return
141
+ end
142
+
143
+ request .headers .Host = bucket .. " ." .. request .headers .Host
144
+
145
+ local path = request .path
146
+ if bucket and path then
147
+ path = path :sub (# bucket + 2 )
148
+ if path == " /" then
149
+ path = " "
150
+ end
151
+
152
+ request .path = path
153
+ end
154
+ end
155
+
156
+
103
157
-- config to contain:
104
158
-- config.endpoint: hostname to connect to
105
159
-- config.credentials: the Credentials class to use
122
176
-- tbl.timestamp: number defaults to 'ngx.time()''
123
177
-- tbl.global_endpoint: if true, then use "us-east-1" as signing region and different
124
178
-- hostname template: see https://github.com/aws/aws-sdk-js/blob/ae07e498e77000e55da70b20996dc8fd2f8b3051/lib/region_config_data.json
125
- local function prepare_awsv4_request (config , request_data )
179
+ local function prepare_awsv4_request (config , request_data , bucket )
180
+ request_data .headers = request_data .headers or {}
181
+ request_data .headers .Host = get_host (config )
182
+ s3_patch (request_data , bucket )
126
183
local region = config .signingRegion or config .region
127
184
local service = config .endpointPrefix or config .targetPrefix -- TODO: targetPrefix as fallback, correct???
128
185
local request_method = request_data .method -- TODO: should this get a fallback/default??
@@ -141,7 +198,7 @@ local function prepare_awsv4_request(config, request_data)
141
198
canonical_querystring = canonicalise_query_string (query )
142
199
end
143
200
144
- local req_headers = request_data .headers or {}
201
+ local req_headers = request_data .headers
145
202
local req_payload = request_data .body
146
203
147
204
-- get credentials
@@ -157,46 +214,18 @@ local function prepare_awsv4_request(config, request_data)
157
214
end
158
215
159
216
local tls = config .tls
160
- local host = config .endpoint
161
- do
162
- local s , e = host :find (" ://" )
163
- if s then
164
- -- the "globalSSL" one from the region_config_data file
165
- local scheme = host :sub (1 , s - 1 ):lower ()
166
- host = host :sub (e + 1 , - 1 )
167
- if config .tls == nil then
168
- config .tls = scheme == " https"
169
- end
170
- end
171
- end
172
217
173
- if tls == nil then
174
- tls = true
175
- end
176
218
local port = config .port or (tls and 443 or 80 )
177
219
local timestamp = ngx .time ()
178
220
local req_date = os.date (" !%Y%m%dT%H%M%SZ" , timestamp )
179
221
local date = os.date (" !%Y%m%d" , timestamp )
180
222
181
- local host_header do -- If the "standard" port is not in use, the port should be added to the Host header
182
- local with_port
183
- if tls then
184
- with_port = port ~= 443
185
- else
186
- with_port = port ~= 80
187
- end
188
- if with_port then
189
- host_header = string.format (" %s:%d" , host , port )
190
- else
191
- host_header = host
192
- end
193
- end
194
-
195
223
local headers = {
196
224
[" X-Amz-Date" ] = req_date ,
197
- [" Host" ] = host_header ,
225
+ [" Host" ] = request_data . Host ,
198
226
[" X-Amz-Security-Token" ] = session_token ,
199
227
}
228
+ request_data .Host = nil
200
229
201
230
local S3 = config .signatureVersion == " s3"
202
231
@@ -289,7 +318,7 @@ local function prepare_awsv4_request(config, request_data)
289
318
290
319
return {
291
320
-- url = url, -- "https://lambda.us-east-1.amazon.com:443/some/path?query1=val1"
292
- host = host , -- "lambda.us-east-1.amazon.com"
321
+ host = headers . Host , -- "lambda.us-east-1.amazon.com"
293
322
port = port , -- 443
294
323
tls = tls , -- true
295
324
path = path or canonicalURI , -- "/some/path"
0 commit comments