Coverage Report

Created: 2026-01-09 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/h2o/lib/handler/configurator/expires.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2014 DeNA Co., Ltd.
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 <stdio.h>
24
#include <string.h>
25
#include "h2o.h"
26
#include "h2o/configurator.h"
27
28
struct expires_configurator_t {
29
    h2o_configurator_t super;
30
    h2o_expires_args_t **args;
31
    h2o_expires_args_t *_args_stack[H2O_CONFIGURATOR_NUM_LEVELS + 1];
32
};
33
34
static int on_config_expires(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node)
35
0
{
36
0
    struct expires_configurator_t *self = (void *)cmd->configurator;
37
0
    uint64_t value;
38
0
    char unit[32];
39
40
0
    if (strcasecmp(node->data.scalar, "OFF") == 0) {
41
0
        free(*self->args);
42
0
        *self->args = NULL;
43
0
    } else if (sscanf(node->data.scalar, "%" SCNu64 " %31s", &value, unit) == 2) {
44
        /* convert value to seconds depending on the unit */
45
0
        if (strncasecmp(unit, H2O_STRLIT("second")) == 0) {
46
            /* ok */
47
0
        } else if (strncasecmp(unit, H2O_STRLIT("minute")) == 0) {
48
0
            value *= 60;
49
0
        } else if (strncasecmp(unit, H2O_STRLIT("hour")) == 0) {
50
0
            value *= 60 * 60;
51
0
        } else if (strncasecmp(unit, H2O_STRLIT("day")) == 0) {
52
0
            value *= 24 * 60 * 60;
53
0
        } else if (strncasecmp(unit, H2O_STRLIT("month")) == 0) {
54
0
            value *= 30 * 24 * 60 * 60;
55
0
        } else if (strncasecmp(unit, H2O_STRLIT("year")) == 0) {
56
0
            value *= 365 * 30 * 24 * 60 * 60;
57
0
        } else {
58
            /* TODO add support for H2O_EXPIRES_MODE_MAX_ABSOLUTE that sets the Expires header? */
59
0
            h2o_configurator_errprintf(cmd, node, "unknown unit:`%s` (see --help)", unit);
60
0
            return -1;
61
0
        }
62
        /* save the value */
63
0
        if (*self->args == NULL)
64
0
            *self->args = h2o_mem_alloc(sizeof(**self->args));
65
0
        (*self->args)->mode = H2O_EXPIRES_MODE_MAX_AGE;
66
0
        (*self->args)->data.max_age = value;
67
0
    } else {
68
0
        h2o_configurator_errprintf(cmd, node,
69
0
                                   "failed to parse the value, should be in form of: `<number> <unit>` or `OFF` (see --help)");
70
0
        return -1;
71
0
    }
72
73
0
    return 0;
74
0
}
75
76
static int on_config_enter(h2o_configurator_t *_self, h2o_configurator_context_t *ctx, yoml_t *node)
77
0
{
78
0
    struct expires_configurator_t *self = (void *)_self;
79
80
0
    if (self->args[0] != NULL) {
81
        /* duplicate */
82
0
        assert(self->args[0]->mode == H2O_EXPIRES_MODE_MAX_AGE);
83
0
        self->args[1] = h2o_mem_alloc(sizeof(**self->args));
84
0
        *self->args[1] = *self->args[0];
85
0
    } else {
86
0
        self->args[1] = NULL;
87
0
    }
88
0
    ++self->args;
89
0
    return 0;
90
0
}
91
92
static int on_config_exit(h2o_configurator_t *_self, h2o_configurator_context_t *ctx, yoml_t *node)
93
0
{
94
0
    struct expires_configurator_t *self = (void *)_self;
95
96
0
    if (*self->args != NULL) {
97
        /* setup */
98
0
        if (ctx->pathconf != NULL && !h2o_configurator_at_extension_level(ctx))
99
0
            h2o_expires_register(ctx->pathconf, *self->args);
100
        /* destruct */
101
0
        assert((*self->args)->mode == H2O_EXPIRES_MODE_MAX_AGE);
102
0
        free(*self->args);
103
0
        *self->args = NULL;
104
0
    }
105
106
0
    --self->args;
107
0
    return 0;
108
0
}
109
110
void h2o_expires_register_configurator(h2o_globalconf_t *conf)
111
0
{
112
0
    struct expires_configurator_t *c = (void *)h2o_configurator_create(conf, sizeof(*c));
113
114
    /* set default vars */
115
0
    c->args = c->_args_stack;
116
117
    /* setup handlers */
118
0
    c->super.enter = on_config_enter;
119
0
    c->super.exit = on_config_exit;
120
0
    h2o_configurator_define_command(&c->super, "expires",
121
0
                                    H2O_CONFIGURATOR_FLAG_GLOBAL | H2O_CONFIGURATOR_FLAG_HOST | H2O_CONFIGURATOR_FLAG_PATH |
122
0
                                        H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR,
123
0
                                    on_config_expires);
124
0
}