Coverage Report

Created: 2026-06-15 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/h2o/lib/core/request.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2014-2016 DeNA Co., Ltd., Kazuho Oku, Tatsuhiro Tsujikawa
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 <fcntl.h>
23
#include <limits.h>
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <sys/uio.h>
27
#include "h2o.h"
28
#include "h2o/socket.h"
29
30
#ifndef IOV_MAX
31
#define IOV_MAX UIO_MAXIOV
32
#endif
33
34
#define INITIAL_INBUFSZ 8192
35
36
struct st_deferred_request_action_t {
37
    h2o_timer_t timeout;
38
    h2o_req_t *req;
39
};
40
41
struct st_reprocess_request_deferred_t {
42
    struct st_deferred_request_action_t super;
43
    h2o_iovec_t method;
44
    const h2o_url_scheme_t *scheme;
45
    h2o_iovec_t authority;
46
    h2o_iovec_t path;
47
    h2o_req_overrides_t *overrides;
48
    int is_delegated;
49
};
50
51
struct st_send_error_deferred_t {
52
    h2o_req_t *req;
53
    int status;
54
    const char *reason;
55
    const char *body;
56
    int flags;
57
    h2o_timer_t _timeout;
58
};
59
60
static void on_deferred_action_dispose(void *_action)
61
0
{
62
0
    struct st_deferred_request_action_t *action = _action;
63
0
    h2o_timer_unlink(&action->timeout);
64
0
}
65
66
static struct st_deferred_request_action_t *create_deferred_action(h2o_req_t *req, size_t sz, h2o_timer_cb cb)
67
0
{
68
0
    struct st_deferred_request_action_t *action = h2o_mem_alloc_shared(&req->pool, sz, on_deferred_action_dispose);
69
0
    action->req = req;
70
0
    h2o_timer_init(&action->timeout, cb);
71
0
    h2o_timer_link(req->conn->ctx->loop, 0, &action->timeout);
72
0
    return action;
73
0
}
74
75
static h2o_hostconf_t *find_hostconf(h2o_hostconf_t **hostconfs, h2o_iovec_t authority, uint16_t default_port,
76
                                     h2o_iovec_t *wildcard_match)
