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); }
static int on_config(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node) { const char *dest; int status = 302; /* default is temporary redirect */ yoml_t *t; switch (node->type) { case YOML_TYPE_SCALAR: dest = node->data.scalar; break; case YOML_TYPE_MAPPING: if ((t = yoml_get(node, "url")) == NULL) { h2o_configurator_errprintf(cmd, node, "mandatory property `url` is missing"); return -1; } if (t->type != YOML_TYPE_SCALAR) { h2o_configurator_errprintf(cmd, t, "property `url` must be a string"); return -1; } dest = t->data.scalar; if ((t = yoml_get(node, "status")) == NULL) { h2o_configurator_errprintf(cmd, node, "mandatory property `status` is missing"); return -1; } if (h2o_configurator_scanf(cmd, t, "%d", &status) != 0) return -1; if (!(300 <= status && status <= 399)) { h2o_configurator_errprintf(cmd, t, "value of property `status` should be within 300 to 399"); return -1; } break; default: h2o_configurator_errprintf(cmd, node, "value must be a string or a mapping"); return -1; } h2o_redirect_register(ctx->pathconf, status, dest); return 0; }