static int on_req(h2o_handler_t *_self, h2o_req_t *req) { struct rp_handler_t *self = (void *)_self; h2o_req_overrides_t *overrides = h2o_mem_alloc_pool(&req->pool, sizeof(*overrides)); const h2o_url_scheme_t *scheme; h2o_iovec_t *authority; /* setup overrides */ *overrides = (h2o_req_overrides_t){}; if (self->sockpool != NULL) { overrides->socketpool = self->sockpool; } else if (self->config.preserve_host) { overrides->hostport.host = self->upstream.host; overrides->hostport.port = h2o_url_get_port(&self->upstream); } overrides->location_rewrite.match = &self->upstream; overrides->location_rewrite.path_prefix = req->pathconf->path; overrides->client_ctx = h2o_context_get_handler_context(req->conn->ctx, &self->super); /* determine the scheme and authority */ if (self->config.preserve_host) { scheme = req->scheme; authority = &req->authority; } else { scheme = self->upstream.scheme; authority = &self->upstream.authority; } /* request reprocess */ h2o_reprocess_request(req, req->method, scheme, *authority, h2o_build_destination(req, self->upstream.path.base, self->upstream.path.len), overrides, 0); return 0; }
void test_build_destination_escaping(void) { h2o_req_t req; h2o_iovec_t dest; int escape = 0; int i, j; struct { char *pathconf; char *dest; char *input; char *output; } tests[] = { {"/abc", "/def", "/abc/xyz?query&m=n/o", "/def/xyz?query&m=n/o"}, {"/abc", "/def", "/%61bc/xyz?query&m=n/o", "/def/xyz?query&m=n/o"}, {"/abc", "/def", "/%61%62c/xyz?query&m=n/o", "/def/xyz?query&m=n/o"}, {"/abc", "/def", "/%61%62%63/xyz?query&m=n/o", "/def/xyz?query&m=n/o"}, {"/abc", "/def", "/./%61%62%63/xyz?query&m=n/o", "/def/xyz?query&m=n/o"}, {"/abc", "/def", "/../%61%62%63/xyz?query&m=n/o", "/def/xyz?query&m=n/o"}, {"/abc", "/def", "/././%61%62%63/xyz?query&m=n/o", "/def/xyz?query&m=n/o"}, {"/abc", "/def", "/./.././%61%62%63/xyz?query&m=n/o", "/def/xyz?query&m=n/o"}, {"/abc", "/def", "/./../blah/../%61%62%63/xyz?query&m=n/o", "/def/xyz?query&m=n/o"}, {"/abc", "/def", "/./../blah/.././%61%62c/xyz?query&m=n/o", "/def/xyz?query&m=n/o"}, {"/abc", "/def", "/./../blah/.././../../%61b%63/xyz?query&m=n/o", "/def/xyz?query&m=n/o"}, {"/abc", "/def", "/abc/xyz/?query&m=n/o", "/def/xyz/?query&m=n/o"}, {"/abc", "/def", "/abc/xyz/.?query&m=n/o", "/def/xyz/.?query&m=n/o"}, {"/abc", "/def", "/abc/xyz/./?query&m=n/o", "/def/xyz/./?query&m=n/o"}, {"/abc", "/def", "/abc/xyz/..?query&m=n/o", "/def/xyz/..?query&m=n/o"}, {"/abc", "/def", "/abc/xyz/../?query&m=n/o", "/def/xyz/../?query&m=n/o"}, {"/abc", "/def", "/abc/xyz/../a?query&m=n/o", "/def/xyz/../a?query&m=n/o"}, {"/abc", "/def", "/abc/%yz/?query&m=n/o", "/def/%yz/?query&m=n/o"}, {"/abc", "/def", "/abc/%78yz/?query&m=n/o", "/def/%78yz/?query&m=n/o"}, {"/", "/", "/xyz/../mno", "/xyz/../mno"}, {"/", "/", "/xyz/../mno/..", "/xyz/../mno/.."}, {"/", "/def", "/xyz/../mno", "/def/xyz/../mno"}, {"/", "/def/", "/xyz/../mno", "/def/xyz/../mno"}, {"/", "/def", "/xyz/../", "/def/xyz/../"}, {"/", "/def/", "/xyz/..", "/def/xyz/.."}, }; h2o_init_request(&req, NULL, NULL); /* 'j' runs the test with a missing leading '/' in the input path */ for (j = 0; j <= 1; j++) { for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { h2o_pathconf_t conf = {NULL, {tests[i].pathconf, strlen(tests[i].pathconf)}}; req.pathconf = &conf; req.path = req.input.path = h2o_iovec_init(tests[i].input + j, strlen(tests[i].input) - j); req.norm_indexes = NULL; req.path_normalized = h2o_url_normalize_path(&req.pool, req.path.base, req.path.len, &req.query_at, &req.norm_indexes); dest = h2o_build_destination(&req, tests[i].dest, strlen(tests[i].dest), escape); note("%s: %d, %sskipping the leading '/'", tests[i].input, i, !j ? "not " : ""); ok(dest.len == strlen(tests[i].output)); ok(h2o_memis(dest.base, dest.len, tests[i].output, strlen(tests[i].output))); } } h2o_mem_clear_pool(&req.pool); }
static int on_req(h2o_handler_t *_self, h2o_req_t *req) { h2o_redirect_handler_t *self = (void *)_self; h2o_iovec_t dest = h2o_build_destination(req, self->prefix.base, self->prefix.len, 1); /* redirect */ if (self->internal) { redirect_internally(self, req, dest); } else { h2o_send_redirect(req, self->status, "Redirected", dest.base, dest.len); } return 0; }
void test_build_destination(void) { h2o_pathconf_t conf_not_slashed = {NULL, {H2O_STRLIT("/abc")}}, conf_slashed = {NULL, {H2O_STRLIT("/abc/")}}; h2o_req_t req; h2o_iovec_t dest; int escape; for (escape = 0; escape <= 1; escape++) { h2o_init_request(&req, NULL, NULL); note("escaping: %s", escape ? "on" : "off"); req.path_normalized = h2o_iovec_init(H2O_STRLIT("/abc/xyz")); req.query_at = req.path_normalized.len; req.input.path = req.path = h2o_concat(&req.pool, req.path_normalized, h2o_iovec_init(H2O_STRLIT("?q"))); /* basic pattern */ req.pathconf = &conf_not_slashed; dest = h2o_build_destination(&req, H2O_STRLIT("/def"), escape); ok(h2o_memis(dest.base, dest.len, H2O_STRLIT("/def/xyz?q"))); dest = h2o_build_destination(&req, H2O_STRLIT("/def/"), escape); ok(h2o_memis(dest.base, dest.len, H2O_STRLIT("/def/xyz?q"))); req.pathconf = &conf_slashed; dest = h2o_build_destination(&req, H2O_STRLIT("/def"), escape); ok(h2o_memis(dest.base, dest.len, H2O_STRLIT("/def/xyz?q"))); dest = h2o_build_destination(&req, H2O_STRLIT("/def/"), escape); ok(h2o_memis(dest.base, dest.len, H2O_STRLIT("/def/xyz?q"))); /* test wo. query */ if (escape) { req.pathconf = &conf_not_slashed; req.query_at = SIZE_MAX; dest = h2o_build_destination(&req, H2O_STRLIT("/def"), escape); ok(h2o_memis(dest.base, dest.len, H2O_STRLIT("/def/xyz"))); } /* no trailing */ req.path_normalized = h2o_iovec_init(H2O_STRLIT("/abc")); req.query_at = req.path_normalized.len; req.input.path = req.path = h2o_concat(&req.pool, req.path_normalized, h2o_iovec_init(H2O_STRLIT("?q"))); req.pathconf = &conf_not_slashed; dest = h2o_build_destination(&req, H2O_STRLIT("/def"), escape); ok(h2o_memis(dest.base, dest.len, H2O_STRLIT("/def?q"))); dest = h2o_build_destination(&req, H2O_STRLIT("/def/"), escape); ok(h2o_memis(dest.base, dest.len, H2O_STRLIT("/def/?q"))); } h2o_mem_clear_pool(&req.pool); }
static int on_req(h2o_handler_t *_self, h2o_req_t *req) { struct rp_handler_t *self = (void *)_self; h2o_req_overrides_t *overrides = h2o_mem_alloc_pool(&req->pool, *overrides, 1); struct rp_handler_context_t *handler_ctx = h2o_context_get_handler_context(req->conn->ctx, &self->super); /* setup overrides */ *overrides = (h2o_req_overrides_t){NULL}; overrides->connpool = &handler_ctx->connpool; overrides->location_rewrite.path_prefix = req->pathconf->path; overrides->use_proxy_protocol = self->config.use_proxy_protocol; overrides->client_ctx = handler_ctx->client_ctx; overrides->headers_cmds = self->config.headers_cmds; overrides->proxy_preserve_host = self->config.preserve_host; /* request reprocess (note: path may become an empty string, to which one of the target URL within the socketpool will be * right-padded when lib/core/proxy connects to upstream; see #1563) */ h2o_iovec_t path = h2o_build_destination(req, NULL, 0, 0); h2o_reprocess_request(req, req->method, req->scheme, req->authority, path, overrides, 0); return 0; }