77
0
{
78
0
    h2o_iovec_t hostname;
79
0
    uint16_t port;
80
0
    char *hostname_lc;
81
82
    /* safe-guard for alloca */
83
0
    if (authority.len >= 65536)
84
0
        return NULL;
85
86
    /* extract the specified hostname and port */
87
0
    if (h2o_url_parse_hostport(authority.base, authority.len, &hostname, &port) == NULL)
88
0
        return NULL;
89
0
    if (port == 65535)
90
0
        port = default_port;
91
92
    /* convert supplied hostname to lower-case */
93
0
    hostname_lc = alloca(hostname.len);
94
0
    h2o_strcopytolower(hostname_lc, hostname.base, hostname.len);
95
96
0
    do {
97
0
        h2o_hostconf_t *hostconf = *hostconfs;
98
0
        if (hostconf->authority.port == port || (hostconf->authority.port == 65535 && port == default_port)) {
99
0
            if (hostconf->authority.host.base[0] == '*') {
100
                /* matching against "*.foo.bar" */
101
0
                size_t cmplen = hostconf->authority.host.len - 1;
102
0
                if (cmplen < hostname.len &&
103
0
                    memcmp(hostconf->authority.host.base + 1, hostname_lc + hostname.len - cmplen, cmplen) == 0) {
104
0
                    *wildcard_match = h2o_iovec_init(hostname.base, hostname.len - cmplen);
105
0
                    return hostconf;
106
0
                }
107
0
            } else {
108
                /* exact match */
109
0
                if (h2o_memis(hostconf->authority.host.base, hostconf->authority.host.len, hostname_lc, hostname.len))
110
0
                    return hostconf;
111
0
            }
112
0
        }
113
0
    } while (*++hostconfs != NULL);
114
115
0
    return NULL;
116
0
}
117
118
static h2o_hostconf_t *find_default_hostconf(h2o_hostconf_t **hostconfs)
119
21.2k
{
120
21.2k
    h2o_hostconf_t *fallback_host = hostconfs[0]->global->fallback_host;
121
122
21.2k
    do {
123
21.2k
        h2o_hostconf_t *hostconf = *hostconfs;
124
21.2k
        if (!hostconf->strict_match)
125
21.2k
            return hostconf;
126
21.2k
    } while (*++hostconfs != NULL);
127
128
0
    return fallback_host;
129
21.2k
}
130
131
h2o_hostconf_t *h2o_req_setup(h2o_req_t *req)
132
21.2k
{
133
21.2k
    h2o_context_t *ctx = req->conn->ctx;
134
21.2k
    h2o_hostconf_t *hostconf;
135
136
21.2k
    req->processed_at = h2o_get_timestamp(ctx, &req->pool);
137
138
    /* find the host context (or use the default if authority is missing or is of zero-length) */
139
21.2k
    if (req->input.authority.len != 0) {
140
8.04k
        if (req->conn->hosts[1] == NULL ||
141
0
            (hostconf = find_hostconf(req->conn->hosts, req->input.authority, req->input.scheme->default_port,
142
0
                                      &req->authority_wildcard_match)) == NULL)
143
8.04k
            hostconf = find_default_hostconf(req->conn->hosts);
144
13.2k
    } else {
145
13.2k
        hostconf = find_default_hostconf(req->conn->hosts);
146
13.2k
        req->input.authority = hostconf->authority.hostport;
147
13.2k
    }
148
149
21.2k
    req->scheme = req->input.scheme;
150
21.2k
    req->method = req->input.method;
151
21.2k
    req->authority = req->input.authority;
152
21.2k
    req->path = req->input.path;
153
21.2k
    int has_null_char;
154
21.2k
    req->path_normalized = h2o_url_normalize_path(&req->pool, req->input.path.base, req->input.path.len, &req->query_at,
155
21.2k
                                                  &req->norm_indexes, &has_null_char);
156
21.2k
    req->path_normalized_has_null_char = has_null_char;
157
21.2k
    req->input.query_at = req->query_at; /* we can do this since input.path == path */
158
159
21.2k
    return hostconf;
160
21.2k
}
161
162
static void call_handlers(h2o_req_t *req, h2o_handler_t **handler)
163
14.8k
{
164
14.8k
    h2o_handler_t **end = req->pathconf->handlers.entries + req->pathconf->handlers.size;
165
166
25.9k
    for (; handler != end; ++handler) {
167
14.8k
        req->handler = *handler;
168
14.8k
        if ((*handler)->on_req(*handler, req) == 0)
169
3.88k
            return;
170
14.8k
    }
171
172
11.0k
    h2o_send_error_404(req, "File Not Found", "not found", 0);
173
11.0k
}
174
175
static void setup_pathconf(h2o_req_t *req, h2o_hostconf_t *hostconf)
176
15.3k
{
177
15.3k
    h2o_pathconf_t *selected_pathconf = &hostconf->fallback_path;
178
15.3k
    size_t i;
179
180
    /* setup pathconf, or redirect to "path/" */
181
40.7k
    for (i = 0; i != hostconf->paths.size; ++i) {
182
40.7k
        h2o_pathconf_t *candidate = hostconf->paths.entries[i];
183
40.7k
        if (req->path_normalized.len >= candidate->path.len &&
184
20.8k
            memcmp(req->path_normalized.base, candidate->path.base, candidate->path.len) == 0 &&
185
15.4k
            (candidate->path.base[candidate->path.len - 1] == '/' || req->path_normalized.len == candidate->path.len ||
186
15.3k
             req->path_normalized.base[candidate->path.len] == '/')) {
187
15.3k
            selected_pathconf = candidate;
188
15.3k
            break;
189
15.3k
        }
190
40.7k
    }
191
15.3k
    h2o_req_bind_conf(req, hostconf, selected_pathconf);
192
15.3k
}
193
194
static void deferred_proceed_cb(h2o_timer_t *entry)
195
0
{
196
0
    h2o_req_t *req = H2O_STRUCT_FROM_MEMBER(h2o_req_t, _timeout_entry, entry);
197
0
    h2o_proceed_response(req);
198
0
}
199
200
static void close_generator_and_filters(h2o_req_t *req)
201
78.9k
{
202
    /* close the generator if it is still open */
203
78.9k
    if (req->_generator != NULL) {
204
        /* close generator */
205
92
        if (req->_generator->stop != NULL)
206
92
            req->_generator->stop(req->_generator, req);
207
92
        req->_generator = NULL;
208
92
    }
209
    /* close the ostreams still open */
210
78.9k
    while (req->_ostr_top->next != NULL) {
211
0
        if (req->_ostr_top->stop != NULL)
212
0
            req->_ostr_top->stop(req->_ostr_top, req);
213
0
        req->_ostr_top = req->_ostr_top->next;
214
0
    }
215
78.9k
}
216
217
static void reset_response(h2o_req_t *req)
218
3.76k
{
219
3.76k
    req->res = (h2o_res_t){0, NULL, SIZE_MAX};
220
3.76k
    req->res.reason = "OK";
221
3.76k
    req->_next_filter_index = 0;
222
3.76k
    req->bytes_sent = 0;
223
3.76k
}
224
225
static void retain_original_response(h2o_req_t *req)
226
24.2k
{
227
24.2k
    if (req->res.original.status != 0)
228
0
        return;
229
230
24.2k
    req->res.original.status = req->res.status;
231
24.2k
    h2o_vector_reserve(&req->pool, &req->res.original.headers, req->res.headers.size);
232
24.2k
    h2o_memcpy(req->res.original.headers.entries, req->res.headers.entries,
233
24.2k
               sizeof(req->res.headers.entries[0]) * req->res.headers.size);
234
24.2k
    req->res.original.headers.size = req->res.headers.size;
235
24.2k
}
236
237
void h2o_write_error_log(h2o_iovec_t prefix, h2o_iovec_t msg)
238
124
{
239
    /* use writev(2) to emit error atomically */
240
124
    struct iovec vecs[] = {{prefix.base, prefix.len}, {msg.base, msg.len}, {"\n", 1}};
241
124
    H2O_BUILD_ASSERT(sizeof(vecs) / sizeof(vecs[0]) <= IOV_MAX);
242
124
    writev(2, vecs, sizeof(vecs) / sizeof(vecs[0]));
243
124
}
244
245
static void on_default_error_callback(void *data, h2o_iovec_t prefix, h2o_iovec_t msg)
246
124
{
247
124
    h2o_req_t *req = (void *)data;
248
124
    if (req->error_logs == NULL)
249
124
        h2o_buffer_init(&req->error_logs, &h2o_socket_buffer_prototype);
250
124
    h2o_buffer_append(&req->error_logs, prefix.base, prefix.len);
251
124
    h2o_buffer_append(&req->error_logs, msg.base, msg.len);
252
253
124
    if (req->pathconf->error_log.emit_request_errors) {
254
124
        h2o_write_error_log(prefix, msg);
255
124
    }
256
124
}
257
258
void h2o_init_request(h2o_req_t *req, h2o_conn_t *conn, h2o_req_t *src)
259
75.2k
{
260
    /* clear all memory (expect memory pool, since it is large) */
261
75.2k
    memset(req, 0, offsetof(h2o_req_t, pool));
262
263
    /* init memory pool (before others, since it may be used) */
264
75.2k
    h2o_mem_init_pool(&req->pool);
265
266
    /* init properties that should be initialized to non-zero */
267
75.2k
    req->conn = conn;
268
75.2k
    req->_timeout_entry.cb = deferred_proceed_cb;
269
75.2k
    req->res.reason = "OK"; /* default to "OK" regardless of the status value, it's not important after all (never sent in HTTP2) */
270
75.2k
    req->res.content_length = SIZE_MAX;
271
75.2k
    req->preferred_chunk_size = SIZE_MAX;
272
75.2k
    req->content_length = SIZE_MAX;
273
75.2k
    req->remaining_delegations = conn == NULL ? 0 : conn->ctx->globalconf->max_delegations;
274
75.2k
    req->remaining_reprocesses = conn == NULL ? 0 : conn->ctx->globalconf->max_reprocesses;
275
75.2k
    req->error_log_delegate.cb = on_default_error_callback;
276
75.2k
    req->error_log_delegate.data = req;
277
278
75.2k
    if (src != NULL) {
279
95
        size_t i;
280
95
#define COPY(buf)                                                                                                                  \
281
285
    do {                                                                                                                           \
282
285
        req->buf.base = h2o_mem_alloc_pool(&req->pool, char, src->buf.len);                                                        \
283
285
        memcpy(req->buf.base, src->buf.base, src->buf.len);                                                                        \
284
285
        req->buf.len = src->buf.len;                                                                                               \
285
285
    } while (0)
286
95
        COPY(input.authority);
287
95
        COPY(input.method);
288
95
        COPY(input.path);
289
95
        req->input.scheme = src->input.scheme;
290
95
        req->version = src->version;
291
95
        req->entity = src->entity;
292
95
        req->http1_is_persistent = src->http1_is_persistent;
293
95
        req->timestamps = src->timestamps;
294
95
        if (src->upgrade.base != NULL) {
295
0
            COPY(upgrade);
296
95
        } else {
297
95
            req->upgrade.base = NULL;
298
95
            req->upgrade.len = 0;
299
95
        }
300
95
#undef COPY
301
95
        h2o_vector_reserve(&req->pool, &req->headers, src->headers.size);
302
95
        req->headers.size = src->headers.size;
303
939
        for (i = 0; i != src->headers.size; ++i) {
304
844
            h2o_header_t *dst_header = req->headers.entries + i, *src_header = src->headers.entries + i;
305
844
            if (h2o_iovec_is_token(src_header->name)) {
306
357
                dst_header->name = src_header->name;
307
487
            } else {
308
487
                dst_header->name = h2o_mem_alloc_pool(&req->pool, *dst_header->name, 1);
309
487
                *dst_header->name = h2o_strdup(&req->pool, src_header->name->base, src_header->name->len);
310
487
            }
311
844
            dst_header->value = h2o_strdup(&req->pool, src_header->value.base, src_header->value.len);
312
844
            dst_header->flags = src_header->flags;
313
844
            if (!src_header->orig_name)
314
0
                dst_header->orig_name = NULL;
315
844
            else
316
844
                dst_header->orig_name = h2o_strdup(&req->pool, src_header->orig_name, src_header->name->len).base;
317
844
        }
318
95
        if (src->env.size != 0) {
319
0
            h2o_vector_reserve(&req->pool, &req->env, src->env.size);
320
0
            req->env.size = src->env.size;
321
0
            for (i = 0; i != req->env.size; ++i)
322
0
                req->env.entries[i] = h2o_strdup(&req->pool, src->env.entries[i].base, src->env.entries[i].len);
323
0
        }
324
95
    }
325
75.2k
}
326
327
void h2o_dispose_request(h2o_req_t *req)
328
75.2k
{
329
75.2k
    close_generator_and_filters(req);
330
331
75.2k
    h2o_timer_unlink(&req->_timeout_entry);
332
333
75.2k
    if (req->pathconf != NULL && req->num_loggers != 0) {
334
0
        for (h2o_logger_t **logger = req->loggers, **end = logger + req->num_loggers; logger != end; ++logger) {
335
0
            (*logger)->log_access((*logger), req);
336
0
        }
337
0
    }
338
339
75.2k
    if (req->error_logs != NULL)
340
124
        h2o_buffer_dispose(&req->error_logs);
341
342
75.2k
    h2o_mem_clear_pool(&req->pool);
343
75.2k
}
344
345
int h2o_req_validate_pseudo_headers(h2o_req_t *req)
346
10.5k
{
347
10.5k
    if (h2o_memis(req->input.method.base, req->input.method.len, H2O_STRLIT("CONNECT-UDP"))) {
348
        /* The draft requires "masque" in `:scheme` but we need to support clients that put "https" there instead. */
349
3
        if (req->input.scheme != &H2O_URL_SCHEME_MASQUE && req->input.scheme != &H2O_URL_SCHEME_HTTPS)
350
2
            return 0;
351
1
        if (!h2o_memis(req->input.path.base, req->input.path.len, H2O_STRLIT("/")))
352
0
            return 0;
353
10.5k
    } else {
354
10.5k
        if (req->input.scheme == &H2O_URL_SCHEME_MASQUE)
355
2
            return 0;
356
10.5k
    }
357
358
10.5k
    return 1;
359
10.5k
}
360
361
h2o_handler_t *h2o_get_first_handler(h2o_req_t *req)
362
1.73k
{
363
1.73k
    if (req->pathconf == NULL) {
364
1.73k
        h2o_hostconf_t *hostconf = h2o_req_setup(req);
365
1.73k
        setup_pathconf(req, hostconf);
366
1.73k
    }
367
1.73k
    return req->pathconf->handlers.size != 0 ? req->pathconf->handlers.entries[0] : NULL;
368
1.73k
}
369
370
void h2o_process_request(h2o_req_t *req)
371
14.8k
{
372
14.8k
    assert(!req->process_called);
373
14.8k
    req->process_called = 1;
374
375
14.8k
    if (req->pathconf == NULL) {
376
13.6k
        h2o_hostconf_t *hostconf = h2o_req_setup(req);
377
13.6k
        setup_pathconf(req, hostconf);
378
13.6k
    }
379
14.8k
    call_handlers(req, req->pathconf->handlers.entries);
380
14.8k
}
381
382
void h2o_delegate_request(h2o_req_t *req)
383
0
{
384
0
    h2o_handler_t **handler = req->pathconf->handlers.entries, **end = handler + req->pathconf->handlers.size;
385
0
    for (;; ++handler) {
386
0
        assert(handler != end);
387
0
        if (*handler == req->handler)
388
0
            break;
389
0
    }
390
0
    ++handler;
391
0
    call_handlers(req, handler);
392
0
}
393
394
static void on_delegate_request_cb(h2o_timer_t *entry)
395
0
{
396
0
    struct st_deferred_request_action_t *args = H2O_STRUCT_FROM_MEMBER(struct st_deferred_request_action_t, timeout, entry);
397
0
    h2o_delegate_request(args->req);
398
0
}
399
400
void h2o_delegate_request_deferred(h2o_req_t *req)
401
0
{
402
0
    create_deferred_action(req, sizeof(struct st_deferred_request_action_t), on_delegate_request_cb);
403
0
}
404
405
static void process_resolved_request(h2o_req_t *req, h2o_hostconf_t **hosts)
406
3.76k
{
407
3.76k
    h2o_hostconf_t *hostconf;
408
3.76k
    if (req->overrides == NULL &&
409
0
        (hostconf = find_hostconf(hosts, req->authority, req->scheme->default_port, &req->authority_wildcard_match)) != NULL) {
410
0
        setup_pathconf(req, hostconf);
411
0
        call_handlers(req, req->pathconf->handlers.entries);
412
0
        return;
413
0
    }
414
415
    /* uses the current pathconf, in other words, proxy uses the previous pathconf for building filters */
416
3.76k
    h2o__proxy_process_request(req);
417
3.76k
}
418
419
void h2o_reprocess_request(h2o_req_t *req, h2o_iovec_t method, const h2o_url_scheme_t *scheme, h2o_iovec_t authority,
420
                           h2o_iovec_t path, h2o_req_overrides_t *overrides, int is_delegated)
