Coverage Report

Created: 2025-07-18 06:40

/src/h2o/lib/handler/errordoc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2015 DeNA Co., Ltd., Kazuho Oku
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5
 * of this software and associated documentation files (the "Software"), to
6
 * deal in the Software without restriction, including without limitation the
7
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8
 * sell copies of the Software, and to permit persons to whom the Software is
9
 * furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in
12
 * all copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
 * IN THE SOFTWARE.
21
 */
22
#include "h2o.h"
23
24
/* used to rewrite status code to the original code */
25
struct st_errordoc_prefilter_t {
26
    h2o_req_prefilter_t super;
27
    h2o_headers_t req_headers;
28
    int status;
29
    const char *reason;
30
    h2o_headers_t res_headers;
31
};
32
33
/* used to capture an error response */
34
struct st_errordoc_filter_t {
35
    h2o_filter_t super;
36
    H2O_VECTOR(h2o_errordoc_t) errordocs;
37
};
38
39
static void add_header(h2o_mem_pool_t *pool, h2o_headers_t *headers, const h2o_header_t *header)
40
0
{
41
0
    h2o_vector_reserve(pool, headers, headers->size + 1);
42
0
    headers->entries[headers->size++] = *header;
43
0
}
44
45
static void on_prefilter_setup_stream(h2o_req_prefilter_t *_self, h2o_req_t *req, h2o_ostream_t **slot)
46
0
{
47
0
    struct st_errordoc_prefilter_t *self = (void *)_self;
48
0
    h2o_headers_t headers_merged = {NULL};
49
0
    size_t i;
50
51
    /* restore request headers (for logging) and response status */
52
0
    req->headers = self->req_headers;
53
0
    req->res.status = self->status;
54
0
    req->res.reason = self->reason;
55
56
    /* generate response headers (by merging the preserved and given) */
57
0
    for (i = 0; i != self->res_headers.size; ++i)
58
0
        add_header(&req->pool, &headers_merged, self->res_headers.entries + i);
59
0
    for (i = 0; i != req->res.headers.size; ++i) {
60
0
        const h2o_header_t *header = req->res.headers.entries + i;
61
0
        if (header->name == &H2O_TOKEN_CONTENT_TYPE->buf || header->name == &H2O_TOKEN_CONTENT_LANGUAGE->buf ||
62
0
            header->name == &H2O_TOKEN_SET_COOKIE->buf)
63
0
            add_header(&req->pool, &headers_merged, header);
64
0
    }
65
0
    req->res.headers = headers_merged;
66
67
0
    h2o_setup_next_prefilter(&self->super, req, slot);
68
0
}
69
70
static void on_ostream_send(h2o_ostream_t *self, h2o_req_t *req, h2o_sendvec_t *inbufs, size_t inbufcnt, h2o_send_state_t state)
71
0
{
72
    /* nothing to do */
73
0
}
74
75
static int prefilter_is_registered(h2o_req_t *req)
76
0
{
77
0
    h2o_req_prefilter_t *prefilter;
78
0
    for (prefilter = req->prefilters; prefilter != NULL; prefilter = prefilter->next)
79
0
        if (prefilter->on_setup_ostream == on_prefilter_setup_stream)
80
0
            return 1;
81
0
    return 0;
82
0
}
83
84
static void on_filter_setup_ostream(h2o_filter_t *_self, h2o_req_t *req, h2o_ostream_t **slot)
85
0
{
86
0
    struct st_errordoc_filter_t *self = (void *)_self;
87
0
    h2o_errordoc_t *errordoc;
88
0
    struct st_errordoc_prefilter_t *prefilter;
89
0
    h2o_iovec_t method;
90
0
    h2o_ostream_t *ostream;
91
0
    size_t i;
92
93
0
    if (req->res.status >= 400 && !prefilter_is_registered(req)) {
94
0
        size_t i;
95
0
        for (i = 0; i != self->errordocs.size; ++i) {
96
0
            errordoc = self->errordocs.entries + i;
97
0
            if (errordoc->status == req->res.status)
98
0
                goto Found;
99
0
        }
100
0
    }
101
102
    /* bypass to the next filter */
103
0
    h2o_setup_next_ostream(req, slot);
104
0
    return;
105
106
0
Found:
107
    /* register prefilter that rewrites the status code after the internal redirect is processed */
108
0
    prefilter = (void *)h2o_add_prefilter(req, H2O_ALIGNOF(*prefilter), sizeof(*prefilter));
109
0
    prefilter->super.on_setup_ostream = on_prefilter_setup_stream;
110
0
    prefilter->req_headers = req->headers;
111
0
    prefilter->status = req->res.status;
112
0
    prefilter->reason = req->res.reason;
113
0
    prefilter->res_headers = (h2o_headers_t){NULL};
114
0
    for (i = 0; i != req->res.headers.size; ++i) {
115
0
        const h2o_header_t *header = req->res.headers.entries + i;
116
0
        if (!(header->name == &H2O_TOKEN_CONTENT_TYPE->buf || header->name == &H2O_TOKEN_CONTENT_LANGUAGE->buf))
117
0
            add_header(&req->pool, &prefilter->res_headers, header);
118
0
    }
119
    /* redirect internally to the error document */
120
0
    method = req->method;
121
0
    if (h2o_memis(method.base, method.len, H2O_STRLIT("POST")))
122
0
        method = h2o_iovec_init(H2O_STRLIT("GET"));
123
0
    req->headers = (h2o_headers_t){NULL};
124
0
    req->res.headers = (h2o_headers_t){NULL};
125
0
    h2o_send_redirect_internal(req, method, errordoc->url.base, errordoc->url.len, 0);
126
    /* create fake ostream that swallows the contents emitted by the generator */
127
0
    ostream = h2o_add_ostream(req, H2O_ALIGNOF(*ostream), sizeof(*ostream), slot);
128
0
    ostream->do_send = on_ostream_send;
129
0
}
130
131
void h2o_errordoc_register(h2o_pathconf_t *pathconf, h2o_errordoc_t *errdocs, size_t cnt)
132
0
{
133
0
    struct st_errordoc_filter_t *self = (void *)h2o_create_filter(pathconf, sizeof(*self));
134
0
    size_t i;
135
136
0
    self->super.on_setup_ostream = on_filter_setup_ostream;
137
0
    h2o_vector_reserve(NULL, &self->errordocs, cnt);
138
0
    self->errordocs.size = cnt;
139
0
    for (i = 0; i != cnt; ++i) {
140
0
        const h2o_errordoc_t *src = errdocs + i;
141
0
        h2o_errordoc_t *dst = self->errordocs.entries + i;
142
0
        dst->status = src->status;
143
0
        dst->url = h2o_strdup(NULL, src->url.base, src->url.len);
144
0
    }
145
0
}