static h2o_pathconf_t *register_handler(h2o_hostconf_t *hostconf, const char *path, int (*on_req)(h2o_handler_t *, h2o_req_t *)) { h2o_pathconf_t *pathconf = h2o_config_register_path(hostconf, path); h2o_handler_t *handler = h2o_create_handler(pathconf, sizeof(*handler)); handler->on_req = on_req; return pathconf; }
static int on_config_paths(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node) { size_t i; /* sort by the length of the path (descending) */ for (i = 0; i != node->data.mapping.size; ++i) { yoml_t *key = node->data.mapping.elements[i].key; if (key->type != YOML_TYPE_SCALAR) { h2o_configurator_errprintf(cmd, key, "key (representing the virtual path) must be a string"); return -1; } } qsort(node->data.mapping.elements, node->data.mapping.size, sizeof(node->data.mapping.elements[0]), (void *)sort_from_longer_paths); for (i = 0; i != node->data.mapping.size; ++i) { yoml_t *key = node->data.mapping.elements[i].key; yoml_t *value = node->data.mapping.elements[i].value; ctx->pathconf = h2o_config_register_path(ctx->hostconf, key->data.scalar); if (h2o_configurator_apply_commands(ctx, value, H2O_CONFIGURATOR_FLAG_PATH, NULL) != 0) return -1; ctx->pathconf = NULL; } return 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; }
static void register_authority(h2o_globalconf_t *globalconf, h2o_iovec_t host, uint16_t port) { static h2o_iovec_t x_authority = {H2O_STRLIT("x-authority")}; h2o_hostconf_t *hostconf = h2o_config_register_host(globalconf, host, port); h2o_pathconf_t *pathconf = h2o_config_register_path(hostconf, "/"); h2o_file_register(pathconf, "t/00unit/assets", NULL, NULL, 0); char *authority = h2o_mem_alloc(host.len + sizeof(":65535")); sprintf(authority, "%.*s:%" PRIu16, (int)host.len, host.base, port); h2o_headers_command_t *cmds = h2o_mem_alloc(sizeof(*cmds) * 2); cmds[0] = (h2o_headers_command_t){H2O_HEADERS_CMD_ADD, &x_authority, {authority, strlen(authority)}}; cmds[1] = (h2o_headers_command_t){H2O_HEADERS_CMD_NULL}; h2o_headers_register(pathconf, cmds); }
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); }
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); }
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; }
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; }
void test_lib__file_c() { h2o_globalconf_t globalconf; h2o_hostconf_t *hostconf; h2o_pathconf_t *pathconf; h2o_context_t ctx; h2o_config_init(&globalconf); hostconf = h2o_config_register_host(&globalconf, "default"); pathconf = h2o_config_register_path(hostconf, "/"); h2o_file_register(pathconf, "t/00unit/assets", NULL, NULL, 0); h2o_context_init(&ctx, test_loop, &globalconf); { h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx); conn->req.method = h2o_iovec_init(H2O_STRLIT("GET")); conn->req.path = h2o_iovec_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_iovec_init(H2O_STRLIT("GET")); conn->req.path = h2o_iovec_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_iovec_init(H2O_STRLIT("GET")); conn->req.path = h2o_iovec_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_iovec_init(H2O_STRLIT("GET")); conn->req.path = h2o_iovec_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_iovec_init(H2O_STRLIT("GET")); conn->req.path = h2o_iovec_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_iovec_init(H2O_STRLIT("GET")); conn->req.path = h2o_iovec_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_iovec_init(H2O_STRLIT("GET")); conn->req.path = h2o_iovec_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); }