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 
add_header(h2o_mem_pool_t * pool,h2o_headers_t * headers,const h2o_header_t * header)39 static void add_header(h2o_mem_pool_t *pool, h2o_headers_t *headers, const h2o_header_t *header)
40 {
41     h2o_vector_reserve(pool, headers, headers->size + 1);
42     headers->entries[headers->size++] = *header;
43 }
44 
on_prefilter_setup_stream(h2o_req_prefilter_t * _self,h2o_req_t * req,h2o_ostream_t ** slot)45 static void on_prefilter_setup_stream(h2o_req_prefilter_t *_self, h2o_req_t *req, h2o_ostream_t **slot)
46 {
47     struct st_errordoc_prefilter_t *self = (void *)_self;
48     h2o_headers_t headers_merged = {NULL};
49     size_t i;
50 
51     /* restore request headers (for logging) and response status */
52     req->headers = self->req_headers;
53     req->res.status = self->status;
54     req->res.reason = self->reason;
55 
56     /* generate response headers (by merging the preserved and given) */
57     for (i = 0; i != self->res_headers.size; ++i)
58         add_header(&req->pool, &headers_merged, self->res_headers.entries + i);
59     for (i = 0; i != req->res.headers.size; ++i) {
60         const h2o_header_t *header = req->res.headers.entries + i;
61         if (header->name == &H2O_TOKEN_CONTENT_TYPE->buf || header->name == &H2O_TOKEN_CONTENT_LANGUAGE->buf ||
62             header->name == &H2O_TOKEN_SET_COOKIE->buf)
63             add_header(&req->pool, &headers_merged, header);
64     }
65     req->res.headers = headers_merged;
66 
67     h2o_setup_next_prefilter(&self->super, req, slot);
68 }
69 
on_ostream_send(h2o_ostream_t * self,h2o_req_t * req,h2o_sendvec_t * inbufs,size_t inbufcnt,h2o_send_state_t state)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 {
72     /* nothing to do */
73 }
74 
prefilter_is_registered(h2o_req_t * req)75 static int prefilter_is_registered(h2o_req_t *req)
76 {
77     h2o_req_prefilter_t *prefilter;
78     for (prefilter = req->prefilters; prefilter != NULL; prefilter = prefilter->next)
79         if (prefilter->on_setup_ostream == on_prefilter_setup_stream)
80             return 1;
81     return 0;
82 }
83 
on_filter_setup_ostream(h2o_filter_t * _self,h2o_req_t * req,h2o_ostream_t ** slot)84 static void on_filter_setup_ostream(h2o_filter_t *_self, h2o_req_t *req, h2o_ostream_t **slot)
85 {
86     struct st_errordoc_filter_t *self = (void *)_self;
87     h2o_errordoc_t *errordoc;
88     struct st_errordoc_prefilter_t *prefilter;
89     h2o_iovec_t method;
90     h2o_ostream_t *ostream;
91     size_t i;
92 
93     if (req->res.status >= 400 && !prefilter_is_registered(req)) {
94         size_t i;
95         for (i = 0; i != self->errordocs.size; ++i) {
96             errordoc = self->errordocs.entries + i;
97             if (errordoc->status == req->res.status)
98                 goto Found;
99         }
100     }
101 
102     /* bypass to the next filter */
103     h2o_setup_next_ostream(req, slot);
104     return;
105 
106 Found:
107     /* register prefilter that rewrites the status code after the internal redirect is processed */
108     prefilter = (void *)h2o_add_prefilter(req, H2O_ALIGNOF(*prefilter), sizeof(*prefilter));
109     prefilter->super.on_setup_ostream = on_prefilter_setup_stream;
110     prefilter->req_headers = req->headers;
111     prefilter->status = req->res.status;
112     prefilter->reason = req->res.reason;
113     prefilter->res_headers = (h2o_headers_t){NULL};
114     for (i = 0; i != req->res.headers.size; ++i) {
115         const h2o_header_t *header = req->res.headers.entries + i;
116         if (!(header->name == &H2O_TOKEN_CONTENT_TYPE->buf || header->name == &H2O_TOKEN_CONTENT_LANGUAGE->buf))
117             add_header(&req->pool, &prefilter->res_headers, header);
118     }
119     /* redirect internally to the error document */
120     method = req->method;
121     if (h2o_memis(method.base, method.len, H2O_STRLIT("POST")))
122         method = h2o_iovec_init(H2O_STRLIT("GET"));
123     req->headers = (h2o_headers_t){NULL};
124     req->res.headers = (h2o_headers_t){NULL};
125     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     ostream = h2o_add_ostream(req, H2O_ALIGNOF(*ostream), sizeof(*ostream), slot);
128     ostream->do_send = on_ostream_send;
129 }
130 
h2o_errordoc_register(h2o_pathconf_t * pathconf,h2o_errordoc_t * errdocs,size_t cnt)131 void h2o_errordoc_register(h2o_pathconf_t *pathconf, h2o_errordoc_t *errdocs, size_t cnt)
132 {
133     struct st_errordoc_filter_t *self = (void *)h2o_create_filter(pathconf, sizeof(*self));
134     size_t i;
135 
136     self->super.on_setup_ostream = on_filter_setup_ostream;
137     h2o_vector_reserve(NULL, &self->errordocs, cnt);
138     self->errordocs.size = cnt;
139     for (i = 0; i != cnt; ++i) {
140         const h2o_errordoc_t *src = errdocs + i;
141         h2o_errordoc_t *dst = self->errordocs.entries + i;
142         dst->status = src->status;
143         dst->url = h2o_strdup(NULL, src->url.base, src->url.len);
144     }
145 }
146