/src/h2o/lib/common/http3client.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2018 Fastly, Kazuho Oku |
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 | | #include <assert.h> |
23 | | #include <errno.h> |
24 | | #include <stdlib.h> |
25 | | #include <sys/types.h> |
26 | | #include "quicly.h" |
27 | | #include "h2o.h" |
28 | | #include "h2o/hostinfo.h" |
29 | | #include "h2o/httpclient.h" |
30 | | #include "h2o/http2_common.h" |
31 | | #include "h2o/http3_common.h" |
32 | | #include "h2o/http3_internal.h" |
33 | | #include "../probes_.h" |
34 | | |
35 | | /** |
36 | | * internal error code used for signalling EOS |
37 | | */ |
38 | 0 | #define ERROR_EOS H2O_HTTP3_ERROR_USER1 |
39 | | /** |
40 | | * Maxmium amount of unsent bytes to be buffered when acting as a tunnel. |
41 | | */ |
42 | | #define TUNNEL_MAX_UNSENT 16384 |
43 | | |
44 | | struct st_h2o_http3client_req_t { |
45 | | /** |
46 | | * superclass |
47 | | */ |
48 | | h2o_httpclient_t super; |
49 | | /** |
50 | | * pointer to the connection |
51 | | */ |
52 | | struct st_h2o_httpclient__h3_conn_t *conn; |
53 | | /** |
54 | | * is NULL until connection is established |
55 | | */ |
56 | | quicly_stream_t *quic; |
57 | | /** |
58 | | * currently only used for pending_requests |
59 | | */ |
60 | | h2o_linklist_t link; |
61 | | /** |
62 | | * |
63 | | */ |
64 | | uint64_t bytes_left_in_data_frame; |
65 | | /** |
66 | | * |
67 | | */ |
68 | | h2o_buffer_t *sendbuf; |
69 | | /** |
70 | | * |
71 | | */ |
72 | | struct { |
73 | | /** |
74 | | * HTTP-level buffer that contains (part of) response body received. Is the variable registered as `h2o_httpclient::buf`. |
75 | | */ |
76 | | h2o_buffer_t *body; |
77 | | /** |
78 | | * QUIC stream-level buffer that contains bytes that have not yet been processed at the HTTP/3 framing decoding level. This |
79 | | * buffer may have gaps. The beginning offset of `partial_frame` is equal to `recvstate.data_off`. |
80 | | */ |
81 | | h2o_buffer_t *stream; |
82 | | /** |
83 | | * Retains the amount of stream-level data that was available in the previous call. This value is used to see if processing |
84 | | * of new stream data is necessary. |
85 | | */ |
86 | | size_t prev_bytes_available; |
87 | | } recvbuf; |
88 | | /** |
89 | | * called when new contigious data becomes available |
90 | | */ |
91 | | quicly_error_t (*handle_input)(struct st_h2o_http3client_req_t *req, const uint8_t **src, const uint8_t *src_end, |
92 | | quicly_error_t err, const char **err_desc); |
93 | | /** |
94 | | * `proceed_req` callback. The callback is invoked when all bytes in the send buffer is emitted for the first time. |
95 | | * `bytes_inflight` contains the number of bytes being transmitted, or SIZE_MAX if nothing is inflight. |
96 | | */ |
97 | | struct { |
98 | | h2o_httpclient_proceed_req_cb cb; |
99 | | size_t bytes_inflight; |
100 | | } proceed_req; |
101 | | /** |
102 | | * |
103 | | */ |
104 | | enum { |
105 | | H2O_HTTP3CLIENT_RESPONSE_STATE_HEAD, |
106 | | H2O_HTTP3CLIENT_RESPONSE_STATE_BODY, |
107 | | H2O_HTTP3CLIENT_RESPONSE_STATE_CLOSED |
108 | | } response_state; |
109 | | /** |
110 | | * callback used for forwarding CONNECT-UDP using H3_DATAGRAMS |
111 | | */ |
112 | | h2o_httpclient_forward_datagram_cb on_read_datagrams; |
113 | | /** |
114 | | * flags |
115 | | */ |
116 | | unsigned offered_datagram_flow_id : 1; |
117 | | }; |
118 | | |
119 | | static quicly_error_t handle_input_expect_data_frame(struct st_h2o_http3client_req_t *req, const uint8_t **src, |
120 | | const uint8_t *src_end, quicly_error_t err, const char **err_desc); |
121 | | static void start_request(struct st_h2o_http3client_req_t *req); |
122 | | static int do_write_req(h2o_httpclient_t *_client, h2o_iovec_t chunk, int is_end_stream); |
123 | | |
124 | | static size_t emit_data(struct st_h2o_http3client_req_t *req, h2o_iovec_t payload) |
125 | 0 | { |
126 | 0 | size_t nbytes; |
127 | |
|
128 | 0 | { /* emit header */ |
129 | 0 | uint8_t buf[9], *p = buf; |
130 | 0 | *p++ = H2O_HTTP3_FRAME_TYPE_DATA; |
131 | 0 | p = quicly_encodev(p, payload.len); |
132 | 0 | nbytes = p - buf; |
133 | 0 | h2o_buffer_append(&req->sendbuf, buf, nbytes); |
134 | 0 | } |
135 | | |
136 | | /* emit payload */ |
137 | 0 | h2o_buffer_append(&req->sendbuf, payload.base, payload.len); |
138 | 0 | nbytes += payload.len; |
139 | |
|
140 | 0 | return nbytes; |
141 | 0 | } |
142 | | |
143 | | static void destroy_request(struct st_h2o_http3client_req_t *req) |
144 | 0 | { |
145 | 0 | assert(req->quic == NULL); |
146 | | |
147 | 0 | h2o_buffer_dispose(&req->sendbuf); |
148 | 0 | h2o_buffer_dispose(&req->recvbuf.body); |
149 | 0 | h2o_buffer_dispose(&req->recvbuf.stream); |
150 | 0 | if (h2o_timer_is_linked(&req->super._timeout)) |
151 | 0 | h2o_timer_unlink(&req->super._timeout); |
152 | 0 | if (h2o_linklist_is_linked(&req->link)) |
153 | 0 | h2o_linklist_unlink(&req->link); |
154 | 0 | free(req); |
155 | 0 | } |
156 | | |
157 | | static void detach_stream(struct st_h2o_http3client_req_t *req) |
158 | 0 | { |
159 | 0 | req->quic->callbacks = &quicly_stream_noop_callbacks; |
160 | 0 | req->quic->data = NULL; |
161 | 0 | req->quic = NULL; |
162 | 0 | } |
163 | | |
164 | | static void close_stream(struct st_h2o_http3client_req_t *req, quicly_error_t err) |
165 | 0 | { |
166 | | /* TODO are we expected to send two error codes? */ |
167 | 0 | if (!quicly_sendstate_transfer_complete(&req->quic->sendstate)) |
168 | 0 | quicly_reset_stream(req->quic, err); |
169 | 0 | if (!quicly_recvstate_transfer_complete(&req->quic->recvstate)) |
170 | 0 | quicly_request_stop(req->quic, err); |
171 | 0 | detach_stream(req); |
172 | 0 | } |
173 | | |
174 | | static void write_datagrams(h2o_httpclient_t *_client, h2o_iovec_t *datagrams, size_t num_datagrams) |
175 | 0 | { |
176 | 0 | struct st_h2o_http3client_req_t *req = H2O_STRUCT_FROM_MEMBER(struct st_h2o_http3client_req_t, super, _client); |
177 | 0 | h2o_http3_send_h3_datagrams(&req->conn->super, req->quic->stream_id, datagrams, num_datagrams); |
178 | 0 | } |
179 | | |
180 | | static struct st_h2o_httpclient__h3_conn_t *find_connection(h2o_httpclient_connection_pool_t *pool, h2o_url_t *origin) |
181 | 0 | { |
182 | 0 | int should_check_target = h2o_socketpool_is_global(pool->socketpool); |
183 | | |
184 | | /* FIXME: |
185 | | * - check connection state(e.g., max_concurrent_streams, if received GOAWAY) |
186 | | * - use hashmap |
187 | | */ |
188 | 0 | for (h2o_linklist_t *l = pool->http3.conns.next; l != &pool->http3.conns; l = l->next) { |
189 | 0 | struct st_h2o_httpclient__h3_conn_t *conn = H2O_STRUCT_FROM_MEMBER(struct st_h2o_httpclient__h3_conn_t, link, l); |
190 | 0 | if (should_check_target && !(conn->server.origin_url.scheme == origin->scheme && |
191 | 0 | h2o_memis(conn->server.origin_url.authority.base, conn->server.origin_url.authority.len, |
192 | 0 | origin->authority.base, origin->authority.len))) |
193 | 0 | continue; |
194 | 0 | return conn; |
195 | 0 | } |
196 | | |
197 | 0 | return NULL; |
198 | 0 | } |
199 | | |
200 | | static void start_pending_requests(struct st_h2o_httpclient__h3_conn_t *conn) |
201 | 0 | { |
202 | 0 | while (!h2o_linklist_is_empty(&conn->pending_requests)) { |
203 | 0 | struct st_h2o_http3client_req_t *req = |
204 | 0 | H2O_STRUCT_FROM_MEMBER(struct st_h2o_http3client_req_t, link, conn->pending_requests.next); |
205 | 0 | h2o_linklist_unlink(&req->link); |
206 | 0 | start_request(req); |
207 | 0 | } |
208 | 0 | } |
209 | | |
210 | | static void call_proceed_req(struct st_h2o_http3client_req_t *req, const char *errstr) |
211 | 0 | { |
212 | 0 | req->proceed_req.bytes_inflight = SIZE_MAX; |
213 | 0 | req->proceed_req.cb(&req->super, errstr); |
214 | 0 | } |
215 | | |
216 | | static const char *get_connection_close_error(quicly_conn_t *conn) |
217 | 0 | { |
218 | 0 | if (quicly_get_state(conn) >= QUICLY_STATE_CLOSING) { |
219 | 0 | quicly_error_t err = quicly_get_close_reason(conn, NULL, NULL, NULL); |
220 | 0 | if (PTLS_ERROR_GET_CLASS(err) == PTLS_ERROR_CLASS_SELF_ALERT || PTLS_ERROR_GET_CLASS(err) == PTLS_ERROR_CLASS_PEER_ALERT) { |
221 | 0 | switch (PTLS_ERROR_TO_ALERT(err)) { |
222 | 0 | case PTLS_ALERT_BAD_CERTIFICATE: |
223 | 0 | case PTLS_ALERT_CERTIFICATE_REVOKED: |
224 | 0 | case PTLS_ALERT_CERTIFICATE_EXPIRED: |
225 | 0 | case PTLS_ALERT_CERTIFICATE_UNKNOWN: |
226 | 0 | case PTLS_ALERT_UNKNOWN_CA: |
227 | 0 | return h2o_socket_error_ssl_cert_invalid; |
228 | 0 | case PTLS_ALERT_CERTIFICATE_REQUIRED: |
229 | 0 | return h2o_socket_error_ssl_no_cert; |
230 | 0 | case 0: /* error-close with a no-error is not a handshake failure -> map to generic error */ |
231 | 0 | break; |
232 | 0 | default: |
233 | 0 | return h2o_socket_error_ssl_handshake; |
234 | 0 | } |
235 | 0 | } |
236 | 0 | } |
237 | 0 | return h2o_httpclient_error_io; |
238 | 0 | } |
239 | | |
240 | | static void report_pending_requests_error(struct st_h2o_httpclient__h3_conn_t *conn, const char *errstr) |
241 | 0 | { |
242 | 0 | assert(errstr != NULL); |
243 | 0 | if (h2o_linklist_is_linked(&conn->link)) |
244 | 0 | h2o_linklist_unlink(&conn->link); |
245 | 0 | while (!h2o_linklist_is_empty(&conn->pending_requests)) { |
246 | 0 | struct st_h2o_http3client_req_t *req = |
247 | 0 | H2O_STRUCT_FROM_MEMBER(struct st_h2o_http3client_req_t, link, conn->pending_requests.next); |
248 | 0 | h2o_linklist_unlink(&req->link); |
249 | 0 | req->super._cb.on_connect(&req->super, errstr, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); |
250 | 0 | destroy_request(req); |
251 | 0 | } |
252 | 0 | assert(h2o_linklist_is_empty(&conn->pending_requests)); |
253 | 0 | } |
254 | | |
255 | | static void destroy_connection(struct st_h2o_httpclient__h3_conn_t *conn, const char *errstr) |
256 | 0 | { |
257 | 0 | assert(errstr != NULL); |
258 | 0 | report_pending_requests_error(conn, errstr); |
259 | 0 | if (conn->getaddr_req != NULL) |
260 | 0 | h2o_hostinfo_getaddr_cancel(conn->getaddr_req); |
261 | 0 | h2o_timer_unlink(&conn->timeout); |
262 | 0 | free(conn->server.origin_url.authority.base); |
263 | 0 | free(conn->server.origin_url.host.base); |
264 | 0 | free(conn->handshake_properties.client.session_ticket.base); |
265 | 0 | h2o_http3_dispose_conn(&conn->super); |
266 | 0 | free(conn); |
267 | 0 | } |
268 | | |
269 | | static void destroy_connection_on_transport_close(h2o_quic_conn_t *_conn) |
270 | 0 | { |
271 | 0 | struct st_h2o_httpclient__h3_conn_t *conn = (void *)_conn; |
272 | 0 | destroy_connection(conn, h2o_httpclient_error_io); |
273 | 0 | } |
274 | | |
275 | | static void on_connect_timeout(h2o_timer_t *timeout) |
276 | 0 | { |
277 | 0 | struct st_h2o_httpclient__h3_conn_t *conn = H2O_STRUCT_FROM_MEMBER(struct st_h2o_httpclient__h3_conn_t, timeout, timeout); |
278 | 0 | destroy_connection(conn, h2o_httpclient_error_connect_timeout); |
279 | 0 | } |
280 | | |
281 | | static void start_connect(struct st_h2o_httpclient__h3_conn_t *conn, struct sockaddr *sa) |
282 | 0 | { |
283 | 0 | quicly_conn_t *qconn; |
284 | 0 | ptls_iovec_t address_token = ptls_iovec_init(NULL, 0); |
285 | 0 | quicly_transport_parameters_t resumed_tp; |
286 | 0 | quicly_error_t ret; |
287 | |
|
288 | 0 | assert(conn->super.super.quic == NULL); |
289 | 0 | assert(conn->getaddr_req == NULL); |
290 | 0 | assert(h2o_timer_is_linked(&conn->timeout)); |
291 | 0 | assert(conn->timeout.cb == on_connect_timeout); |
292 | | |
293 | | /* create QUIC connection context and attach */ |
294 | 0 | if (conn->ctx->http3->load_session != NULL) { |
295 | 0 | if (!conn->ctx->http3->load_session(conn->ctx, sa, conn->server.origin_url.host.base, &address_token, |
296 | 0 | &conn->handshake_properties.client.session_ticket, &resumed_tp)) |
297 | 0 | goto Fail; |
298 | 0 | } |
299 | 0 | assert(conn->ctx->http3->h3.next_cid != NULL && "to identify connections, next_cid must be set"); |
300 | 0 | if ((ret = quicly_connect(&qconn, &conn->ctx->http3->quic, conn->server.origin_url.host.base, sa, NULL, |
301 | 0 | conn->ctx->http3->h3.next_cid, address_token, &conn->handshake_properties, |
302 | 0 | conn->handshake_properties.client.session_ticket.base != NULL ? &resumed_tp : NULL, NULL)) != 0) { |
303 | 0 | conn->super.super.quic = NULL; /* just in case */ |
304 | 0 | goto Fail; |
305 | 0 | } |
306 | 0 | ++conn->ctx->http3->h3.next_cid->master_id; /* FIXME check overlap */ |
307 | 0 | if ((ret = h2o_http3_setup(&conn->super, qconn)) != 0) |
308 | 0 | goto Fail; |
309 | | |
310 | 0 | if (quicly_connection_is_ready(conn->super.super.quic)) |
311 | 0 | start_pending_requests(conn); |
312 | |
|
313 | 0 | h2o_quic_send(&conn->super.super); |
314 | |
|
315 | 0 | free(address_token.base); |
316 | 0 | return; |
317 | 0 | Fail: |
318 | 0 | free(address_token.base); |
319 | 0 | destroy_connection(conn, h2o_httpclient_error_internal); |
320 | 0 | } |
321 | | |
322 | | static void on_getaddr(h2o_hostinfo_getaddr_req_t *getaddr_req, const char *errstr, struct addrinfo *res, void *_conn) |
323 | 0 | { |
324 | 0 | struct st_h2o_httpclient__h3_conn_t *conn = _conn; |
325 | |
|
326 | 0 | assert(getaddr_req == conn->getaddr_req); |
327 | 0 | conn->getaddr_req = NULL; |
328 | |
|
329 | 0 | if (errstr != NULL) { |
330 | 0 | destroy_connection(conn, errstr); |
331 | 0 | return; |
332 | 0 | } |
333 | | |
334 | | /* TODO implement eyeballing (and on the TCP-side too) */ |
335 | 0 | struct addrinfo *selected = h2o_hostinfo_select_one(res); |
336 | 0 | start_connect(conn, selected->ai_addr); |
337 | 0 | } |
338 | | |
339 | | static void handle_control_stream_frame(h2o_http3_conn_t *_conn, uint64_t type, const uint8_t *payload, size_t len) |
340 | 0 | { |
341 | 0 | struct st_h2o_httpclient__h3_conn_t *conn = (void *)_conn; |
342 | 0 | quicly_error_t err; |
343 | 0 | const char *err_desc = NULL; |
344 | |
|
345 | 0 | if (!h2o_http3_has_received_settings(&conn->super)) { |
346 | 0 | if (type != H2O_HTTP3_FRAME_TYPE_SETTINGS) { |
347 | 0 | err = H2O_HTTP3_ERROR_MISSING_SETTINGS; |
348 | 0 | goto Fail; |
349 | 0 | } |
350 | 0 | if ((err = h2o_http3_handle_settings_frame(&conn->super, payload, len, &err_desc)) != 0) |
351 | 0 | goto Fail; |
352 | 0 | assert(h2o_http3_has_received_settings(&conn->super)); |
353 | | /* issue requests (unless it has been done already due to 0-RTT key being available) */ |
354 | 0 | start_pending_requests(conn); |
355 | 0 | } else { |
356 | 0 | switch (type) { |
357 | 0 | case H2O_HTTP3_FRAME_TYPE_SETTINGS: |
358 | 0 | err = H2O_HTTP3_ERROR_FRAME_UNEXPECTED; |
359 | 0 | err_desc = "unexpected SETTINGS frame"; |
360 | 0 | goto Fail; |
361 | 0 | case H2O_HTTP3_FRAME_TYPE_GOAWAY: { |
362 | 0 | h2o_http3_goaway_frame_t frame; |
363 | 0 | if ((err = h2o_http3_decode_goaway_frame(&frame, payload, len, &err_desc)) != 0) |
364 | 0 | goto Fail; |
365 | | /* FIXME: stop issuing new requests */ |
366 | 0 | break; |
367 | 0 | } |
368 | 0 | default: |
369 | 0 | break; |
370 | 0 | } |
371 | 0 | } |
372 | | |
373 | 0 | return; |
374 | 0 | Fail: |
375 | 0 | h2o_quic_close_connection(&conn->super.super, err, err_desc); |
376 | 0 | } |
377 | | |
378 | | struct st_h2o_httpclient__h3_conn_t *create_connection(h2o_httpclient_ctx_t *ctx, h2o_httpclient_connection_pool_t *pool, |
379 | | h2o_url_t *origin) |
380 | 0 | { |
381 | | /* FIXME When using a non-global socket pool, let the socket pool load balance H3 connections among the list of targets being |
382 | | * available. But until then, we use the first entry. */ |
383 | 0 | if (!h2o_socketpool_is_global(pool->socketpool)) |
384 | 0 | origin = &pool->socketpool->targets.entries[0]->url; |
385 | |
|
386 | 0 | static const h2o_http3_conn_callbacks_t callbacks = {{destroy_connection_on_transport_close}, handle_control_stream_frame}; |
387 | 0 | static const h2o_http3_qpack_context_t qpack_ctx = {0 /* TODO */}; |
388 | |
|
389 | 0 | struct st_h2o_httpclient__h3_conn_t *conn = h2o_mem_alloc(sizeof(*conn)); |
390 | |
|
391 | 0 | h2o_http3_init_conn(&conn->super, &ctx->http3->h3, &callbacks, &qpack_ctx, ctx->http3->max_frame_payload_size); |
392 | 0 | memset((char *)conn + sizeof(conn->super), 0, sizeof(*conn) - sizeof(conn->super)); |
393 | 0 | conn->ctx = ctx; |
394 | 0 | h2o_url_copy(NULL, &conn->server.origin_url, origin); |
395 | 0 | sprintf(conn->server.named_serv, "%" PRIu16, h2o_url_get_port(origin)); |
396 | 0 | conn->handshake_properties.client.negotiated_protocols.list = h2o_http3_alpn; |
397 | 0 | conn->handshake_properties.client.negotiated_protocols.count = sizeof(h2o_http3_alpn) / sizeof(h2o_http3_alpn[0]); |
398 | 0 | h2o_linklist_insert(&pool->http3.conns, &conn->link); |
399 | 0 | h2o_linklist_init_anchor(&conn->pending_requests); |
400 | |
|
401 | 0 | conn->getaddr_req = h2o_hostinfo_getaddr(conn->ctx->getaddr_receiver, conn->server.origin_url.host, |
402 | 0 | h2o_iovec_init(conn->server.named_serv, strlen(conn->server.named_serv)), |
403 | 0 | pool->socketpool->address_family, SOCK_DGRAM, IPPROTO_UDP, |
404 | 0 | AI_ADDRCONFIG | AI_NUMERICSERV, on_getaddr, conn); |
405 | 0 | h2o_timer_link(conn->ctx->loop, conn->ctx->connect_timeout, &conn->timeout); |
406 | 0 | conn->timeout.cb = on_connect_timeout; |
407 | |
|
408 | 0 | return conn; |
409 | 0 | } |
410 | | |
411 | | static void notify_response_error(struct st_h2o_http3client_req_t *req, const char *errstr) |
412 | 0 | { |
413 | 0 | assert(errstr != NULL); |
414 | | |
415 | 0 | switch (req->response_state) { |
416 | 0 | case H2O_HTTP3CLIENT_RESPONSE_STATE_HEAD: |
417 | 0 | req->super._cb.on_head(&req->super, errstr, NULL); |
418 | 0 | break; |
419 | 0 | case H2O_HTTP3CLIENT_RESPONSE_STATE_BODY: |
420 | 0 | req->super._cb.on_body(&req->super, errstr, NULL, 0); |
421 | 0 | break; |
422 | 0 | default: |
423 | 0 | break; |
424 | 0 | } |
425 | 0 | req->response_state = H2O_HTTP3CLIENT_RESPONSE_STATE_CLOSED; |
426 | 0 | } |
427 | | |
428 | | static int call_on_body(struct st_h2o_http3client_req_t *req, const char *errstr) |
429 | 0 | { |
430 | 0 | assert(req->response_state == H2O_HTTP3CLIENT_RESPONSE_STATE_BODY); |
431 | | |
432 | 0 | int ret = req->super._cb.on_body(&req->super, errstr, NULL, 0); |
433 | 0 | if (errstr != NULL) |
434 | 0 | req->response_state = H2O_HTTP3CLIENT_RESPONSE_STATE_CLOSED; |
435 | |
|
436 | 0 | return ret; |
437 | 0 | } |
438 | | |
439 | | static quicly_error_t handle_input_data_payload(struct st_h2o_http3client_req_t *req, const uint8_t **src, const uint8_t *src_end, |
440 | | quicly_error_t err, const char **err_desc) |
441 | 0 | { |
442 | | /* save data, update states */ |
443 | 0 | if (req->bytes_left_in_data_frame != 0) { |
444 | 0 | size_t payload_bytes = req->bytes_left_in_data_frame; |
445 | 0 | if (src_end - *src < payload_bytes) |
446 | 0 | payload_bytes = src_end - *src; |
447 | 0 | h2o_buffer_append(&req->recvbuf.body, *src, payload_bytes); |
448 | 0 | *src += payload_bytes; |
449 | 0 | req->bytes_left_in_data_frame -= payload_bytes; |
450 | 0 | } |
451 | 0 | if (req->bytes_left_in_data_frame == 0) |
452 | 0 | req->handle_input = handle_input_expect_data_frame; |
453 | | |
454 | | /* call the handler */ |
455 | 0 | const char *errstr = NULL; |
456 | 0 | if (*src == src_end && err != 0) { |
457 | | /* FIXME also check content-length? see what other protocol handlers do */ |
458 | 0 | errstr = err == ERROR_EOS && req->bytes_left_in_data_frame == 0 ? h2o_httpclient_error_is_eos : h2o_httpclient_error_io; |
459 | 0 | } |
460 | 0 | if (call_on_body(req, errstr) != 0) |
461 | 0 | return H2O_HTTP3_ERROR_INTERNAL; |
462 | | |
463 | 0 | return 0; |
464 | 0 | } |
465 | | |
466 | | quicly_error_t handle_input_expect_data_frame(struct st_h2o_http3client_req_t *req, const uint8_t **src, const uint8_t *src_end, |
467 | | quicly_error_t err, const char **err_desc) |
468 | 0 | { |
469 | 0 | assert(req->bytes_left_in_data_frame == 0); |
470 | 0 | if (*src == src_end) { |
471 | | /* return early if no input, no state change */ |
472 | 0 | if (err == 0) |
473 | 0 | return 0; |
474 | | /* either EOS or an unexpected close; delegate the task to the payload processing function */ |
475 | 0 | } else { |
476 | | /* otherwise, read the frame */ |
477 | 0 | h2o_http3_read_frame_t frame; |
478 | 0 | quicly_error_t ret; |
479 | 0 | if ((ret = h2o_http3_read_frame(&frame, 1, H2O_HTTP3_STREAM_TYPE_REQUEST, req->conn->super.max_frame_payload_size, src, |
480 | 0 | src_end, err_desc)) != 0) { |
481 | | /* incomplete */ |
482 | 0 | if (ret == H2O_HTTP3_ERROR_INCOMPLETE && err == 0) |
483 | 0 | return ret; |
484 | 0 | call_on_body(req, h2o_httpclient_error_malformed_frame); |
485 | 0 | return ret; |
486 | 0 | } |
487 | 0 | switch (frame.type) { |
488 | 0 | case H2O_HTTP3_FRAME_TYPE_DATA: |
489 | 0 | break; |
490 | 0 | case H2O_HTTP3_FRAME_TYPE_HEADERS: |
491 | 0 | if (req->super.upgrade_to != NULL) |
492 | 0 | return H2O_HTTP3_ERROR_FRAME_UNEXPECTED; |
493 | | /* flow continues */ |
494 | 0 | default: |
495 | | /* FIXME handle push_promise, trailers */ |
496 | 0 | return 0; |
497 | 0 | } |
498 | 0 | req->bytes_left_in_data_frame = frame.length; |
499 | 0 | } |
500 | | |
501 | | /* unexpected close of DATA frame is handled by handle_input_data_payload. We rely on the function to detect if the DATA frame |
502 | | * is closed right after the frame header */ |
503 | 0 | req->handle_input = handle_input_data_payload; |
504 | 0 | return handle_input_data_payload(req, src, src_end, err, err_desc); |
505 | 0 | } |
506 | | |
507 | | static quicly_error_t handle_input_expect_headers(struct st_h2o_http3client_req_t *req, const uint8_t **src, const uint8_t *src_end, |
508 | | quicly_error_t err, const char **err_desc) |
509 | 0 | { |
510 | 0 | h2o_http3_read_frame_t frame; |
511 | 0 | int status; |
512 | 0 | h2o_headers_t headers = {NULL}; |
513 | 0 | h2o_iovec_t datagram_flow_id = {}; |
514 | 0 | uint8_t header_ack[H2O_HPACK_ENCODE_INT_MAX_LENGTH]; |
515 | 0 | size_t header_ack_len; |
516 | 0 | int frame_is_eos; |
517 | 0 | quicly_error_t ret; |
518 | | |
519 | | /* read HEADERS frame */ |
520 | 0 | if ((ret = h2o_http3_read_frame(&frame, 1, H2O_HTTP3_STREAM_TYPE_REQUEST, req->conn->super.max_frame_payload_size, src, src_end, |
521 | 0 | err_desc)) != 0) { |
522 | 0 | if (ret == H2O_HTTP3_ERROR_INCOMPLETE) { |
523 | 0 | if (err != 0) { |
524 | 0 | notify_response_error(req, h2o_httpclient_error_io); |
525 | 0 | return 0; |
526 | 0 | } |
527 | 0 | return ret; |
528 | 0 | } |
529 | 0 | notify_response_error(req, "response header too large"); |
530 | 0 | return H2O_HTTP3_ERROR_EXCESSIVE_LOAD; /* FIXME correct code? */ |
531 | 0 | } |
532 | 0 | frame_is_eos = *src == src_end && err != 0; |
533 | 0 | if (frame.type != H2O_HTTP3_FRAME_TYPE_HEADERS) { |
534 | 0 | switch (frame.type) { |
535 | 0 | case H2O_HTTP3_FRAME_TYPE_DATA: |
536 | 0 | *err_desc = "received DATA frame before HEADERS"; |
537 | 0 | return H2O_HTTP3_ERROR_FRAME_UNEXPECTED; |
538 | 0 | default: |
539 | 0 | return 0; |
540 | 0 | } |
541 | 0 | } |
542 | 0 | if ((ret = h2o_qpack_parse_response(req->super.pool, req->conn->super.qpack.dec, req->quic->stream_id, &status, &headers, |
543 | 0 | &datagram_flow_id, header_ack, &header_ack_len, frame.payload, frame.length, err_desc)) != |
544 | 0 | 0) { |
545 | 0 | if (ret == H2O_HTTP2_ERROR_INCOMPLETE) { |
546 | | /* the request is blocked by the QPACK stream */ |
547 | 0 | req->handle_input = NULL; /* FIXME */ |
548 | 0 | return 0; |
549 | 0 | } |
550 | 0 | if (*err_desc == NULL) |
551 | 0 | *err_desc = "qpack error"; |
552 | 0 | notify_response_error(req, *err_desc); |
553 | 0 | return H2O_HTTP3_ERROR_GENERAL_PROTOCOL; /* FIXME */ |
554 | 0 | } |
555 | 0 | if (header_ack_len != 0) |
556 | 0 | h2o_http3_send_qpack_header_ack(&req->conn->super, header_ack, header_ack_len); |
557 | |
|
558 | 0 | if (datagram_flow_id.base != NULL) { |
559 | 0 | if (!req->offered_datagram_flow_id) { |
560 | 0 | *err_desc = "no offered datagram-flow-id"; |
561 | 0 | return H2O_HTTP3_ERROR_GENERAL_PROTOCOL; |
562 | 0 | } |
563 | | /* TODO validate the returned value */ |
564 | 0 | } |
565 | | |
566 | | /* handle 1xx */ |
567 | 0 | if (100 <= status && status <= 199) { |
568 | 0 | if (status == 101) { |
569 | 0 | *err_desc = "unexpected 101"; |
570 | 0 | notify_response_error(req, *err_desc); |
571 | 0 | return H2O_HTTP3_ERROR_GENERAL_PROTOCOL; |
572 | 0 | } |
573 | 0 | if (frame_is_eos) { |
574 | 0 | notify_response_error(req, h2o_httpclient_error_io); |
575 | 0 | return 0; |
576 | 0 | } |
577 | 0 | if (req->super.informational_cb != NULL && |
578 | 0 | req->super.informational_cb(&req->super, 0x300, status, h2o_iovec_init(NULL, 0), headers.entries, headers.size) != 0) { |
579 | 0 | return H2O_HTTP3_ERROR_INTERNAL; |
580 | 0 | } |
581 | 0 | return 0; |
582 | 0 | } |
583 | | |
584 | | /* handle final response, creating tunnel object if necessary */ |
585 | 0 | h2o_httpclient_on_head_t on_head = {.version = 0x300, |
586 | 0 | .msg = h2o_iovec_init(NULL, 0), |
587 | 0 | .status = status, |
588 | 0 | .headers = headers.entries, |
589 | 0 | .num_headers = headers.size}; |
590 | 0 | if (h2o_httpclient__tunnel_is_ready(&req->super, status, on_head.version)) { |
591 | 0 | on_head.forward_datagram.write_ = write_datagrams; |
592 | 0 | on_head.forward_datagram.read_ = &req->on_read_datagrams; |
593 | 0 | } |
594 | 0 | req->super._cb.on_body = req->super._cb.on_head(&req->super, frame_is_eos ? h2o_httpclient_error_is_eos : NULL, &on_head); |
595 | 0 | req->response_state = H2O_HTTP3CLIENT_RESPONSE_STATE_BODY; |
596 | 0 | if (req->super._cb.on_body == NULL) |
597 | 0 | return frame_is_eos ? 0 : H2O_HTTP3_ERROR_INTERNAL; |
598 | | |
599 | | /* handle body */ |
600 | 0 | req->handle_input = handle_input_expect_data_frame; |
601 | 0 | return 0; |
602 | 0 | } |
603 | | |
604 | | static void on_stream_destroy(quicly_stream_t *qs, quicly_error_t err) |
605 | 0 | { |
606 | 0 | struct st_h2o_http3client_req_t *req; |
607 | |
|
608 | 0 | if ((req = qs->data) == NULL) |
609 | 0 | return; |
610 | 0 | notify_response_error(req, get_connection_close_error(qs->conn)); |
611 | 0 | detach_stream(req); |
612 | 0 | destroy_request(req); |
613 | 0 | } |
614 | | |
615 | | static void on_send_shift(quicly_stream_t *qs, size_t delta) |
616 | 0 | { |
617 | 0 | struct st_h2o_http3client_req_t *req = qs->data; |
618 | |
|
619 | 0 | assert(req != NULL); |
620 | 0 | h2o_buffer_consume(&req->sendbuf, delta); |
621 | 0 | } |
622 | | |
623 | | static void on_send_emit(quicly_stream_t *qs, size_t off, void *dst, size_t *len, int *wrote_all) |
624 | 0 | { |
625 | 0 | struct st_h2o_http3client_req_t *req = qs->data; |
626 | |
|
627 | 0 | if (*len >= req->sendbuf->size - off) { |
628 | 0 | *len = req->sendbuf->size - off; |
629 | 0 | *wrote_all = 1; |
630 | 0 | } else { |
631 | 0 | *wrote_all = 0; |
632 | 0 | } |
633 | 0 | memcpy(dst, req->sendbuf->bytes + off, *len); |
634 | |
|
635 | 0 | if (*wrote_all && req->proceed_req.bytes_inflight != SIZE_MAX) |
636 | 0 | call_proceed_req(req, NULL); |
637 | 0 | } |
638 | | |
639 | | static void on_send_stop(quicly_stream_t *qs, quicly_error_t err) |
640 | 0 | { |
641 | 0 | struct st_h2o_http3client_req_t *req; |
642 | |
|
643 | 0 | if ((req = qs->data) == NULL) |
644 | 0 | return; |
645 | | |
646 | 0 | if (!quicly_sendstate_transfer_complete(&req->quic->sendstate)) |
647 | 0 | quicly_reset_stream(req->quic, err); |
648 | |
|
649 | 0 | if (req->proceed_req.bytes_inflight != SIZE_MAX) |
650 | 0 | call_proceed_req(req, h2o_httpclient_error_io /* TODO better error code? */); |
651 | |
|
652 | 0 | if (!quicly_recvstate_transfer_complete(&req->quic->recvstate)) { |
653 | 0 | quicly_request_stop(req->quic, H2O_HTTP3_ERROR_REQUEST_CANCELLED); |
654 | 0 | notify_response_error(req, h2o_httpclient_error_io); |
655 | 0 | } |
656 | 0 | detach_stream(req); |
657 | 0 | destroy_request(req); |
658 | 0 | } |
659 | | |
660 | | static quicly_error_t on_receive_process_bytes(struct st_h2o_http3client_req_t *req, const uint8_t **src, const uint8_t *src_end, |
661 | | const char **err_desc) |
662 | 0 | { |
663 | 0 | int is_eos = quicly_recvstate_transfer_complete(&req->quic->recvstate); |
664 | 0 | assert(is_eos || *src != src_end); |
665 | 0 | quicly_error_t ret; |
666 | |
|
667 | 0 | do { |
668 | 0 | if ((ret = req->handle_input(req, src, src_end, is_eos ? ERROR_EOS : 0, err_desc)) != 0) { |
669 | 0 | if (ret == H2O_HTTP3_ERROR_INCOMPLETE) |
670 | 0 | ret = is_eos ? H2O_HTTP3_ERROR_FRAME : 0; |
671 | 0 | break; |
672 | 0 | } |
673 | 0 | } while (*src != src_end); |
674 | |
|
675 | 0 | return ret; |
676 | 0 | } |
677 | | |
678 | | static void on_receive(quicly_stream_t *qs, size_t off, const void *input, size_t len) |
679 | 0 | { |
680 | 0 | struct st_h2o_http3client_req_t *req = qs->data; |
681 | 0 | size_t bytes_consumed; |
682 | 0 | quicly_error_t err = 0; |
683 | 0 | const char *err_desc = NULL; |
684 | | |
685 | | /* process the input, update stream-level receive buffer */ |
686 | 0 | if (req->recvbuf.stream->size == 0 && off == 0) { |
687 | | |
688 | | /* fast path; process the input directly, save the remaining bytes */ |
689 | 0 | const uint8_t *src = input; |
690 | 0 | err = on_receive_process_bytes(req, &src, src + len, &err_desc); |
691 | 0 | bytes_consumed = src - (const uint8_t *)input; |
692 | 0 | if (bytes_consumed != len) |
693 | 0 | h2o_buffer_append(&req->recvbuf.stream, src, len - bytes_consumed); |
694 | 0 | } else { |
695 | | /* slow path; copy data to partial_frame */ |
696 | 0 | size_t size_required = off + len; |
697 | 0 | if (req->recvbuf.stream->size < size_required) { |
698 | 0 | h2o_buffer_reserve(&req->recvbuf.stream, size_required - req->recvbuf.stream->size); |
699 | 0 | req->recvbuf.stream->size = size_required; |
700 | 0 | } |
701 | 0 | memcpy(req->recvbuf.stream->bytes + off, input, len); |
702 | | |
703 | | /* just return if no new data is available */ |
704 | 0 | size_t bytes_available = quicly_recvstate_bytes_available(&req->quic->recvstate); |
705 | 0 | if (req->recvbuf.prev_bytes_available == bytes_available) |
706 | 0 | return; |
707 | | |
708 | | /* process the bytes that have not been processed, update stream-level buffer */ |
709 | 0 | const uint8_t *src = (const uint8_t *)req->recvbuf.stream->bytes; |
710 | 0 | err = on_receive_process_bytes(req, &src, (const uint8_t *)req->recvbuf.stream->bytes + bytes_available, &err_desc); |
711 | 0 | bytes_consumed = src - (const uint8_t *)req->recvbuf.stream->bytes; |
712 | 0 | h2o_buffer_consume(&req->recvbuf.stream, bytes_consumed); |
713 | 0 | } |
714 | | |
715 | | /* update QUIC stream-level state */ |
716 | 0 | if (bytes_consumed != 0) |
717 | 0 | quicly_stream_sync_recvbuf(req->quic, bytes_consumed); |
718 | 0 | req->recvbuf.prev_bytes_available = quicly_recvstate_bytes_available(&req->quic->recvstate); |
719 | | |
720 | | /* cleanup */ |
721 | 0 | if (quicly_recvstate_transfer_complete(&req->quic->recvstate)) { |
722 | | /* destroy the request if send-side is already closed, otherwise wait until the send-side gets closed */ |
723 | 0 | if (quicly_sendstate_transfer_complete(&req->quic->sendstate)) { |
724 | 0 | detach_stream(req); |
725 | 0 | destroy_request(req); |
726 | 0 | } |
727 | 0 | } else if (err != 0) { |
728 | 0 | notify_response_error(req, h2o_httpclient_error_io); |
729 | 0 | int send_is_open = quicly_sendstate_is_open(&req->quic->sendstate); |
730 | 0 | close_stream(req, err); |
731 | | /* immediately dispose of the request if possible, or wait for the send-side to close */ |
732 | 0 | if (!send_is_open) { |
733 | 0 | destroy_request(req); |
734 | 0 | } else if (req->proceed_req.bytes_inflight != SIZE_MAX) { |
735 | 0 | call_proceed_req(req, h2o_httpclient_error_io); |
736 | 0 | destroy_request(req); |
737 | 0 | } else { |
738 | | /* wait for write_req to be called */ |
739 | 0 | } |
740 | 0 | } |
741 | 0 | } |
742 | | |
743 | | static void on_receive_reset(quicly_stream_t *qs, quicly_error_t err) |
744 | 0 | { |
745 | 0 | struct st_h2o_http3client_req_t *req = qs->data; |
746 | |
|
747 | 0 | notify_response_error(req, h2o_httpclient_error_io); |
748 | 0 | close_stream(req, H2O_HTTP3_ERROR_REQUEST_CANCELLED); |
749 | 0 | destroy_request(req); |
750 | 0 | } |
751 | | |
752 | | void start_request(struct st_h2o_http3client_req_t *req) |
753 | 0 | { |
754 | 0 | h2o_iovec_t method; |
755 | 0 | h2o_url_t url; |
756 | 0 | const h2o_header_t *headers; |
757 | 0 | size_t num_headers; |
758 | 0 | h2o_iovec_t body; |
759 | 0 | h2o_httpclient_properties_t props = {NULL}; |
760 | 0 | char datagram_flow_id_buf[sizeof(H2O_UINT64_LONGEST_STR)]; |
761 | 0 | quicly_error_t ret; |
762 | |
|
763 | 0 | assert(req->quic == NULL); |
764 | 0 | assert(!h2o_linklist_is_linked(&req->link)); |
765 | | |
766 | 0 | if ((req->super._cb.on_head = req->super._cb.on_connect(&req->super, NULL, &method, &url, &headers, &num_headers, &body, |
767 | 0 | &req->proceed_req.cb, &props, &req->conn->server.origin_url)) == NULL) { |
768 | 0 | destroy_request(req); |
769 | 0 | return; |
770 | 0 | } |
771 | | |
772 | 0 | if ((ret = quicly_open_stream(req->conn->super.super.quic, &req->quic, 0)) != 0) { |
773 | 0 | notify_response_error(req, "failed to open stream"); |
774 | 0 | destroy_request(req); |
775 | 0 | return; |
776 | 0 | } |
777 | 0 | req->quic->data = req; |
778 | | |
779 | | /* send request (TODO optimize) */ |
780 | 0 | h2o_iovec_t protocol = {}; |
781 | 0 | h2o_iovec_t datagram_flow_id = {}; |
782 | 0 | if (req->super.upgrade_to == h2o_httpclient_upgrade_to_connect && |
783 | 0 | h2o_memis(method.base, method.len, H2O_STRLIT("CONNECT-UDP")) && req->conn->super.peer_settings.h3_datagram) { |
784 | 0 | datagram_flow_id.len = sprintf(datagram_flow_id_buf, "%" PRIu64, req->quic->stream_id); |
785 | 0 | datagram_flow_id.base = datagram_flow_id_buf; |
786 | 0 | req->offered_datagram_flow_id = 1; |
787 | 0 | } else if (req->super.upgrade_to != NULL && req->super.upgrade_to != h2o_httpclient_upgrade_to_connect) { |
788 | 0 | protocol = h2o_iovec_init(req->super.upgrade_to, strlen(req->super.upgrade_to)); |
789 | 0 | } |
790 | 0 | h2o_iovec_t headers_frame = |
791 | 0 | h2o_qpack_flatten_request(req->conn->super.qpack.enc, req->super.pool, req->quic->stream_id, NULL, method, url.scheme, |
792 | 0 | url.authority, url.path, protocol, headers, num_headers, datagram_flow_id); |
793 | 0 | h2o_buffer_append(&req->sendbuf, headers_frame.base, headers_frame.len); |
794 | 0 | if (body.len != 0) |
795 | 0 | emit_data(req, body); |
796 | 0 | if (req->proceed_req.cb != NULL) { |
797 | 0 | req->super.write_req = do_write_req; |
798 | 0 | req->proceed_req.bytes_inflight = body.len; |
799 | 0 | } |
800 | 0 | if (req->proceed_req.cb == NULL && req->super.upgrade_to == NULL) |
801 | 0 | quicly_sendstate_shutdown(&req->quic->sendstate, req->sendbuf->size); |
802 | 0 | quicly_stream_sync_sendbuf(req->quic, 1); |
803 | |
|
804 | 0 | req->handle_input = handle_input_expect_headers; |
805 | 0 | } |
806 | | |
807 | | static void cancel_request(h2o_httpclient_t *_client) |
808 | 0 | { |
809 | 0 | struct st_h2o_http3client_req_t *req = (void *)_client; |
810 | 0 | if (req->quic != NULL) |
811 | 0 | close_stream(req, H2O_HTTP3_ERROR_REQUEST_CANCELLED); |
812 | 0 | destroy_request(req); |
813 | 0 | } |
814 | | |
815 | | static void do_get_conn_properties(h2o_httpclient_t *_client, h2o_httpclient_conn_properties_t *properties) |
816 | 0 | { |
817 | 0 | struct st_h2o_http3client_req_t *req = (void *)_client; |
818 | 0 | ptls_t *tls; |
819 | 0 | ptls_cipher_suite_t *cipher; |
820 | |
|
821 | 0 | if (req->quic != NULL && (tls = quicly_get_tls(req->quic->conn), (cipher = ptls_get_cipher(tls)) != NULL)) { |
822 | 0 | properties->ssl.protocol_version = "TLSv1.3"; |
823 | 0 | properties->ssl.session_reused = ptls_is_psk_handshake(tls); |
824 | 0 | properties->ssl.cipher = cipher->name; |
825 | 0 | properties->ssl.cipher_bits = (int)cipher->aead->key_size; |
826 | 0 | } else { |
827 | 0 | properties->ssl.protocol_version = NULL; |
828 | 0 | properties->ssl.session_reused = -1; |
829 | 0 | properties->ssl.cipher = NULL; |
830 | 0 | properties->ssl.cipher_bits = 0; |
831 | 0 | } |
832 | 0 | properties->sock = NULL; |
833 | 0 | } |
834 | | |
835 | | static void do_update_window(h2o_httpclient_t *_client) |
836 | 0 | { |
837 | | /* TODO Stop receiving data for the stream when `buf` grows to certain extent. Then, resume when this function is being called. |
838 | | */ |
839 | 0 | } |
840 | | |
841 | | int do_write_req(h2o_httpclient_t *_client, h2o_iovec_t chunk, int is_end_stream) |
842 | 0 | { |
843 | 0 | struct st_h2o_http3client_req_t *req = (void *)_client; |
844 | |
|
845 | 0 | assert(req->proceed_req.bytes_inflight == SIZE_MAX); |
846 | | |
847 | | /* Notify error to the application, if the stream has already been closed (due to e.g., a stream error) or if the send-side has |
848 | | * been closed (due to STOP_SENDING). Also, destroy the request if the receive side has already been closed. */ |
849 | 0 | if (req->quic == NULL || !quicly_sendstate_is_open(&req->quic->sendstate)) { |
850 | 0 | if (req->quic != NULL && quicly_recvstate_transfer_complete(&req->quic->recvstate)) |
851 | 0 | close_stream(req, H2O_HTTP3_ERROR_REQUEST_CANCELLED); |
852 | 0 | if (req->quic == NULL) |
853 | 0 | destroy_request(req); |
854 | 0 | return 1; |
855 | 0 | } |
856 | | |
857 | 0 | emit_data(req, chunk); |
858 | | |
859 | | /* shutdown if we've written all request body */ |
860 | 0 | if (is_end_stream) { |
861 | 0 | assert(quicly_sendstate_is_open(&req->quic->sendstate)); |
862 | 0 | quicly_sendstate_shutdown(&req->quic->sendstate, req->quic->sendstate.acked.ranges[0].end + req->sendbuf->size); |
863 | 0 | } else { |
864 | 0 | assert(chunk.len != 0); |
865 | 0 | } |
866 | | |
867 | 0 | req->proceed_req.bytes_inflight = chunk.len; |
868 | 0 | quicly_stream_sync_sendbuf(req->quic, 1); |
869 | 0 | h2o_quic_schedule_timer(&req->conn->super.super); |
870 | 0 | return 0; |
871 | 0 | } |
872 | | |
873 | | void h2o_httpclient__connect_h3(h2o_httpclient_t **_client, h2o_mem_pool_t *pool, void *data, h2o_httpclient_ctx_t *ctx, |
874 | | h2o_httpclient_connection_pool_t *connpool, h2o_url_t *target, const char *upgrade_to, |
875 | | h2o_httpclient_connect_cb cb) |
876 | 0 | { |
877 | 0 | struct st_h2o_httpclient__h3_conn_t *conn; |
878 | 0 | struct st_h2o_http3client_req_t *req; |
879 | |
|
880 | 0 | if ((conn = find_connection(connpool, target)) == NULL) |
881 | 0 | conn = create_connection(ctx, connpool, target); |
882 | |
|
883 | 0 | req = h2o_mem_alloc(sizeof(*req)); |
884 | 0 | *req = (struct st_h2o_http3client_req_t){ |
885 | 0 | .super = {pool, |
886 | 0 | ctx, |
887 | 0 | connpool, |
888 | 0 | &req->recvbuf.body, |
889 | 0 | data, |
890 | 0 | NULL, |
891 | 0 | {h2o_gettimeofday(ctx->loop)}, |
892 | 0 | upgrade_to, |
893 | 0 | {0}, |
894 | 0 | {0}, |
895 | 0 | cancel_request, |
896 | 0 | do_get_conn_properties, |
897 | 0 | do_update_window}, |
898 | 0 | .conn = conn, |
899 | 0 | .proceed_req = {.cb = NULL, .bytes_inflight = SIZE_MAX}, |
900 | 0 | }; |
901 | 0 | req->super._cb.on_connect = cb; |
902 | 0 | h2o_buffer_init(&req->sendbuf, &h2o_socket_buffer_prototype); |
903 | 0 | h2o_buffer_init(&req->recvbuf.body, &h2o_socket_buffer_prototype); |
904 | 0 | h2o_buffer_init(&req->recvbuf.stream, &h2o_socket_buffer_prototype); |
905 | |
|
906 | 0 | if (_client != NULL) |
907 | 0 | *_client = &req->super; |
908 | |
|
909 | 0 | if (h2o_http3_has_received_settings(&conn->super)) { |
910 | 0 | start_request(req); |
911 | 0 | h2o_quic_schedule_timer(&conn->super.super); |
912 | 0 | } else { |
913 | 0 | h2o_linklist_insert(&conn->pending_requests, &req->link); |
914 | 0 | } |
915 | 0 | } |
916 | | |
917 | | void h2o_httpclient_http3_notify_connection_update(h2o_quic_ctx_t *ctx, h2o_quic_conn_t *_conn) |
918 | 0 | { |
919 | 0 | struct st_h2o_httpclient__h3_conn_t *conn = (void *)_conn; |
920 | |
|
921 | 0 | if (h2o_timer_is_linked(&conn->timeout) && conn->timeout.cb == on_connect_timeout) { |
922 | | /* TODO check connection state? */ |
923 | 0 | h2o_timer_unlink(&conn->timeout); |
924 | 0 | } |
925 | | |
926 | | /* close requests that have not yet been started; the ones already started will have their stream destroy callback |
927 | | * called immediately afterwards */ |
928 | 0 | if (quicly_get_state(conn->super.super.quic) >= QUICLY_STATE_CLOSING) { |
929 | 0 | conn->super.state = H2O_HTTP3_CONN_STATE_IS_CLOSING; |
930 | 0 | report_pending_requests_error(conn, get_connection_close_error(conn->super.super.quic)); |
931 | 0 | } |
932 | 0 | } |
933 | | |
934 | | static quicly_error_t stream_open_cb(quicly_stream_open_t *self, quicly_stream_t *qs) |
935 | 0 | { |
936 | 0 | if (quicly_stream_is_unidirectional(qs->stream_id)) { |
937 | 0 | h2o_http3_on_create_unidirectional_stream(qs); |
938 | 0 | } else { |
939 | 0 | static const quicly_stream_callbacks_t callbacks = {on_stream_destroy, on_send_shift, on_send_emit, |
940 | 0 | on_send_stop, on_receive, on_receive_reset}; |
941 | 0 | assert(quicly_stream_is_client_initiated(qs->stream_id)); |
942 | 0 | qs->callbacks = &callbacks; |
943 | 0 | } |
944 | 0 | return 0; |
945 | 0 | } |
946 | | |
947 | | quicly_stream_open_t h2o_httpclient_http3_on_stream_open = {stream_open_cb}; |
948 | | |
949 | | static void on_receive_datagram_frame(quicly_receive_datagram_frame_t *self, quicly_conn_t *qc, ptls_iovec_t datagram) |
950 | 0 | { |
951 | 0 | struct st_h2o_httpclient__h3_conn_t *conn = |
952 | 0 | H2O_STRUCT_FROM_MEMBER(struct st_h2o_httpclient__h3_conn_t, super, *quicly_get_data(qc)); |
953 | 0 | uint64_t flow_id; |
954 | 0 | h2o_iovec_t payload; |
955 | 0 | quicly_stream_t *qs; |
956 | | |
957 | | /* decode, validate, get stream */ |
958 | 0 | if ((flow_id = h2o_http3_decode_h3_datagram(&payload, datagram.base, datagram.len)) == UINT64_MAX || |
959 | 0 | !(quicly_stream_is_client_initiated(flow_id) && !quicly_stream_is_unidirectional(flow_id))) { |
960 | 0 | h2o_quic_close_connection(&conn->super.super, H2O_HTTP3_ERROR_GENERAL_PROTOCOL, "invalid DATAGRAM frame"); |
961 | 0 | return; |
962 | 0 | } |
963 | 0 | if ((qs = quicly_get_stream(conn->super.super.quic, flow_id)) == NULL) |
964 | 0 | return; |
965 | | |
966 | 0 | struct st_h2o_http3client_req_t *req = qs->data; |
967 | 0 | if (req->on_read_datagrams != NULL) |
968 | 0 | req->on_read_datagrams(&req->super, &payload, 1); |
969 | 0 | } |
970 | | |
971 | | quicly_receive_datagram_frame_t h2o_httpclient_http3_on_receive_datagram_frame = {on_receive_datagram_frame}; |