Coverage Report

Created: 2025-07-18 06:42

/src/h2o/lib/handler/configurator/file.c
Line
Count
Source (jump to first uncovered line)
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 "h2o.h"
23
#include "h2o/configurator.h"
24
25
struct st_h2o_file_config_vars_t {
26
    const char **index_files;
27
    int flags;
28
};
29
30
struct st_h2o_file_configurator_t {
31
    h2o_configurator_t super;
32
    struct st_h2o_file_config_vars_t *vars;
33
    struct st_h2o_file_config_vars_t _vars_stack[H2O_CONFIGURATOR_NUM_LEVELS + 1];
34
};
35
36
static int on_config_dir(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node)
37
0
{
38
0
    struct st_h2o_file_configurator_t *self = (void *)cmd->configurator;
39
40
0
    h2o_file_register(ctx->pathconf, node->data.scalar, self->vars->index_files, *ctx->mimemap, self->vars->flags);
41
0
    return 0;
42
0
}
43
44
static int on_config_file(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node)
45
0
{
46
0
    struct st_h2o_file_configurator_t *self = (void *)cmd->configurator;
47
0
    h2o_mimemap_type_t *mime_type =
48
0
        h2o_mimemap_get_type_by_extension(*ctx->mimemap, h2o_get_filext(node->data.scalar, strlen(node->data.scalar)));
49
0
    h2o_file_register_file(ctx->pathconf, node->data.scalar, mime_type, self->vars->flags);
50
0
    return 0;
51
0
}
52
53
static int on_config_index(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node)
54
0
{
55
0
    struct st_h2o_file_configurator_t *self = (void *)cmd->configurator;
56
0
    size_t i;
57
58
0
    free(self->vars->index_files);
59
0
    self->vars->index_files = h2o_mem_alloc(sizeof(self->vars->index_files[0]) * (node->data.sequence.size + 1));
60
0
    for (i = 0; i != node->data.sequence.size; ++i) {
61
0
        yoml_t *element = node->data.sequence.elements[i];
62
0
        if (element->type != YOML_TYPE_SCALAR) {
63
0
            h2o_configurator_errprintf(cmd, element, "argument must be a sequence of scalars");
64
0
            return -1;
65
0
        }
66
0
        self->vars->index_files[i] = element->data.scalar;
67
0
    }
68
0
    self->vars->index_files[i] = NULL;
69
70
0
    return 0;
71
0
}
72
73
static int on_config_etag(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node)
74
0
{
75
0
    struct st_h2o_file_configurator_t *self = (void *)cmd->configurator;
76
77
0
    switch (h2o_configurator_get_one_of(cmd, node, "OFF,ON")) {
78
0
    case 0: /* off */
79
0
        self->vars->flags |= H2O_FILE_FLAG_NO_ETAG;
80
0
        break;
81
0
    case 1: /* on */
82
0
        self->vars->flags &= ~H2O_FILE_FLAG_NO_ETAG;
83
0
        break;
84
0
    default: /* error */
85
0
        return -1;
86
0
    }
87
88
0
    return 0;
89
0
}
90
91
static int on_config_send_compressed(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node)
92
0
{
93
0
    struct st_h2o_file_configurator_t *self = (void *)cmd->configurator;
94
95
0
    switch (h2o_configurator_get_one_of(cmd, node, "OFF,ON,gunzip")) {
96
0
    case 0: /* off */
97
0
        self->vars->flags &= ~H2O_FILE_FLAG_SEND_COMPRESSED;
98
0
        break;
99
0
    case 1: /* on */
100
0
        self->vars->flags |= H2O_FILE_FLAG_SEND_COMPRESSED;
101
0
        break;
102
0
    case 2: /* gunzip */
103
0
        self->vars->flags |= (H2O_FILE_FLAG_SEND_COMPRESSED | H2O_FILE_FLAG_GUNZIP);
104
0
        break;
105
0
    default: /* error */
106
0
        return -1;
107
0
    }
108
109
0
    return 0;
110
0
}
111
112
static int on_config_dir_listing(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node)
113
0
{
114
0
    struct st_h2o_file_configurator_t *self = (void *)cmd->configurator;
115
116
0
    switch (h2o_configurator_get_one_of(cmd, node, "OFF,ON")) {
117
0
    case 0: /* off */
118
0
        self->vars->flags &= ~H2O_FILE_FLAG_DIR_LISTING;
119
0
        break;
120
0
    case 1: /* on */
121
0
        self->vars->flags |= H2O_FILE_FLAG_DIR_LISTING;
122
0
        break;
123
0
    default: /* error */
124
0
        return -1;
125
0
    }
126
127
0
    return 0;
128
0
}
129
130
static int on_config_io_uring(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node)
131
0
{
132
0
    struct st_h2o_file_configurator_t *self = (void *)cmd->configurator;
133
134
0
    switch (h2o_configurator_get_one_of(cmd, node, "OFF,ON")) {
135
0
    case 0: /* off */
136
0
        self->vars->flags &= ~H2O_FILE_FLAG_IO_URING;
137
0
        break;
138
0
    case 1: /* on */
139
0
        self->vars->flags |= H2O_FILE_FLAG_IO_URING;
140
0
        break;
141
0
    default: /* error */
142
0
        return -1;
143
0
    }
144
145
0
    return 0;
146
0
}
147
148
static const char **dup_strlist(const char **s)
149
0
{
150
0
    size_t i;
151
0
    const char **ret;
152
153
0
    for (i = 0; s[i] != NULL; ++i)
154
0
        ;
155
0
    ret = h2o_mem_alloc(sizeof(*ret) * (i + 1));
156
0
    for (i = 0; s[i] != NULL; ++i)
157
0
        ret[i] = s[i];
158
0
    ret[i] = NULL;
159
160
0
    return ret;
161
0
}
162
163
static int on_config_enter(h2o_configurator_t *_self, h2o_configurator_context_t *ctx, yoml_t *node)
164
0
{
165
0
    struct st_h2o_file_configurator_t *self = (void *)_self;
166
0
    ++self->vars;
167
0
    self->vars[0].index_files = dup_strlist(self->vars[-1].index_files);
168
0
    self->vars[0].flags = self->vars[-1].flags;
169
0
    return 0;
170
0
}
171
172
static int on_config_exit(h2o_configurator_t *_self, h2o_configurator_context_t *ctx, yoml_t *node)
173
0
{
174
0
    struct st_h2o_file_configurator_t *self = (void *)_self;
175
0
    free(self->vars->index_files);
176
0
    --self->vars;
177
0
    return 0;
178
0
}
179
180
void h2o_file_register_configurator(h2o_globalconf_t *globalconf)
181
0
{
182
0
    struct st_h2o_file_configurator_t *self = (void *)h2o_configurator_create(globalconf, sizeof(*self));
183
184
0
    self->super.enter = on_config_enter;
185
0
    self->super.exit = on_config_exit;
186
0
    self->vars = self->_vars_stack;
187
0
    self->vars->index_files = h2o_file_default_index_files;
188
189
0
    h2o_configurator_define_command(
190
0
        &self->super, "file.dir", H2O_CONFIGURATOR_FLAG_PATH | H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR | H2O_CONFIGURATOR_FLAG_DEFERRED,
191
0
        on_config_dir);
192
0
    h2o_configurator_define_command(
193
0
        &self->super, "file.file",
194
0
        H2O_CONFIGURATOR_FLAG_PATH | H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR | H2O_CONFIGURATOR_FLAG_DEFERRED, on_config_file);
195
0
    h2o_configurator_define_command(&self->super, "file.index",
196
0
                                    (H2O_CONFIGURATOR_FLAG_ALL_LEVELS & ~H2O_CONFIGURATOR_FLAG_EXTENSION) |
197
0
                                        H2O_CONFIGURATOR_FLAG_EXPECT_SEQUENCE,
198
0
                                    on_config_index);
199
0
    h2o_configurator_define_command(&self->super, "file.etag",
200
0
                                    (H2O_CONFIGURATOR_FLAG_ALL_LEVELS & ~H2O_CONFIGURATOR_FLAG_EXTENSION) |
201
0
                                        H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR,
202
0
                                    on_config_etag);
203
0
    h2o_configurator_define_command(&self->super, "file.send-compressed",
204
0
                                    (H2O_CONFIGURATOR_FLAG_ALL_LEVELS & ~H2O_CONFIGURATOR_FLAG_EXTENSION) |
205
0
                                        H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR,
206
0
                                    on_config_send_compressed);
207
0
    h2o_configurator_define_command(&self->super, "file.send-gzip",
208
0
                                    (H2O_CONFIGURATOR_FLAG_ALL_LEVELS & ~H2O_CONFIGURATOR_FLAG_EXTENSION) |
209
0
                                        H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR,
210
0
                                    on_config_send_compressed);
211
0
    h2o_configurator_define_command(&self->super, "file.dirlisting",
212
0
                                    (H2O_CONFIGURATOR_FLAG_ALL_LEVELS & ~H2O_CONFIGURATOR_FLAG_EXTENSION) |
213
0
                                        H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR,
214
0
                                    on_config_dir_listing);
215
0
    h2o_configurator_define_command(&self->super, "file.io_uring",
216
0
                                    (H2O_CONFIGURATOR_FLAG_ALL_LEVELS & ~H2O_CONFIGURATOR_FLAG_EXTENSION) |
217
0
                                        H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR,
218
0
                                    on_config_io_uring);
219
0
}