|
| 1 | +# HTTP |
| 2 | + |
| 3 | +HTTP is the most common application protocol used by Smithy clients. This guide |
| 4 | +provides advice on how to integrate and expose HTTP clients. |
| 5 | + |
| 6 | +## Configuration |
| 7 | + |
| 8 | +Smithy clients should allow their users to configure the HTTP client that the |
| 9 | +Smithy client uses to send requests. Users may want to change the client used to |
| 10 | +something that has different performance characteristics or support for features |
| 11 | +that they need. |
| 12 | + |
| 13 | +## HTTP interfaces |
| 14 | + |
| 15 | +Smithy clients should provide interfaces for HTTP clients that standardize how |
| 16 | +the Smithy client interacts with the HTTP client, allowing any HTTP client to be |
| 17 | +used as long as it implements the interface. |
| 18 | + |
| 19 | +### Clients |
| 20 | + |
| 21 | +It is recommended to make HTTP clients implementations of |
| 22 | +[`ClientTransport`](#transport-clients). |
| 23 | + |
| 24 | +```java |
| 25 | +public interface HttpClient implements ClientTransport<HttpRequest, HttpResponse> { |
| 26 | + HttpResponse send(Context context, HttpRequest request); |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +#### Context |
| 31 | + |
| 32 | +HTTP clients don't have many common context parameters, but they should check |
| 33 | +the context for a request timeout setting and use it if it's present. |
| 34 | + |
| 35 | +```java |
| 36 | +/** |
| 37 | + * This utility class holds shared context key definitions that are useful |
| 38 | + * for HTTP implementations. |
| 39 | + */ |
| 40 | +public final class HttpContext { |
| 41 | + public static final Context.Key<Duration> HTTP_REQUEST_TIMEOUT = Context.key("HTTP.RequestTimeout"); |
| 42 | + |
| 43 | + // This is a utility class that is not intended to be constructed, so it |
| 44 | + // has a private constructor. |
| 45 | + private HttpContext() {} |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +### Requests and Responses |
| 50 | + |
| 51 | +{rfc}`9110` discusses HTTP requests and responses collectively as "messages", |
| 52 | +and it can be useful to encode their shared features in a shared interface. |
| 53 | + |
| 54 | +```java |
| 55 | +public interface HttpMessage { |
| 56 | + /** |
| 57 | + * Get the headers of the message. |
| 58 | + * |
| 59 | + * @return headers. |
| 60 | + */ |
| 61 | + HttpFields headers(); |
| 62 | + |
| 63 | + /** |
| 64 | + * Get the body of the message, or null. |
| 65 | + * |
| 66 | + * @return the message body or null. |
| 67 | + */ |
| 68 | + DataStream body(); |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +Requests introduce the `method` and `uri` properties. |
| 73 | + |
| 74 | +```java |
| 75 | +public interface HttpRequest extends HttpMessage { |
| 76 | + /** |
| 77 | + * Get the method of the request. |
| 78 | + * |
| 79 | + * @return the method. |
| 80 | + */ |
| 81 | + String method(); |
| 82 | + |
| 83 | + /** |
| 84 | + * Get the URI of the request. |
| 85 | + * |
| 86 | + * @return the request URI. |
| 87 | + */ |
| 88 | + URI uri(); |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +Responses introduce a status code. |
| 93 | + |
| 94 | +```java |
| 95 | +public interface HttpResponse extends HttpMessage { |
| 96 | + /** |
| 97 | + * Get the status code of the response. |
| 98 | + * |
| 99 | + * @return the status code. |
| 100 | + */ |
| 101 | + int statusCode(); |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### Fields |
| 106 | + |
| 107 | +Most users who have interacted with HTTP directly are familiar with the concept |
| 108 | +of headers. Headers were originally introduced in HTTP/1.0 and, since then, the |
| 109 | +concept of key/value pairs has expanded to include trailers and other arbitrary |
| 110 | +metadata. As of {rfc}`9110`, these key/value pairs are exclusively referred to |
| 111 | +as {rfc}`fields <9110#section-5>`. |
| 112 | + |
| 113 | +When designing HTTP interfaces for Smithy clients, be careful to understand |
| 114 | +field semantics. In particular, it is important to understand that field keys |
| 115 | +are case-insensitive and may appear more than once in an HTTP message. Since |
| 116 | +field keys may appear more than once, it is recommended that they are |
| 117 | +represented as an iterable collection of pairs or as a map whose value type is a |
| 118 | +list. This allows protocol implementations to safely handle joining and |
| 119 | +splitting. |
| 120 | + |
| 121 | +It is recommended to have utilities to convert fields to and from maps. Fields |
| 122 | +are often conceptualized as maps, so providing these utilities allows users to |
| 123 | +access fields in a more comfortable way without sacrificing correctness. |
| 124 | + |
| 125 | +```java |
| 126 | +public interface HttpFields extends Iterable<Map.Entry<String, List<String>>> { |
| 127 | + /** |
| 128 | + * Create an HttpFields instance from a map. |
| 129 | + * |
| 130 | + * @param fields Field map to use as a data source. |
| 131 | + * @return the created fields. |
| 132 | + */ |
| 133 | + static HttpFields of(Map<String, List<String>> fields) { |
| 134 | + // This constructs a theoretical default implementation of the |
| 135 | + // HttpFields interface that creates an unmodifiable copy of the given |
| 136 | + // map. |
| 137 | + return fields.isEmpty() ? UnmodifiableHttpFields.EMPTY : new UnmodifiableHttpFields(fields); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Convert the HttpFields to a map. |
| 142 | + * |
| 143 | + * @return the fields as a map. |
| 144 | + */ |
| 145 | + Map<String, List<String>> toMap(); |
| 146 | + |
| 147 | + /** |
| 148 | + * Check if the given field is case-insensitively present. |
| 149 | + * |
| 150 | + * @param name Name of the field to check. |
| 151 | + * @return true if the field is present. |
| 152 | + */ |
| 153 | + default boolean containsField(String name) { |
| 154 | + return !getAllValues(name).isEmpty(); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Get the first field value of a specific field by case-insensitive name. |
| 159 | + * |
| 160 | + * Smithy clients know whether a given field should have a single value or |
| 161 | + * a list value. This helper method simplifies usage for fields with a |
| 162 | + * single value. |
| 163 | + * |
| 164 | + * @param name Name of the field to get. |
| 165 | + * @return the matching field value, or null if not found. |
| 166 | + */ |
| 167 | + default String getFirstValue(String name) { |
| 168 | + var list = getAllValues(name); |
| 169 | + return list.isEmpty() ? null : list.get(0); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Get the values of a specific field by case-insensitive name. |
| 174 | + * |
| 175 | + * @param name Name of the field to get the values of. |
| 176 | + * @return the values of the field, or an empty list. |
| 177 | + */ |
| 178 | + List<String> getAllValues(String name); |
| 179 | +} |
| 180 | +``` |
| 181 | + |
| 182 | +#### Implementation recommendations |
| 183 | + |
| 184 | +It is not recommended to automatically attempt to join values for a given field |
| 185 | +key at the HTTP layer. {rfc}`9110#section-5` allows field values to be joined |
| 186 | +with a comma, but doing so automatically can introduce data corruption if one of |
| 187 | +the field values already includes a comma. {rfc}`Section 5.6 <9110#section-5.6>` |
| 188 | +includes productions that can help to handle those edge cases, but whether they |
| 189 | +are used or not is up to the protocol definition. |
0 commit comments