421
3.76k
{
422
3.76k
    retain_original_response(req);
423
424
    /* close generators and filters that are already running */
425
3.76k
    close_generator_and_filters(req);
426
427
    /* setup the request/response parameters */
428
3.76k
    req->handler = NULL;
429
3.76k
    req->method = method;
430
3.76k
    req->scheme = scheme;
431
3.76k
    req->authority = authority;
432
3.76k
    req->path = path;
433
3.76k
    int has_null_char;
434
3.76k
    req->path_normalized =
435
3.76k
        h2o_url_normalize_path(&req->pool, req->path.base, req->path.len, &req->query_at, &req->norm_indexes, &has_null_char);
436
3.76k
    req->path_normalized_has_null_char = has_null_char;
437
3.76k
    req->authority_wildcard_match = h2o_iovec_init(NULL, 0);
438
3.76k
    req->overrides = overrides;
439
3.76k
    req->res_is_delegated |= is_delegated;
440
3.76k
    req->reprocess_if_too_early = 0;
441
3.76k
    reset_response(req);
442
443
    /* check the delegation (or reprocess) counter */
444
3.76k
    if (req->res_is_delegated) {
445
0
        if (req->remaining_delegations == 0) {
446
            /* TODO log */
447
0
            h2o_send_error_502(req, "Gateway Error", "too many internal delegations", 0);
448
0
            return;
449
0
        }
450
0
        --req->remaining_delegations;
451
3.76k
    } else {
452
3.76k
        if (req->remaining_reprocesses == 0) {
453
            /* TODO log */
454
0
            h2o_send_error_502(req, "Gateway Error", "too many internal reprocesses", 0);
455
0
            return;
456
0
        }
457
3.76k
        --req->remaining_reprocesses;
458
3.76k
    }
459
460
3.76k
    process_resolved_request(req, req->conn->ctx->globalconf->hosts);
461
3.76k
}
462
463
static void on_reprocess_request_cb(h2o_timer_t *entry)
464
0
{
465
0
    struct st_reprocess_request_deferred_t *args =
466
0
        H2O_STRUCT_FROM_MEMBER(struct st_reprocess_request_deferred_t, super.timeout, entry);
467
0
    h2o_reprocess_request(args->super.req, args->method, args->scheme, args->authority, args->path, args->overrides,
468
0
                          args->is_delegated);
469
0
}
470
471
void h2o_reprocess_request_deferred(h2o_req_t *req, h2o_iovec_t method, const h2o_url_scheme_t *scheme, h2o_iovec_t authority,
472
                                    h2o_iovec_t path, h2o_req_overrides_t *overrides, int is_delegated)
