-
Notifications
You must be signed in to change notification settings - Fork 0
/
http1.c
388 lines (333 loc) · 12.9 KB
/
http1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* Copyright (c) 2014 DeNA Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <alloca.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdlib.h>
#include "h2o.h"
#include "h2o/http1.h"
#include "h2o/http2.h"
struct st_h2o_http1_req_entity_reader {
int (*handle_incoming_entity)(h2o_http1_conn_t *conn);
size_t entity_offset;
};
static void finalostream_send(h2o_ostream_t *_self, h2o_req_t *req, h2o_buf_t *inbufs, size_t inbufcnt, int is_final);
static void init_request(h2o_http1_conn_t *conn, int reinit)
{
if (reinit)
h2o_dispose_request(&conn->req);
h2o_init_request(&conn->req, &conn->super, NULL);
conn->req._ostr_top = &conn->_ostr_final.super;
conn->_ostr_final.super.do_send = finalostream_send;
conn->_ostr_final.sent_headers = 0;
}
static void close_connection(h2o_http1_conn_t *conn)
{
h2o_timeout_unlink(conn->_timeout, &conn->_timeout_entry);
h2o_dispose_request(&conn->req);
free(conn->_req_entity_reader);
if (conn->sock != NULL)
h2o_socket_close(conn->sock);
free(conn);
}
static void set_timeout(h2o_http1_conn_t *conn, h2o_timeout_t *timeout, h2o_timeout_cb cb)
{
if (conn->_timeout != NULL) {
h2o_timeout_unlink(conn->_timeout, &conn->_timeout_entry);
conn->_timeout_entry.cb = NULL;
}
conn->_timeout = timeout;
if (timeout != NULL) {
h2o_timeout_link(conn->super.ctx->loop, timeout, &conn->_timeout_entry);
conn->_timeout_entry.cb = cb;
}
}
static void process_request(h2o_http1_conn_t *conn)
{
if (conn->sock->ssl == NULL && conn->req.upgrade.base != NULL && h2o_lcstris(conn->req.upgrade.base, conn->req.upgrade.len, H2O_STRLIT("h2c-14"))) {
if (h2o_http2_handle_upgrade(&conn->req) == 0) {
return;
}
}
h2o_process_request(&conn->req);
}
static int create_chunked_entity_reader(h2o_http1_conn_t *conn)
{
return -1;
}
static int handle_content_length_entity_read(h2o_http1_conn_t *conn)
{
/* wait until: reqsize == conn->_input.size */
if (conn->sock->input->size < conn->_reqsize)
return -2;
/* all input has arrived */
h2o_vector_reserve(&conn->req.pool, (h2o_vector_t*)&conn->req.entity, sizeof(h2o_buf_t), 1);
conn->req.entity.entries[0].base = conn->sock->input->bytes + conn->_req_entity_reader->entity_offset;
conn->req.entity.entries[0].len = conn->_reqsize - conn->_req_entity_reader->entity_offset;
conn->req.entity.size = 1;
free(conn->_req_entity_reader);
conn->_req_entity_reader = NULL;
set_timeout(conn, NULL, NULL);
h2o_socket_read_stop(conn->sock);
process_request(conn);
return 0;
}
static int create_content_length_entity_reader(h2o_http1_conn_t *conn, size_t content_length)
{
struct st_h2o_http1_req_entity_reader *reader = h2o_malloc(sizeof(*reader));
conn->_req_entity_reader = reader;
reader->handle_incoming_entity = handle_content_length_entity_read;
reader->entity_offset = conn->_reqsize;
conn->_reqsize += content_length;
return 0;
}
static int create_entity_reader(h2o_http1_conn_t *conn, const struct phr_header *entity_header)
{
if (entity_header->name_len == sizeof("content-encoding") - 1) {
/* content-encoding */
if (h2o_lcstris(entity_header->value, entity_header->value_len, H2O_STRLIT("chunked"))) {
return create_chunked_entity_reader(conn);
}
} else {
/* content-length */
char *endptr;
intmax_t content_length = strtoimax(h2o_strdup(&conn->req.pool, entity_header->value, entity_header->value_len).base, &endptr, 10);
if (*endptr == '\0' && content_length != INTMAX_MAX && 0 <= content_length && content_length <= conn->super.ctx->global_config->max_request_entity_size) {
return create_content_length_entity_reader(conn, (size_t)content_length);
}
}
/* failed */
return -1;
}
static ssize_t fixup_request(h2o_http1_conn_t *conn, struct phr_header *headers, size_t num_headers, int minor_version)
{
ssize_t entity_header_index;
h2o_buf_t connection = { NULL, 0 }, host = { NULL, 0 }, upgrade = { NULL, 0 };
conn->req.scheme = h2o_buf_init(H2O_STRLIT("http"));
conn->req.version = 0x100 | minor_version;
/* init headers */
entity_header_index = h2o_init_headers(&conn->req.pool, &conn->req.headers, headers, num_headers, &connection, &host, &upgrade);
/* move host header to req->authority */
if (host.base != NULL)
conn->req.authority = host;
/* setup persistent flag (and upgrade info) */
if (connection.base != NULL) {
/* TODO contains_token function can be faster */
if (h2o_contains_token(connection.base, connection.len, H2O_STRLIT("keep-alive"))) {
conn->req.http1_is_persistent = 1;
}
if (upgrade.base != NULL && h2o_contains_token(connection.base, connection.len, H2O_STRLIT("upgrade"))) {
conn->req.upgrade = upgrade;
}
} else if (conn->req.version >= 0x101) {
/* defaults to keep-alive if >= HTTP/1.1 */
conn->req.http1_is_persistent = 1;
}
return entity_header_index;
}
static int handle_incoming_request(h2o_http1_conn_t *conn)
{
size_t inreqlen = conn->sock->input->size < H2O_MAX_REQLEN ? conn->sock->input->size : H2O_MAX_REQLEN;
int reqlen, minor_version;
struct phr_header headers[H2O_MAX_HEADERS];
size_t num_headers = H2O_MAX_HEADERS;
ssize_t entity_body_header_index;
reqlen = phr_parse_request(
conn->sock->input->bytes, inreqlen,
(const char**)&conn->req.method.base, &conn->req.method.len,
(const char**)&conn->req.path.base, &conn->req.path.len,
&minor_version,
headers, &num_headers,
conn->_prevreqlen);
conn->_prevreqlen = inreqlen;
switch (reqlen) {
default: // parse complete
conn->_reqsize = reqlen;
if ((entity_body_header_index = fixup_request(conn, headers, num_headers, minor_version)) != -1) {
if (create_entity_reader(conn, headers + entity_body_header_index) != 0) {
set_timeout(conn, NULL, NULL);
h2o_socket_read_stop(conn->sock);
conn->req.http1_is_persistent = 0;
h2o_send_error(&conn->req, 400, "Invalid Request", "unknown entity encoding");
return 0;
}
return conn->_req_entity_reader->handle_incoming_entity(conn);
} else {
set_timeout(conn, NULL, NULL);
h2o_socket_read_stop(conn->sock);
process_request(conn);
}
return 0;
case -2: // incomplete
if (inreqlen == H2O_MAX_REQLEN) {
// request is too long (TODO notify)
close_connection(conn);
return 0;
}
return 1;
case -1: // error
close_connection(conn);
return 0;
}
}
static void reqread_on_read(h2o_socket_t *sock, int status)
{
h2o_http1_conn_t *conn = sock->data;
if (status != 0) {
close_connection(conn);
return;
}
if (conn->_req_entity_reader == NULL)
handle_incoming_request(conn);
else
conn->_req_entity_reader->handle_incoming_entity(conn);
}
static void reqread_on_timeout(h2o_timeout_entry_t *entry)
{
h2o_http1_conn_t *conn = H2O_STRUCT_FROM_MEMBER(h2o_http1_conn_t, _timeout_entry, entry);
/* TODO log */
conn->req.http1_is_persistent = 0;
close_connection(conn);
}
static inline void reqread_start(h2o_http1_conn_t *conn)
{
set_timeout(conn, &conn->super.ctx->req_timeout, reqread_on_timeout);
h2o_socket_read_start(conn->sock, reqread_on_read);
}
static void on_send_next(h2o_socket_t *sock, int status)
{
h2o_http1_conn_t *conn = sock->data;
if (status != 0)
close_connection(conn);
else
h2o_proceed_response(&conn->req);
}
static void on_send_complete(h2o_socket_t *sock, int status)
{
h2o_http1_conn_t *conn = sock->data;
assert(conn->req._ostr_top == &conn->_ostr_final.super);
if (! conn->req.http1_is_persistent) {
/* TODO use lingering close */
close_connection(conn);
return;
}
/* handle next request */
init_request(conn, 1);
h2o_consume_input_buffer(&conn->sock->input, conn->_reqsize);
conn->_prevreqlen = 0;
conn->_reqsize = 0;
if (conn->sock->input->size != 0) {
if (handle_incoming_request(conn) == 0) {
return;
}
}
reqread_start(conn);
}
static void on_upgrade_complete(h2o_socket_t *socket, int status)
{
h2o_http1_conn_t *conn = socket->data;
h2o_http1_upgrade_cb cb = conn->upgrade.cb;
void *data = conn->upgrade.data;
h2o_socket_t *sock = NULL;
size_t reqsize = 0;
/* destruct the connection (after detaching the socket) */
if (status == 0) {
sock = conn->sock;
conn->sock = NULL;
reqsize = conn->_reqsize;
}
close_connection(conn);
cb(data, sock, reqsize);
}
static void flatten_headers(h2o_req_t *req, h2o_buf_t *bufs, const char *connection)
{
h2o_context_t *ctx = req->conn->ctx;
h2o_timestamp_t ts;
h2o_get_timestamp(ctx, &req->pool, &ts);
if (req->res.content_length != SIZE_MAX) {
bufs[0] = h2o_sprintf(
&req->pool,
"HTTP/1.1 %d %s\r\ndate: %.*s\r\nserver: %.*s\r\nconnection: %s\r\ncontent-length: %zu\r\n",
req->res.status, req->res.reason,
(int)H2O_TIMESTR_RFC1123_LEN, ts.str->rfc1123,
(int)ctx->global_config->server_name.len, ctx->global_config->server_name.base,
connection,
req->res.content_length);
} else {
bufs[0] = h2o_sprintf(
&req->pool,
"HTTP/1.1 %d %s\r\ndate: %.*s\r\nserver: %.*s\r\nconnection: %s\r\n",
req->res.status, req->res.reason,
(int)H2O_TIMESTR_RFC1123_LEN, ts.str->rfc1123,
(int)ctx->global_config->server_name.len, ctx->global_config->server_name.base,
connection);
}
bufs[1] = h2o_flatten_headers(&req->pool, &req->res.headers);
}
void finalostream_send(h2o_ostream_t *_self, h2o_req_t *req, h2o_buf_t *inbufs, size_t inbufcnt, int is_final)
{
h2o_http1_finalostream_t *self = (void*)_self;
h2o_http1_conn_t *conn = (h2o_http1_conn_t*)req->conn;
h2o_buf_t *bufs = alloca(sizeof(h2o_buf_t) * (inbufcnt + 2));
int bufcnt = 0;
assert(self == &conn->_ostr_final);
if (! self->sent_headers) {
/* build headers and send */
self->sent_headers = 1;
flatten_headers(req, bufs + bufcnt, req->http1_is_persistent ? "keep-alive" : "close");
bufcnt += 2;
}
memcpy(bufs + bufcnt, inbufs, sizeof(h2o_buf_t) * inbufcnt);
bufcnt += inbufcnt;
if (bufcnt != 0) {
h2o_socket_write(conn->sock, bufs, bufcnt, is_final ? on_send_complete : on_send_next);
} else {
on_send_complete(conn->sock, 0);
}
}
static int get_peername(h2o_conn_t *_conn, struct sockaddr *name, socklen_t *namelen)
{
h2o_http1_conn_t *conn = (h2o_http1_conn_t*)_conn;
return h2o_socket_getpeername(conn->sock, name, namelen);
}
void h2o_http1_accept(h2o_context_t *ctx, h2o_socket_t *sock)
{
h2o_http1_conn_t *conn = h2o_malloc(sizeof(*conn));
/* zero-fill all properties expect req */
memset(conn, 0, offsetof(h2o_http1_conn_t, req));
/* init properties that need to be non-zero */
conn->super.ctx = ctx;
conn->super.getpeername = get_peername;
conn->sock = sock;
sock->data = conn;
init_request(conn, 0);
reqread_start(conn);
}
void h2o_http1_upgrade(h2o_http1_conn_t *conn, h2o_buf_t *inbufs, size_t inbufcnt, h2o_http1_upgrade_cb on_complete, void *user_data)
{
h2o_buf_t *bufs = alloca(sizeof(h2o_buf_t) * (inbufcnt + 2));
conn->upgrade.data = user_data;
conn->upgrade.cb = on_complete;
flatten_headers(&conn->req, bufs, "upgrade");
memcpy(bufs + 2, inbufs, sizeof(h2o_buf_t) * inbufcnt);
h2o_socket_write(conn->sock, bufs, (int)(inbufcnt + 2), on_upgrade_complete);
}