@@ -242,6 +242,10 @@ protected long getRetryDelayInMillis() {
242242        };
243243    }
244244
245+     private  static  S3HttpHandler .S3Request  parseRequest (HttpExchange  exchange ) {
246+         return  new  S3HttpHandler ("bucket" ).parseRequest (exchange );
247+     }
248+ 
245249    public  void  testWriteBlobWithRetries () throws  Exception  {
246250        final  int  maxRetries  = randomInt (5 );
247251        final  CountDown  countDown  = new  CountDown (maxRetries  + 1 );
@@ -250,10 +254,8 @@ public void testWriteBlobWithRetries() throws Exception {
250254
251255        final  byte [] bytes  = randomBlobContent ();
252256        httpServer .createContext (downloadStorageEndpoint (blobContainer , "write_blob_max_retries" ), exchange  -> {
253-             final  S3HttpHandler .RequestComponents  requestComponents  = S3HttpHandler .parseRequestComponents (
254-                 S3HttpHandler .getRawRequestString (exchange )
255-             );
256-             if  ("PUT" .equals (requestComponents .method ()) && requestComponents .query ().isEmpty ()) {
257+             final  S3HttpHandler .S3Request  s3Request  = parseRequest (exchange );
258+             if  (s3Request .isPutObjectRequest ()) {
257259                if  (countDown .countDown ()) {
258260                    final  BytesReference  body  = Streams .readFully (exchange .getRequestBody ());
259261                    if  (Objects .deepEquals (bytes , BytesReference .toBytes (body ))) {
@@ -342,12 +344,10 @@ public void testWriteLargeBlob() throws Exception {
342344        final  CountDown  countDownComplete  = new  CountDown (nbErrors );
343345
344346        httpServer .createContext (downloadStorageEndpoint (blobContainer , "write_large_blob" ), exchange  -> {
345-             final  S3HttpHandler .RequestComponents  requestComponents  = S3HttpHandler .parseRequestComponents (
346-                 S3HttpHandler .getRawRequestString (exchange )
347-             );
347+             final  S3HttpHandler .S3Request  s3Request  = parseRequest (exchange );
348348            final  long  contentLength  = Long .parseLong (exchange .getRequestHeaders ().getFirst ("Content-Length" ));
349349
350-             if  ("POST" . equals ( requestComponents . method ()) &&  requestComponents . query (). equals ( "uploads" )) {
350+             if  (s3Request . isInitiateMultipartUploadRequest ( )) {
351351                // initiate multipart upload request 
352352                if  (countDownInitiate .countDown ()) {
353353                    byte [] response  = (""" 
@@ -363,39 +363,37 @@ public void testWriteLargeBlob() throws Exception {
363363                    exchange .close ();
364364                    return ;
365365                }
366-             } else  if  ("PUT" .equals (requestComponents .method ())
367-                 && requestComponents .query ().contains ("uploadId=TEST" )
368-                 && requestComponents .query ().contains ("partNumber=" )) {
369-                     // upload part request 
370-                     MD5DigestCalculatingInputStream  md5  = new  MD5DigestCalculatingInputStream (exchange .getRequestBody ());
371-                     BytesReference  bytes  = Streams .readFully (md5 );
372-                     assertThat ((long ) bytes .length (), anyOf (equalTo (lastPartSize ), equalTo (bufferSize .getBytes ())));
373-                     assertThat (contentLength , anyOf (equalTo (lastPartSize ), equalTo (bufferSize .getBytes ())));
374- 
375-                     if  (countDownUploads .decrementAndGet () % 2  == 0 ) {
376-                         exchange .getResponseHeaders ().add ("ETag" , Base16 .encodeAsString (md5 .getMd5Digest ()));
377-                         exchange .sendResponseHeaders (HttpStatus .SC_OK , -1 );
378-                         exchange .close ();
379-                         return ;
380-                     }
366+             } else  if  (s3Request .isUploadPartRequest ()) {
367+                 // upload part request 
368+                 MD5DigestCalculatingInputStream  md5  = new  MD5DigestCalculatingInputStream (exchange .getRequestBody ());
369+                 BytesReference  bytes  = Streams .readFully (md5 );
370+                 assertThat ((long ) bytes .length (), anyOf (equalTo (lastPartSize ), equalTo (bufferSize .getBytes ())));
371+                 assertThat (contentLength , anyOf (equalTo (lastPartSize ), equalTo (bufferSize .getBytes ())));
372+ 
373+                 if  (countDownUploads .decrementAndGet () % 2  == 0 ) {
374+                     exchange .getResponseHeaders ().add ("ETag" , Base16 .encodeAsString (md5 .getMd5Digest ()));
375+                     exchange .sendResponseHeaders (HttpStatus .SC_OK , -1 );
376+                     exchange .close ();
377+                     return ;
378+                 }
381379
382-                 } else  if  ("POST" .equals (requestComponents .method ()) && requestComponents .query ().equals ("uploadId=TEST" )) {
383-                     // complete multipart upload request 
384-                     if  (countDownComplete .countDown ()) {
385-                         Streams .readFully (exchange .getRequestBody ());
386-                         byte [] response  = (""" 
387-                             <?xml version="1.0" encoding="UTF-8"?> 
388-                             <CompleteMultipartUploadResult> 
389-                               <Bucket>bucket</Bucket> 
390-                               <Key>write_large_blob</Key> 
391-                             </CompleteMultipartUploadResult>""" ).getBytes (StandardCharsets .UTF_8 );
392-                         exchange .getResponseHeaders ().add ("Content-Type" , "application/xml" );
393-                         exchange .sendResponseHeaders (HttpStatus .SC_OK , response .length );
394-                         exchange .getResponseBody ().write (response );
395-                         exchange .close ();
396-                         return ;
397-                     }
380+             } else  if  (s3Request .isCompleteMultipartUploadRequest ()) {
381+                 // complete multipart upload request 
382+                 if  (countDownComplete .countDown ()) {
383+                     Streams .readFully (exchange .getRequestBody ());
384+                     byte [] response  = (""" 
385+                         <?xml version="1.0" encoding="UTF-8"?> 
386+                         <CompleteMultipartUploadResult> 
387+                           <Bucket>bucket</Bucket> 
388+                           <Key>write_large_blob</Key> 
389+                         </CompleteMultipartUploadResult>""" ).getBytes (StandardCharsets .UTF_8 );
390+                     exchange .getResponseHeaders ().add ("Content-Type" , "application/xml" );
391+                     exchange .sendResponseHeaders (HttpStatus .SC_OK , response .length );
392+                     exchange .getResponseBody ().write (response );
393+                     exchange .close ();
394+                     return ;
398395                }
396+             }
399397
400398            // sends an error back or let the request time out 
401399            if  (useTimeout  == false ) {
@@ -444,12 +442,10 @@ public void testWriteLargeBlobStreaming() throws Exception {
444442        final  CountDown  countDownComplete  = new  CountDown (nbErrors );
445443
446444        httpServer .createContext (downloadStorageEndpoint (blobContainer , "write_large_blob_streaming" ), exchange  -> {
447-             final  S3HttpHandler .RequestComponents  requestComponents  = S3HttpHandler .parseRequestComponents (
448-                 S3HttpHandler .getRawRequestString (exchange )
449-             );
445+             final  S3HttpHandler .S3Request  s3Request  = parseRequest (exchange );
450446            final  long  contentLength  = Long .parseLong (exchange .getRequestHeaders ().getFirst ("Content-Length" ));
451447
452-             if  ("POST" . equals ( requestComponents . method ()) &&  requestComponents . query (). equals ( "uploads" )) {
448+             if  (s3Request . isInitiateMultipartUploadRequest ( )) {
453449                // initiate multipart upload request 
454450                if  (countDownInitiate .countDown ()) {
455451                    byte [] response  = (""" 
@@ -465,38 +461,36 @@ public void testWriteLargeBlobStreaming() throws Exception {
465461                    exchange .close ();
466462                    return ;
467463                }
468-             } else  if  ("PUT" .equals (requestComponents .method ())
469-                 && requestComponents .query ().contains ("uploadId=TEST" )
470-                 && requestComponents .query ().contains ("partNumber=" )) {
471-                     // upload part request 
472-                     MD5DigestCalculatingInputStream  md5  = new  MD5DigestCalculatingInputStream (exchange .getRequestBody ());
473-                     BytesReference  bytes  = Streams .readFully (md5 );
474- 
475-                     if  (counterUploads .incrementAndGet () % 2  == 0 ) {
476-                         bytesReceived .addAndGet (bytes .length ());
477-                         exchange .getResponseHeaders ().add ("ETag" , Base16 .encodeAsString (md5 .getMd5Digest ()));
478-                         exchange .sendResponseHeaders (HttpStatus .SC_OK , -1 );
479-                         exchange .close ();
480-                         return ;
481-                     }
464+             } else  if  (s3Request .isUploadPartRequest ()) {
465+                 // upload part request 
466+                 MD5DigestCalculatingInputStream  md5  = new  MD5DigestCalculatingInputStream (exchange .getRequestBody ());
467+                 BytesReference  bytes  = Streams .readFully (md5 );
468+ 
469+                 if  (counterUploads .incrementAndGet () % 2  == 0 ) {
470+                     bytesReceived .addAndGet (bytes .length ());
471+                     exchange .getResponseHeaders ().add ("ETag" , Base16 .encodeAsString (md5 .getMd5Digest ()));
472+                     exchange .sendResponseHeaders (HttpStatus .SC_OK , -1 );
473+                     exchange .close ();
474+                     return ;
475+                 }
482476
483-                 } else  if  ("POST" .equals (requestComponents .method ()) && requestComponents .query ().equals ("uploadId=TEST" )) {
484-                     // complete multipart upload request 
485-                     if  (countDownComplete .countDown ()) {
486-                         Streams .readFully (exchange .getRequestBody ());
487-                         byte [] response  = (""" 
488-                             <?xml version="1.0" encoding="UTF-8"?> 
489-                             <CompleteMultipartUploadResult> 
490-                               <Bucket>bucket</Bucket> 
491-                               <Key>write_large_blob_streaming</Key> 
492-                             </CompleteMultipartUploadResult>""" ).getBytes (StandardCharsets .UTF_8 );
493-                         exchange .getResponseHeaders ().add ("Content-Type" , "application/xml" );
494-                         exchange .sendResponseHeaders (HttpStatus .SC_OK , response .length );
495-                         exchange .getResponseBody ().write (response );
496-                         exchange .close ();
497-                         return ;
498-                     }
477+             } else  if  (s3Request .isCompleteMultipartUploadRequest ()) {
478+                 // complete multipart upload request 
479+                 if  (countDownComplete .countDown ()) {
480+                     Streams .readFully (exchange .getRequestBody ());
481+                     byte [] response  = (""" 
482+                         <?xml version="1.0" encoding="UTF-8"?> 
483+                         <CompleteMultipartUploadResult> 
484+                           <Bucket>bucket</Bucket> 
485+                           <Key>write_large_blob_streaming</Key> 
486+                         </CompleteMultipartUploadResult>""" ).getBytes (StandardCharsets .UTF_8 );
487+                     exchange .getResponseHeaders ().add ("Content-Type" , "application/xml" );
488+                     exchange .sendResponseHeaders (HttpStatus .SC_OK , response .length );
489+                     exchange .getResponseBody ().write (response );
490+                     exchange .close ();
491+                     return ;
499492                }
493+             }
500494
501495            // sends an error back or let the request time out 
502496            if  (useTimeout  == false ) {
0 commit comments