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