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 void test_normalize_path(void) { h2o_mem_pool_t pool; h2o_mem_init_pool(&pool); size_t q; h2o_iovec_t b = h2o_url_normalize_path(&pool, H2O_STRLIT("/"), &q); ok(b.len == 1); ok(memcmp(b.base, H2O_STRLIT("/")) == 0); ok(q == SIZE_MAX); b = h2o_url_normalize_path(&pool, H2O_STRLIT("/abc"), &q); ok(b.len == 4); ok(memcmp(b.base, H2O_STRLIT("/abc")) == 0); ok(q == SIZE_MAX); b = h2o_url_normalize_path(&pool, H2O_STRLIT("/abc"), &q); ok(b.len == 4); ok(memcmp(b.base, H2O_STRLIT("/abc")) == 0); ok(q == SIZE_MAX); b = h2o_url_normalize_path(&pool, H2O_STRLIT("/abc/../def"), &q); ok(b.len == 4); ok(memcmp(b.base, H2O_STRLIT("/def")) == 0); ok(q == SIZE_MAX); b = h2o_url_normalize_path(&pool, H2O_STRLIT("/abc/../../def"), &q); ok(b.len == 4); ok(memcmp(b.base, H2O_STRLIT("/def")) == 0); ok(q == SIZE_MAX); b = h2o_url_normalize_path(&pool, H2O_STRLIT("/abc/./def"), &q); ok(b.len == 8); ok(memcmp(b.base, H2O_STRLIT("/abc/def")) == 0); ok(q == SIZE_MAX); b = h2o_url_normalize_path(&pool, H2O_STRLIT("/abc/def/.."), &q); ok(b.len == 5); ok(memcmp(b.base, H2O_STRLIT("/abc/")) == 0); ok(q == SIZE_MAX); b = h2o_url_normalize_path(&pool, H2O_STRLIT("/abc/def/."), &q); ok(b.len == 9); ok(memcmp(b.base, H2O_STRLIT("/abc/def/")) == 0); ok(q == SIZE_MAX); b = h2o_url_normalize_path(&pool, H2O_STRLIT("/abc?xx"), &q); ok(b.len == 4); ok(memcmp(b.base, H2O_STRLIT("/abc")) == 0); ok(q == 4); b = h2o_url_normalize_path(&pool, H2O_STRLIT("/abc/../def?xx"), &q); ok(b.len == 4); ok(memcmp(b.base, H2O_STRLIT("/def")) == 0); ok(q == 11); b = h2o_url_normalize_path(&pool, H2O_STRLIT("/a%62c"), &q); ok(b.len == 4); ok(memcmp(b.base, H2O_STRLIT("/abc")) == 0); ok(q == SIZE_MAX); b = h2o_url_normalize_path(&pool, H2O_STRLIT("/a%6"), &q); ok(b.len == 4); ok(memcmp(b.base, H2O_STRLIT("/a%6")) == 0); ok(q == SIZE_MAX); b = h2o_url_normalize_path(&pool, H2O_STRLIT("/a%6?"), &q); ok(b.len == 4); ok(memcmp(b.base, H2O_STRLIT("/a%6")) == 0); ok(q == 4); h2o_mem_clear_pool(&pool); }