• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 
6 package software.amazon.awssdk.crt.http;
7 
8 import software.amazon.awssdk.crt.CrtRuntimeException;
9 import software.amazon.awssdk.crt.http.HttpVersion;
10 
11 import java.nio.ByteBuffer;
12 
13 /**
14  * Represents a single Client Request to be sent on a HTTP connection
15  */
16 public class HttpRequest extends HttpRequestBase {
17 
18     /**
19      *
20      * @param method      http verb to use
21      * @param encodedPath path of the http request
22      */
HttpRequest(String method, String encodedPath)23     public HttpRequest(String method, String encodedPath) {
24         this(method, encodedPath, new HttpHeader[] {}, null);
25     }
26 
27     /**
28      *
29      * @param method      http verb to use
30      * @param encodedPath path of the http request
31      * @param headers     set of http request headers to include
32      * @param bodyStream  (optional) interface to an object that will stream out the
33      *                    request body
34      */
HttpRequest(String method, String encodedPath, HttpHeader[] headers, HttpRequestBodyStream bodyStream)35     public HttpRequest(String method, String encodedPath, HttpHeader[] headers, HttpRequestBodyStream bodyStream) {
36         super(headers, bodyStream);
37         this.method = method;
38         this.encodedPath = encodedPath;
39     }
40 
41     /**
42      * Package private. Used by JNI to convert native http representation to a Java
43      * object, because accessing this struct from JNI is too slow.
44      *
45      * Requests are marshalled as follows:
46      *
47      * each string field is: [4-bytes BE] [variable length bytes specified by the
48      * previous field] Each request is then: [method][path][header name-value pairs]
49      *
50      * @param marshalledRequest serialized http request to be parsed.
51      * @param bodyStream        body stream for the http request.
52      */
HttpRequest(ByteBuffer marshalledRequest, HttpRequestBodyStream bodyStream)53     HttpRequest(ByteBuffer marshalledRequest, HttpRequestBodyStream bodyStream) {
54         if (marshalledRequest.remaining() < BUFFER_INT_SIZE * 3) {
55             throw new CrtRuntimeException("Invalid marshalled request object.");
56         }
57         this.version = HttpVersion.getEnumValueFromInteger(marshalledRequest.getInt());
58 
59         int methodLength = marshalledRequest.getInt();
60         byte[] methodBlob = new byte[methodLength];
61         marshalledRequest.get(methodBlob);
62         this.method = new String(methodBlob, UTF8);
63         if (marshalledRequest.remaining() < BUFFER_INT_SIZE) {
64             throw new CrtRuntimeException("Invalid marshalled request object.");
65         }
66 
67         int pathLength = marshalledRequest.getInt();
68         byte[] pathBlob = new byte[pathLength];
69         marshalledRequest.get(pathBlob);
70         this.encodedPath = new String(pathBlob, UTF8);
71 
72         this.headers = HttpHeader.loadHeadersListFromMarshalledHeadersBlob(marshalledRequest);
73         this.bodyStream = bodyStream;
74     }
75 
76     /**
77      * @return the HTTP method of this request
78      */
getMethod()79     public String getMethod() {
80         return method;
81     }
82 
83     /**
84      * @return the encoded path of this request
85      */
getEncodedPath()86     public String getEncodedPath() {
87         return encodedPath;
88     }
89 
90     /**
91      * Sets the request's encoded path
92      *
93      * @param encodedPath the new encoded path
94      */
setEncodedPath(final String encodedPath)95     public void setEncodedPath(final String encodedPath) {
96         this.encodedPath = encodedPath;
97     }
98 }
99