/src/h2o/lib/handler/configurator/server_timing.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2018 Fastly, 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 | | #include "h2o/configurator.h" |
25 | | |
26 | | enum { SERVER_TIMING_MODE_OFF, SERVER_TIMING_MODE_ON, SERVER_TIMING_MODE_ENFORCE }; |
27 | | |
28 | | struct st_server_timing_config_t { |
29 | | int mode; |
30 | | }; |
31 | | |
32 | | struct server_timing_configurator_t { |
33 | | h2o_configurator_t super; |
34 | | struct st_server_timing_config_t *vars, _vars_stack[H2O_CONFIGURATOR_NUM_LEVELS + 1]; |
35 | | }; |
36 | | |
37 | | static int on_config_server_timing(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node) |
38 | 0 | { |
39 | 0 | struct server_timing_configurator_t *self = (void *)cmd->configurator; |
40 | |
|
41 | 0 | ssize_t ret = h2o_configurator_get_one_of(cmd, node, "OFF,ON,ENFORCE"); |
42 | 0 | if (ret == -1) |
43 | 0 | return -1; |
44 | 0 | self->vars->mode = (int)ret; |
45 | |
|
46 | 0 | return 0; |
47 | 0 | } |
48 | | |
49 | | static int on_config_enter(h2o_configurator_t *_self, h2o_configurator_context_t *ctx, yoml_t *node) |
50 | 0 | { |
51 | 0 | struct server_timing_configurator_t *self = (void *)_self; |
52 | |
|
53 | 0 | self->vars[1] = self->vars[0]; |
54 | 0 | ++self->vars; |
55 | 0 | return 0; |
56 | 0 | } |
57 | | |
58 | | static int on_config_exit(h2o_configurator_t *_self, h2o_configurator_context_t *ctx, yoml_t *node) |
59 | 0 | { |
60 | 0 | struct server_timing_configurator_t *self = (void *)_self; |
61 | |
|
62 | 0 | if (ctx->pathconf != NULL && !h2o_configurator_at_extension_level(ctx) && self->vars->mode != SERVER_TIMING_MODE_OFF) |
63 | 0 | h2o_server_timing_register(ctx->pathconf, self->vars->mode == SERVER_TIMING_MODE_ENFORCE); |
64 | |
|
65 | 0 | --self->vars; |
66 | 0 | return 0; |
67 | 0 | } |
68 | | |
69 | | void h2o_server_timing_register_configurator(h2o_globalconf_t *conf) |
70 | 0 | { |
71 | 0 | struct server_timing_configurator_t *c = (void *)h2o_configurator_create(conf, sizeof(*c)); |
72 | | |
73 | | /* set default vars */ |
74 | 0 | c->vars = c->_vars_stack; |
75 | | |
76 | | /* setup handlers */ |
77 | 0 | c->super.enter = on_config_enter; |
78 | 0 | c->super.exit = on_config_exit; |
79 | | |
80 | | /* server_timing: ON | OFF */ |
81 | 0 | h2o_configurator_define_command(&c->super, "server-timing", |
82 | 0 | H2O_CONFIGURATOR_FLAG_GLOBAL | H2O_CONFIGURATOR_FLAG_HOST | H2O_CONFIGURATOR_FLAG_PATH, |
83 | 0 | on_config_server_timing); |
84 | 0 | } |