Ejemplo n.º 1
0
int main(int argc, char **argv)
{
    uv_loop_t *loop = uv_default_loop();
    uv_tcp_t listener;
    struct sockaddr_in sockaddr;
    h2o_handler_t ws_handler;
    int r;

    if ((r = uv_tcp_init(loop, &listener)) != 0) {
        fprintf(stderr, "uv_tcp_init:%s\n", uv_strerror(r));
        goto Error;
    }
    uv_ip4_addr("127.0.0.1", 7890, &sockaddr);
    if ((r = uv_tcp_bind(&listener, (struct sockaddr*)&sockaddr, sizeof(sockaddr))) != 0) {
        fprintf(stderr, "uv_tcp_bind:%s\n", uv_strerror(r));
        goto Error;
    }
    if ((r = uv_listen((uv_stream_t*)&listener, 128, on_connect)) != 0) {
        fprintf(stderr, "uv_listen:%s\n", uv_strerror(r));
        goto Error;
    }

    h2o_config_init(&config);
    memset(&ws_handler, 0, sizeof(ws_handler));
    ws_handler.on_req = on_req;
    h2o_linklist_insert(&config.default_host.handlers, &ws_handler._link);
    h2o_context_init(&ctx, loop, &config);
    //ssl_ctx = h2o_ssl_new_server_context("server.crt", "server.key", NULL);

    return uv_run(loop, UV_RUN_DEFAULT);

Error:
    return 1;
}
Ejemplo n.º 2
0
void test_issues293()
{
    h2o_globalconf_t globalconf;

    h2o_config_init(&globalconf);

    /* register two hosts, using 80 and 443 */
    register_authority(&globalconf, h2o_iovec_init(H2O_STRLIT("default")), 65535);
    register_authority(&globalconf, h2o_iovec_init(H2O_STRLIT("host1")), 80);
    register_authority(&globalconf, h2o_iovec_init(H2O_STRLIT("host1")), 443);
    register_authority(&globalconf, h2o_iovec_init(H2O_STRLIT("host2")), 80);
    register_authority(&globalconf, h2o_iovec_init(H2O_STRLIT("host2")), 443);
    register_authority(&globalconf, h2o_iovec_init(H2O_STRLIT("host3")), 65535);

    h2o_context_init(&ctx, test_loop, &globalconf);

    /* run the tests */
    check(&H2O_URL_SCHEME_HTTP, "host1", "host1:80");
    check(&H2O_URL_SCHEME_HTTPS, "host1", "host1:443");
    check(&H2O_URL_SCHEME_HTTP, "host2", "host2:80");
    check(&H2O_URL_SCHEME_HTTPS, "host2", "host2:443");

    /* supplied port number in the Host header must be preferred */
    check(&H2O_URL_SCHEME_HTTP, "host1:80", "host1:80");
    check(&H2O_URL_SCHEME_HTTP, "host1:443", "host1:443");
    check(&H2O_URL_SCHEME_HTTPS, "host1:80", "host1:80");
    check(&H2O_URL_SCHEME_HTTPS, "host1:443", "host1:443");
    check(&H2O_URL_SCHEME_HTTP, "host2:80", "host2:80");
    check(&H2O_URL_SCHEME_HTTP, "host2:443", "host2:443");
    check(&H2O_URL_SCHEME_HTTPS, "host2:80", "host2:80");
    check(&H2O_URL_SCHEME_HTTPS, "host2:443", "host2:443");

    /* host-level conf without default port */
    check(&H2O_URL_SCHEME_HTTP, "host3", "host3:65535");
    check(&H2O_URL_SCHEME_HTTPS, "host3", "host3:65535");
    check(&H2O_URL_SCHEME_HTTP, "host3", "host3:65535");
    check(&H2O_URL_SCHEME_HTTPS, "host3", "host3:65535");
    check(&H2O_URL_SCHEME_HTTP, "host3:80", "host3:65535");
    check(&H2O_URL_SCHEME_HTTPS, "host3:80", "default:65535");
    check(&H2O_URL_SCHEME_HTTP, "host3:443", "default:65535");
    check(&H2O_URL_SCHEME_HTTPS, "host3:443", "host3:65535");

    /* upper-case */
    check(&H2O_URL_SCHEME_HTTP, "HoST1", "host1:80");
    check(&H2O_URL_SCHEME_HTTP, "HoST1:80", "host1:80");
    check(&H2O_URL_SCHEME_HTTPS, "HoST1", "host1:443");
    check(&H2O_URL_SCHEME_HTTPS, "HoST1:443", "host1:443");

    h2o_context_dispose(&ctx);
    h2o_config_dispose(&globalconf);
}
Ejemplo n.º 3
0
int main(int argc, char **argv)
{
    h2o_hostconf_t *hostconf;

    signal(SIGPIPE, SIG_IGN);

    h2o_config_init(&config);
    hostconf = h2o_config_register_host(&config, "default");
    register_handler(hostconf, "/post-test", post_test);
    register_handler(hostconf, "/chunked-test", chunked_test);
    h2o_file_register(h2o_config_register_path(hostconf, "/"), "examples/doc_root", NULL, NULL, 0);

#if 0 /* reproxy is not yet implemented */
    register_handler(hostconf, "/reproxy-test", reproxy_test);
    h2o_reproxy_register(hostconf);
#endif

#if H2O_USE_LIBUV
    uv_loop_t loop;
    uv_loop_init(&loop);
    h2o_context_init(&ctx, &loop, &config);
#else
    h2o_context_init(&ctx, h2o_evloop_create(), &config);
#endif

    /* disabled by default: uncomment the block below to use HTTPS instead of HTTP */
    /*
    if (setup_ssl("server.crt", "server.key") != 0)
        goto Error;
    */

    /* disabled by default: uncomment the line below to enable access logging */
    /* h2o_access_log_register(&config.default_host, "/dev/stdout", NULL); */

    if (create_listener() != 0) {
        fprintf(stderr, "failed to listen to 127.0.0.1:7890:%s\n", strerror(errno));
        goto Error;
    }

#if H2O_USE_LIBUV
    uv_run(ctx.loop, UV_RUN_DEFAULT);
#else
    while (h2o_evloop_run(ctx.loop) == 0)
        ;
#endif

Error:
    return 1;
}
Ejemplo n.º 4
0
void test_lib__handler__redirect_c()
{
    h2o_globalconf_t globalconf;
    h2o_hostconf_t *hostconf;
    h2o_pathconf_t *pathconf;

    h2o_config_init(&globalconf);
    hostconf = h2o_config_register_host(&globalconf, h2o_iovec_init(H2O_STRLIT("default")), 65535);
    pathconf = h2o_config_register_path(hostconf, "/", 0);
    h2o_redirect_register(pathconf, 0, 301, "https://example.com/bar/");

    h2o_context_init(&ctx, test_loop, &globalconf);

    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx, ctx.globalconf->hosts);
        conn->req.input.method = h2o_iovec_init(H2O_STRLIT("GET"));
        conn->req.input.path = h2o_iovec_init(H2O_STRLIT("/"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 301);
        ok(check_header(&conn->req.res, H2O_TOKEN_LOCATION, "https://example.com/bar/"));
        ok(conn->body->size != 0);
        h2o_loopback_destroy(conn);
    }
    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx, ctx.globalconf->hosts);
        conn->req.input.method = h2o_iovec_init(H2O_STRLIT("GET"));
        conn->req.input.path = h2o_iovec_init(H2O_STRLIT("/abc"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 301);
        ok(check_header(&conn->req.res, H2O_TOKEN_LOCATION, "https://example.com/bar/abc"));
        ok(conn->body->size != 0);
        h2o_loopback_destroy(conn);
    }
    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx, ctx.globalconf->hosts);
        conn->req.input.method = h2o_iovec_init(H2O_STRLIT("HEAD"));
        conn->req.input.path = h2o_iovec_init(H2O_STRLIT("/"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 301);
        ok(check_header(&conn->req.res, H2O_TOKEN_LOCATION, "https://example.com/bar/"));
        ok(conn->body->size == 0);
        h2o_loopback_destroy(conn);
    }

    h2o_context_dispose(&ctx);
    h2o_config_dispose(&globalconf);
}
Ejemplo n.º 5
0
void test_lib__handler__fastcgi_c()
{
    h2o_globalconf_t globalconf;
    h2o_hostconf_t *hostconf;
    h2o_pathconf_t *pathconf;

    h2o_config_init(&globalconf);
    globalconf.server_name = h2o_iovec_init(H2O_STRLIT("h2o/1.2.1-alpha1"));
    hostconf = h2o_config_register_host(&globalconf, h2o_iovec_init(H2O_STRLIT("default")), 65535);
    pathconf = h2o_config_register_path(hostconf, "/");

    h2o_context_init(&ctx, test_loop, &globalconf);

    subtest("build-request", test_build_request);

    h2o_context_dispose(&ctx);
    h2o_config_dispose(&globalconf);
}
Ejemplo n.º 6
0
int main(int argc, char **argv)
{
    h2o_hostconf_t *hostconf;
    h2o_pathconf_t *pathconf;

    h2o_config_init(&config);
    hostconf = h2o_config_register_host(&config, h2o_iovec_init(H2O_STRLIT("default")), 65535);
    pathconf = h2o_config_register_path(hostconf, "/", 0);
    h2o_create_handler(pathconf, sizeof(h2o_handler_t))->on_req = on_req;

#if H2O_USE_LIBUV
    uv_loop_t loop;
    uv_loop_init(&loop);
    h2o_context_init(&ctx, &loop, &config);
#else
    h2o_context_init(&ctx, h2o_evloop_create(), &config);
#endif

    /* disabled by default: uncomment the block below to use HTTPS instead of HTTP */
    /*
    if (setup_ssl("server.crt", "server.key") != 0)
        goto Error;
    */

    accept_ctx.ctx = &ctx;
    accept_ctx.hosts = config.hosts;

    if (create_listener() != 0) {
        fprintf(stderr, "failed to listen to 127.0.0.1:7890:%s\n", strerror(errno));
        goto Error;
    }

#if H2O_USE_LIBUV
    uv_run(ctx.loop, UV_RUN_DEFAULT);
#else
    while (h2o_evloop_run(ctx.loop, INT32_MAX) == 0)
        ;
#endif

Error:
    return 1;
}
Ejemplo n.º 7
0
int main(int argc, char **argv)
{
    uv_loop_t *loop = uv_default_loop();
    uv_tcp_t listener;
    struct sockaddr_in sockaddr;
    h2o_hostconf_t *hostconf;
    h2o_pathconf_t *pathconf;
    int r;

    if ((r = uv_tcp_init(loop, &listener)) != 0) {
        fprintf(stderr, "uv_tcp_init:%s\n", uv_strerror(r));
        goto Error;
    }
    uv_ip4_addr("127.0.0.1", 7890, &sockaddr);
    if ((r = uv_tcp_bind(&listener, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) != 0) {
        fprintf(stderr, "uv_tcp_bind:%s\n", uv_strerror(r));
        goto Error;
    }
    if ((r = uv_listen((uv_stream_t *)&listener, 128, on_connect)) != 0) {
        fprintf(stderr, "uv_listen:%s\n", uv_strerror(r));
        goto Error;
    }

    h2o_config_init(&config);
    hostconf = h2o_config_register_host(&config, "default");
    pathconf = h2o_config_register_path(hostconf, "/");
    h2o_create_handler(pathconf, sizeof(h2o_handler_t))->on_req = on_req;

    h2o_context_init(&ctx, loop, &config);

    /* disabled by default: uncomment the block below to use HTTPS instead of HTTP */
    /*
    if (setup_ssl("server.crt", "server.key") != 0)
        goto Error;
    */

    return uv_run(loop, UV_RUN_DEFAULT);

Error:
    return 1;
}
Ejemplo n.º 8
0
Archivo: simple.c Proyecto: Gwill/h2o
int main(int argc, char **argv)
{
    signal(SIGPIPE, SIG_IGN);

    h2o_config_init(&config);
    register_handler(&config.default_host, post_test);
    register_handler(&config.default_host, chunked_test);
    register_handler(&config.default_host, reproxy_test);
    h2o_register_file_handler(&config.default_host, "/", "htdocs", "index.html");
    h2o_define_mimetype(&config.default_host.mimemap, "html", "text/html");
    h2o_register_reproxy_filter(&config.default_host);

#if H2O_USE_LIBUV
    uv_loop_t loop;
    uv_loop_init(&loop);
    h2o_context_init(&ctx, &loop, &config);
#else
    h2o_context_init(&ctx, h2o_evloop_create(), &config);
#endif

    //ssl_ctx = h2o_ssl_new_server_context("server.crt", "server.key", h2o_http2_tls_identifiers);
    //h2o_register_access_logger(&ctx, "/dev/stdout");

    if (create_listener() != 0) {
        fprintf(stderr, "failed to listen to 127.0.0.1:7890:%s\n", strerror(errno));
        goto Error;
    }

#if H2O_USE_LIBUV
    uv_run(ctx.loop, UV_RUN_DEFAULT);
#else
    while (h2o_evloop_run(ctx.loop) == 0)
        ;
#endif

Error:
    return 1;
}
Ejemplo n.º 9
0
Archivo: main.c Proyecto: 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;
}
Ejemplo n.º 10
0
Archivo: file.c Proyecto: lhjay1/h2o
void test_lib__file_c()
{
    h2o_globalconf_t globalconf;
    h2o_hostconf_t *hostconf;
    h2o_context_t ctx;

    h2o_config_init(&globalconf);
    hostconf = h2o_config_register_host(&globalconf, "default");
    h2o_file_register(hostconf, "/", "t/00unit/file", NULL, NULL);

    h2o_context_init(&ctx, test_loop, &globalconf);

    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
        conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
        conn->req.path = h2o_buf_init(H2O_STRLIT("/"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 200);
        ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/html"));
        ok(h2o_memis(conn->body->bytes, conn->body->size, H2O_STRLIT("hello html\n")));
        h2o_loopback_destroy(conn);
    }
    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
        conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
        conn->req.path = h2o_buf_init(H2O_STRLIT("/index.html"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 200);
        ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/html"));
        ok(h2o_memis(conn->body->bytes, conn->body->size, H2O_STRLIT("hello html\n")));
        h2o_loopback_destroy(conn);
    }
    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
        conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
        conn->req.path = h2o_buf_init(H2O_STRLIT("/1000.txt"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 200);
        ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain"));
        ok(conn->body->size == 1000);
        ok(strcmp(sha1sum(conn->body->bytes, conn->body->size), "dfd3ae1f5c475555fad62efe42e07309fa45f2ed") == 0);
        h2o_loopback_destroy(conn);
    }
    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
        conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
        conn->req.path = h2o_buf_init(H2O_STRLIT("/1000000.txt"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 200);
        ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain"));
        ok(conn->body->size == 1000000);
        ok(strcmp(sha1sum(conn->body->bytes, conn->body->size), "00c8ab71d0914dce6a1ec2eaa0fda0df7044b2a2") == 0);
        h2o_loopback_destroy(conn);
    }
    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
        conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
        conn->req.path = h2o_buf_init(H2O_STRLIT("/index_txt/"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 200);
        ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain"));
        ok(h2o_memis(conn->body->bytes, conn->body->size, H2O_STRLIT("hello text\n")));
        h2o_loopback_destroy(conn);
    }
    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
        conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
        conn->req.path = h2o_buf_init(H2O_STRLIT("/index_txt"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 301);
        ok(check_header(&conn->req.res, H2O_TOKEN_LOCATION, "http://default/index_txt/"));
        h2o_loopback_destroy(conn);
    }
    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
        conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
        conn->req.path = h2o_buf_init(H2O_STRLIT("/index_txt_as_dir/"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 301);
        ok(check_header(&conn->req.res, H2O_TOKEN_LOCATION, "http://default/index_txt_as_dir/index.txt/"));
        h2o_loopback_destroy(conn);
    }

    h2o_context_dispose(&ctx);
    h2o_config_dispose(&globalconf);
}
Ejemplo n.º 11
0
Archivo: main.c Proyecto: Gwill/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 port_configurator_t port_configurator = {
        { {}, "port", NULL, on_config_port, on_config_port_complete, on_config_port_context_create },
        0
    };
    static struct num_threads_configurator_t num_threads_configurator = {
        { {}, "num-threads", NULL, on_config_num_threads, NULL, NULL },
        1 /* default number of threads is 1 */
    };

    const char *config_file = "h2o.conf";
    int opt_ch;
    yoml_t *config_yoml;
    h2o_global_configuration_t 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();
            exit(0);
            break;
        default:
            assert(0);
            break;
        }
    }
    argc -= optind;
    argv += optind;

    /* configure */
    h2o_config_init(&config);
    h2o_linklist_insert(&config.global_configurators, &port_configurator.super._link);
    h2o_linklist_insert(&config.global_configurators, &num_threads_configurator.super._link);
    if ((config_yoml = load_config(config_file)) == NULL)
        exit(EX_CONFIG);
    if (h2o_config_configure(&config, config_file, config_yoml) != 0)
        exit(EX_CONFIG);
    yoml_free(config_yoml);

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

    return 0;
}