|
52 | 52 | import static org.elasticsearch.test.fixture.HttpHeaderParser.parseRangeHeader; |
53 | 53 | import static org.junit.Assert.assertEquals; |
54 | 54 | import static org.junit.Assert.assertNotNull; |
| 55 | +import static org.junit.Assert.assertNull; |
55 | 56 | import static org.w3c.dom.Node.ELEMENT_NODE; |
56 | 57 |
|
57 | 58 | /** |
@@ -187,39 +188,42 @@ public void handle(final HttpExchange exchange) throws IOException { |
187 | 188 | } |
188 | 189 |
|
189 | 190 | } else if (request.isCompleteMultipartUploadRequest()) { |
190 | | - final var upload = removeUpload(request.getQueryParamOnce("uploadId")); |
191 | | - if (upload == null) { |
192 | | - if (Randomness.get().nextBoolean()) { |
193 | | - exchange.sendResponseHeaders(RestStatus.NOT_FOUND.getStatus(), -1); |
| 191 | + final byte[] responseBody; |
| 192 | + synchronized (uploads) { |
| 193 | + final var upload = removeUpload(request.getQueryParamOnce("uploadId")); |
| 194 | + if (upload == null) { |
| 195 | + if (Randomness.get().nextBoolean()) { |
| 196 | + responseBody = null; |
| 197 | + } else { |
| 198 | + responseBody = """ |
| 199 | + <?xml version="1.0" encoding="UTF-8"?> |
| 200 | + <Error> |
| 201 | + <Code>NoSuchUpload</Code> |
| 202 | + <Message>No such upload</Message> |
| 203 | + <RequestId>test-request-id</RequestId> |
| 204 | + <HostId>test-host-id</HostId> |
| 205 | + </Error>""".getBytes(StandardCharsets.UTF_8); |
| 206 | + } |
194 | 207 | } else { |
195 | | - byte[] response = (""" |
196 | | - <?xml version="1.0" encoding="UTF-8"?> |
197 | | - <Error> |
198 | | - <Code>NoSuchUpload</Code> |
199 | | - <Message>No such upload</Message> |
200 | | - <RequestId>test-request-id</RequestId> |
201 | | - <HostId>test-host-id</HostId> |
202 | | - </Error>""").getBytes(StandardCharsets.UTF_8); |
203 | | - exchange.getResponseHeaders().add("Content-Type", "application/xml"); |
204 | | - exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length); |
205 | | - exchange.getResponseBody().write(response); |
| 208 | + final var blobContents = upload.complete(extractPartEtags(Streams.readFully(exchange.getRequestBody()))); |
| 209 | + blobs.put(request.path(), blobContents); |
| 210 | + responseBody = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 211 | + + "<CompleteMultipartUploadResult>\n" |
| 212 | + + "<Bucket>" |
| 213 | + + bucket |
| 214 | + + "</Bucket>\n" |
| 215 | + + "<Key>" |
| 216 | + + request.path() |
| 217 | + + "</Key>\n" |
| 218 | + + "</CompleteMultipartUploadResult>").getBytes(StandardCharsets.UTF_8); |
206 | 219 | } |
| 220 | + } |
| 221 | + if (responseBody == null) { |
| 222 | + exchange.sendResponseHeaders(RestStatus.NOT_FOUND.getStatus(), -1); |
207 | 223 | } else { |
208 | | - final var blobContents = upload.complete(extractPartEtags(Streams.readFully(exchange.getRequestBody()))); |
209 | | - blobs.put(request.path(), blobContents); |
210 | | - |
211 | | - byte[] response = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
212 | | - + "<CompleteMultipartUploadResult>\n" |
213 | | - + "<Bucket>" |
214 | | - + bucket |
215 | | - + "</Bucket>\n" |
216 | | - + "<Key>" |
217 | | - + request.path() |
218 | | - + "</Key>\n" |
219 | | - + "</CompleteMultipartUploadResult>").getBytes(StandardCharsets.UTF_8); |
220 | 224 | exchange.getResponseHeaders().add("Content-Type", "application/xml"); |
221 | | - exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length); |
222 | | - exchange.getResponseBody().write(response); |
| 225 | + exchange.sendResponseHeaders(RestStatus.OK.getStatus(), responseBody.length); |
| 226 | + exchange.getResponseBody().write(responseBody); |
223 | 227 | } |
224 | 228 | } else if (request.isAbortMultipartUploadRequest()) { |
225 | 229 | final var upload = removeUpload(request.getQueryParamOnce("uploadId")); |
@@ -524,7 +528,7 @@ private static HttpHeaderParser.Range parsePartRange(final HttpExchange exchange |
524 | 528 | MultipartUpload putUpload(String path) { |
525 | 529 | final var upload = new MultipartUpload(UUIDs.randomBase64UUID(), path); |
526 | 530 | synchronized (uploads) { |
527 | | - uploads.put(upload.getUploadId(), upload); |
| 531 | + assertNull("upload " + upload.getUploadId() + " should not exist", uploads.put(upload.getUploadId(), upload)); |
528 | 532 | return upload; |
529 | 533 | } |
530 | 534 | } |
|
0 commit comments