/src/h2o/lib/handler/reproxy.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2014,2015 DeNA Co., Ltd., Kazuho Oku, Daisuke Maki |
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 | | static void on_send(h2o_ostream_t *self, h2o_req_t *req, h2o_sendvec_t *inbufs, size_t inbufcnt, h2o_send_state_t state) |
25 | 0 | { |
26 | | /* nothing to do */ |
27 | 0 | } |
28 | | |
29 | | static void on_setup_ostream(h2o_filter_t *self, h2o_req_t *req, h2o_ostream_t **slot) |
30 | 0 | { |
31 | 0 | h2o_iovec_t dest, method; |
32 | 0 | ssize_t xru_index; |
33 | | |
34 | | /* obtain x-reproxy-url header, or skip to next ostream */ |
35 | 0 | if ((xru_index = h2o_find_header(&req->res.headers, H2O_TOKEN_X_REPROXY_URL, -1)) == -1) { |
36 | 0 | h2o_setup_next_ostream(req, slot); |
37 | 0 | return; |
38 | 0 | } |
39 | 0 | dest = req->res.headers.entries[xru_index].value; |
40 | 0 | h2o_delete_header(&req->res.headers, xru_index); |
41 | | |
42 | | /* setup params */ |
43 | 0 | switch (req->res.status) { |
44 | 0 | case 307: |
45 | 0 | case 308: |
46 | 0 | method = req->method; |
47 | 0 | break; |
48 | 0 | default: |
49 | 0 | method = h2o_iovec_init(H2O_STRLIT("GET")); |
50 | 0 | req->entity = (h2o_iovec_t){NULL}; |
51 | 0 | break; |
52 | 0 | } |
53 | | |
54 | | /* request internal redirect (is deferred) */ |
55 | 0 | h2o_send_redirect_internal(req, method, dest.base, dest.len, 0); |
56 | | |
57 | | /* setup filter (that swallows the response until the timeout gets fired) */ |
58 | 0 | h2o_ostream_t *ostream = h2o_add_ostream(req, H2O_ALIGNOF(*ostream), sizeof(*ostream), slot); |
59 | 0 | ostream->do_send = on_send; |
60 | 0 | } |
61 | | |
62 | | void h2o_reproxy_register(h2o_pathconf_t *pathconf) |
63 | 0 | { |
64 | 0 | h2o_filter_t *self = h2o_create_filter(pathconf, sizeof(*self)); |
65 | 0 | self->on_setup_ostream = on_setup_ostream; |
66 | 0 | } |