473
0
{
474
0
    struct st_reprocess_request_deferred_t *args =
475
0
        (struct st_reprocess_request_deferred_t *)create_deferred_action(req, sizeof(*args), on_reprocess_request_cb);
476
0
    args->method = method;
477
0
    args->scheme = scheme;
478
0
    args->authority = authority;
479
0
    args->path = path;
480
0
    args->overrides = overrides;
481
0
    args->is_delegated = is_delegated;
482
0
}
483
484
void h2o_replay_request(h2o_req_t *req)
485
0
{
486
0
    close_generator_and_filters(req);
487
0
    reset_response(req);
488
489
0
    if (req->handler != NULL) {
490
0
        h2o_handler_t **handler = req->pathconf->handlers.entries, **end = handler + req->pathconf->handlers.size;
491
0
        for (;; ++handler) {
492
0
            assert(handler != end);
493
0
            if (*handler == req->handler)
494
0
                break;
495
0
        }
496
0
        call_handlers(req, handler);
497
0
    } else {
498
0
        process_resolved_request(req, req->conn->hosts);
499
0
    }
500
0
}
501
502
static void on_replay_request_cb(h2o_timer_t *entry)
503
0
{
504
0
    struct st_deferred_request_action_t *args = H2O_STRUCT_FROM_MEMBER(struct st_deferred_request_action_t, timeout, entry);
505
0
    h2o_replay_request(args->req);
506
0
}
507
508
void h2o_replay_request_deferred(h2o_req_t *req)
509
0
{
510
0
    create_deferred_action(req, sizeof(struct st_deferred_request_action_t), on_replay_request_cb);
511
0
}
512
513
void h2o_start_response(h2o_req_t *req, h2o_generator_t *generator)
514
20.4k
{
515
20.4k
    retain_original_response(req);
516
517
    /* set generator */
518
20.4k
    assert(req->_generator == NULL);
519
20.4k
    req->_generator = generator;
520
521
20.4k
    if (req->is_tunnel_req && (req->res.status == 101 || req->res.status == 200)) {
522
        /* a tunnel has been established; forward response as is */
523
20.4k
    } else {
524
        /* setup response filters */
525
20.4k
        if (req->prefilters != NULL) {
526
0
            req->prefilters->on_setup_ostream(req->prefilters, req, &req->_ostr_top);
527
20.4k
        } else {
528
20.4k
            h2o_setup_next_ostream(req, &req->_ostr_top);
529
20.4k
        }
530
20.4k
    }
531
20.4k
}
532
533
static void do_sendvec(h2o_req_t *req, h2o_sendvec_t *bufs, size_t bufcnt, h2o_send_state_t state)
534
22.9k
{
535
22.9k
    assert(req->_generator != NULL);
536
537
22.9k
    if (!h2o_send_state_is_in_progress(state))
538
20.3k
        req->_generator = NULL;
539
540
22.9k
    req->_ostr_top->do_send(req->_ostr_top, req, bufs, bufcnt, state);
541
22.9k
}
542
543
void h2o_send(h2o_req_t *req, h2o_iovec_t *bufs, size_t bufcnt, h2o_send_state_t state)
544
22.9k
{
545
22.9k
    h2o_sendvec_t *vecs = alloca(sizeof(*vecs) * bufcnt);
546
22.9k
    size_t i;
547
548
42.4k
    for (i = 0; i != bufcnt; ++i)
549
19.4k
        h2o_sendvec_init_raw(vecs + i, bufs[i].base, bufs[i].len);
550
551
22.9k
    do_sendvec(req, vecs, bufcnt, state);
552
22.9k
}
553
554
void h2o_sendvec(h2o_req_t *req, h2o_sendvec_t *bufs, size_t bufcnt, h2o_send_state_t state)
555
0
{
556
0
    assert(bufcnt == 0 || (bufs[0].callbacks->read_ == &h2o_sendvec_read_raw || bufcnt == 1));
557
0
    do_sendvec(req, bufs, bufcnt, state);
558
0
}
559
560
h2o_req_prefilter_t *h2o_add_prefilter(h2o_req_t *req, size_t alignment, size_t sz)
561
0
{
562
0
    h2o_req_prefilter_t *prefilter = h2o_mem_alloc_pool_aligned(&req->pool, alignment, sz);
563
0
    prefilter->next = req->prefilters;
564
0
    req->prefilters = prefilter;
565
0
    return prefilter;
566
0
}
567
568
h2o_ostream_t *h2o_add_ostream(h2o_req_t *req, size_t alignment, size_t sz, h2o_ostream_t **slot)
569
0
{
570
0
    h2o_ostream_t *ostr = h2o_mem_alloc_pool_aligned(&req->pool, alignment, sz);
571
0
    ostr->next = *slot;
572
0
    ostr->do_send = NULL;
573
0
    ostr->stop = NULL;
574
0
    ostr->send_informational = NULL;
575
576
0
    *slot = ostr;
577
578
0
    return ostr;
579
0
}
580
581
void h2o_req_apply_env(h2o_req_t *req, h2o_envconf_t *env)
582
0
{
583
0
    size_t i;
584
585
0
    if (env->parent != NULL)
586
0
        h2o_req_apply_env(req, env->parent);
587
0
    for (i = 0; i != env->unsets.size; ++i)
588
0
        h2o_req_unsetenv(req, env->unsets.entries[i].base, env->unsets.entries[i].len);
589
0
    for (i = 0; i != env->sets.size; i += 2)
590
0
        *h2o_req_getenv(req, env->sets.entries[i].base, env->sets.entries[i].len, 1) = env->sets.entries[i + 1];
591
0
}
592
593
void h2o_req_bind_conf(h2o_req_t *req, h2o_hostconf_t *hostconf, h2o_pathconf_t *pathconf)
594
21.2k
{
595
21.2k
    req->hostconf = hostconf;
596
21.2k
    req->pathconf = pathconf;
597
598
    /* copy filters and loggers */
599
21.2k
    req->filters = pathconf->_filters.entries;
600
21.2k
    req->num_filters = pathconf->_filters.size;
601
21.2k
    req->loggers = pathconf->_loggers.entries;
602
21.2k
    req->num_loggers = pathconf->_loggers.size;
603
604
21.2k
    if (pathconf->env != NULL)
605
0
        h2o_req_apply_env(req, pathconf->env);
606
21.2k
}
607
608
void h2o_proceed_response_deferred(h2o_req_t *req)
609
0
{
610
0
    h2o_timer_link(req->conn->ctx->loop, 0, &req->_timeout_entry);
611
0
}
612
613
void h2o_ostream_send_next(h2o_ostream_t *ostream, h2o_req_t *req, h2o_sendvec_t *bufs, size_t bufcnt, h2o_send_state_t state)
614
0
{
615
0
    if (!h2o_send_state_is_in_progress(state)) {
616
0
        assert(req->_ostr_top == ostream);
617
0
        req->_ostr_top = ostream->next;
618
0
    }
619
0
    ostream->next->do_send(ostream->next, req, bufs, bufcnt, state);
620
0
}
621
622
void h2o_req_fill_mime_attributes(h2o_req_t *req)
623
0
{
624
0
    ssize_t content_type_index;
625
0
    h2o_mimemap_type_t *mime;
626
627
0
    if (req->res.mime_attr != NULL)
628
0
        return;
629
630
0
    if ((content_type_index = h2o_find_header(&req->res.headers, H2O_TOKEN_CONTENT_TYPE, -1)) != -1 &&
631
0
        (mime = h2o_mimemap_get_type_by_mimetype(req->pathconf->mimemap, req->res.headers.entries[content_type_index].value, 0)) !=
632
0
            NULL)
633
0
        req->res.mime_attr = &mime->data.attr;
634
0
    else
635
0
        req->res.mime_attr = &h2o_mime_attributes_as_is;
636
0
}
637
638
void h2o_send_inline(h2o_req_t *req, const char *body, size_t len)
639
17.0k
{
640
17.0k
    static h2o_generator_t generator = {NULL, NULL};
641
642
17.0k
    h2o_iovec_t buf = h2o_strdup(&req->pool, body, len);
643
    /* the function intentionally does not set the content length, since it may be used for generating 304 response, etc. */
644
    /* req->res.content_length = buf.len; */
645
646
17.0k
    h2o_start_response(req, &generator);
647
648
17.0k
    if (h2o_memis(req->input.method.base, req->input.method.len, H2O_STRLIT("HEAD")))
649
316
        h2o_send(req, NULL, 0, H2O_SEND_STATE_FINAL);
650
16.7k
    else
651
16.7k
        h2o_send(req, &buf, 1, H2O_SEND_STATE_FINAL);
652
17.0k
}
653
654
void h2o_send_error_generic(h2o_req_t *req, int status, const char *reason, const char *body, int flags)
655
17.0k
{
656
17.0k
    if (req->pathconf == NULL) {
657
5.90k
        h2o_hostconf_t *hostconf = h2o_req_setup(req);
658
5.90k
        h2o_req_bind_conf(req, hostconf, &hostconf->fallback_path);
659
5.90k
    }
660
661
    /* If the request is broken or incomplete, do not apply filters, as it would be dangerous to do so. Legitimate clients would not
662
     * send broken requests, so we do not need to decorate error responses using errordoc handler or anything else. */
663
17.0k
    if ((flags & H2O_SEND_ERROR_BROKEN_REQUEST) != 0)
664
1.21k
        req->_next_filter_index = SIZE_MAX;
665
666
17.0k
    if ((flags & H2O_SEND_ERROR_HTTP1_CLOSE_CONNECTION) != 0)
667
1.47k
        req->http1_is_persistent = 0;
668
669
17.0k
    req->res.status = status;
670
17.0k
    req->res.reason = reason;
671
17.0k
    req->res.content_length = strlen(body);
672
673
17.0k
    if ((flags & H2O_SEND_ERROR_KEEP_HEADERS) == 0)
674
17.0k
        memset(&req->res.headers, 0, sizeof(req->res.headers));
675
676
17.0k
    h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, NULL, H2O_STRLIT("text/plain; charset=utf-8"));
