1 /*
2 * Copyright (c) 2016 DeNA Co., Ltd., Ichito Nagata
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 <inttypes.h>
23 #include "h2o.h"
24
25 struct st_h2o_http2_debug_state_handler_t {
26 h2o_handler_t super;
27 int hpack_enabled;
28 };
29
on_req(h2o_handler_t * _self,h2o_req_t * req)30 static int on_req(h2o_handler_t *_self, h2o_req_t *req)
31 {
32 struct st_h2o_http2_debug_state_handler_t *self = (void *)_self;
33
34 static h2o_generator_t generator = {NULL, NULL};
35
36 if (req->conn->callbacks->get_debug_state == NULL) {
37 return -1;
38 }
39
40 h2o_http2_debug_state_t *debug_state = req->conn->callbacks->get_debug_state(req, self->hpack_enabled);
41
42 // stringify these variables to embed in Debug Header
43 h2o_iovec_t conn_flow_in, conn_flow_out;
44 conn_flow_in.base = h2o_mem_alloc_pool(&req->pool, char, sizeof(H2O_INT64_LONGEST_STR));
45 conn_flow_in.len = sprintf(conn_flow_in.base, "%zd", debug_state->conn_flow_in);
46 conn_flow_out.base = h2o_mem_alloc_pool(&req->pool, char, sizeof(H2O_INT64_LONGEST_STR));
47 conn_flow_out.len = sprintf(conn_flow_out.base, "%zd", debug_state->conn_flow_out);
48
49 req->res.status = 200;
50 h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, NULL, H2O_STRLIT("application/json; charset=utf-8"));
51 h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CACHE_CONTROL, NULL, H2O_STRLIT("no-cache, no-store"));
52 h2o_add_header_by_str(&req->pool, &req->res.headers, H2O_STRLIT("conn-flow-in"), 0, NULL, conn_flow_in.base, conn_flow_in.len);
53 h2o_add_header_by_str(&req->pool, &req->res.headers, H2O_STRLIT("conn-flow-out"), 0, NULL, conn_flow_out.base,
54 conn_flow_out.len);
55
56 h2o_start_response(req, &generator);
57 h2o_send(req, debug_state->json.entries,
58 h2o_memis(req->input.method.base, req->input.method.len, H2O_STRLIT("HEAD")) ? 0 : debug_state->json.size,
59 H2O_SEND_STATE_FINAL);
60 return 0;
61 }
62
h2o_http2_debug_state_register(h2o_hostconf_t * conf,int hpack_enabled)63 void h2o_http2_debug_state_register(h2o_hostconf_t *conf, int hpack_enabled)
64 {
65 h2o_pathconf_t *pathconf = h2o_config_register_path(conf, "/.well-known/h2/state", 0);
66 struct st_h2o_http2_debug_state_handler_t *self = (void *)h2o_create_handler(pathconf, sizeof(*self));
67 self->super.on_req = on_req;
68 self->hpack_enabled = hpack_enabled;
69 }
70