h2o_file_handler_t *h2o_file_register(h2o_hostconf_t *host_config, const char *virtual_path, const char *real_path, const char **index_files, h2o_mimemap_t *mimemap) { h2o_file_handler_t *self; size_t i; if (index_files == NULL) index_files = default_index_files; /* allocate memory */ for (i = 0; index_files[i] != NULL; ++i) ; self = (void*)h2o_create_handler(host_config, offsetof(h2o_file_handler_t, index_files[0]) + sizeof(self->index_files[0]) * (i + 1)); /* setup callbacks */ self->super.dispose = on_dispose; self->super.on_req = on_req; /* setup attributes */ self->virtual_path = append_slash_and_dup(virtual_path); self->real_path = append_slash_and_dup(real_path); if (mimemap != NULL) { h2o_mempool_addref_shared(mimemap); self->mimemap = mimemap; } else { self->mimemap = h2o_mimemap_create(); } for (i = 0; index_files[i] != NULL; ++i) { self->index_files[i] = h2o_strdup(NULL, index_files[i], SIZE_MAX); if (self->max_index_file_len < self->index_files[i].len) self->max_index_file_len = self->index_files[i].len; } return self; }
void h2o_config_init(h2o_globalconf_t *config) { memset(config, 0, sizeof(*config)); config->hosts = h2o_mem_alloc(sizeof(config->hosts[0])); config->hosts[0] = NULL; h2o_linklist_init_anchor(&config->configurators); config->server_name = h2o_iovec_init(H2O_STRLIT("h2o/" H2O_VERSION)); config->max_request_entity_size = H2O_DEFAULT_MAX_REQUEST_ENTITY_SIZE; config->max_delegations = H2O_DEFAULT_MAX_DELEGATIONS; config->handshake_timeout = H2O_DEFAULT_HANDSHAKE_TIMEOUT; config->http1.req_timeout = H2O_DEFAULT_HTTP1_REQ_TIMEOUT; config->http1.upgrade_to_http2 = H2O_DEFAULT_HTTP1_UPGRADE_TO_HTTP2; config->http1.callbacks = H2O_HTTP1_CALLBACKS; config->http2.idle_timeout = H2O_DEFAULT_HTTP2_IDLE_TIMEOUT; config->http2.graceful_shutdown_timeout = H2O_DEFAULT_HTTP2_GRACEFUL_SHUTDOWN_TIMEOUT; config->proxy.io_timeout = H2O_DEFAULT_PROXY_IO_TIMEOUT; config->proxy.connect_timeout = H2O_DEFAULT_PROXY_IO_TIMEOUT; config->proxy.first_byte_timeout = H2O_DEFAULT_PROXY_IO_TIMEOUT; config->proxy.emit_x_forwarded_headers = 1; config->proxy.emit_via_header = 1; config->proxy.emit_missing_date_header = 1; config->http2.max_concurrent_requests_per_connection = H2O_HTTP2_SETTINGS_HOST_MAX_CONCURRENT_STREAMS; config->http2.max_streams_for_priority = 16; config->http2.active_stream_window_size = H2O_DEFAULT_HTTP2_ACTIVE_STREAM_WINDOW_SIZE; config->http2.latency_optimization.min_rtt = 50; // milliseconds config->http2.latency_optimization.max_additional_delay = 10; config->http2.latency_optimization.max_cwnd = 65535; config->http2.callbacks = H2O_HTTP2_CALLBACKS; config->send_informational_mode = H2O_SEND_INFORMATIONAL_MODE_EXCEPT_H1; config->mimemap = h2o_mimemap_create(); h2o_socketpool_init_global(&config->proxy.global_socketpool, SIZE_MAX); h2o_configurator__init_core(config); }
void h2o_file_register_configurator(h2o_globalconf_t *globalconf) { struct st_h2o_file_configurator_t *self = (void *)h2o_configurator_create(globalconf, sizeof(*self)); self->super.enter = on_config_enter; self->super.exit = on_config_exit; self->vars = self->_vars_stack; self->vars->mimemap = h2o_mimemap_create(); self->vars->index_files = h2o_file_default_index_files; h2o_configurator_define_command(&self->super, "file.dir", H2O_CONFIGURATOR_FLAG_PATH | H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR | H2O_CONFIGURATOR_FLAG_DEFERRED, on_config_dir); h2o_configurator_define_command(&self->super, "file.index", (H2O_CONFIGURATOR_FLAG_ALL_LEVELS & ~H2O_CONFIGURATOR_FLAG_EXTENSION) | H2O_CONFIGURATOR_FLAG_EXPECT_SEQUENCE, on_config_index); h2o_configurator_define_command(&self->super, "file.mime.settypes", (H2O_CONFIGURATOR_FLAG_ALL_LEVELS & ~H2O_CONFIGURATOR_FLAG_EXTENSION) | H2O_CONFIGURATOR_FLAG_EXPECT_MAPPING, on_config_mime_settypes); h2o_configurator_define_command(&self->super, "file.mime.addtypes", (H2O_CONFIGURATOR_FLAG_ALL_LEVELS & ~H2O_CONFIGURATOR_FLAG_EXTENSION) | H2O_CONFIGURATOR_FLAG_EXPECT_MAPPING, on_config_mime_addtypes); h2o_configurator_define_command(&self->super, "file.mime.removetypes", (H2O_CONFIGURATOR_FLAG_ALL_LEVELS & ~H2O_CONFIGURATOR_FLAG_EXTENSION) | H2O_CONFIGURATOR_FLAG_EXPECT_SEQUENCE, on_config_mime_removetypes); h2o_configurator_define_command(&self->super, "file.mime.setdefaulttype", (H2O_CONFIGURATOR_FLAG_ALL_LEVELS & ~H2O_CONFIGURATOR_FLAG_EXTENSION) | H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR, on_config_mime_setdefaulttype); h2o_configurator_define_command(&self->super, "file.custom-handler", (H2O_CONFIGURATOR_FLAG_ALL_LEVELS & ~H2O_CONFIGURATOR_FLAG_EXTENSION) | H2O_CONFIGURATOR_FLAG_SEMI_DEFERRED, on_config_custom_handler); h2o_configurator_define_command(&self->super, "file.etag", (H2O_CONFIGURATOR_FLAG_ALL_LEVELS & ~H2O_CONFIGURATOR_FLAG_EXTENSION) | H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR, on_config_etag); h2o_configurator_define_command(&self->super, "file.send-gzip", (H2O_CONFIGURATOR_FLAG_ALL_LEVELS & ~H2O_CONFIGURATOR_FLAG_EXTENSION) | H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR, on_config_send_gzip); h2o_configurator_define_command(&self->super, "file.dirlisting", (H2O_CONFIGURATOR_FLAG_ALL_LEVELS & ~H2O_CONFIGURATOR_FLAG_EXTENSION) | H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR, on_config_dir_listing); }
static int on_config_mime_settypes(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node) { struct st_h2o_file_configurator_t *self = (void *)cmd->configurator; h2o_mimemap_t *newmap = h2o_mimemap_create(); h2o_mimemap_set_default_type(newmap, h2o_mimemap_get_default_type(self->vars->mimemap), 1); if (set_mimetypes(cmd, newmap, node) != 0) { h2o_mem_release_shared(newmap); return -1; } h2o_mem_release_shared(self->vars->mimemap); self->vars->mimemap = newmap; return 0; }
void h2o_file_register_configurator(h2o_globalconf_t *globalconf) { struct st_h2o_file_configurator_t *self = (void*)h2o_config_create_configurator(globalconf, sizeof(*self)); self->super.enter = on_config_enter; self->super.exit = on_config_exit; self->vars = self->_vars_stack; self->vars->mimemap = h2o_mimemap_create(); self->vars->index_files = default_index_files; h2o_config_define_command( &self->super, "file.dir", H2O_CONFIGURATOR_FLAG_PATH | H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR | H2O_CONFIGURATOR_FLAG_DEFERRED, on_config_dir, "directory under which to serve the target path"); h2o_config_define_command( &self->super, "file.index", H2O_CONFIGURATOR_FLAG_GLOBAL | H2O_CONFIGURATOR_FLAG_HOST | H2O_CONFIGURATOR_FLAG_PATH | H2O_CONFIGURATOR_FLAG_EXPECT_SEQUENCE, on_config_index, "sequence of index file names (default: index.html index.htm index.txt)"); h2o_config_define_command( &self->super, "file.mime.settypes", H2O_CONFIGURATOR_FLAG_GLOBAL | H2O_CONFIGURATOR_FLAG_HOST | H2O_CONFIGURATOR_FLAG_PATH | H2O_CONFIGURATOR_FLAG_EXPECT_MAPPING, on_config_mime_settypes, "map of mime-type -> (extension | sequence-of-extensions)"); h2o_config_define_command( &self->super, "file.mime.addtypes", H2O_CONFIGURATOR_FLAG_GLOBAL | H2O_CONFIGURATOR_FLAG_HOST | H2O_CONFIGURATOR_FLAG_PATH | H2O_CONFIGURATOR_FLAG_EXPECT_MAPPING, on_config_mime_addtypes, "map of mime-type -> (extension | sequence-of-extensions)"); h2o_config_define_command( &self->super, "file.mime.removetypes", H2O_CONFIGURATOR_FLAG_GLOBAL | H2O_CONFIGURATOR_FLAG_HOST | H2O_CONFIGURATOR_FLAG_PATH | H2O_CONFIGURATOR_FLAG_EXPECT_SEQUENCE, on_config_mime_removetypes, "sequence of extensions"); h2o_config_define_command( &self->super, "file.mime.setdefaulttype", H2O_CONFIGURATOR_FLAG_GLOBAL | H2O_CONFIGURATOR_FLAG_HOST | H2O_CONFIGURATOR_FLAG_PATH | H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR, on_config_mime_setdefaulttype, "default mime-type"); }
h2o_file_handler_t *h2o_file_register(h2o_pathconf_t *pathconf, const char *real_path, const char **index_files, h2o_mimemap_t *mimemap, int flags) { h2o_file_handler_t *self; size_t i; if (index_files == NULL) index_files = default_index_files; /* allocate memory */ for (i = 0; index_files[i] != NULL; ++i) ; self = (void *)h2o_create_handler(pathconf, offsetof(h2o_file_handler_t, index_files[0]) + sizeof(self->index_files[0]) * (i + 1)); /* setup callbacks */ self->super.on_context_init = on_context_init; self->super.on_context_dispose = on_context_dispose; self->super.dispose = on_dispose; self->super.on_req = on_req; /* setup attributes */ self->conf_path = h2o_strdup_slashed(NULL, pathconf->path.base, pathconf->path.len); self->real_path = h2o_strdup_slashed(NULL, real_path, SIZE_MAX); if (mimemap != NULL) { h2o_mem_addref_shared(mimemap); self->mimemap = mimemap; } else { self->mimemap = h2o_mimemap_create(); } self->flags = flags; for (i = 0; index_files[i] != NULL; ++i) { self->index_files[i] = h2o_strdup(NULL, index_files[i], SIZE_MAX); if (self->max_index_file_len < self->index_files[i].len) self->max_index_file_len = self->index_files[i].len; } return self; }
void h2o_config_init(h2o_globalconf_t *config) { memset(config, 0, sizeof(*config)); config->hosts = h2o_mem_alloc(sizeof(config->hosts[0])); config->hosts[0] = NULL; h2o_linklist_init_anchor(&config->configurators); config->server_name = h2o_iovec_init(H2O_STRLIT("h2o/" H2O_VERSION)); config->max_request_entity_size = H2O_DEFAULT_MAX_REQUEST_ENTITY_SIZE; config->max_delegations = H2O_DEFAULT_MAX_DELEGATIONS; config->handshake_timeout = H2O_DEFAULT_HANDSHAKE_TIMEOUT; config->http1.req_timeout = H2O_DEFAULT_HTTP1_REQ_TIMEOUT; config->http1.upgrade_to_http2 = H2O_DEFAULT_HTTP1_UPGRADE_TO_HTTP2; config->http1.callbacks = H2O_HTTP1_CALLBACKS; config->http2.idle_timeout = H2O_DEFAULT_HTTP2_IDLE_TIMEOUT; config->proxy.io_timeout = H2O_DEFAULT_PROXY_IO_TIMEOUT; config->http2.max_concurrent_requests_per_connection = H2O_HTTP2_SETTINGS_HOST.max_concurrent_streams; config->http2.max_streams_for_priority = 16; config->http2.callbacks = H2O_HTTP2_CALLBACKS; config->mimemap = h2o_mimemap_create(); h2o_configurator__init_core(config); }