677
678
17.0k
    h2o_send_inline(req, body, SIZE_MAX);
679
17.0k
}
680
681
#define DECL_SEND_ERROR_DEFERRED(status_)                                                                                          \
682
    static void send_error_deferred_cb_##status_(h2o_timer_t *entry)                                                               \
683
0
    {                                                                                                                              \
684
0
        struct st_send_error_deferred_t *args = H2O_STRUCT_FROM_MEMBER(struct st_send_error_deferred_t, _timeout, entry);          \
685
0
        reset_response(args->req);                                                                                                 \
686
0
        args->req->conn->ctx->emitted_error_status[H2O_STATUS_ERROR_##status_]++;                                                  \
687
0
        h2o_send_error_generic(args->req, args->status, args->reason, args->body, args->flags);                                    \
688
0
    }                                                                                                                              \
689
                                                                                                                                   \
690
    static void h2o_send_error_deferred_##status_(h2o_req_t *req, const char *reason, const char *body, int flags)                 \
691
0
    {                                                                                                                              \
692
0
        struct st_send_error_deferred_t *args = h2o_mem_alloc_pool(&req->pool, *args, 1);                                          \
693
0
        *args = (struct st_send_error_deferred_t){req, status_, reason, body, flags};                                              \
694
0
        h2o_timer_init(&args->_timeout, send_error_deferred_cb_##status_);                                                         \
695
0
        h2o_timer_link(req->conn->ctx->loop, 0, &args->_timeout);                                                                  \
696
0
    }
697
698
0
DECL_SEND_ERROR_DEFERRED(502)
699
700
#undef DECL_SEND_ERROR_DEFERRED
701
702
static size_t append_with_limit(char *dst, h2o_iovec_t input, size_t limit)
703
248
{
704
248
    if (input.len < limit) {
705
243
        memcpy(dst, input.base, input.len);
706
243
        return input.len;
707
243
    } else {
708
5
        memcpy(dst, input.base, (limit - 3));
709
5
        memcpy(dst + (limit - 3), "...", 3);
710
5
        return limit;
711
5
    }
712
248
}
713
714
void h2o_req_log_error(h2o_req_t *req, const char *module, const char *fmt, ...)
715
124
{
716
248
#define INITIAL_BUF_SIZE 256
717
718
124
    char *errbuf = h2o_mem_alloc_pool(&req->pool, char, INITIAL_BUF_SIZE);
719
124
    int errlen;
720
124
    va_list args;
721
722
124
    va_start(args, fmt);
723
124
    errlen = vsnprintf(errbuf, INITIAL_BUF_SIZE, fmt, args);
724
124
    va_end(args);
725
726
124
    if (errlen >= INITIAL_BUF_SIZE) {
727
0
        errbuf = h2o_mem_alloc_pool(&req->pool, char, errlen + 1);
728
0
        va_start(args, fmt);
729
0
        errlen = vsnprintf(errbuf, errlen + 1, fmt, args);
730
0
        va_end(args);
731
0
    }
732
124
    h2o_iovec_t msg = h2o_iovec_init(errbuf, errlen);
733
734
124
#undef INITIAL_BUF_SIZE
735
736
    /* build prefix */
737
124
    char *pbuf = h2o_mem_alloc_pool(&req->pool, char, sizeof("[] in request::") + strlen(module) + 64 + 32);
738
124
    char *p = pbuf;
739
124
    p += sprintf(p, "[%s] in request:", module);
740
124
    p += append_with_limit(p, req->authority, 64);
741
124
    p += append_with_limit(p, req->path, 32);
742
124
    *p++ = ':';
743
124
    h2o_iovec_t prefix = h2o_iovec_init(pbuf, p - pbuf);
744
745
    /* run error callback (save and emit the log if needed) */
746
124
    req->error_log_delegate.cb(req->error_log_delegate.data, prefix, msg);
747
124
}
748
749
void h2o_send_redirect(h2o_req_t *req, int status, const char *reason, const char *url, size_t url_len)
750
0
{
751
0
    if (req->res_is_delegated) {
752
0
        h2o_iovec_t method = h2o_get_redirect_method(req->method, status);
753
0
        h2o_send_redirect_internal(req, method, url, url_len, 0);
754
0
        return;
755
0
    }
756
757
0
    static h2o_generator_t generator = {NULL, NULL};
758
0
    static const h2o_iovec_t body_prefix = {H2O_STRLIT("<!DOCTYPE html><TITLE>Moved</TITLE><P>The document has moved <A HREF=\"")};
759
0
    static const h2o_iovec_t body_suffix = {H2O_STRLIT("\">here</A>")};
760
761
    /* build and send response */
762
0
    h2o_iovec_t bufs[3];
763
0
    size_t bufcnt;
764
0
    if (h2o_memis(req->input.method.base, req->input.method.len, H2O_STRLIT("HEAD"))) {
765
0
        req->res.content_length = SIZE_MAX;
766
0
        bufcnt = 0;
767
0
    } else {
768
0
        bufs[0] = body_prefix;
769
0
        bufs[1] = h2o_htmlescape(&req->pool, url, url_len);
770
0
        bufs[2] = body_suffix;
771
0
        bufcnt = 3;
772
0
        req->res.content_length = body_prefix.len + bufs[1].len + body_suffix.len;
773
0
    }
774
0
    req->res.status = status;
775
0
    req->res.reason = reason;
776
0
    req->res.headers = (h2o_headers_t){NULL};
777
0
    h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_LOCATION, NULL, url, url_len);
778
0
    h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, NULL, H2O_STRLIT("text/html; charset=utf-8"));
779
0
    h2o_start_response(req, &generator);
780
0
    h2o_send(req, bufs, bufcnt, H2O_SEND_STATE_FINAL);
781
0
}
782
783
void h2o_send_redirect_internal(h2o_req_t *req, h2o_iovec_t method, const char *url_str, size_t url_len, int preserve_overrides)
784
0
{
785
0
    h2o_url_t url;
786
787
    /* parse the location URL */
788
0
    if (h2o_url_parse_relative(&req->pool, url_str, url_len, &url) != 0) {
789
        /* TODO log h2o_error_printf("[proxy] cannot handle location header: %.*s\n", (int)url_len, url); */
790
0
        h2o_send_error_deferred_502(req, "Gateway Error", "internal error", 0);
791
0
        return;
792
0
    }
793
    /* convert the location to absolute (while creating copies of the values passed to the deferred call) */
794
0
    if (url.scheme == NULL)
795
0
        url.scheme = req->scheme;
796
0
    if (url.authority.base == NULL) {
797
0
        if (req->hostconf != NULL)
798
0
            url.authority = req->hostconf->authority.hostport;
799
0
        else
800
0
            url.authority = req->authority;
801
0
    } else {
802
0
        if (h2o_lcstris(url.authority.base, url.authority.len, req->authority.base, req->authority.len)) {
803
0
            url.authority = req->authority;
804
0
        } else {
805
0
            url.authority = h2o_strdup(&req->pool, url.authority.base, url.authority.len);
806
0
            preserve_overrides = 0;
807
0
        }
808
0
    }
809
0
    h2o_iovec_t base_path = req->path;
810
0
    h2o_url_resolve_path(&base_path, &url.path);
811
0
    url.path = h2o_concat(&req->pool, base_path, url.path);
812
813
0
    h2o_reprocess_request_deferred(req, method, url.scheme, url.authority, url.path, preserve_overrides ? req->overrides : NULL, 1);
814
0
}
815
816
h2o_iovec_t h2o_get_redirect_method(h2o_iovec_t method, int status)
817
0
{
818
0
    if (h2o_memis(method.base, method.len, H2O_STRLIT("POST")) && !(status == 307 || status == 308))
819
0
        method = h2o_iovec_init(H2O_STRLIT("GET"));
820
0
    return method;
821
0
}
822
823
static void do_push_path(void *_req, const char *path, size_t path_len, int is_critical)
824
0
{
825
0
    h2o_req_t *req = _req;
826
827
0
    if (req->conn->callbacks->push_path != NULL)
828
0
        req->conn->callbacks->push_path(req, path, path_len, is_critical);
829
0
}
830
831
h2o_iovec_t h2o_push_path_in_link_header(h2o_req_t *req, const char *value, size_t value_len)
832
0
{
833
0
    h2o_iovec_t ret = h2o_iovec_init(value, value_len);
834
835
0
    h2o_extract_push_path_from_link_header(&req->pool, value, value_len, req->path_normalized, req->input.scheme,
836
0
                                           req->input.authority, req->res_is_delegated ? req->scheme : NULL,
837
0
                                           req->res_is_delegated ? &req->authority : NULL, do_push_path, req, &ret,
838
0
                                           req->hostconf->http2.allow_cross_origin_push);
839
840
0
    return ret;
841
0
}
842
843
void h2o_resp_add_date_header(h2o_req_t *req)
844
14.0k
{
845
14.0k
    h2o_timestamp_t ts = h2o_get_timestamp(req->conn->ctx, &req->pool);
846
14.0k
    h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_DATE, NULL, ts.str->rfc1123, strlen(ts.str->rfc1123));
