/src/h2o/lib/core/config.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2014-2016 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 <limits.h> |
24 | | #include <stdarg.h> |
25 | | #include <stdio.h> |
26 | | #include <stdlib.h> |
27 | | #include <string.h> |
28 | | #include <sys/types.h> |
29 | | #include <netinet/udp.h> |
30 | | #include "h2o.h" |
31 | | #include "h2o/configurator.h" |
32 | | #include "h2o/http1.h" |
33 | | #include "h2o/http2.h" |
34 | | #include "h2o/http3_server.h" |
35 | | #include "h2o/version.h" |
36 | | |
37 | | static h2o_hostconf_t *create_hostconf(h2o_globalconf_t *globalconf) |
38 | 6 | { |
39 | 6 | h2o_hostconf_t *hostconf = h2o_mem_alloc(sizeof(*hostconf)); |
40 | 6 | *hostconf = (h2o_hostconf_t){globalconf}; |
41 | 6 | hostconf->http2.push_preload = 1; /* enabled by default */ |
42 | 6 | h2o_config_init_pathconf(&hostconf->fallback_path, globalconf, NULL, globalconf->mimemap); |
43 | 6 | hostconf->mimemap = globalconf->mimemap; |
44 | 6 | h2o_mem_addref_shared(hostconf->mimemap); |
45 | 6 | return hostconf; |
46 | 6 | } |
47 | | |
48 | | static void destroy_hostconf(h2o_hostconf_t *hostconf) |
49 | 0 | { |
50 | 0 | size_t i; |
51 | |
|
52 | 0 | if (hostconf->authority.hostport.base != hostconf->authority.host.base) |
53 | 0 | free(hostconf->authority.hostport.base); |
54 | 0 | free(hostconf->authority.host.base); |
55 | 0 | for (i = 0; i != hostconf->paths.size; ++i) { |
56 | 0 | h2o_pathconf_t *pathconf = hostconf->paths.entries[i]; |
57 | 0 | h2o_config_dispose_pathconf(pathconf); |
58 | 0 | free(pathconf); |
59 | 0 | } |
60 | 0 | free(hostconf->paths.entries); |
61 | 0 | h2o_config_dispose_pathconf(&hostconf->fallback_path); |
62 | 0 | h2o_mem_release_shared(hostconf->mimemap); |
63 | |
|
64 | 0 | free(hostconf); |
65 | 0 | } |
66 | | |
67 | | static void on_dispose_envconf(void *_envconf) |
68 | 0 | { |
69 | 0 | h2o_envconf_t *envconf = _envconf; |
70 | 0 | size_t i; |
71 | |
|
72 | 0 | if (envconf->parent != NULL) |
73 | 0 | h2o_mem_release_shared(envconf->parent); |
74 | |
|
75 | 0 | for (i = 0; i != envconf->unsets.size; ++i) |
76 | 0 | h2o_mem_release_shared(envconf->unsets.entries[i].base); |
77 | 0 | free(envconf->unsets.entries); |
78 | 0 | for (i = 0; i != envconf->sets.size; ++i) |
79 | 0 | h2o_mem_release_shared(envconf->sets.entries[i].base); |
80 | 0 | free(envconf->sets.entries); |
81 | 0 | } |
82 | | |
83 | | h2o_envconf_t *h2o_config_create_envconf(h2o_envconf_t *parent) |
84 | 0 | { |
85 | 0 | h2o_envconf_t *envconf = h2o_mem_alloc_shared(NULL, sizeof(*envconf), on_dispose_envconf); |
86 | 0 | *envconf = (h2o_envconf_t){NULL}; |
87 | |
|
88 | 0 | if (parent != NULL) { |
89 | 0 | envconf->parent = parent; |
90 | 0 | h2o_mem_addref_shared(parent); |
91 | 0 | } |
92 | 0 | return envconf; |
93 | 0 | } |
94 | | |
95 | | void h2o_config_setenv(h2o_envconf_t *envconf, const char *name, const char *value) |
96 | 0 | { |
97 | 0 | size_t name_len = strlen(name), i; |
98 | 0 | h2o_iovec_t *value_slot; |
99 | | |
100 | | /* remove from the list of unsets */ |
101 | 0 | for (i = 0; i != envconf->unsets.size; ++i) { |
102 | 0 | if (h2o_memis(envconf->unsets.entries[i].base, envconf->unsets.entries[i].len, name, name_len)) { |
103 | 0 | h2o_mem_release_shared(envconf->unsets.entries[i].base); |
104 | 0 | h2o_vector_erase(&envconf->unsets, i); |
105 | 0 | break; |
106 | 0 | } |
107 | 0 | } |
108 | | /* find the slot */ |
109 | 0 | for (i = 0; i != envconf->sets.size; i += 2) { |
110 | 0 | if (h2o_memis(envconf->sets.entries[i].base, envconf->sets.entries[i].len, name, name_len)) { |
111 | 0 | value_slot = envconf->sets.entries + i + 1; |
112 | 0 | h2o_mem_release_shared(value_slot->base); |
113 | 0 | goto SetValue; |
114 | 0 | } |
115 | 0 | } |
116 | | /* name not found in existing sets */ |
117 | 0 | h2o_vector_reserve(NULL, &envconf->sets, envconf->sets.size + 2); |
118 | 0 | envconf->sets.entries[envconf->sets.size++] = h2o_strdup_shared(NULL, name, name_len); |
119 | 0 | value_slot = envconf->sets.entries + envconf->sets.size++; |
120 | 0 | SetValue: |
121 | 0 | *value_slot = h2o_strdup_shared(NULL, value, SIZE_MAX); |
122 | 0 | } |
123 | | |
124 | | void h2o_config_unsetenv(h2o_envconf_t *envconf, const char *name) |
125 | 0 | { |
126 | 0 | size_t i, name_len = strlen(name); |
127 | | |
128 | | /* do nothing if already set */ |
129 | 0 | for (i = 0; i != envconf->unsets.size; ++i) |
130 | 0 | if (h2o_memis(envconf->unsets.entries[i].base, envconf->unsets.entries[i].len, name, name_len)) |
131 | 0 | return; |
132 | | /* register */ |
133 | 0 | h2o_vector_reserve(NULL, &envconf->unsets, envconf->unsets.size + 1); |
134 | 0 | envconf->unsets.entries[envconf->unsets.size++] = h2o_strdup_shared(NULL, name, name_len); |
135 | 0 | } |
136 | | |
137 | | void h2o_config_init_pathconf(h2o_pathconf_t *pathconf, h2o_globalconf_t *globalconf, const char *path, h2o_mimemap_t *mimemap) |
138 | 14 | { |
139 | 14 | memset(pathconf, 0, sizeof(*pathconf)); |
140 | 14 | pathconf->global = globalconf; |
141 | 14 | if (path != NULL) |
142 | 8 | pathconf->path = h2o_strdup(NULL, path, SIZE_MAX); |
143 | 14 | h2o_mem_addref_shared(mimemap); |
144 | 14 | pathconf->mimemap = mimemap; |
145 | 14 | pathconf->error_log.emit_request_errors = 1; |
146 | 14 | } |
147 | | |
148 | | void h2o_config_dispose_pathconf(h2o_pathconf_t *pathconf) |
149 | 0 | { |
150 | 0 | #define DESTROY_LIST(type, list) \ |
151 | 0 | do { \ |
152 | 0 | size_t i; \ |
153 | 0 | for (i = 0; i != list.size; ++i) { \ |
154 | 0 | type *e = list.entries[i]; \ |
155 | 0 | if (e->dispose != NULL) \ |
156 | 0 | e->dispose(e); \ |
157 | 0 | free(e); \ |
158 | 0 | } \ |
159 | 0 | free(list.entries); \ |
160 | 0 | } while (0) |
161 | 0 | DESTROY_LIST(h2o_handler_t, pathconf->handlers); |
162 | 0 | DESTROY_LIST(h2o_filter_t, pathconf->_filters); |
163 | 0 | DESTROY_LIST(h2o_logger_t, pathconf->_loggers); |
164 | 0 | #undef DESTROY_LIST |
165 | |
|
166 | 0 | free(pathconf->path.base); |
167 | 0 | if (pathconf->mimemap != NULL) |
168 | 0 | h2o_mem_release_shared(pathconf->mimemap); |
169 | 0 | if (pathconf->env != NULL) |
170 | 0 | h2o_mem_release_shared(pathconf->env); |
171 | 0 | } |
172 | | |
173 | | void h2o_config_init(h2o_globalconf_t *config) |
174 | 3 | { |
175 | 3 | memset(config, 0, sizeof(*config)); |
176 | 3 | config->hosts = h2o_mem_alloc(sizeof(config->hosts[0])); |
177 | 3 | config->hosts[0] = NULL; |
178 | 3 | h2o_linklist_init_anchor(&config->configurators); |
179 | 3 | config->server_name = h2o_iovec_init(H2O_STRLIT("h2o/" H2O_VERSION)); |
180 | 3 | config->max_request_entity_size = H2O_DEFAULT_MAX_REQUEST_ENTITY_SIZE; |
181 | 3 | config->max_delegations = H2O_DEFAULT_MAX_DELEGATIONS; |
182 | 3 | config->max_reprocesses = H2O_DEFAULT_MAX_REPROCESSES; |
183 | 3 | config->handshake_timeout = H2O_DEFAULT_HANDSHAKE_TIMEOUT; |
184 | 3 | config->http1.req_timeout = H2O_DEFAULT_HTTP1_REQ_TIMEOUT; |
185 | 3 | config->http1.req_io_timeout = H2O_DEFAULT_HTTP1_REQ_IO_TIMEOUT; |
186 | 3 | config->http1.upgrade_to_http2 = H2O_DEFAULT_HTTP1_UPGRADE_TO_HTTP2; |
187 | 3 | config->http2.idle_timeout = H2O_DEFAULT_HTTP2_IDLE_TIMEOUT; |
188 | 3 | config->http2.graceful_shutdown_timeout = H2O_DEFAULT_HTTP2_GRACEFUL_SHUTDOWN_TIMEOUT; |
189 | 3 | config->proxy.io_timeout = H2O_DEFAULT_PROXY_IO_TIMEOUT; |
190 | 3 | config->proxy.connect_timeout = H2O_DEFAULT_PROXY_IO_TIMEOUT; |
191 | 3 | config->proxy.first_byte_timeout = H2O_DEFAULT_PROXY_IO_TIMEOUT; |
192 | 3 | config->proxy.emit_x_forwarded_headers = 1; |
193 | 3 | config->proxy.emit_via_header = 1; |
194 | 3 | config->proxy.emit_missing_date_header = 1; |
195 | 3 | config->proxy.zerocopy = H2O_PROXY_ZEROCOPY_ENABLED; |
196 | 3 | config->http2.max_streams = H2O_HTTP2_SETTINGS_HOST_MAX_CONCURRENT_STREAMS; |
197 | 3 | config->http2.max_concurrent_requests_per_connection = H2O_HTTP2_SETTINGS_HOST_MAX_CONCURRENT_STREAMS; |
198 | 3 | config->http2.max_concurrent_streaming_requests_per_connection = H2O_HTTP2_DEFAULT_MAX_CONCURRENT_STREAMING_REQUESTS; |
199 | 3 | config->http2.max_streams_for_priority = 16; |
200 | 3 | config->http2.active_stream_window_size = H2O_DEFAULT_HTTP2_ACTIVE_STREAM_WINDOW_SIZE; |
201 | 3 | config->http2.latency_optimization.min_rtt = 50; // milliseconds |
202 | 3 | config->http2.latency_optimization.max_additional_delay = 10; |
203 | 3 | config->http2.latency_optimization.max_cwnd = 65535; |
204 | 3 | config->http2.dos_delay = 100; /* 100ms processing delay when observing suspicious behavior */ |
205 | 3 | config->http3.idle_timeout = quicly_spec_context.transport_params.max_idle_timeout; |
206 | 3 | config->http3.active_stream_window_size = H2O_DEFAULT_HTTP3_ACTIVE_STREAM_WINDOW_SIZE; |
207 | 3 | config->http3.allow_delayed_ack = 1; |
208 | 3 | config->http3.use_gso = 1; |
209 | 3 | config->http3.max_concurrent_streaming_requests_per_connection = H2O_HTTP3_DEFAULT_MAX_CONCURRENT_STREAMING_REQUESTS; |
210 | 3 | config->send_informational_mode = H2O_SEND_INFORMATIONAL_MODE_EXCEPT_H1; |
211 | 3 | config->mimemap = h2o_mimemap_create(); |
212 | 3 | h2o_socketpool_init_global(&config->proxy.global_socketpool, SIZE_MAX); |
213 | | |
214 | 3 | h2o_configurator__init_core(config); |
215 | | |
216 | 3 | config->fallback_host = create_hostconf(config); |
217 | 3 | config->fallback_host->authority.port = 65535; |
218 | 3 | config->fallback_host->authority.host = h2o_strdup(NULL, H2O_STRLIT("*")); |
219 | 3 | config->fallback_host->authority.hostport = h2o_strdup(NULL, H2O_STRLIT("*")); |
220 | 3 | } |
221 | | |
222 | | h2o_pathconf_t *h2o_config_register_path(h2o_hostconf_t *hostconf, const char *path, int flags) |
223 | 8 | { |
224 | 8 | h2o_pathconf_t *pathconf = h2o_mem_alloc(sizeof(*pathconf)); |
225 | 8 | h2o_config_init_pathconf(pathconf, hostconf->global, path, hostconf->mimemap); |
226 | | |
227 | | /* Find the slot to insert the new pathconf. Sort order is descending by the path length so that longer pathconfs overriding |
228 | | * subdirectories of shorter ones would work, regardless of the regisration order. Pathconfs sharing the same length are sorted |
229 | | * in the ascending order of memcmp / strcmp (as we have always done in the h2o standalone server). */ |
230 | 8 | size_t slot; |
231 | 15 | for (slot = 0; slot < hostconf->paths.size; ++slot) { |
232 | 7 | if (pathconf->path.len > hostconf->paths.entries[slot]->path.len) |
233 | 0 | break; |
234 | 7 | if (pathconf->path.len == hostconf->paths.entries[slot]->path.len && |
235 | 7 | memcmp(pathconf->path.base, hostconf->paths.entries[slot]->path.base, pathconf->path.len) < 0) |
236 | 0 | break; |
237 | 7 | } |
238 | | |
239 | 8 | h2o_vector_reserve(NULL, &hostconf->paths, hostconf->paths.size + 1); |
240 | 8 | if (slot < hostconf->paths.size) |
241 | 0 | memmove(hostconf->paths.entries + slot + 1, hostconf->paths.entries + slot, |
242 | 0 | (hostconf->paths.size - slot) * sizeof(hostconf->paths.entries[0])); |
243 | 8 | hostconf->paths.entries[slot] = pathconf; |
244 | 8 | ++hostconf->paths.size; |
245 | | |
246 | 8 | return pathconf; |
247 | 8 | } |
248 | | |
249 | | void h2o_config_register_status_handler(h2o_globalconf_t *config, h2o_status_handler_t *status_handler) |
250 | 0 | { |
251 | | /* check if the status handler is already registered */ |
252 | 0 | size_t i; |
253 | 0 | for (i = 0; i != config->statuses.size; ++i) |
254 | 0 | if (config->statuses.entries[i] == status_handler) |
255 | 0 | return; |
256 | | /* register the new handler */ |
257 | 0 | h2o_vector_reserve(NULL, &config->statuses, config->statuses.size + 1); |
258 | 0 | config->statuses.entries[config->statuses.size++] = status_handler; |
259 | 0 | } |
260 | | |
261 | | h2o_hostconf_t *h2o_config_register_host(h2o_globalconf_t *config, h2o_iovec_t host, uint16_t port) |
262 | 3 | { |
263 | 3 | h2o_hostconf_t *hostconf = NULL; |
264 | 3 | h2o_iovec_t host_lc; |
265 | | |
266 | 3 | assert(host.len != 0); |
267 | | |
268 | | /* convert hostname to lowercase */ |
269 | 0 | host_lc = h2o_strdup(NULL, host.base, host.len); |
270 | 3 | h2o_strtolower(host_lc.base, host_lc.len); |
271 | | |
272 | 3 | { /* return NULL if given authority is already registered */ |
273 | 3 | h2o_hostconf_t **p; |
274 | 3 | for (p = config->hosts; *p != NULL; ++p) |
275 | 0 | if (h2o_memis((*p)->authority.host.base, (*p)->authority.host.len, host_lc.base, host_lc.len) && |
276 | 0 | (*p)->authority.port == port) |
277 | 0 | goto Exit; |
278 | 3 | } |
279 | | |
280 | | /* create hostconf */ |
281 | 3 | hostconf = create_hostconf(config); |
282 | 3 | hostconf->authority.host = host_lc; |
283 | 3 | host_lc = (h2o_iovec_t){NULL}; |
284 | 3 | hostconf->authority.port = port; |
285 | 3 | if (hostconf->authority.port == 65535) { |
286 | 3 | hostconf->authority.hostport = hostconf->authority.host; |
287 | 3 | } else { |
288 | 0 | hostconf->authority.hostport.base = h2o_mem_alloc(hostconf->authority.host.len + sizeof("[]:" H2O_UINT16_LONGEST_STR)); |
289 | 0 | if (strchr(hostconf->authority.host.base, ':') != NULL) { |
290 | 0 | hostconf->authority.hostport.len = |
291 | 0 | sprintf(hostconf->authority.hostport.base, "[%s]:%" PRIu16, hostconf->authority.host.base, port); |
292 | 0 | } else { |
293 | 0 | hostconf->authority.hostport.len = |
294 | 0 | sprintf(hostconf->authority.hostport.base, "%s:%" PRIu16, hostconf->authority.host.base, port); |
295 | 0 | } |
296 | 0 | } |
297 | | |
298 | | /* append to the list */ |
299 | 3 | h2o_append_to_null_terminated_list((void *)&config->hosts, hostconf); |
300 | | |
301 | 3 | Exit: |
302 | 3 | free(host_lc.base); |
303 | 3 | return hostconf; |
304 | 3 | } |
305 | | |
306 | | void h2o_config_dispose(h2o_globalconf_t *config) |
307 | 0 | { |
308 | 0 | size_t i; |
309 | |
|
310 | 0 | for (i = 0; config->hosts[i] != NULL; ++i) { |
311 | 0 | h2o_hostconf_t *hostconf = config->hosts[i]; |
312 | 0 | destroy_hostconf(hostconf); |
313 | 0 | } |
314 | 0 | free(config->hosts); |
315 | |
|
316 | 0 | destroy_hostconf(config->fallback_host); |
317 | |
|
318 | 0 | h2o_socketpool_dispose(&config->proxy.global_socketpool); |
319 | 0 | h2o_mem_release_shared(config->mimemap); |
320 | 0 | h2o_configurator__dispose_configurators(config); |
321 | 0 | } |
322 | | |
323 | | h2o_handler_t *h2o_create_handler(h2o_pathconf_t *conf, size_t sz) |
324 | 8 | { |
325 | 8 | h2o_handler_t *handler = h2o_mem_alloc(sz); |
326 | | |
327 | 8 | memset(handler, 0, sz); |
328 | 8 | handler->_config_slot = conf->global->_num_config_slots++; |
329 | | |
330 | 8 | h2o_vector_reserve(NULL, &conf->handlers, conf->handlers.size + 1); |
331 | 8 | conf->handlers.entries[conf->handlers.size++] = handler; |
332 | | |
333 | 8 | return handler; |
334 | 8 | } |
335 | | |
336 | | h2o_filter_t *h2o_create_filter(h2o_pathconf_t *conf, size_t sz) |
337 | 0 | { |
338 | 0 | h2o_filter_t *filter = h2o_mem_alloc(sz); |
339 | |
|
340 | 0 | memset(filter, 0, sz); |
341 | 0 | filter->_config_slot = conf->global->_num_config_slots++; |
342 | |
|
343 | 0 | h2o_vector_reserve(NULL, &conf->_filters, conf->_filters.size + 1); |
344 | 0 | memmove(conf->_filters.entries + 1, conf->_filters.entries, conf->_filters.size * sizeof(conf->_filters.entries[0])); |
345 | 0 | conf->_filters.entries[0] = filter; |
346 | 0 | ++conf->_filters.size; |
347 | |
|
348 | 0 | return filter; |
349 | 0 | } |
350 | | |
351 | | h2o_logger_t *h2o_create_logger(h2o_pathconf_t *conf, size_t sz) |
352 | 0 | { |
353 | 0 | h2o_logger_t *logger = h2o_mem_alloc(sz); |
354 | |
|
355 | 0 | memset(logger, 0, sz); |
356 | 0 | logger->_config_slot = conf->global->_num_config_slots++; |
357 | |
|
358 | 0 | h2o_vector_reserve(NULL, &conf->_loggers, conf->_loggers.size + 1); |
359 | 0 | conf->_loggers.entries[conf->_loggers.size++] = logger; |
360 | |
|
361 | 0 | return logger; |
362 | 0 | } |