Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2014-2016 DeNA Co., Ltd., Kazuho Oku, Shota Fukumori, |
3 | | * Fastly, Inc. |
4 | | * |
5 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
6 | | * of this software and associated documentation files (the "Software"), to |
7 | | * deal in the Software without restriction, including without limitation the |
8 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
9 | | * sell copies of the Software, and to permit persons to whom the Software is |
10 | | * furnished to do so, subject to the following conditions: |
11 | | * |
12 | | * The above copyright notice and this permission notice shall be included in |
13 | | * all copies or substantial portions of the Software. |
14 | | * |
15 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
20 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
21 | | * IN THE SOFTWARE. |
22 | | */ |
23 | | #include <inttypes.h> |
24 | | #include <stddef.h> |
25 | | #include <stdio.h> |
26 | | #include <stdlib.h> |
27 | | #include "picohttpparser.h" |
28 | | #include "h2o.h" |
29 | | #include "h2o/http1.h" |
30 | | #include "h2o/http2.h" |
31 | | #include "./probes_.h" |
32 | | |
33 | | enum enum_h2o_http1_ostream_state { |
34 | | OSTREAM_STATE_HEAD, |
35 | | OSTREAM_STATE_BODY, |
36 | | OSTREAM_STATE_DONE, |
37 | | }; |
38 | | |
39 | | struct st_h2o_http1_finalostream_t { |
40 | | h2o_ostream_t super; |
41 | | enum enum_h2o_http1_ostream_state state; |
42 | | char *chunked_buf; /* buffer used for chunked-encoding (NULL unless chunked encoding is used) */ |
43 | | struct { |
44 | | /** |
45 | | * if `h2o_socket_write` is currently writing an informational response |
46 | | */ |
47 | | unsigned write_inflight : 1; |
48 | | /** |
49 | | * buffer used to store informational responses to be sent, when write of an informational response is inflight |
50 | | */ |
51 | | h2o_iovec_vector_t pending; |
52 | | /** |
53 | | * buffer used to delay the execution of `finalostream_send`, when write of an informational respnose is inflight; |
54 | | * availability is indicated by `inbufs != NULL` |
55 | | */ |
56 | | struct { |
57 | | h2o_sendvec_t *inbufs; |
58 | | size_t inbufcnt; |
59 | | h2o_send_state_t send_state; |
60 | | } pending_final; |
61 | | } informational; |
62 | | }; |
63 | | |
64 | | struct st_h2o_http1_conn_t { |
65 | | h2o_conn_t super; |
66 | | h2o_socket_t *sock; |
67 | | h2o_timer_t _timeout_entry; |
68 | | h2o_timer_t _io_timeout_entry; |
69 | | uint64_t _req_index; |
70 | | size_t _prevreqlen; |
71 | | size_t _unconsumed_request_size; |
72 | | struct st_h2o_http1_req_entity_reader *_req_entity_reader; |
73 | | struct st_h2o_http1_finalostream_t _ostr_final; |
74 | | struct { |
75 | | void *data; |
76 | | h2o_http1_upgrade_cb cb; |
77 | | } upgrade; |
78 | | /** |
79 | | * the request body buffer |
80 | | */ |
81 | | h2o_buffer_t *req_body; |
82 | | /** |
83 | | * the HTTP request / response (intentionally placed at the last, since it is a large structure and has it's own ctor) |
84 | | */ |
85 | | h2o_req_t req; |
86 | | }; |
87 | | |
88 | | struct st_h2o_http1_req_entity_reader { |
89 | | void (*handle_incoming_entity)(struct st_h2o_http1_conn_t *conn); |
90 | | }; |
91 | | |
92 | | struct st_h2o_http1_content_length_entity_reader { |
93 | | struct st_h2o_http1_req_entity_reader super; |
94 | | size_t content_length; |
95 | | }; |
96 | | |
97 | | struct st_h2o_http1_chunked_entity_reader { |
98 | | struct st_h2o_http1_req_entity_reader super; |
99 | | struct phr_chunked_decoder decoder; |
100 | | }; |
101 | | |
102 | | static void finalostream_send(h2o_ostream_t *_self, h2o_req_t *req, h2o_sendvec_t *inbufs, size_t inbufcnt, h2o_send_state_t state); |
103 | | static void finalostream_send_informational(h2o_ostream_t *_self, h2o_req_t *req); |
104 | | static void reqread_on_read(h2o_socket_t *sock, const char *err); |
105 | | static void reqread_on_timeout(h2o_timer_t *entry); |
106 | | static void req_io_on_timeout(h2o_timer_t *entry); |
107 | | static void reqread_start(struct st_h2o_http1_conn_t *conn); |
108 | | static int foreach_request(h2o_conn_t *_conn, int (*cb)(h2o_req_t *req, void *cbdata), void *cbdata); |
109 | | |
110 | | static void init_request(struct st_h2o_http1_conn_t *conn) |
111 | 16.2k | { |
112 | 16.2k | if (conn->_req_index != 0) { |
113 | 6.09k | if (conn->req_body != NULL) |
114 | 1.01k | h2o_buffer_dispose(&conn->req_body); |
115 | 6.09k | h2o_dispose_request(&conn->req); |
116 | 6.09k | if (conn->_unconsumed_request_size) |
117 | 5.07k | h2o_buffer_consume(&conn->sock->input, conn->_unconsumed_request_size); |
118 | 6.09k | } |
119 | 16.2k | assert(conn->req_body == NULL); |
120 | 16.2k | h2o_init_request(&conn->req, &conn->super, NULL); |
121 | | |
122 | 16.2k | ++conn->_req_index; |
123 | 16.2k | conn->req._ostr_top = &conn->_ostr_final.super; |
124 | | |
125 | 16.2k | conn->_ostr_final = (struct st_h2o_http1_finalostream_t){{ |
126 | 16.2k | NULL, /* next */ |
127 | 16.2k | finalostream_send, /* do_send */ |
128 | 16.2k | NULL, /* stop */ |
129 | 16.2k | conn->super.ctx->globalconf->send_informational_mode == H2O_SEND_INFORMATIONAL_MODE_ALL ? finalostream_send_informational |
130 | 16.2k | : NULL, /* send_informational */ |
131 | 16.2k | }}; |
132 | 16.2k | } |
133 | | |
134 | | static void close_connection(struct st_h2o_http1_conn_t *conn, int close_socket) |
135 | 10.1k | { |
136 | 10.1k | if (conn->sock != NULL) |
137 | 5.72k | H2O_PROBE_CONN0(H1_CLOSE, &conn->super); |
138 | 10.1k | h2o_timer_unlink(&conn->_timeout_entry); |
139 | 10.1k | h2o_timer_unlink(&conn->_io_timeout_entry); |
140 | 10.1k | if (conn->req_body != NULL) |
141 | 707 | h2o_buffer_dispose(&conn->req_body); |
142 | 10.1k | h2o_dispose_request(&conn->req); |
143 | 10.1k | if (conn->sock != NULL && close_socket) |
144 | 5.60k | h2o_socket_close(conn->sock); |
145 | 10.1k | h2o_destroy_connection(&conn->super); |
146 | 10.1k | } |
147 | | |
148 | | static void cleanup_connection(struct st_h2o_http1_conn_t *conn) |
149 | 9.04k | { |
150 | 9.04k | if (!conn->req.http1_is_persistent) { |
151 | | /* TODO use lingering close */ |
152 | 2.95k | close_connection(conn, 1); |
153 | 2.95k | return; |
154 | 2.95k | } |
155 | | |
156 | 6.09k | assert(conn->req.proceed_req == NULL); |
157 | 6.09k | assert(conn->_req_entity_reader == NULL); |
158 | | |
159 | | /* handle next request */ |
160 | 6.09k | init_request(conn); |
161 | 6.09k | conn->req.write_req.cb = NULL; |
162 | 6.09k | conn->req.write_req.ctx = NULL; |
163 | 6.09k | conn->req.proceed_req = NULL; |
164 | 6.09k | conn->_prevreqlen = 0; |
165 | 6.09k | conn->_unconsumed_request_size = 0; |
166 | | |
167 | 6.09k | if (conn->sock->input->size == 0) |
168 | 1.83k | h2o_conn_set_state(&conn->super, H2O_CONN_STATE_IDLE); |
169 | | |
170 | 6.09k | reqread_start(conn); |
171 | 6.09k | } |
172 | | |
173 | | /** |
174 | | * timer is activated if cb != NULL, disactivated otherwise |
175 | | */ |
176 | | static void set_req_timeout(struct st_h2o_http1_conn_t *conn, uint64_t timeout, h2o_timer_cb cb) |
177 | 26.7k | { |
178 | 26.7k | if (conn->req.is_tunnel_req) |
179 | 189 | cb = NULL; |
180 | 26.7k | if (conn->_timeout_entry.cb != NULL) |
181 | 9.23k | h2o_timer_unlink(&conn->_timeout_entry); |
182 | 26.7k | conn->_timeout_entry.cb = cb; |
183 | 26.7k | if (cb != NULL) |
184 | 17.4k | h2o_timer_link(conn->super.ctx->loop, timeout, &conn->_timeout_entry); |
185 | 26.7k | } |
186 | | |
187 | | static void set_req_io_timeout(struct st_h2o_http1_conn_t *conn, uint64_t timeout, h2o_timer_cb cb) |
188 | 50.2k | { |
189 | 50.2k | if (conn->req.is_tunnel_req) |
190 | 309 | cb = NULL; |
191 | 50.2k | if (conn->_io_timeout_entry.cb != NULL) |
192 | 30.9k | h2o_timer_unlink(&conn->_io_timeout_entry); |
193 | 50.2k | conn->_io_timeout_entry.cb = cb; |
194 | 50.2k | if (cb != NULL) |
195 | 40.8k | h2o_timer_link(conn->super.ctx->loop, timeout, &conn->_io_timeout_entry); |
196 | 50.2k | } |
197 | | |
198 | | static void clear_timeouts(struct st_h2o_http1_conn_t *conn) |
199 | 9.33k | { |
200 | 9.33k | set_req_timeout(conn, 0, NULL); |
201 | 9.33k | set_req_io_timeout(conn, 0, NULL); |
202 | 9.33k | } |
203 | | |
204 | | static void entity_read_do_send_error(struct st_h2o_http1_conn_t *conn, int status, size_t status_error_index, const char *reason, |
205 | | const char *body) |
206 | 230 | { |
207 | 230 | conn->req.proceed_req = NULL; |
208 | 230 | conn->_req_entity_reader = NULL; |
209 | 230 | clear_timeouts(conn); |
210 | 230 | h2o_socket_read_stop(conn->sock); |
211 | | /* FIXME We should check if `h2o_proceed_request` has been called, rather than trying to guess if we have (I'm unsure if the |
212 | | * contract is for h2o_req_t::_generator to become non-NULL immediately after `h2o_proceed_request` is being called). */ |
213 | 230 | if (conn->req._generator == NULL && conn->_ostr_final.state == OSTREAM_STATE_HEAD) { |
214 | 211 | conn->super.ctx->emitted_error_status[status_error_index]++; |
215 | 211 | h2o_send_error_generic(&conn->req, status, reason, body, H2O_SEND_ERROR_HTTP1_CLOSE_CONNECTION); |
216 | 211 | } else { |
217 | 19 | conn->req.http1_is_persistent = 0; |
218 | 19 | if (conn->_ostr_final.state == OSTREAM_STATE_DONE) |
219 | 19 | cleanup_connection(conn); |
220 | 19 | } |
221 | 230 | } |
222 | | |
223 | | #define DECL_ENTITY_READ_SEND_ERROR_XXX(status_) \ |
224 | | static void entity_read_send_error_##status_(struct st_h2o_http1_conn_t *conn, const char *reason, const char *body) \ |
225 | 230 | { \ |
226 | 230 | entity_read_do_send_error(conn, status_, H2O_STATUS_ERROR_##status_, reason, body); \ |
227 | 230 | } http1.c:entity_read_send_error_400 Line | Count | Source | 225 | 123 | { \ | 226 | 123 | entity_read_do_send_error(conn, status_, H2O_STATUS_ERROR_##status_, reason, body); \ | 227 | 123 | } |
http1.c:entity_read_send_error_502 Line | Count | Source | 225 | 18 | { \ | 226 | 18 | entity_read_do_send_error(conn, status_, H2O_STATUS_ERROR_##status_, reason, body); \ | 227 | 18 | } |
http1.c:entity_read_send_error_413 Line | Count | Source | 225 | 89 | { \ | 226 | 89 | entity_read_do_send_error(conn, status_, H2O_STATUS_ERROR_##status_, reason, body); \ | 227 | 89 | } |
|
228 | | |
229 | | DECL_ENTITY_READ_SEND_ERROR_XXX(400) |
230 | | DECL_ENTITY_READ_SEND_ERROR_XXX(413) |
231 | | DECL_ENTITY_READ_SEND_ERROR_XXX(502) |
232 | | |
233 | | static void handle_one_body_fragment(struct st_h2o_http1_conn_t *conn, size_t fragment_size, size_t extra_bytes, int complete) |
234 | 2.86k | { |
235 | 2.86k | if (fragment_size == 0 && !complete) { |
236 | 568 | h2o_buffer_consume(&conn->sock->input, extra_bytes); |
237 | 568 | return; |
238 | 568 | } |
239 | | |
240 | 2.29k | clear_timeouts(conn); |
241 | 2.29k | h2o_socket_read_stop(conn->sock); |
242 | | |
243 | | /* move data being read to req_body */ |
244 | 2.29k | if (!h2o_buffer_try_append(&conn->req_body, conn->sock->input->bytes, fragment_size)) { |
245 | 0 | entity_read_send_error_502(conn, "Bad Gateway", "Bad Gateway"); |
246 | 0 | return; |
247 | 0 | } |
248 | 2.29k | h2o_buffer_consume(&conn->sock->input, fragment_size + extra_bytes); |
249 | 2.29k | conn->req.req_body_bytes_received += fragment_size; |
250 | | |
251 | | /* invoke action */ |
252 | 2.29k | conn->req.entity = h2o_iovec_init(conn->req_body->bytes, conn->req_body->size); |
253 | 2.29k | if (conn->req.write_req.cb(conn->req.write_req.ctx, complete) != 0) { |
254 | 0 | entity_read_send_error_502(conn, "Bad Gateway", "Bad Gateway"); |
255 | 0 | return; |
256 | 0 | } |
257 | 2.29k | if (complete) { |
258 | 1.04k | conn->req.proceed_req = NULL; |
259 | 1.04k | conn->_req_entity_reader = NULL; |
260 | 1.04k | if (conn->_ostr_final.state == OSTREAM_STATE_DONE) { |
261 | 301 | cleanup_connection(conn); |
262 | 301 | } |
263 | 1.04k | } |
264 | 2.29k | } |
265 | | |
266 | | static void handle_chunked_entity_read(struct st_h2o_http1_conn_t *conn) |
267 | 1.09k | { |
268 | 1.09k | struct st_h2o_http1_chunked_entity_reader *reader = (void *)conn->_req_entity_reader; |
269 | 1.09k | size_t bufsz; |
270 | 1.09k | ssize_t ret; |
271 | | |
272 | | /* decode the incoming data */ |
273 | 1.09k | if ((bufsz = conn->sock->input->size) == 0) |
274 | 21 | return; |
275 | 1.06k | ret = phr_decode_chunked(&reader->decoder, conn->sock->input->bytes, &bufsz); |
276 | 1.06k | if (ret != -1 && bufsz + conn->req.req_body_bytes_received >= conn->super.ctx->globalconf->max_request_entity_size) { |
277 | 0 | entity_read_send_error_413(conn, "Request Entity Too Large", "request entity is too large"); |
278 | 0 | return; |
279 | 0 | } |
280 | 1.06k | if (ret < 0) { |
281 | 934 | if (ret == -2) { |
282 | | /* incomplete */ |
283 | 876 | handle_one_body_fragment(conn, bufsz, conn->sock->input->size - bufsz, 0); |
284 | 876 | } else { |
285 | | /* error */ |
286 | 58 | entity_read_send_error_400(conn, "Invalid Request", "broken chunked-encoding"); |
287 | 58 | } |
288 | 934 | } else { |
289 | | /* complete */ |
290 | 135 | assert(bufsz + ret <= conn->sock->input->size); |
291 | 135 | conn->sock->input->size = bufsz + ret; |
292 | 135 | handle_one_body_fragment(conn, bufsz, 0, 1); |
293 | 135 | } |
294 | 1.06k | } |
295 | | |
296 | | static int create_chunked_entity_reader(struct st_h2o_http1_conn_t *conn) |
297 | 558 | { |
298 | 558 | struct st_h2o_http1_chunked_entity_reader *reader = h2o_mem_alloc_pool(&conn->req.pool, *reader, 1); |
299 | 558 | conn->_req_entity_reader = &reader->super; |
300 | | |
301 | 558 | reader->super.handle_incoming_entity = handle_chunked_entity_read; |
302 | 558 | memset(&reader->decoder, 0, sizeof(reader->decoder)); |
303 | 558 | reader->decoder.consume_trailer = 1; |
304 | | |
305 | 558 | return 0; |
306 | 558 | } |
307 | | |
308 | | static void handle_content_length_entity_read(struct st_h2o_http1_conn_t *conn) |
309 | 2.00k | { |
310 | 2.00k | int complete = 0; |
311 | 2.00k | struct st_h2o_http1_content_length_entity_reader *reader = (void *)conn->_req_entity_reader; |
312 | 2.00k | size_t length = conn->sock->input->size; |
313 | | |
314 | 2.00k | if (conn->req.req_body_bytes_received + conn->sock->input->size >= reader->content_length) { |
315 | 906 | complete = 1; |
316 | 906 | length = reader->content_length - conn->req.req_body_bytes_received; |
317 | 906 | } |
318 | 2.00k | if (!complete && length == 0) |
319 | 150 | return; |
320 | | |
321 | 1.85k | handle_one_body_fragment(conn, length, 0, complete); |
322 | 1.85k | } |
323 | | |
324 | | static int create_content_length_entity_reader(struct st_h2o_http1_conn_t *conn, size_t content_length) |
325 | 1.16k | { |
326 | 1.16k | struct st_h2o_http1_content_length_entity_reader *reader = h2o_mem_alloc_pool(&conn->req.pool, *reader, 1); |
327 | 1.16k | conn->_req_entity_reader = &reader->super; |
328 | | |
329 | 1.16k | reader->super.handle_incoming_entity = handle_content_length_entity_read; |
330 | 1.16k | reader->content_length = content_length; |
331 | | |
332 | 1.16k | return 0; |
333 | 1.16k | } |
334 | | |
335 | | static int create_entity_reader(struct st_h2o_http1_conn_t *conn, const struct phr_header *entity_header) |
336 | 1.77k | { |
337 | | /* strlen("content-length") is unequal to sizeof("transfer-encoding"), and thus checking the length only is sufficient */ |
338 | 1.77k | if (entity_header->name_len == sizeof("transfer-encoding") - 1) { |
339 | | /* transfer-encoding */ |
340 | 589 | if (!h2o_lcstris(entity_header->value, entity_header->value_len, H2O_STRLIT("chunked"))) { |
341 | 31 | entity_read_send_error_400(conn, "Invalid Request", "unknown transfer-encoding"); |
342 | 31 | return -1; |
343 | 31 | } |
344 | 558 | return create_chunked_entity_reader(conn); |
345 | 1.18k | } else { |
346 | | /* content-length */ |
347 | 1.18k | size_t content_length = h2o_strtosize(entity_header->value, entity_header->value_len); |
348 | 1.18k | if (content_length == SIZE_MAX) { |
349 | 34 | entity_read_send_error_400(conn, "Invalid Request", "broken content-length header"); |
350 | 34 | return -1; |
351 | 34 | } |
352 | 1.15k | if (content_length > conn->super.ctx->globalconf->max_request_entity_size) { |
353 | 89 | entity_read_send_error_413(conn, "Request Entity Too Large", "request entity is too large"); |
354 | 89 | return -1; |
355 | 89 | } |
356 | 1.06k | conn->req.content_length = content_length; |
357 | 1.06k | return create_content_length_entity_reader(conn, (size_t)content_length); |
358 | 1.15k | } |
359 | | /* failed */ |
360 | 0 | return -1; |
361 | 1.77k | } |
362 | | |
363 | | static const char *init_headers(h2o_mem_pool_t *pool, h2o_headers_t *headers, const struct phr_header *src, size_t len, |
364 | | h2o_iovec_t *connection, h2o_iovec_t *host, h2o_iovec_t *upgrade, h2o_iovec_t *expect, |
365 | | ssize_t *entity_header_index) |
366 | 8.60k | { |
367 | 8.60k | *entity_header_index = -1; |
368 | | |
369 | 8.60k | assert(headers->size == 0); |
370 | | |
371 | | /* setup */ |
372 | 8.60k | if (len != 0) { |
373 | 6.01k | size_t i; |
374 | 6.01k | h2o_vector_reserve(pool, headers, len); |
375 | 48.8k | for (i = 0; i != len; ++i) { |
376 | 42.8k | const h2o_token_t *name_token; |
377 | | /* reject multiline header */ |
378 | 42.8k | if (src[i].name_len == 0) |
379 | 44 | return "line folding of header fields is not supported"; |
380 | 42.8k | char orig_case[src[i].name_len]; |
381 | | /* preserve the original case */ |
382 | 42.8k | memcpy(orig_case, src[i].name, src[i].name_len); |
383 | | /* convert to lower-case in-place */ |
384 | 42.8k | h2o_strtolower((char *)src[i].name, src[i].name_len); |
385 | 42.8k | if ((name_token = h2o_lookup_token(src[i].name, src[i].name_len)) != NULL) { |
386 | 17.8k | if (name_token->flags.is_init_header_special) { |
387 | 3.85k | if (name_token == H2O_TOKEN_HOST) { |
388 | 507 | host->base = (char *)src[i].value; |
389 | 507 | host->len = src[i].value_len; |
390 | 3.34k | } else if (name_token == H2O_TOKEN_CONTENT_LENGTH) { |
391 | 1.59k | if (*entity_header_index == -1) |
392 | 1.18k | *entity_header_index = i; |
393 | 1.75k | } else if (name_token == H2O_TOKEN_TRANSFER_ENCODING) { |
394 | 675 | *entity_header_index = i; |
395 | 1.08k | } else if (name_token == H2O_TOKEN_EXPECT) { |
396 | 69 | expect->base = (char *)src[i].value; |
397 | 69 | expect->len = src[i].value_len; |
398 | 1.01k | } else if (name_token == H2O_TOKEN_UPGRADE) { |
399 | 1.01k | upgrade->base = (char *)src[i].value; |
400 | 1.01k | upgrade->len = src[i].value_len; |
401 | 1.01k | } else { |
402 | 0 | assert(!"logic flaw"); |
403 | 0 | } |
404 | 13.9k | } else { |
405 | 13.9k | h2o_add_header(pool, headers, name_token, orig_case, src[i].value, src[i].value_len); |
406 | 13.9k | if (name_token == H2O_TOKEN_CONNECTION) |
407 | 1.13k | *connection = headers->entries[headers->size - 1].value; |
408 | 13.9k | } |
409 | 25.0k | } else { |
410 | 25.0k | h2o_add_header_by_str(pool, headers, src[i].name, src[i].name_len, 0, orig_case, src[i].value, src[i].value_len); |
411 | 25.0k | } |
412 | 42.8k | } |
413 | 6.01k | } |
414 | | |
415 | 8.55k | return NULL; |
416 | 8.60k | } |
417 | | |
418 | | static int upgrade_is_h2(h2o_iovec_t upgrade) |
419 | 669 | { |
420 | 669 | if (h2o_lcstris(upgrade.base, upgrade.len, H2O_STRLIT("h2c")) || h2o_lcstris(upgrade.base, upgrade.len, H2O_STRLIT("h2c-14")) || |
421 | 669 | h2o_lcstris(upgrade.base, upgrade.len, H2O_STRLIT("h2c-16"))) |
422 | 567 | return 1; |
423 | 102 | return 0; |
424 | 669 | } |
425 | | |
426 | | static const char fixup_request_is_h2_upgrade[] = "fixup h2 upgrade"; |
427 | | |
428 | | static const char *fixup_request(struct st_h2o_http1_conn_t *conn, struct phr_header *headers, size_t num_headers, |
429 | | int minor_version, h2o_iovec_t *expect, ssize_t *entity_header_index) |
430 | 8.60k | { |
431 | 8.60k | h2o_iovec_t connection = {NULL, 0}, host = {NULL, 0}, upgrade = {NULL, 0}; |
432 | 8.60k | enum { METHOD_NORMAL, METHOD_CONNECT, METHOD_CONNECT_UDP } method_type; |
433 | 8.60k | const char *ret; |
434 | | |
435 | 8.60k | expect->base = NULL; |
436 | 8.60k | expect->len = 0; |
437 | | |
438 | 8.60k | conn->req.input.scheme = conn->sock->ssl != NULL ? &H2O_URL_SCHEME_HTTPS : &H2O_URL_SCHEME_HTTP; |
439 | 8.60k | conn->req.version = 0x100 | (minor_version != 0); |
440 | | |
441 | | /* RFC 7231 6.2: a server MUST NOT send a 1xx response to an HTTP/1.0 client */ |
442 | 8.60k | if (conn->req.version < 0x101) |
443 | 1.95k | conn->_ostr_final.super.send_informational = NULL; |
444 | | |
445 | 8.60k | if (h2o_memis(conn->req.input.method.base, conn->req.input.method.len, H2O_STRLIT("CONNECT"))) { |
446 | 85 | method_type = METHOD_CONNECT; |
447 | 8.51k | } else if (h2o_memis(conn->req.input.method.base, conn->req.input.method.len, H2O_STRLIT("CONNECT-UDP"))) { |
448 | 1 | method_type = METHOD_CONNECT_UDP; |
449 | 8.51k | } else { |
450 | 8.51k | method_type = METHOD_NORMAL; |
451 | 8.51k | } |
452 | | |
453 | | /* init headers */ |
454 | 8.60k | if ((ret = init_headers(&conn->req.pool, &conn->req.headers, headers, num_headers, &connection, &host, &upgrade, expect, |
455 | 8.60k | entity_header_index)) != NULL) |
456 | 44 | return ret; |
457 | | |
458 | | /* copy the values to pool, since the buffer pointed by the headers may get realloced */ |
459 | 8.55k | if (*entity_header_index != -1 || method_type != METHOD_NORMAL || upgrade.base != NULL) { |
460 | 2.69k | size_t i; |
461 | 2.69k | conn->req.input.method = h2o_strdup(&conn->req.pool, conn->req.input.method.base, conn->req.input.method.len); |
462 | 2.69k | conn->req.input.path = h2o_strdup(&conn->req.pool, conn->req.input.path.base, conn->req.input.path.len); |
463 | 15.8k | for (i = 0; i != conn->req.headers.size; ++i) { |
464 | 13.1k | h2o_header_t *header = conn->req.headers.entries + i; |
465 | 13.1k | if (!h2o_iovec_is_token(header->name)) { |
466 | 8.49k | *header->name = h2o_strdup(&conn->req.pool, header->name->base, header->name->len); |
467 | 8.49k | } |
468 | 13.1k | header->value = h2o_strdup(&conn->req.pool, header->value.base, header->value.len); |
469 | 13.1k | } |
470 | 2.69k | if (host.base != NULL) |
471 | 188 | host = h2o_strdup(&conn->req.pool, host.base, host.len); |
472 | 2.69k | if (upgrade.base != NULL) |
473 | 868 | upgrade = h2o_strdup(&conn->req.pool, upgrade.base, upgrade.len); |
474 | 2.69k | } |
475 | | |
476 | 8.55k | if (method_type == METHOD_CONNECT) { |
477 | | /* CONNECT method, validate, setting the target host in `req->input.authority`. Path becomes empty. */ |
478 | 85 | if (conn->req.version < 0x101 || conn->req.input.path.len == 0 || |
479 | 85 | (host.base != NULL && !h2o_memis(conn->req.input.path.base, conn->req.input.path.len, host.base, host.len)) || |
480 | 85 | *entity_header_index != -1) |
481 | 68 | return "invalid request"; |
482 | 17 | conn->req.input.authority = conn->req.input.path; |
483 | 17 | conn->req.input.path = h2o_iovec_init(NULL, 0); |
484 | 17 | conn->req.is_tunnel_req = 1; |
485 | 8.47k | } else { |
486 | | /* request line is in ordinary form, path might contain absolute URL; if so, convert it */ |
487 | 8.47k | if (conn->req.input.path.len != 0 && conn->req.input.path.base[0] != '/') { |
488 | 6.96k | h2o_url_t url; |
489 | 6.96k | if (h2o_url_parse(&conn->req.pool, conn->req.input.path.base, conn->req.input.path.len, &url) == 0) { |
490 | 296 | conn->req.input.scheme = url.scheme; |
491 | 296 | conn->req.input.path = url.path; |
492 | 296 | host = url.authority; /* authority part of the absolute form overrides the host header field (RFC 7230 S5.4) */ |
493 | 296 | } |
494 | 6.96k | } |
495 | | /* move host header to req->authority */ |
496 | 8.47k | if (host.base != NULL) |
497 | 526 | conn->req.input.authority = host; |
498 | | /* each protocol implementation validates masque */ |
499 | 8.47k | if (!h2o_req_validate_pseudo_headers(&conn->req)) |
500 | 2 | return "invalid request"; |
501 | | /* special handling for CONNECT-UDP, else it is an ordinary request */ |
502 | 8.47k | if (method_type == METHOD_CONNECT_UDP) { |
503 | 0 | conn->req.is_tunnel_req = 1; |
504 | 8.47k | } else { |
505 | | /* handle Connection and Upgrade header fields */ |
506 | 8.47k | if (connection.base != NULL) { |
507 | | /* TODO contains_token function can be faster */ |
508 | 973 | if (h2o_contains_token(connection.base, connection.len, H2O_STRLIT("keep-alive"), ',')) { |
509 | 543 | conn->req.http1_is_persistent = 1; |
510 | 543 | } |
511 | | /* Upgrade is respected only for requests without bodies. Use of upgrade on a request with body is unsupported, |
512 | | * because we reuse the entity reader for reading the body and the tunnelled data. */ |
513 | 973 | if (upgrade.base != NULL && h2o_contains_token(connection.base, connection.len, H2O_STRLIT("upgrade"), ',') && |
514 | 973 | *entity_header_index == -1) { |
515 | | /* early return if upgrading to h2 */ |
516 | 669 | if (upgrade_is_h2(upgrade) && conn->sock->ssl == NULL && conn->super.ctx->globalconf->http1.upgrade_to_http2) |
517 | 567 | return fixup_request_is_h2_upgrade; |
518 | 102 | conn->req.upgrade = upgrade; |
519 | 102 | conn->req.is_tunnel_req = 1; |
520 | 102 | conn->req.http1_is_persistent = 0; |
521 | 102 | } |
522 | 7.49k | } else if (conn->req.version >= 0x101) { |
523 | | /* defaults to keep-alive if >= HTTP/1.1 */ |
524 | 6.01k | conn->req.http1_is_persistent = 1; |
525 | 6.01k | } |
526 | | /* disable keep-alive if shutdown is requested */ |
527 | 7.90k | if (conn->req.http1_is_persistent && conn->super.ctx->shutdown_requested) |
528 | 0 | conn->req.http1_is_persistent = 0; |
529 | 7.90k | } |
530 | 8.47k | } |
531 | | |
532 | 7.92k | return NULL; |
533 | 8.55k | } |
534 | | |
535 | | static void on_continue_sent(h2o_socket_t *sock, const char *err) |
536 | 0 | { |
537 | 0 | struct st_h2o_http1_conn_t *conn = sock->data; |
538 | |
|
539 | 0 | if (err != NULL) { |
540 | 0 | close_connection(conn, 1); |
541 | 0 | return; |
542 | 0 | } |
543 | | |
544 | 0 | h2o_socket_read_start(sock, reqread_on_read); |
545 | 0 | conn->_req_entity_reader->handle_incoming_entity(conn); |
546 | 0 | } |
547 | | |
548 | | static int contains_crlf_only(const char *s, size_t len) |
549 | 177 | { |
550 | 279 | for (; len != 0; ++s, --len) |
551 | 263 | if (!(*s == '\r' || *s == '\n')) |
552 | 161 | return 0; |
553 | 16 | return 1; |
554 | 177 | } |
555 | | |
556 | | static void send_bad_request(struct st_h2o_http1_conn_t *conn, const char *body) |
557 | 1.19k | { |
558 | 1.19k | h2o_socket_read_stop(conn->sock); |
559 | 1.19k | h2o_send_error_400(&conn->req, "Bad Request", body, H2O_SEND_ERROR_BROKEN_REQUEST | H2O_SEND_ERROR_HTTP1_CLOSE_CONNECTION); |
560 | 1.19k | } |
561 | | |
562 | | static void resume_request_read(struct st_h2o_http1_conn_t *conn) |
563 | 1.15k | { |
564 | 1.15k | set_req_timeout(conn, conn->super.ctx->globalconf->http1.req_timeout, reqread_on_timeout); |
565 | 1.15k | set_req_io_timeout(conn, conn->super.ctx->globalconf->http1.req_io_timeout, req_io_on_timeout); |
566 | 1.15k | h2o_socket_read_start(conn->sock, reqread_on_read); |
567 | 1.15k | } |
568 | | |
569 | | static void proceed_request(h2o_req_t *req, const char *errstr) |
570 | 682 | { |
571 | 682 | struct st_h2o_http1_conn_t *conn = H2O_STRUCT_FROM_MEMBER(struct st_h2o_http1_conn_t, req, req); |
572 | | |
573 | 682 | if (errstr != NULL) { |
574 | 18 | entity_read_send_error_502(conn, "Bad Gateway", "Bad Gateway"); |
575 | 18 | return; |
576 | 18 | } |
577 | | |
578 | 664 | assert(conn->req.entity.len == conn->req_body->size); |
579 | 664 | h2o_buffer_consume(&conn->req_body, conn->req_body->size); |
580 | | |
581 | 664 | resume_request_read(conn); |
582 | 664 | } |
583 | | |
584 | | static int write_req_non_streaming(void *_req, int is_end_stream) |
585 | 985 | { |
586 | 985 | struct st_h2o_http1_conn_t *conn = H2O_STRUCT_FROM_MEMBER(struct st_h2o_http1_conn_t, req, _req); |
587 | | |
588 | 985 | if (is_end_stream) { |
589 | 494 | conn->req.proceed_req = NULL; |
590 | 494 | h2o_process_request(&conn->req); |
591 | 494 | } else { |
592 | 491 | resume_request_read(conn); |
593 | 491 | } |
594 | 985 | return 0; |
595 | 985 | } |
596 | | |
597 | | static int write_req_first(void *_req, int is_end_stream) |
598 | 1.30k | { |
599 | 1.30k | struct st_h2o_http1_conn_t *conn = H2O_STRUCT_FROM_MEMBER(struct st_h2o_http1_conn_t, req, _req); |
600 | | |
601 | | /* if possible, switch to streaming request body mode */ |
602 | 1.30k | if (!is_end_stream && h2o_req_can_stream_request(&conn->req)) { |
603 | 657 | conn->req.write_req.cb = NULL; /* will be set to something before `proceed_req` is being invoked */ |
604 | 657 | conn->req.proceed_req = proceed_request; |
605 | 657 | h2o_process_request(&conn->req); |
606 | 657 | return 0; |
607 | 657 | } |
608 | | |
609 | 643 | conn->req.write_req.cb = write_req_non_streaming; |
610 | 643 | return write_req_non_streaming(&conn->req, is_end_stream); |
611 | 1.30k | } |
612 | | |
613 | | static int write_req_connect_first(void *_req, int is_end_stream) |
614 | 83 | { |
615 | 83 | struct st_h2o_http1_conn_t *conn = H2O_STRUCT_FROM_MEMBER(struct st_h2o_http1_conn_t, req, _req); |
616 | | |
617 | 83 | conn->req.write_req.cb = NULL; /* will not be called again until proceed_req is called by the generator */ |
618 | 83 | if (is_end_stream) |
619 | 0 | conn->req.proceed_req = NULL; |
620 | | |
621 | 83 | return 0; |
622 | 83 | } |
623 | | |
624 | | static void handle_incoming_request(struct st_h2o_http1_conn_t *conn) |
625 | 16.1k | { |
626 | 16.1k | size_t inreqlen = conn->sock->input->size < H2O_MAX_REQLEN ? conn->sock->input->size : H2O_MAX_REQLEN; |
627 | 16.1k | int reqlen, minor_version; |
628 | 16.1k | struct phr_header headers[H2O_MAX_HEADERS]; |
629 | 16.1k | size_t num_headers = H2O_MAX_HEADERS; |
630 | 16.1k | ssize_t entity_body_header_index; |
631 | 16.1k | h2o_iovec_t expect; |
632 | | |
633 | 16.1k | if (conn->sock->input->size != 0) |
634 | 16.1k | h2o_conn_set_state(&conn->super, H2O_CONN_STATE_ACTIVE); |
635 | | |
636 | | /* need to set request_begin_at here for keep-alive connection */ |
637 | 16.1k | if (h2o_timeval_is_null(&conn->req.timestamps.request_begin_at)) |
638 | 14.6k | conn->req.timestamps.request_begin_at = h2o_gettimeofday(conn->super.ctx->loop); |
639 | | |
640 | 16.1k | reqlen = phr_parse_request(conn->sock->input->bytes, inreqlen, (const char **)&conn->req.input.method.base, |
641 | 16.1k | &conn->req.input.method.len, (const char **)&conn->req.input.path.base, &conn->req.input.path.len, |
642 | 16.1k | &minor_version, headers, &num_headers, conn->_prevreqlen); |
643 | 16.1k | conn->_prevreqlen = inreqlen; |
644 | | |
645 | 16.1k | switch (reqlen) { |
646 | 8.60k | default: { // parse complete |
647 | 8.60k | conn->_unconsumed_request_size = reqlen; |
648 | 8.60k | const char *err; |
649 | 8.60k | if ((err = fixup_request(conn, headers, num_headers, minor_version, &expect, &entity_body_header_index)) != NULL && |
650 | 8.60k | err != fixup_request_is_h2_upgrade) { |
651 | 114 | clear_timeouts(conn); |
652 | 114 | send_bad_request(conn, err); |
653 | 114 | return; |
654 | 114 | } |
655 | 8.48k | h2o_probe_log_request(&conn->req, conn->_req_index); |
656 | 8.48k | if (err == fixup_request_is_h2_upgrade) { |
657 | 567 | clear_timeouts(conn); |
658 | 567 | h2o_socket_read_stop(conn->sock); |
659 | 567 | if (h2o_http2_handle_upgrade(&conn->req, conn->super.connected_at) != 0) |
660 | 440 | h2o_send_error_400(&conn->req, "Invalid Request", "Broken upgrade request to HTTP/2", 0); |
661 | 7.92k | } else if (entity_body_header_index != -1) { |
662 | | /* Request has body, start reading it. Invocation of `h2o_process_request` is delayed to reduce backend concurrency. */ |
663 | 1.77k | conn->req.timestamps.request_body_begin_at = h2o_gettimeofday(conn->super.ctx->loop); |
664 | 1.77k | if (expect.base != NULL) { |
665 | 2 | if (!h2o_lcstris(expect.base, expect.len, H2O_STRLIT("100-continue"))) { |
666 | 2 | clear_timeouts(conn); |
667 | 2 | h2o_socket_read_stop(conn->sock); |
668 | 2 | h2o_send_error_417(&conn->req, "Expectation Failed", "unknown expectation", |
669 | 2 | H2O_SEND_ERROR_HTTP1_CLOSE_CONNECTION); |
670 | 2 | return; |
671 | 2 | } |
672 | 2 | } |
673 | 1.77k | if (create_entity_reader(conn, headers + entity_body_header_index) != 0) |
674 | 154 | return; |
675 | 1.62k | conn->req.write_req.cb = write_req_first; |
676 | 1.62k | conn->req.write_req.ctx = &conn->req; |
677 | 1.62k | conn->_unconsumed_request_size = 0; |
678 | 1.62k | h2o_buffer_consume(&conn->sock->input, reqlen); |
679 | 1.62k | h2o_buffer_init(&conn->req_body, &h2o_socket_buffer_prototype); |
680 | 1.62k | if (expect.base != NULL) { |
681 | 0 | static const h2o_iovec_t res = {H2O_STRLIT("HTTP/1.1 100 Continue\r\n\r\n")}; |
682 | 0 | h2o_socket_write(conn->sock, (void *)&res, 1, on_continue_sent); |
683 | | /* processing of the incoming entity is postponed until the 100 response is sent */ |
684 | 0 | h2o_socket_read_stop(conn->sock); |
685 | 0 | return; |
686 | 0 | } |
687 | 1.62k | conn->_req_entity_reader->handle_incoming_entity(conn); |
688 | 6.14k | } else if (conn->req.is_tunnel_req) { |
689 | | /* Is a CONNECT request or an upgrade (e.g., WebSocket). Request is submitted immediately and body is streamed. */ |
690 | 119 | if (!h2o_req_can_stream_request(&conn->req) && |
691 | 119 | h2o_memis(conn->req.input.method.base, conn->req.input.method.len, H2O_STRLIT("CONNECT"))) { |
692 | 17 | h2o_send_error_405(&conn->req, "Method Not Allowed", "Method Not Allowed", 0); |
693 | 17 | return; |
694 | 17 | } |
695 | 102 | if (create_content_length_entity_reader(conn, SIZE_MAX) != 0) |
696 | 0 | return; |
697 | 102 | conn->_unconsumed_request_size = 0; |
698 | 102 | h2o_buffer_consume(&conn->sock->input, reqlen); |
699 | 102 | h2o_buffer_init(&conn->req_body, &h2o_socket_buffer_prototype); |
700 | 102 | conn->req.write_req.cb = write_req_connect_first; |
701 | 102 | conn->req.write_req.ctx = &conn->req; |
702 | 102 | conn->req.proceed_req = proceed_request; |
703 | 102 | conn->_req_entity_reader->handle_incoming_entity(conn); /* read payload received early before submitting the request */ |
704 | 102 | if (conn->req.entity.base == NULL) |
705 | 19 | conn->req.entity = h2o_iovec_init("", 0); /* if nothing was read, still indicate that body exists */ |
706 | | /* stop reading (this might or might not be done by `handle_incoming_entity`) until HTTP response is given */ |
707 | 102 | h2o_socket_read_stop(conn->sock); |
708 | 102 | clear_timeouts(conn); |
709 | 102 | h2o_process_request(&conn->req); |
710 | 6.02k | } else { |
711 | | /* Ordinary request without request body. */ |
712 | 6.02k | clear_timeouts(conn); |
713 | 6.02k | h2o_socket_read_stop(conn->sock); |
714 | 6.02k | h2o_process_request(&conn->req); |
715 | 6.02k | } |
716 | 8.48k | } |
717 | 8.31k | return; |
718 | 8.31k | case -2: // incomplete |
719 | 1.99k | if (inreqlen == H2O_MAX_REQLEN) { |
720 | 0 | send_bad_request(conn, "Bad Request"); |
721 | 0 | } |
722 | 1.99k | return; |
723 | 5.55k | case -1: // error |
724 | | /* upgrade to HTTP/2 if the request starts with: PRI * HTTP/2 */ |
725 | 5.55k | if (conn->super.ctx->globalconf->http1.upgrade_to_http2) { |
726 | | /* should check up to the first octet that phr_parse_request returns an error */ |
727 | 5.55k | static const h2o_iovec_t HTTP2_SIG = {H2O_STRLIT("PRI * HTTP/2")}; |
728 | 5.55k | if (conn->sock->input->size >= HTTP2_SIG.len && memcmp(conn->sock->input->bytes, HTTP2_SIG.base, HTTP2_SIG.len) == 0) { |
729 | 4.46k | h2o_accept_ctx_t accept_ctx = {conn->super.ctx, conn->super.hosts}; |
730 | 4.46k | h2o_socket_t *sock = conn->sock; |
731 | 4.46k | struct timeval connected_at = conn->super.connected_at; |
732 | | /* destruct the connection after detatching the socket */ |
733 | 4.46k | conn->sock = NULL; |
734 | 4.46k | close_connection(conn, 1); |
735 | | /* and accept as http2 connection */ |
736 | 4.46k | h2o_http2_accept(&accept_ctx, sock, connected_at); |
737 | 4.46k | return; |
738 | 4.46k | } |
739 | 5.55k | } |
740 | 1.09k | if (inreqlen <= 4 && contains_crlf_only(conn->sock->input->bytes, inreqlen)) { |
741 | 16 | close_connection(conn, 1); |
742 | 1.07k | } else { |
743 | 1.07k | send_bad_request(conn, "Bad Request"); |
744 | 1.07k | } |
745 | 1.09k | return; |
746 | 16.1k | } |
747 | 16.1k | } |
748 | | |
749 | | void reqread_on_read(h2o_socket_t *sock, const char *err) |
750 | 15.8k | { |
751 | 15.8k | struct st_h2o_http1_conn_t *conn = sock->data; |
752 | | |
753 | 15.8k | if (err != NULL) { |
754 | 2.63k | close_connection(conn, 1); |
755 | 2.63k | return; |
756 | 2.63k | } |
757 | | |
758 | 13.2k | set_req_io_timeout(conn, conn->super.ctx->globalconf->http1.req_io_timeout, req_io_on_timeout); |
759 | 13.2k | if (conn->_req_entity_reader == NULL) |
760 | 11.8k | handle_incoming_request(conn); |
761 | 1.36k | else |
762 | 1.36k | conn->_req_entity_reader->handle_incoming_entity(conn); |
763 | 13.2k | } |
764 | | |
765 | | static void close_idle_connection(h2o_conn_t *_conn) |
766 | 0 | { |
767 | 0 | struct st_h2o_http1_conn_t *conn = (void *)_conn; |
768 | 0 | conn->req.http1_is_persistent = 0; |
769 | 0 | close_connection(conn, 1); |
770 | 0 | } |
771 | | |
772 | | static void on_timeout(struct st_h2o_http1_conn_t *conn) |
773 | 0 | { |
774 | 0 | if (conn->_req_index == 1) { |
775 | | /* assign hostconf and bind conf so that the request can be logged */ |
776 | 0 | h2o_hostconf_t *hostconf = h2o_req_setup(&conn->req); |
777 | 0 | h2o_req_bind_conf(&conn->req, hostconf, &hostconf->fallback_path); |
778 | | /* set error status for logging */ |
779 | 0 | conn->req.res.reason = "Request Timeout"; |
780 | 0 | } |
781 | |
|
782 | 0 | close_idle_connection(&conn->super); |
783 | 0 | } |
784 | | |
785 | | static void req_io_on_timeout(h2o_timer_t *entry) |
786 | 0 | { |
787 | 0 | struct st_h2o_http1_conn_t *conn = H2O_STRUCT_FROM_MEMBER(struct st_h2o_http1_conn_t, _io_timeout_entry, entry); |
788 | 0 | conn->super.ctx->http1.events.request_io_timeouts++; |
789 | 0 | on_timeout(conn); |
790 | 0 | } |
791 | | |
792 | | static void reqread_on_timeout(h2o_timer_t *entry) |
793 | 0 | { |
794 | 0 | struct st_h2o_http1_conn_t *conn = H2O_STRUCT_FROM_MEMBER(struct st_h2o_http1_conn_t, _timeout_entry, entry); |
795 | 0 | conn->super.ctx->http1.events.request_timeouts++; |
796 | 0 | on_timeout(conn); |
797 | 0 | } |
798 | | |
799 | | static inline void reqread_start(struct st_h2o_http1_conn_t *conn) |
800 | 16.2k | { |
801 | 16.2k | set_req_timeout(conn, conn->super.ctx->globalconf->http1.req_timeout, reqread_on_timeout); |
802 | 16.2k | set_req_io_timeout(conn, conn->super.ctx->globalconf->http1.req_io_timeout, req_io_on_timeout); |
803 | 16.2k | h2o_socket_read_start(conn->sock, reqread_on_read); |
804 | 16.2k | if (conn->sock->input->size != 0) |
805 | 4.25k | handle_incoming_request(conn); |
806 | 16.2k | } |
807 | | |
808 | | static void on_send_next(h2o_socket_t *sock, const char *err) |
809 | 1.37k | { |
810 | 1.37k | struct st_h2o_http1_conn_t *conn = sock->data; |
811 | | |
812 | 1.37k | if (err != NULL) |
813 | 2 | close_connection(conn, 1); |
814 | 1.37k | else |
815 | 1.37k | h2o_proceed_response(&conn->req); |
816 | 1.37k | } |
817 | | |
818 | | static void on_send_complete_post_trailers(h2o_socket_t *sock, const char *err) |
819 | 0 | { |
820 | 0 | struct st_h2o_http1_conn_t *conn = sock->data; |
821 | |
|
822 | 0 | if (err != NULL) |
823 | 0 | conn->req.http1_is_persistent = 0; |
824 | |
|
825 | 0 | conn->_ostr_final.state = OSTREAM_STATE_DONE; |
826 | 0 | if (conn->req.proceed_req == NULL) |
827 | 0 | cleanup_connection(conn); |
828 | 0 | } |
829 | | |
830 | | static void on_send_complete(h2o_socket_t *sock, const char *err) |
831 | 9.12k | { |
832 | 9.12k | struct st_h2o_http1_conn_t *conn = sock->data; |
833 | | |
834 | 9.12k | if (err == NULL) { |
835 | 9.12k | if (conn->req._ostr_top != &conn->_ostr_final.super) { |
836 | 0 | err = "pull error"; |
837 | 9.12k | } else { |
838 | | /* success */ |
839 | 9.12k | conn->req.timestamps.response_end_at = h2o_gettimeofday(conn->super.ctx->loop); |
840 | 9.12k | } |
841 | 9.12k | } |
842 | | |
843 | 9.12k | if (err != NULL) |
844 | 2 | conn->req.http1_is_persistent = 0; |
845 | | |
846 | 9.12k | if (err == NULL && conn->req.send_server_timing && conn->_ostr_final.chunked_buf != NULL) { |
847 | 0 | h2o_iovec_t trailer; |
848 | 0 | if ((trailer = h2o_build_server_timing_trailer(&conn->req, H2O_STRLIT("server-timing: "), H2O_STRLIT("\r\n\r\n"))).len != |
849 | 0 | 0) { |
850 | 0 | h2o_socket_write(conn->sock, &trailer, 1, on_send_complete_post_trailers); |
851 | 0 | return; |
852 | 0 | } |
853 | 0 | } |
854 | | |
855 | 9.12k | conn->_ostr_final.state = OSTREAM_STATE_DONE; |
856 | | |
857 | 9.12k | if (conn->req.is_tunnel_req) { |
858 | | /* We have received a complete request (the end of the request is the request headers, see `fixup_request`), and the |
859 | | * connection is not going to handle any more requests. Therefore, we can close the connection immediately, regardless of if |
860 | | * the connection had been turned into a tunnel. */ |
861 | 119 | assert(!conn->req.http1_is_persistent); |
862 | 119 | cleanup_connection(conn); |
863 | 9.00k | } else if (conn->req.proceed_req == NULL) { |
864 | | /* Upstream has sent an early response. Continue forwarding the request body. */ |
865 | 8.60k | cleanup_connection(conn); |
866 | 8.60k | } |
867 | 9.12k | } |
868 | | |
869 | | static void on_upgrade_complete(h2o_socket_t *socket, const char *err) |
870 | 127 | { |
871 | 127 | struct st_h2o_http1_conn_t *conn = socket->data; |
872 | 127 | h2o_http1_upgrade_cb cb = conn->upgrade.cb; |
873 | 127 | void *data = conn->upgrade.data; |
874 | 127 | h2o_socket_t *sock = NULL; |
875 | 127 | size_t headers_size = 0; |
876 | | |
877 | | /* destruct the connection (after detaching the socket) */ |
878 | 127 | if (err == 0) { |
879 | 127 | sock = conn->sock; |
880 | 127 | headers_size = conn->_unconsumed_request_size; |
881 | 127 | close_connection(conn, 0); |
882 | 127 | } else { |
883 | 0 | close_connection(conn, 1); |
884 | 0 | } |
885 | | |
886 | 127 | cb(data, sock, headers_size); |
887 | 127 | } |
888 | | |
889 | | static size_t flatten_headers_estimate_size(h2o_req_t *req, size_t server_name_and_connection_len) |
890 | 9.25k | { |
891 | 9.25k | size_t len = sizeof("HTTP/1.1 \r\nserver: \r\nconnection: \r\ncontent-length: \r\n\r\n") + 3 + strlen(req->res.reason) + |
892 | 9.25k | server_name_and_connection_len + sizeof(H2O_UINT64_LONGEST_STR) - 1 + sizeof("cache-control: private") - 1; |
893 | 9.25k | const h2o_header_t *header, *end; |
894 | | |
895 | 19.6k | for (header = req->res.headers.entries, end = header + req->res.headers.size; header != end; ++header) |
896 | 10.4k | len += header->name->len + header->value.len + 4; |
897 | | |
898 | 9.25k | return len; |
899 | 9.25k | } |
900 | | |
901 | | static size_t flatten_res_headers(char *buf, h2o_req_t *req) |
902 | 9.25k | { |
903 | 9.25k | char *dst = buf; |
904 | 9.25k | size_t i; |
905 | 19.6k | for (i = 0; i != req->res.headers.size; ++i) { |
906 | 10.4k | const h2o_header_t *header = req->res.headers.entries + i; |
907 | 10.4k | memcpy(dst, header->orig_name ? header->orig_name : header->name->base, header->name->len); |
908 | 10.4k | dst += header->name->len; |
909 | 10.4k | *dst++ = ':'; |
910 | 10.4k | *dst++ = ' '; |
911 | 10.4k | memcpy(dst, header->value.base, header->value.len); |
912 | 10.4k | dst += header->value.len; |
913 | 10.4k | *dst++ = '\r'; |
914 | 10.4k | *dst++ = '\n'; |
915 | 10.4k | } |
916 | | |
917 | 9.25k | return dst - buf; |
918 | 9.25k | } |
919 | | |
920 | | static size_t flatten_headers(char *buf, h2o_req_t *req, const char *connection) |
921 | 9.25k | { |
922 | 9.25k | h2o_context_t *ctx = req->conn->ctx; |
923 | 9.25k | char *dst = buf; |
924 | | |
925 | 9.25k | assert(req->res.status <= 999); |
926 | | |
927 | | /* send essential headers with the first chars uppercased for max. interoperability (#72) */ |
928 | 9.25k | if (req->res.content_length != SIZE_MAX) { |
929 | 7.00k | dst += sprintf(dst, "HTTP/1.1 %d %s\r\nConnection: %s\r\nContent-Length: %zu\r\n", req->res.status, req->res.reason, |
930 | 7.00k | connection, req->res.content_length); |
931 | 7.00k | } else { |
932 | 2.25k | dst += sprintf(dst, "HTTP/1.1 %d %s\r\nConnection: %s\r\n", req->res.status, req->res.reason, connection); |
933 | 2.25k | } |
934 | 9.25k | if (ctx->globalconf->server_name.len) { |
935 | 9.25k | dst += sprintf(dst, "Server: %s\r\n", ctx->globalconf->server_name.base); |
936 | 9.25k | } |
937 | | |
938 | 9.25k | dst += flatten_res_headers(dst, req); |
939 | 9.25k | *dst++ = '\r'; |
940 | 9.25k | *dst++ = '\n'; |
941 | | |
942 | 9.25k | return dst - buf; |
943 | 9.25k | } |
944 | | |
945 | | static int should_use_chunked_encoding(h2o_req_t *req) |
946 | 9.12k | { |
947 | 9.12k | if (req->is_tunnel_req) |
948 | 119 | return 0; |
949 | 9.01k | if (req->version != 0x101) |
950 | 2.84k | return 0; |
951 | | /* do nothing if content-length is known */ |
952 | 6.16k | if (req->res.content_length != SIZE_MAX) |
953 | 4.29k | return 0; |
954 | | /* RFC 2616 4.4 states that the following status codes (and response to a HEAD method) should not include message body */ |
955 | 1.86k | if ((100 <= req->res.status && req->res.status <= 199) || req->res.status == 204 || req->res.status == 304) |
956 | 0 | return 0; |
957 | 1.86k | if (h2o_memis(req->input.method.base, req->input.method.len, H2O_STRLIT("HEAD"))) |
958 | 694 | return 0; |
959 | | |
960 | 1.17k | return 1; |
961 | 1.86k | } |
962 | | |
963 | | static void setup_chunked(struct st_h2o_http1_finalostream_t *self, h2o_req_t *req) |
964 | 9.12k | { |
965 | 9.12k | if (should_use_chunked_encoding(req)) { |
966 | 1.17k | h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_TRANSFER_ENCODING, NULL, H2O_STRLIT("chunked")); |
967 | 1.17k | self->chunked_buf = h2o_mem_alloc_pool_aligned(&req->pool, 1, sizeof(size_t) * 2 + sizeof("\r\n")); |
968 | 1.17k | } |
969 | 9.12k | } |
970 | | |
971 | | static void encode_chunked(h2o_sendvec_t *prefix, h2o_sendvec_t *suffix, h2o_send_state_t state, size_t chunk_size, |
972 | | int send_trailers, char *buffer) |
973 | 2.29k | { |
974 | 2.29k | h2o_sendvec_init_raw(prefix, NULL, 0); |
975 | 2.29k | h2o_sendvec_init_raw(suffix, NULL, 0); |
976 | | |
977 | | /* create chunk header and output data */ |
978 | 2.29k | if (chunk_size != 0) { |
979 | 1.17k | prefix->raw = buffer; |
980 | 1.17k | prefix->len = sprintf(buffer, "%zx\r\n", chunk_size); |
981 | 1.17k | if (state != H2O_SEND_STATE_ERROR) { |
982 | 1.17k | suffix->raw = "\r\n0\r\n\r\n"; |
983 | 1.17k | suffix->len = state == H2O_SEND_STATE_FINAL ? (send_trailers ? 5 : 7) : 2; |
984 | 1.17k | } |
985 | 1.17k | } else if (state == H2O_SEND_STATE_FINAL) { |
986 | 1.12k | suffix->raw = "0\r\n\r\n"; |
987 | 1.12k | suffix->len = send_trailers ? 3 : 5; |
988 | 1.12k | } |
989 | | |
990 | | /* if state is error, send a broken chunk to pass the error down to the browser */ |
991 | 2.29k | if (state == H2O_SEND_STATE_ERROR) { |
992 | 1 | suffix->raw = "\r\n1\r\n"; |
993 | 1 | suffix->len = 5; |
994 | 1 | } |
995 | 2.29k | } |
996 | | |
997 | | void finalostream_send(h2o_ostream_t *_self, h2o_req_t *_req, h2o_sendvec_t *inbufs, size_t inbufcnt, h2o_send_state_t send_state) |
998 | 10.5k | { |
999 | 10.5k | struct st_h2o_http1_conn_t *conn = (struct st_h2o_http1_conn_t *)_req->conn; |
1000 | 10.5k | h2o_sendvec_t bufs[inbufcnt + 1 + 2]; /* 1 for header, 2 for chunked encoding */ |
1001 | 10.5k | size_t bufcnt = 0, chunked_prefix_index = 0; |
1002 | | |
1003 | 10.5k | assert(&conn->req == _req); |
1004 | 10.5k | assert(_self == &conn->_ostr_final.super); |
1005 | | |
1006 | 10.5k | if (conn->_ostr_final.informational.write_inflight) { |
1007 | 0 | conn->_ostr_final.informational.pending_final.inbufs = h2o_mem_alloc_pool(&conn->req.pool, h2o_sendvec_t, inbufcnt); |
1008 | 0 | memcpy(conn->_ostr_final.informational.pending_final.inbufs, inbufs, sizeof(*inbufs) * inbufcnt); |
1009 | 0 | conn->_ostr_final.informational.pending_final.inbufcnt = inbufcnt; |
1010 | 0 | conn->_ostr_final.informational.pending_final.send_state = send_state; |
1011 | 0 | return; |
1012 | 0 | } |
1013 | | |
1014 | 10.5k | if (send_state == H2O_SEND_STATE_ERROR) { |
1015 | 1 | conn->req.http1_is_persistent = 0; |
1016 | 1 | conn->req.send_server_timing = 0; |
1017 | 1 | if (conn->req.upstream_refused) { |
1018 | | /* to let the client retry, immediately close the connection without sending any data */ |
1019 | 0 | on_send_complete(conn->sock, NULL); |
1020 | 0 | return; |
1021 | 0 | } |
1022 | 1 | } |
1023 | | |
1024 | 10.5k | if (conn->_ostr_final.state == OSTREAM_STATE_HEAD) { |
1025 | | /* build headers and send */ |
1026 | 9.12k | conn->req.timestamps.response_start_at = h2o_gettimeofday(conn->super.ctx->loop); |
1027 | 9.12k | setup_chunked(&conn->_ostr_final, &conn->req); |
1028 | 9.12k | if (conn->req.send_server_timing) |
1029 | 0 | h2o_add_server_timing_header(&conn->req, conn->_ostr_final.chunked_buf != NULL); |
1030 | | |
1031 | 9.12k | const char *connection = conn->req.http1_is_persistent ? "keep-alive" : "close"; |
1032 | 9.12k | if (conn->req.is_tunnel_req && conn->req.res.status == 101 && conn->req.upgrade.base) |
1033 | 2 | connection = "upgrade"; |
1034 | 9.12k | size_t headers_est_size = |
1035 | 9.12k | flatten_headers_estimate_size(&conn->req, conn->super.ctx->globalconf->server_name.len + strlen(connection)); |
1036 | 9.12k | h2o_sendvec_init_raw(bufs + bufcnt, h2o_mem_alloc_pool(&conn->req.pool, char, headers_est_size), 0); |
1037 | 9.12k | bufs[bufcnt].len = flatten_headers(bufs[bufcnt].raw, &conn->req, connection); |
1038 | 9.12k | conn->req.header_bytes_sent += bufs[bufcnt].len; |
1039 | 9.12k | ++bufcnt; |
1040 | 9.12k | h2o_probe_log_response(&conn->req, conn->_req_index); |
1041 | 9.12k | conn->_ostr_final.state = OSTREAM_STATE_BODY; |
1042 | 9.12k | } |
1043 | | |
1044 | 10.5k | if (conn->_ostr_final.chunked_buf != NULL) |
1045 | 2.29k | chunked_prefix_index = bufcnt++; |
1046 | | |
1047 | 10.5k | size_t bytes_sent = 0; |
1048 | 18.7k | for (size_t i = 0; i != inbufcnt; ++i) { |
1049 | 8.27k | bufs[bufcnt++] = inbufs[i]; |
1050 | 8.27k | bytes_sent += inbufs[i].len; |
1051 | 8.27k | } |
1052 | 10.5k | conn->req.bytes_sent += bytes_sent; |
1053 | | |
1054 | 10.5k | if (conn->_ostr_final.chunked_buf != NULL) { |
1055 | 2.29k | encode_chunked(bufs + chunked_prefix_index, bufs + bufcnt, send_state, bytes_sent, conn->req.send_server_timing != 0, |
1056 | 2.29k | conn->_ostr_final.chunked_buf); |
1057 | 2.29k | if (bufs[bufcnt].len != 0) |
1058 | 2.29k | ++bufcnt; |
1059 | 2.29k | } |
1060 | | |
1061 | 10.5k | if (bufcnt != 0) |
1062 | 10.2k | set_req_io_timeout(conn, conn->super.ctx->globalconf->http1.req_io_timeout, req_io_on_timeout); |
1063 | | |
1064 | 10.5k | h2o_socket_sendvec(conn->sock, bufs, bufcnt, h2o_send_state_is_in_progress(send_state) ? on_send_next : on_send_complete); |
1065 | 10.5k | } |
1066 | | |
1067 | | static void on_send_informational_complete(h2o_socket_t *sock, const char *err); |
1068 | | |
1069 | | static void do_send_informational(struct st_h2o_http1_conn_t *conn) |
1070 | 0 | { |
1071 | 0 | assert(!conn->_ostr_final.informational.write_inflight && conn->_ostr_final.informational.pending.size != 0); |
1072 | | |
1073 | 0 | conn->_ostr_final.informational.write_inflight = 1; |
1074 | 0 | h2o_socket_write(conn->sock, conn->_ostr_final.informational.pending.entries, conn->_ostr_final.informational.pending.size, |
1075 | 0 | on_send_informational_complete); |
1076 | 0 | conn->_ostr_final.informational.pending.size = 0; |
1077 | 0 | } |
1078 | | |
1079 | | static void on_send_informational_complete(h2o_socket_t *sock, const char *err) |
1080 | 0 | { |
1081 | 0 | struct st_h2o_http1_conn_t *conn = sock->data; |
1082 | 0 | if (err != NULL) { |
1083 | 0 | close_connection(conn, 1); |
1084 | 0 | return; |
1085 | 0 | } |
1086 | | |
1087 | 0 | conn->_ostr_final.informational.write_inflight = 0; |
1088 | |
|
1089 | 0 | if (conn->_ostr_final.informational.pending_final.inbufs != NULL) { |
1090 | 0 | finalostream_send(&conn->_ostr_final.super, &conn->req, conn->_ostr_final.informational.pending_final.inbufs, |
1091 | 0 | conn->_ostr_final.informational.pending_final.inbufcnt, |
1092 | 0 | conn->_ostr_final.informational.pending_final.send_state); |
1093 | 0 | return; |
1094 | 0 | } |
1095 | | |
1096 | 0 | if (conn->_ostr_final.informational.pending.size != 0) |
1097 | 0 | do_send_informational(conn); |
1098 | 0 | } |
1099 | | |
1100 | | static void finalostream_send_informational(h2o_ostream_t *_self, h2o_req_t *req) |
1101 | 0 | { |
1102 | 0 | struct st_h2o_http1_conn_t *conn = (struct st_h2o_http1_conn_t *)req->conn; |
1103 | 0 | assert(_self == &conn->_ostr_final.super); |
1104 | | |
1105 | 0 | size_t len = sizeof("HTTP/1.1 \r\n\r\n") + 3 + strlen(req->res.reason) - 1; |
1106 | 0 | h2o_iovec_t buf = h2o_iovec_init(NULL, len); |
1107 | |
|
1108 | 0 | int i; |
1109 | 0 | for (i = 0; i != req->res.headers.size; ++i) |
1110 | 0 | buf.len += req->res.headers.entries[i].name->len + req->res.headers.entries[i].value.len + 4; |
1111 | |
|
1112 | 0 | buf.base = h2o_mem_alloc_pool(&req->pool, char, buf.len); |
1113 | 0 | char *dst = buf.base; |
1114 | 0 | dst += sprintf(dst, "HTTP/1.1 %d %s\r\n", req->res.status, req->res.reason); |
1115 | 0 | dst += flatten_res_headers(dst, req); |
1116 | 0 | *dst++ = '\r'; |
1117 | 0 | *dst++ = '\n'; |
1118 | |
|
1119 | 0 | req->header_bytes_sent += dst - buf.base; |
1120 | |
|
1121 | 0 | h2o_vector_reserve(&req->pool, &conn->_ostr_final.informational.pending, conn->_ostr_final.informational.pending.size + 1); |
1122 | 0 | conn->_ostr_final.informational.pending.entries[conn->_ostr_final.informational.pending.size++] = buf; |
1123 | |
|
1124 | 0 | if (!conn->_ostr_final.informational.write_inflight) |
1125 | 0 | do_send_informational(conn); |
1126 | 0 | } |
1127 | | |
1128 | | static socklen_t get_sockname(h2o_conn_t *_conn, struct sockaddr *sa) |
1129 | 0 | { |
1130 | 0 | struct st_h2o_http1_conn_t *conn = (void *)_conn; |
1131 | 0 | return h2o_socket_getsockname(conn->sock, sa); |
1132 | 0 | } |
1133 | | |
1134 | | static socklen_t get_peername(h2o_conn_t *_conn, struct sockaddr *sa) |
1135 | 2.15k | { |
1136 | 2.15k | struct st_h2o_http1_conn_t *conn = (void *)_conn; |
1137 | 2.15k | return h2o_socket_getpeername(conn->sock, sa); |
1138 | 2.15k | } |
1139 | | |
1140 | | static ptls_t *get_ptls(h2o_conn_t *_conn) |
1141 | 2.15k | { |
1142 | 2.15k | struct st_h2o_http1_conn_t *conn = (void *)_conn; |
1143 | 2.15k | assert(conn->sock != NULL && "it never becomes NULL, right?"); |
1144 | 2.15k | return h2o_socket_get_ptls(conn->sock); |
1145 | 2.15k | } |
1146 | | |
1147 | | static int skip_tracing(h2o_conn_t *_conn) |
1148 | 0 | { |
1149 | 0 | struct st_h2o_http1_conn_t *conn = (void *)_conn; |
1150 | 0 | return h2o_socket_skip_tracing(conn->sock); |
1151 | 0 | } |
1152 | | |
1153 | | static int can_zerocopy(h2o_conn_t *_conn) |
1154 | 2.15k | { |
1155 | 2.15k | struct st_h2o_http1_conn_t *conn = (void *)_conn; |
1156 | 2.15k | return conn->sock->ssl == NULL || h2o_socket_can_tls_offload(conn->sock); |
1157 | 2.15k | } |
1158 | | |
1159 | | static uint64_t get_req_id(h2o_req_t *req) |
1160 | 0 | { |
1161 | 0 | struct st_h2o_http1_conn_t *conn = H2O_STRUCT_FROM_MEMBER(struct st_h2o_http1_conn_t, req, req); |
1162 | 0 | return conn->_req_index; |
1163 | 0 | } |
1164 | | |
1165 | | static h2o_socket_t *steal_socket(h2o_conn_t *_conn) |
1166 | 0 | { |
1167 | 0 | struct st_h2o_http1_conn_t *conn = (void *)_conn; |
1168 | 0 | h2o_socket_t *sock = conn->sock; |
1169 | |
|
1170 | 0 | if (sock->ssl != NULL) |
1171 | 0 | return NULL; |
1172 | | |
1173 | 0 | close_connection(conn, 0); |
1174 | 0 | return sock; |
1175 | 0 | } |
1176 | | |
1177 | | #define DEFINE_LOGGER(name) \ |
1178 | | static h2o_iovec_t log_##name(h2o_req_t *req) \ |
1179 | 0 | { \ |
1180 | 0 | struct st_h2o_http1_conn_t *conn = (void *)req->conn; \ |
1181 | 0 | return h2o_socket_log_##name(conn->sock, &req->pool); \ |
1182 | 0 | } Unexecuted instantiation: http1.c:log_tcp_congestion_controller Unexecuted instantiation: http1.c:log_tcp_delivery_rate Unexecuted instantiation: http1.c:log_ssl_protocol_version Unexecuted instantiation: http1.c:log_ssl_session_reused Unexecuted instantiation: http1.c:log_ssl_cipher Unexecuted instantiation: http1.c:log_ssl_cipher_bits Unexecuted instantiation: http1.c:log_ssl_session_id Unexecuted instantiation: http1.c:log_ssl_server_name Unexecuted instantiation: http1.c:log_ssl_negotiated_protocol Unexecuted instantiation: http1.c:log_ssl_ech_config_id Unexecuted instantiation: http1.c:log_ssl_ech_kem Unexecuted instantiation: http1.c:log_ssl_ech_cipher Unexecuted instantiation: http1.c:log_ssl_ech_cipher_bits Unexecuted instantiation: http1.c:log_ssl_backend |
1183 | | |
1184 | | DEFINE_LOGGER(tcp_congestion_controller) |
1185 | | DEFINE_LOGGER(tcp_delivery_rate) |
1186 | | DEFINE_LOGGER(ssl_protocol_version) |
1187 | | DEFINE_LOGGER(ssl_session_reused) |
1188 | | DEFINE_LOGGER(ssl_cipher) |
1189 | | DEFINE_LOGGER(ssl_cipher_bits) |
1190 | | DEFINE_LOGGER(ssl_session_id) |
1191 | | DEFINE_LOGGER(ssl_server_name) |
1192 | | DEFINE_LOGGER(ssl_negotiated_protocol) |
1193 | | DEFINE_LOGGER(ssl_ech_config_id) |
1194 | | DEFINE_LOGGER(ssl_ech_kem) |
1195 | | DEFINE_LOGGER(ssl_ech_cipher) |
1196 | | DEFINE_LOGGER(ssl_ech_cipher_bits) |
1197 | | DEFINE_LOGGER(ssl_backend) |
1198 | | |
1199 | | #undef DEFINE_LOGGER |
1200 | | |
1201 | | static h2o_iovec_t log_request_index(h2o_req_t *req) |
1202 | 0 | { |
1203 | 0 | struct st_h2o_http1_conn_t *conn = (void *)req->conn; |
1204 | 0 | char *s = h2o_mem_alloc_pool(&req->pool, char, sizeof(H2O_UINT64_LONGEST_STR)); |
1205 | 0 | size_t len = sprintf(s, "%" PRIu64, conn->_req_index); |
1206 | 0 | return h2o_iovec_init(s, len); |
1207 | 0 | } |
1208 | | |
1209 | | static int foreach_request(h2o_conn_t *_conn, int (*cb)(h2o_req_t *req, void *cbdata), void *cbdata) |
1210 | 0 | { |
1211 | 0 | struct st_h2o_http1_conn_t *conn = (void *)_conn; |
1212 | 0 | return cb(&conn->req, cbdata); |
1213 | 0 | } |
1214 | | |
1215 | | static void initiate_graceful_shutdown(h2o_conn_t *_conn) |
1216 | 0 | { |
1217 | | /* note: nothing special needs to be done for handling graceful shutdown */ |
1218 | 0 | } |
1219 | | |
1220 | | static const h2o_conn_callbacks_t h1_callbacks = { |
1221 | | .get_sockname = get_sockname, |
1222 | | .get_peername = get_peername, |
1223 | | .get_ptls = get_ptls, |
1224 | | .skip_tracing = skip_tracing, |
1225 | | .close_idle_connection = close_idle_connection, |
1226 | | .foreach_request = foreach_request, |
1227 | | .request_shutdown = initiate_graceful_shutdown, |
1228 | | .can_zerocopy = can_zerocopy, |
1229 | | .get_req_id = get_req_id, |
1230 | | .steal_socket = steal_socket, |
1231 | | .log_ = {{ |
1232 | | .transport = |
1233 | | { |
1234 | | .cc_name = log_tcp_congestion_controller, |
1235 | | .delivery_rate = log_tcp_delivery_rate, |
1236 | | }, |
1237 | | .ssl = |
1238 | | { |
1239 | | .protocol_version = log_ssl_protocol_version, |
1240 | | .session_reused = log_ssl_session_reused, |
1241 | | .cipher = log_ssl_cipher, |
1242 | | .cipher_bits = log_ssl_cipher_bits, |
1243 | | .session_id = log_ssl_session_id, |
1244 | | .server_name = log_ssl_server_name, |
1245 | | .negotiated_protocol = log_ssl_negotiated_protocol, |
1246 | | .ech_config_id = log_ssl_ech_config_id, |
1247 | | .ech_kem = log_ssl_ech_kem, |
1248 | | .ech_cipher = log_ssl_ech_cipher, |
1249 | | .ech_cipher_bits = log_ssl_ech_cipher_bits, |
1250 | | .backend = log_ssl_backend, |
1251 | | }, |
1252 | | .http1 = |
1253 | | { |
1254 | | .request_index = log_request_index, |
1255 | | }, |
1256 | | }}, |
1257 | | }; |
1258 | | |
1259 | | static int conn_is_h1(h2o_conn_t *conn) |
1260 | 127 | { |
1261 | 127 | return conn->callbacks == &h1_callbacks; |
1262 | 127 | } |
1263 | | |
1264 | | void h2o_http1_accept(h2o_accept_ctx_t *ctx, h2o_socket_t *sock, struct timeval connected_at) |
1265 | 10.1k | { |
1266 | 10.1k | struct st_h2o_http1_conn_t *conn = |
1267 | 10.1k | (void *)h2o_create_connection(sizeof(*conn), ctx->ctx, ctx->hosts, connected_at, &h1_callbacks); |
1268 | | |
1269 | | /* zero-fill all properties expect req */ |
1270 | 10.1k | memset((char *)conn + sizeof(conn->super), 0, offsetof(struct st_h2o_http1_conn_t, req) - sizeof(conn->super)); |
1271 | | |
1272 | | /* init properties that need to be non-zero */ |
1273 | 10.1k | conn->sock = sock; |
1274 | 10.1k | sock->data = conn; |
1275 | | |
1276 | 10.1k | H2O_PROBE_CONN(H1_ACCEPT, &conn->super, conn->sock, &conn->super, h2o_conn_get_uuid(&conn->super)); |
1277 | | |
1278 | 10.1k | init_request(conn); |
1279 | 10.1k | reqread_start(conn); |
1280 | 10.1k | } |
1281 | | |
1282 | | void h2o_http1_upgrade(h2o_req_t *req, h2o_iovec_t *inbufs, size_t inbufcnt, h2o_http1_upgrade_cb on_complete, void *user_data) |
1283 | 127 | { |
1284 | | |
1285 | 127 | assert(conn_is_h1(req->conn)); |
1286 | 127 | struct st_h2o_http1_conn_t *conn = (void *)req->conn; |
1287 | | |
1288 | 127 | h2o_iovec_t *bufs = alloca(sizeof(h2o_iovec_t) * (inbufcnt + 1)); |
1289 | | |
1290 | 127 | conn->upgrade.data = user_data; |
1291 | 127 | conn->upgrade.cb = on_complete; |
1292 | | |
1293 | 127 | bufs[0].base = h2o_mem_alloc_pool( |
1294 | 127 | &conn->req.pool, char, |
1295 | 127 | flatten_headers_estimate_size(&conn->req, conn->super.ctx->globalconf->server_name.len + sizeof("upgrade") - 1)); |
1296 | 127 | bufs[0].len = flatten_headers(bufs[0].base, &conn->req, conn->req.res.status == 101 ? "upgrade" : "close"); |
1297 | 127 | h2o_memcpy(bufs + 1, inbufs, sizeof(h2o_iovec_t) * inbufcnt); |
1298 | | |
1299 | 127 | h2o_socket_write(conn->sock, bufs, inbufcnt + 1, on_upgrade_complete); |
1300 | 127 | } |