847
14.0k
}
848
849
void h2o_send_informational(h2o_req_t *req)
850
0
{
851
    /* 1xx must be sent before h2o_start_response is called*/
852
0
    assert(req->_generator == NULL);
853
0
    assert(req->_ostr_top->next == NULL);
854
0
    assert(100 <= req->res.status && req->res.status <= 199 && req->res.status != 101);
855
856
0
    if (req->_ostr_top->send_informational == NULL)
857
0
        goto Clear;
858
859
0
    size_t index;
860
0
    if ((index = h2o_find_header(&req->headers, H2O_TOKEN_NO_EARLY_HINTS, -1)) != -1) {
861
0
        h2o_iovec_t value = req->headers.entries[index].value;
862
0
        if (value.len == 1 && value.base[0] == '1')
863
0
            goto Clear;
864
0
    }
865
866
0
    int i = 0;
867
0
    for (i = 0; i != req->num_filters; ++i) {
868
0
        h2o_filter_t *filter = req->filters[i];
869
0
        if (filter->on_informational != NULL)
870
0
            filter->on_informational(filter, req);
871
0
    }
872
873
0
    if (req->res.status == 103 && req->res.headers.size == 0)
874
0
        goto Clear;
875
876
0
    req->_ostr_top->send_informational(req->_ostr_top, req);
877
878
0
Clear:
879
    /* clear status and headers */
880
0
    req->res.status = 0;
881
0
    req->res.headers = (h2o_headers_t){NULL, 0, 0};
882
0
}
883
884
int h2o_req_resolve_internal_redirect_url(h2o_req_t *req, h2o_iovec_t dest, h2o_url_t *resolved)
885
0
{
886
0
    h2o_url_t input;
887
888
    /* resolve the URL */
889
0
    if (h2o_url_parse_relative(&req->pool, dest.base, dest.len, &input) != 0) {
890
0
        return -1;
891
0
    }
892
0
    if (input.scheme != NULL && input.authority.base != NULL) {
893
0
        *resolved = input;
894
0
    } else {
895
0
        h2o_url_t base;
896
        /* we MUST to set authority to that of hostconf, or internal redirect might create a TCP connection */
897
0
        if (h2o_url_init(&base, req->scheme, req->hostconf->authority.hostport, req->path) != 0) {
898
0
            return -1;
899
0
        }
900
0
        h2o_url_resolve(&req->pool, &base, &input, resolved);
901
0
    }
902
903
0
    return 0;
904
0
}