Coverage Report

Created: 2023-06-07 06:20

/src/h2o/lib/handler/configurator/status.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2016 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
#include "h2o/configurator.h"
24
25
static int on_config_status(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node)
26
0
{
27
0
    switch (h2o_configurator_get_one_of(cmd, node, "OFF,ON")) {
28
0
    case 0: /* OFF */
29
0
        return 0;
30
0
    case 1: /* ON */
31
0
        h2o_status_register(ctx->pathconf);
32
0
        return 0;
33
0
    default: /* error */
34
0
        return -1;
35
0
    }
36
0
}
37
38
struct st_status_configurator {
39
    h2o_configurator_t super;
40
    int stack;
41
    int duration_stats;
42
};
43
44
static int on_config_duration_stats(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node)
45
0
{
46
0
    struct st_status_configurator *c = (void *)cmd->configurator;
47
0
    ssize_t ret;
48
0
    switch (ret = h2o_configurator_get_one_of(cmd, node, "OFF,ON")) {
49
0
    case 0: /* OFF */
50
0
    case 1: /* ON */
51
0
        c->duration_stats = (int)ret;
52
0
        return 0;
53
0
    default: /* error */
54
0
        return -1;
55
0
    }
56
0
}
57
58
int on_enter_status(h2o_configurator_t *_conf, h2o_configurator_context_t *ctx, yoml_t *node)
59
0
{
60
0
    struct st_status_configurator *c = (void *)_conf;
61
0
    c->stack++;
62
0
    return 0;
63
0
}
64
65
int on_exit_status(h2o_configurator_t *_conf, h2o_configurator_context_t *ctx, yoml_t *node)
66
0
{
67
0
    struct st_status_configurator *c = (void *)_conf;
68
0
    c->stack--;
69
0
    if (!c->stack && c->duration_stats) {
70
0
        h2o_duration_stats_register(ctx->globalconf);
71
0
    }
72
0
    return 0;
73
0
}
74
75
void h2o_status_register_configurator(h2o_globalconf_t *conf)
76
0
{
77
0
    struct st_status_configurator *c = (void *)h2o_configurator_create(conf, sizeof(*c));
78
0
    c->super.enter = on_enter_status;
79
0
    c->super.exit = on_exit_status;
80
81
0
    h2o_configurator_define_command(
82
0
        &c->super, "status", H2O_CONFIGURATOR_FLAG_PATH | H2O_CONFIGURATOR_FLAG_DEFERRED | H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR,
83
0
        on_config_status);
84
85
0
    h2o_configurator_define_command(&c->super, "duration-stats", H2O_CONFIGURATOR_FLAG_GLOBAL | H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR,
86
0
                                    on_config_duration_stats);
87
0
}