示例#1
0
文件: file.c 项目: lhjay1/h2o
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");
}
示例#2
0
文件: main.c 项目: Debug-Orz/h2o
int main(int argc, char **argv)
{
    static struct option longopts[] = {
        { "conf", required_argument, NULL, 'c' },
        { "help", no_argument, NULL, 'h' },
        { NULL, 0, NULL, 0 }
    };

    static struct config_t config = {
        {}, /* global_config */
        NULL, /* listeners */
        0, /* num_listeners */
        1024, /* max_connections */
        1, /* num_threads */
        NULL, /* thread_ids */
        {}, /* state */
    };

    const char *config_file = "h2o.conf";
    int opt_ch;
    yoml_t *config_yoml;

    h2o_config_init(&config.global_config);
    config.global_config.close_cb = on_close;

    {
        h2o_configurator_t *c = h2o_config_create_configurator(&config.global_config, sizeof(*c));
        c->exit = on_config_listen_exit;
        h2o_config_define_command(
            c, "listen", H2O_CONFIGURATOR_FLAG_GLOBAL,
            on_config_listen,
            "port at which the server should listen for incoming requests (mandatory)",
            " - if the value is a scalar, it is treated as the port number (or as the",
            "   service name)",
            " - if the value is a mapping, following properties are recognized:",
            "     port: incoming port number or service name (mandatory)",
            "     host: incoming address (default: any address)",
            "     ssl:  if using SSL (default: none)",
            "       certificate-file: path of the certificate file",
            "       key-file:         path of the key file");
        h2o_config_define_command(
            c, "max-connections", H2O_CONFIGURATOR_FLAG_GLOBAL,
            on_config_max_connections,
            "max connections (default: 1024)");
        h2o_config_define_command(
            c, "num-threads", H2O_CONFIGURATOR_FLAG_GLOBAL,
            on_config_num_threads,
            "number of worker threads (default: 1)");
    }

    h2o_access_log_register_configurator(&config.global_config);
    h2o_file_register_configurator(&config.global_config);
    h2o_proxy_register_configurator(&config.global_config);

    /* parse options */
    while ((opt_ch = getopt_long(argc, argv, "c:h", longopts, NULL)) != -1) {
        switch (opt_ch) {
        case 'c':
            config_file = optarg;
            break;
        case 'h':
            usage(&config.global_config);
            exit(0);
            break;
        default:
            assert(0);
            break;
        }
    }
    argc -= optind;
    argv += optind;

    /* configure */
    if ((config_yoml = load_config(config_file)) == NULL)
        exit(EX_CONFIG);
    if (h2o_config_configure(&config.global_config, config_file, config_yoml) != 0)
        exit(EX_CONFIG);
    yoml_free(config_yoml);

    setup_signal_handlers();

    if (config.num_threads <= 1) {
        run_loop(&config);
    } else {
        config.thread_ids = alloca(sizeof(pthread_t) * config.num_threads);
        unsigned i;
        for (i = 0; i != config.num_threads; ++i) {
            pthread_create(config.thread_ids + i, NULL, run_loop, &config);
        }
        for (i = 0; i < config.num_threads; ++i) {
            pthread_join(config.thread_ids[i], NULL);
        }
    }

    return 0;
}