/src/h2o/lib/common/httpclient.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2018 Ichito Nagata, Fastly, Inc. |
3 | | * |
4 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | | * of this software and associated documentation files (the "Software"), to |
6 | | * deal in the Software without restriction, including without limitation the |
7 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
8 | | * sell copies of the Software, and to permit persons to whom the Software is |
9 | | * furnished to do so, subject to the following conditions: |
10 | | * |
11 | | * The above copyright notice and this permission notice shall be included in |
12 | | * all copies or substantial portions of the Software. |
13 | | * |
14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
19 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
20 | | * IN THE SOFTWARE. |
21 | | */ |
22 | | |
23 | | #include "h2o/httpclient.h" |
24 | | |
25 | | const char h2o_httpclient_error_is_eos[] = "end of stream"; |
26 | | const char h2o_httpclient_error_refused_stream[] = "refused stream"; |
27 | | const char h2o_httpclient_error_unknown_alpn_protocol[] = "unknown alpn protocol"; |
28 | | const char h2o_httpclient_error_io[] = "I/O error"; |
29 | | const char h2o_httpclient_error_connect_timeout[] = "connection timeout"; |
30 | | const char h2o_httpclient_error_first_byte_timeout[] = "first byte timeout"; |
31 | | const char h2o_httpclient_error_io_timeout[] = "I/O timeout"; |
32 | | const char h2o_httpclient_error_invalid_content_length[] = "invalid content-length"; |
33 | | const char h2o_httpclient_error_flow_control[] = "flow control error"; |
34 | | const char h2o_httpclient_error_http1_line_folding[] = "line folding of header fields is not supported"; |
35 | | const char h2o_httpclient_error_http1_unexpected_transfer_encoding[] = "unexpected type of transfer-encoding"; |
36 | | const char h2o_httpclient_error_http1_parse_failed[] = "failed to parse the response"; |
37 | | const char h2o_httpclient_error_http1_headers_too_long[] = "response headers too long"; |
38 | | const char h2o_httpclient_error_protocol_violation[] = "protocol violation"; |
39 | | const char h2o_httpclient_error_internal[] = "internal error"; |
40 | | const char h2o_httpclient_error_malformed_frame[] = "malformed HTTP frame"; |
41 | | const char h2o_httpclient_error_unexpected_101[] = "received unexpected 101 response"; |
42 | | const char h2o_httpclient_error_too_many_informational[] = "too many informational responses"; |
43 | | |
44 | | /** |
45 | | * Used to indicate that the HTTP request is to be "upgraded" into a CONNECT tunnel. |
46 | | */ |
47 | | const char h2o_httpclient_upgrade_to_connect[] = "\nCONNECT / CONNECT-UDP method"; |
48 | | |
49 | | void h2o_httpclient_connection_pool_init(h2o_httpclient_connection_pool_t *connpool, h2o_socketpool_t *sockpool) |
50 | 3 | { |
51 | 3 | connpool->socketpool = sockpool; |
52 | 3 | h2o_linklist_init_anchor(&connpool->http2.conns); |
53 | 3 | h2o_linklist_init_anchor(&connpool->http3.conns); |
54 | 3 | } |
55 | | |
56 | | static void close_client(h2o_httpclient_t *client) |
57 | 76 | { |
58 | 76 | if (client->_connect_req != NULL) { |
59 | 76 | h2o_socketpool_cancel_connect(client->_connect_req); |
60 | 76 | client->_connect_req = NULL; |
61 | 76 | } |
62 | | |
63 | 76 | if (h2o_timer_is_linked(&client->_timeout)) |
64 | 76 | h2o_timer_unlink(&client->_timeout); |
65 | | |
66 | 76 | free(client); |
67 | 76 | } |
68 | | |
69 | | static void on_connect_error(h2o_httpclient_t *client, const char *errstr) |
70 | 0 | { |
71 | 0 | assert(errstr != NULL); |
72 | 0 | client->_cb.on_connect(client, errstr, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL); |
73 | 0 | close_client(client); |
74 | 0 | } |
75 | | |
76 | | static void on_connect_timeout(h2o_timer_t *entry) |
77 | 0 | { |
78 | 0 | h2o_httpclient_t *client = H2O_STRUCT_FROM_MEMBER(h2o_httpclient_t, _timeout, entry); |
79 | 0 | on_connect_error(client, h2o_httpclient_error_connect_timeout); |
80 | 0 | } |
81 | | |
82 | | static void do_cancel(h2o_httpclient_t *_client) |
83 | 76 | { |
84 | 76 | h2o_httpclient_t *client = (void *)_client; |
85 | 76 | close_client(client); |
86 | 76 | } |
87 | | |
88 | | static h2o_httpclient_t *create_client(h2o_httpclient_t **_client, h2o_mem_pool_t *pool, void *data, h2o_httpclient_ctx_t *ctx, |
89 | | h2o_httpclient_connection_pool_t *connpool, const char *upgrade_to, |
90 | | h2o_httpclient_connect_cb on_connect) |
91 | 3.75k | { |
92 | 3.75k | #define SZ_MAX(x, y) ((x) > (y) ? (x) : (y)) |
93 | 3.75k | size_t sz = SZ_MAX(h2o_httpclient__h1_size, h2o_httpclient__h2_size); |
94 | 3.75k | #undef SZ_MAX |
95 | 3.75k | h2o_httpclient_t *client = h2o_mem_alloc(sz); |
96 | 3.75k | memset(client, 0, sz); |
97 | 3.75k | client->pool = pool; |
98 | 3.75k | client->ctx = ctx; |
99 | 3.75k | client->data = data; |
100 | 3.75k | client->upgrade_to = upgrade_to; |
101 | 3.75k | client->connpool = connpool; |
102 | 3.75k | client->cancel = do_cancel; |
103 | 3.75k | client->_cb.on_connect = on_connect; |
104 | 3.75k | client->_timeout.cb = on_connect_timeout; |
105 | 3.75k | client->timings.start_at = h2o_gettimeofday(ctx->loop); |
106 | | |
107 | 3.75k | if (_client != NULL) |
108 | 3.75k | *_client = client; |
109 | | |
110 | 3.75k | return client; |
111 | 3.75k | } |
112 | | |
113 | | static void on_pool_connect(h2o_socket_t *sock, const char *errstr, void *data, h2o_url_t *origin) |
114 | 3.68k | { |
115 | 3.68k | h2o_httpclient_t *client = data; |
116 | | |
117 | 3.68k | h2o_timer_unlink(&client->_timeout); |
118 | | |
119 | 3.68k | client->_connect_req = NULL; |
120 | | |
121 | 3.68k | if (sock == NULL) { |
122 | 0 | assert(errstr != NULL); |
123 | 0 | on_connect_error(client, errstr); |
124 | 0 | return; |
125 | 0 | } |
126 | | |
127 | 3.68k | h2o_iovec_t alpn_proto; |
128 | 3.68k | if (client->ctx->force_cleartext_http2 && sock->ssl == NULL && client->ctx->protocol_selector.ratio.http2 == 100) { |
129 | | /* The client has prior knowledge and wants to use http2 without |
130 | | * discovery or upgrade. Make sure that we're using a 100% |
131 | | * H2 context, to avoid having this connection reused for H1 |
132 | | * accidentaly */ |
133 | 0 | goto ForceH2; |
134 | 3.68k | } else if (sock->ssl == NULL || (alpn_proto = h2o_socket_ssl_get_selected_protocol(sock)).len == 0) { |
135 | 3.68k | h2o_httpclient__h1_on_connect(client, sock, origin); |
136 | 3.68k | } else { |
137 | 0 | if (h2o_memis(alpn_proto.base, alpn_proto.len, H2O_STRLIT("h2"))) { |
138 | 0 | ForceH2: |
139 | | /* detach this socket from the socketpool to count the number of h1 connections correctly */ |
140 | 0 | h2o_socketpool_detach(client->connpool->socketpool, sock); |
141 | 0 | h2o_httpclient__h2_on_connect(client, sock, origin); |
142 | 0 | } else if (memcmp(alpn_proto.base, "http/1.1", alpn_proto.len) == 0) { |
143 | 0 | h2o_httpclient__h1_on_connect(client, sock, origin); |
144 | 0 | } else { |
145 | 0 | on_connect_error(client, h2o_httpclient_error_unknown_alpn_protocol); |
146 | 0 | } |
147 | 0 | } |
148 | 3.68k | } |
149 | | |
150 | | enum { |
151 | | /** |
152 | | * indicates that H1 should be chosen |
153 | | */ |
154 | | PROTOCOL_SELECTOR_H1, |
155 | | /** |
156 | | * indicates that H2 should be chosen (though the server might fallback to H1) |
157 | | */ |
158 | | PROTOCOL_SELECTOR_H2, |
159 | | /** |
160 | | * indicates that H3 should be chosen |
161 | | */ |
162 | | PROTOCOL_SELECTOR_H3, |
163 | | /** |
164 | | * used when ratio.http2 < 0; see h2o_httpclient_ctx_t::protocol_selector.ratio.http2 |
165 | | */ |
166 | | PROTOCOL_SELECTOR_SERVER_DRIVEN, |
167 | | /** |
168 | | * total number |
169 | | */ |
170 | | PROTOCOL_SELECTOR_COUNT |
171 | | }; |
172 | | |
173 | | static size_t select_protocol(struct st_h2o_httpclient_protocol_selector_t *selector) |
174 | 3.75k | { |
175 | 3.75k | H2O_BUILD_ASSERT(PTLS_ELEMENTSOF(selector->_deficits) == PROTOCOL_SELECTOR_COUNT); |
176 | | |
177 | | /* update the deficits */ |
178 | 3.75k | if (selector->ratio.http2 < 0) { |
179 | 0 | selector->_deficits[PROTOCOL_SELECTOR_SERVER_DRIVEN] += 100 - selector->ratio.http3; |
180 | 3.75k | } else { |
181 | 3.75k | selector->_deficits[PROTOCOL_SELECTOR_H1] += 100 - selector->ratio.http2 - selector->ratio.http3; |
182 | 3.75k | selector->_deficits[PROTOCOL_SELECTOR_H2] += selector->ratio.http2; |
183 | 3.75k | } |
184 | 3.75k | selector->_deficits[PROTOCOL_SELECTOR_H3] += selector->ratio.http3; |
185 | | |
186 | | /* select one with the highest value */ |
187 | 3.75k | size_t result = 0; |
188 | 15.0k | for (size_t i = 1; i < PROTOCOL_SELECTOR_COUNT; ++i) { |
189 | 11.2k | if (selector->_deficits[result] < selector->_deficits[i]) |
190 | 0 | result = i; |
191 | 11.2k | } |
192 | | |
193 | | /* decrement the one being selected */ |
194 | 3.75k | selector->_deficits[result] -= 100; |
195 | | |
196 | 3.75k | return result; |
197 | 3.75k | } |
198 | | |
199 | | static struct st_h2o_httpclient__h2_conn_t *find_h2conn(h2o_httpclient_connection_pool_t *pool, h2o_url_t *target) |
200 | 0 | { |
201 | 0 | int should_check_target = h2o_socketpool_is_global(pool->socketpool); |
202 | |
|
203 | 0 | for (h2o_linklist_t *l = pool->http2.conns.next; l != &pool->http2.conns; l = l->next) { |
204 | 0 | struct st_h2o_httpclient__h2_conn_t *conn = H2O_STRUCT_FROM_MEMBER(struct st_h2o_httpclient__h2_conn_t, link, l); |
205 | 0 | if (should_check_target && !(conn->origin_url.scheme == target->scheme && |
206 | 0 | h2o_memis(conn->origin_url.authority.base, conn->origin_url.authority.len, |
207 | 0 | target->authority.base, target->authority.len))) |
208 | 0 | continue; |
209 | 0 | if (conn->num_streams >= h2o_httpclient__h2_get_max_concurrent_streams(conn)) |
210 | 0 | continue; |
211 | 0 | return conn; |
212 | 0 | } |
213 | | |
214 | 0 | return NULL; |
215 | 0 | } |
216 | | |
217 | | static void connect_using_socket_pool(h2o_httpclient_t **_client, h2o_mem_pool_t *pool, void *data, h2o_httpclient_ctx_t *ctx, |
218 | | h2o_httpclient_connection_pool_t *connpool, h2o_url_t *origin, const char *upgrade_to, |
219 | | h2o_httpclient_connect_cb on_connect, h2o_iovec_t alpn_protos) |
220 | 3.75k | { |
221 | 3.75k | h2o_httpclient_t *client = create_client(_client, pool, data, ctx, connpool, upgrade_to, on_connect); |
222 | 3.75k | h2o_timer_link(client->ctx->loop, client->ctx->connect_timeout, &client->_timeout); |
223 | 3.75k | h2o_socketpool_connect(&client->_connect_req, connpool->socketpool, origin, ctx->loop, ctx->getaddr_receiver, alpn_protos, |
224 | 3.75k | on_pool_connect, client); |
225 | 3.75k | } |
226 | | |
227 | | static void connect_using_h2conn(h2o_httpclient_t **_client, h2o_mem_pool_t *pool, void *data, |
228 | | struct st_h2o_httpclient__h2_conn_t *conn, h2o_httpclient_connection_pool_t *connpool, |
229 | | const char *upgrade_to, h2o_httpclient_connect_cb on_connect) |
230 | 0 | { |
231 | 0 | h2o_httpclient_t *client = create_client(_client, pool, data, conn->ctx, connpool, upgrade_to, on_connect); |
232 | 0 | h2o_httpclient__h2_on_connect(client, conn->sock, &conn->origin_url); |
233 | 0 | } |
234 | | |
235 | | void h2o_httpclient_connect(h2o_httpclient_t **_client, h2o_mem_pool_t *pool, void *data, h2o_httpclient_ctx_t *ctx, |
236 | | h2o_httpclient_connection_pool_t *connpool, h2o_url_t *origin, const char *upgrade_to, |
237 | | h2o_httpclient_connect_cb on_connect) |
238 | 3.75k | { |
239 | 3.75k | static const h2o_iovec_t no_protos = {}, both_protos = {H2O_STRLIT("\x02" |
240 | 3.75k | "h2" |
241 | 3.75k | "\x08" |
242 | 3.75k | "http/1.1")}; |
243 | 3.75k | assert(connpool != NULL); |
244 | | |
245 | 3.75k | size_t selected_protocol = select_protocol(&ctx->protocol_selector); |
246 | | |
247 | | /* adjust selected protocol if the attempt is to create a tunnel */ |
248 | 3.75k | if (upgrade_to != NULL) { |
249 | | /* TODO provide a knob to map each upgrade token to some, all, or no HTTP version. Until that is done, upgrade other than to |
250 | | * a CONNECT and CONNECT-UDP tunnel is directed to H1. */ |
251 | 0 | if (upgrade_to != h2o_httpclient_upgrade_to_connect && strcmp(upgrade_to, "connect-udp") != 0) |
252 | 0 | selected_protocol = PROTOCOL_SELECTOR_H1; |
253 | 0 | } |
254 | | |
255 | 3.75k | switch (selected_protocol) { |
256 | 3.75k | case PROTOCOL_SELECTOR_H1: |
257 | | /* H1: use the socket pool to obtain a connection, without any ALPN */ |
258 | 3.75k | connect_using_socket_pool(_client, pool, data, ctx, connpool, origin, upgrade_to, on_connect, no_protos); |
259 | 3.75k | break; |
260 | 0 | case PROTOCOL_SELECTOR_H2: { |
261 | | /* H2: use existing H2 connection (if any) or create a new connection offering both H1 and H2 */ |
262 | 0 | struct st_h2o_httpclient__h2_conn_t *h2conn = find_h2conn(connpool, origin); |
263 | 0 | if (h2conn != NULL) { |
264 | 0 | connect_using_h2conn(_client, pool, data, h2conn, connpool, upgrade_to, on_connect); |
265 | 0 | } else { |
266 | 0 | connect_using_socket_pool(_client, pool, data, ctx, connpool, origin, upgrade_to, on_connect, both_protos); |
267 | 0 | } |
268 | 0 | } break; |
269 | 0 | case PROTOCOL_SELECTOR_H3: |
270 | 0 | h2o_httpclient__connect_h3(_client, pool, data, ctx, connpool, origin, upgrade_to, on_connect); |
271 | 0 | break; |
272 | 0 | case PROTOCOL_SELECTOR_SERVER_DRIVEN: { |
273 | | /* offer H2 the server, but evenly distribute the load among existing H1 and H2 connections */ |
274 | 0 | struct st_h2o_httpclient__h2_conn_t *h2conn = find_h2conn(connpool, origin); |
275 | 0 | if (h2conn != NULL && connpool->socketpool->_shared.pooled_count != 0) { |
276 | | /* both of h1 and h2 connections exist, compare in-use ratio */ |
277 | 0 | double http1_ratio = (double)(connpool->socketpool->_shared.count - connpool->socketpool->_shared.pooled_count) / |
278 | 0 | connpool->socketpool->_shared.count; |
279 | 0 | double http2_ratio = (double)h2conn->num_streams / h2o_httpclient__h2_get_max_concurrent_streams(h2conn); |
280 | 0 | if (http2_ratio <= http1_ratio) { |
281 | 0 | connect_using_h2conn(_client, pool, data, h2conn, connpool, upgrade_to, on_connect); |
282 | 0 | } else { |
283 | 0 | connect_using_socket_pool(_client, pool, data, ctx, connpool, origin, upgrade_to, on_connect, no_protos); |
284 | 0 | } |
285 | 0 | } else if (h2conn != NULL) { |
286 | | /* h2 connection exists */ |
287 | 0 | connect_using_h2conn(_client, pool, data, h2conn, connpool, upgrade_to, on_connect); |
288 | 0 | } else if (connpool->socketpool->_shared.pooled_count != 0) { |
289 | | /* h1 connection exists */ |
290 | 0 | connect_using_socket_pool(_client, pool, data, ctx, connpool, origin, upgrade_to, on_connect, no_protos); |
291 | 0 | } else { |
292 | | /* no connections, connect using ALPN */ |
293 | 0 | connect_using_socket_pool(_client, pool, data, ctx, connpool, origin, upgrade_to, on_connect, both_protos); |
294 | 0 | } |
295 | 0 | } break; |
296 | 3.75k | } |
297 | 3.75k | } |
298 | | |
299 | | void h2o_httpclient_set_conn_properties_of_socket(h2o_socket_t *sock, h2o_httpclient_conn_properties_t *properties) |
300 | 3.68k | { |
301 | 3.68k | properties->ssl.protocol_version = h2o_socket_get_ssl_protocol_version(sock); |
302 | 3.68k | properties->ssl.session_reused = h2o_socket_get_ssl_session_reused(sock); |
303 | 3.68k | properties->ssl.cipher = h2o_socket_get_ssl_cipher(sock); |
304 | 3.68k | properties->ssl.cipher_bits = h2o_socket_get_ssl_cipher_bits(sock); |
305 | 3.68k | properties->sock = sock; |
306 | 3.68k | } |