diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 02:57:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 02:57:58 +0000 |
commit | be1c7e50e1e8809ea56f2c9d472eccd8ffd73a97 (patch) | |
tree | 9754ff1ca740f6346cf8483ec915d4054bc5da2d /web/server/h2o/libh2o/t/00unit/lib/handler | |
parent | Initial commit. (diff) | |
download | netdata-be1c7e50e1e8809ea56f2c9d472eccd8ffd73a97.tar.xz netdata-be1c7e50e1e8809ea56f2c9d472eccd8ffd73a97.zip |
Adding upstream version 1.44.3.upstream/1.44.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'web/server/h2o/libh2o/t/00unit/lib/handler')
-rw-r--r-- | web/server/h2o/libh2o/t/00unit/lib/handler/compress.c | 125 | ||||
-rw-r--r-- | web/server/h2o/libh2o/t/00unit/lib/handler/fastcgi.c | 183 | ||||
-rw-r--r-- | web/server/h2o/libh2o/t/00unit/lib/handler/file.c | 752 | ||||
-rw-r--r-- | web/server/h2o/libh2o/t/00unit/lib/handler/headers.c | 127 | ||||
-rw-r--r-- | web/server/h2o/libh2o/t/00unit/lib/handler/mimemap.c | 127 | ||||
-rw-r--r-- | web/server/h2o/libh2o/t/00unit/lib/handler/redirect.c | 82 |
6 files changed, 1396 insertions, 0 deletions
diff --git a/web/server/h2o/libh2o/t/00unit/lib/handler/compress.c b/web/server/h2o/libh2o/t/00unit/lib/handler/compress.c new file mode 100644 index 00000000..5b430b4c --- /dev/null +++ b/web/server/h2o/libh2o/t/00unit/lib/handler/compress.c @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2015 DeNA Co., Ltd., Kazuho Oku + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#include <stdlib.h> +#include <zlib.h> +#include "../../test.h" +#define BUF_SIZE 256 +#include "../../../../lib/handler/compress/gzip.c" +#include "../../../../lib/handler/compress.c" + +static void check_result(h2o_iovec_t *vecs, size_t num_vecs, const char *expected, size_t expectedlen) +{ + z_stream zs; + char decbuf[expectedlen + 1]; + + memset(&zs, 0, sizeof(zs)); + zs.zalloc = alloc_cb; + zs.zfree = free_cb; + zs.next_out = (void *)decbuf; + zs.avail_out = (unsigned)sizeof(decbuf); + + inflateInit2(&zs, WINDOW_BITS); + + int inflate_ret = -1; + size_t i; + for (i = 0; i != num_vecs; ++i) { + zs.next_in = (void *)vecs[i].base; + zs.avail_in = (unsigned)vecs[i].len; + inflate_ret = inflate(&zs, Z_NO_FLUSH); + if (zs.avail_out == 0) { + ok(0); + return; + } + if (zs.avail_in != 0) { + ok(0); + return; + } + } + + ok(inflate_ret == Z_STREAM_END); + inflateEnd(&zs); + + ok(zs.avail_out == sizeof(decbuf) - expectedlen); + ok(memcmp(decbuf, expected, expectedlen) == 0); +} + +void test_gzip_simple(void) +{ + h2o_mem_pool_t pool; + h2o_iovec_t inbuf, *outbufs; + size_t outbufcnt; + + h2o_mem_init_pool(&pool); + + h2o_compress_context_t *compressor = h2o_compress_gzip_open(&pool, Z_BEST_SPEED); + inbuf = h2o_iovec_init(H2O_STRLIT("hello world")); + compressor->transform(compressor, &inbuf, 1, 1, &outbufs, &outbufcnt); + + check_result(outbufs, outbufcnt, H2O_STRLIT("hello world")); + + h2o_mem_clear_pool(&pool); +} + +void test_gzip_multi(void) +{ +#define P1 \ + "Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she " \ + "had peeped into the book her sister was reading, but it had no pictures or conversations in it, `and what is the use of a " \ + "book,' thought Alice `without pictures or conversation?'\n\n" +#define P2 \ + "So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), " \ + "whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when " \ + "suddenly a White Rabbit with pink eyes ran close by her.\n\n" +#define P3 \ + "There was nothing so very remarkable in that; nor did Alice think it so very much out of the way to hear the Rabbit say to " \ + "itself, `Oh dear! Oh dear! I shall be late!' (when she thought it over afterwards, it occurred to her that she ought to " \ + "have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually took a watch out of its " \ + "waistcoat-pocket, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she " \ + "had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, " \ + "she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the " \ + "hedge.\n\n" + + h2o_mem_pool_t pool; + h2o_iovec_t inbufs[] = {{H2O_STRLIT(P1)}, {H2O_STRLIT(P2)}, {H2O_STRLIT(P3)}}, *outbufs; + size_t outbufcnt; + + h2o_mem_init_pool(&pool); + + h2o_compress_context_t *compressor = h2o_compress_gzip_open(&pool, Z_BEST_SPEED); + compressor->transform(compressor, inbufs, sizeof(inbufs) / sizeof(inbufs[0]), 1, &outbufs, &outbufcnt); + + assert(outbufcnt > 1); /* we want to test multi-vec output */ + + check_result(outbufs, outbufcnt, H2O_STRLIT(P1 P2 P3)); + + h2o_mem_clear_pool(&pool); + +#undef P1 +#undef P2 +#undef P3 +} + +void test_lib__handler__gzip_c() +{ + subtest("gzip_simple", test_gzip_simple); + subtest("gzip_multi", test_gzip_multi); +} diff --git a/web/server/h2o/libh2o/t/00unit/lib/handler/fastcgi.c b/web/server/h2o/libh2o/t/00unit/lib/handler/fastcgi.c new file mode 100644 index 00000000..f79fbf70 --- /dev/null +++ b/web/server/h2o/libh2o/t/00unit/lib/handler/fastcgi.c @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2015 DeNA Co., Ltd., Kazuho Oku + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#include <stdlib.h> +#include "../../test.h" +#include "../../../../lib/handler/fastcgi.c" + +static h2o_context_t ctx; + +static int check_params(h2o_iovec_t *vecs, size_t *index, uint16_t request_id, const char *expected, size_t expected_len) +{ +#define DECODE_UINT16(p) (((unsigned char *)&p)[0] << 8 | ((unsigned char *)&p)[1]) + + char buf[4096]; + size_t offset = 0; + + while (1) { + if (vecs[*index].len != FCGI_RECORD_HEADER_SIZE) { + fprintf(stderr, "record too short (index: %zu)\n", *index); + return 0; + } + struct st_fcgi_record_header_t *header = (void *)vecs[*index].base; + if (header->version != FCGI_VERSION_1 || header->type != FCGI_PARAMS || header->paddingLength != 0 || + header->reserved != 0) { + fprintf(stderr, "header is corrupt (index: %zu)\n", *index); + return 0; + } + if (DECODE_UINT16(header->requestId) != request_id) { + fprintf(stderr, "unexpected request id (index: %zu)\n", *index); + return 0; + } + ++*index; + if (DECODE_UINT16(header->contentLength) == 0) + break; + if (vecs[*index].len != DECODE_UINT16(header->contentLength)) { + fprintf(stderr, "unexpected body size (index: %zu)\n", *index); + return 0; + } + memcpy(buf + offset, vecs[*index].base, vecs[*index].len); + offset += vecs[*index].len; + ++*index; + } + + if (!h2o_memis(buf, offset, expected, expected_len)) { + fprintf(stderr, "PARAMS content mistach\n"); + return 0; + } + + return 1; + +#undef DECODE_UINT16 +} + +static void test_build_request(void) +{ + h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx, ctx.globalconf->hosts); + h2o_fastcgi_config_vars_t config = {5000, 0}; + iovec_vector_t vecs; + size_t vec_index; + + conn->req.input.method = conn->req.method = h2o_iovec_init(H2O_STRLIT("GET")); + conn->req.input.scheme = conn->req.scheme = &H2O_URL_SCHEME_HTTP; + conn->req.input.authority = conn->req.authority = h2o_iovec_init(H2O_STRLIT("localhost")); + conn->req.input.path = conn->req.path = h2o_iovec_init(H2O_STRLIT("/")); + conn->req.path_normalized = conn->req.path; + conn->req.query_at = SIZE_MAX; + conn->req.version = 0x101; + conn->req.hostconf = *ctx.globalconf->hosts; + conn->req.pathconf = conn->req.hostconf->paths.entries; + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_COOKIE, NULL, H2O_STRLIT("foo=bar")); + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_USER_AGENT, NULL, + H2O_STRLIT("Mozilla/5.0 (X11; Linux) KHTML/4.9.1 (like Gecko) Konqueror/4.9")); + + /* build with max_record_size=65535 */ + build_request(&conn->req, &vecs, 0x1234, 65535, &config); + ok(h2o_memis(vecs.entries[0].base, vecs.entries[0].len, H2O_STRLIT("\x01\x01\x12\x34\x00\x08\x00\x00" + "\x00\x01\0\0\0\0\0\0"))); + vec_index = 1; + ok(check_params(vecs.entries, &vec_index, 0x1234, + H2O_STRLIT("\x0b\x00SCRIPT_NAME" /* */ + "\x09\x01PATH_INFO/" /* */ + "\x0c\x00QUERY_STRING" /* */ + "\x0b\x09REMOTE_ADDR127.0.0.1" /* */ + "\x0b\x05REMOTE_PORT55555" /* */ + "\x0e\x03REQUEST_METHODGET" /* */ + "\x09\x09HTTP_HOSTlocalhost" /* */ + "\x0b\x01REQUEST_URI/" /* */ + "\x0b\x09SERVER_ADDR127.0.0.1" /* */ + "\x0b\x02SERVER_PORT80" /* */ + "\x0b\x07SERVER_NAMEdefault" /* */ + "\x0f\x08SERVER_PROTOCOLHTTP/1.1" /* */ + "\x0f\x10SERVER_SOFTWAREh2o/1.2.1-alpha1" /* */ + "\x0f\x3fHTTP_USER_AGENTMozilla/5.0 (X11; Linux) KHTML/4.9.1 (like Gecko) Konqueror/4.9" /* */ + "\x0b\x07HTTP_COOKIEfoo=bar" /* */ + ))); + ok(h2o_memis(vecs.entries[vec_index].base, vecs.entries[vec_index].len, H2O_STRLIT("\x01\x05\x12\x34\x00\x00\x00\x00"))); + ++vec_index; + ok(vec_index == vecs.size); + + /* build with max_record_size=64, DOCUMENT_ROOT, additional cookie, and content */ + config.document_root = h2o_iovec_init(H2O_STRLIT("/var/www/htdocs")); + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_COOKIE, NULL, H2O_STRLIT("hoge=fuga")); + conn->req.entity = h2o_iovec_init(H2O_STRLIT("The above copyright notice and this permission notice shall be included in all " + "copies or substantial portions of the Software.")); + build_request(&conn->req, &vecs, 0x1234, 64, &config); + ok(h2o_memis(vecs.entries[0].base, vecs.entries[0].len, H2O_STRLIT("\x01\x01\x12\x34\x00\x08\x00\x00" + "\x00\x01\0\0\0\0\0\0"))); + vec_index = 1; + ok(check_params(vecs.entries, &vec_index, 0x1234, + H2O_STRLIT("\x0e\x03" + "CONTENT_LENGTH126" /* */ + "\x0b\x00SCRIPT_NAME" /* */ + "\x09\x01PATH_INFO/" /* */ + "\x0d\x0f" + "DOCUMENT_ROOT/var/www/htdocs" /* */ + "\x0f\x10PATH_TRANSLATED/var/www/htdocs/" /* */ + "\x0c\x00QUERY_STRING" /* */ + "\x0b\x09REMOTE_ADDR127.0.0.1" /* */ + "\x0b\x05REMOTE_PORT55555" /* */ + "\x0e\x03REQUEST_METHODGET" /* */ + "\x09\x09HTTP_HOSTlocalhost" /* */ + "\x0b\x01REQUEST_URI/" /* */ + "\x0b\x09SERVER_ADDR127.0.0.1" /* */ + "\x0b\x02SERVER_PORT80" /* */ + "\x0b\x07SERVER_NAMEdefault" /* */ + "\x0f\x08SERVER_PROTOCOLHTTP/1.1" /* */ + "\x0f\x10SERVER_SOFTWAREh2o/1.2.1-alpha1" /* */ + "\x0f\x3fHTTP_USER_AGENTMozilla/5.0 (X11; Linux) KHTML/4.9.1 (like Gecko) Konqueror/4.9" /* */ + "\x0b\x11HTTP_COOKIEfoo=bar;hoge=fuga" /* */ + ))); + ok(h2o_memis(vecs.entries[vec_index].base, vecs.entries[vec_index].len, H2O_STRLIT("\x01\x05\x12\x34\x00\x40\x00\x00"))); + ++vec_index; + ok(h2o_memis(vecs.entries[vec_index].base, vecs.entries[vec_index].len, + H2O_STRLIT("The above copyright notice and this permission notice shall be i"))); + ++vec_index; + ok(h2o_memis(vecs.entries[vec_index].base, vecs.entries[vec_index].len, H2O_STRLIT("\x01\x05\x12\x34\x00\x3e\x00\x00"))); + ++vec_index; + ok(h2o_memis(vecs.entries[vec_index].base, vecs.entries[vec_index].len, + H2O_STRLIT("ncluded in all copies or substantial portions of the Software."))); + ++vec_index; + ok(h2o_memis(vecs.entries[vec_index].base, vecs.entries[vec_index].len, H2O_STRLIT("\x01\x05\x12\x34\x00\x00\x00\x00"))); + ++vec_index; + ok(vec_index == vecs.size); + + h2o_loopback_destroy(conn); +} + +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, "/", 0); + + h2o_context_init(&ctx, test_loop, &globalconf); + + subtest("build-request", test_build_request); + + h2o_context_dispose(&ctx); + h2o_config_dispose(&globalconf); +} diff --git a/web/server/h2o/libh2o/t/00unit/lib/handler/file.c b/web/server/h2o/libh2o/t/00unit/lib/handler/file.c new file mode 100644 index 00000000..6c0e5948 --- /dev/null +++ b/web/server/h2o/libh2o/t/00unit/lib/handler/file.c @@ -0,0 +1,752 @@ +/* + * Copyright (c) 2014 DeNA Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#include <stdlib.h> +#include "../../test.h" +#include "../../../../lib/handler/file.c" + +static h2o_context_t ctx; + +static int check_header(h2o_res_t *res, const h2o_token_t *header_name, const char *expected) +{ + size_t index = h2o_find_header(&res->headers, header_name, SIZE_MAX); + if (index == SIZE_MAX) + return 0; + return h2o_lcstris(res->headers.entries[index].value.base, res->headers.entries[index].value.len, expected, strlen(expected)); +} + +static int check_multirange_body(char *resbody, const char *boundary, const h2o_iovec_t *expected, size_t partlen) +{ + char *bptr = resbody; + const h2o_iovec_t *eptr = expected; + int not_first_line = 0; + while (partlen--) { + if (not_first_line) { + if (!h2o_memis(bptr, 2, H2O_STRLIT("\r\n"))) + return 0; + bptr += 2; + } else + not_first_line = 1; + if (!h2o_memis(bptr, 2, H2O_STRLIT("--"))) + return 0; + bptr += 2; + if (!h2o_memis(bptr, BOUNDARY_SIZE, boundary, BOUNDARY_SIZE)) + return 0; + bptr += 20; + if (!h2o_memis(bptr, 2, H2O_STRLIT("\r\n"))) + return 0; + bptr += 2; + if (!h2o_memis(bptr, eptr->len, eptr->base, eptr->len)) + return 0; + bptr += eptr->len; + eptr++; + } + if (!h2o_memis(bptr, 4, H2O_STRLIT("\r\n--"))) + return 0; + bptr += 4; + if (!h2o_memis(bptr, BOUNDARY_SIZE, boundary, BOUNDARY_SIZE)) + return 0; + bptr += 20; + if (!h2o_memis(bptr, 4, H2O_STRLIT("--\r\n"))) + return 0; + return 1; +} + +static void test_process_range(void) +{ + h2o_mem_pool_t testpool; + size_t ret, *ranges; + h2o_iovec_t testrange; + h2o_mem_init_pool(&testpool); + + { /* check single range within filesize */ + testrange = h2o_iovec_init(H2O_STRLIT("bytes=, 0-10")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ret == 1); + ok(*ranges++ == 0); + ok(*ranges == 11); + } + + { /* check single range with only start */ + testrange = h2o_iovec_init(H2O_STRLIT("bytes=60-")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ret == 1); + ok(*ranges++ == 60); + ok(*ranges == 40); + } + + { /* check single suffix range */ + testrange = h2o_iovec_init(H2O_STRLIT("bytes=-10")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ret == 1); + ok(*ranges++ == 90); + ok(*ranges == 10); + } + + { /* this and next two check multiple ranges within filesize */ + testrange = h2o_iovec_init(H2O_STRLIT("bytes=0-10, -10")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ret == 2); + ok(*ranges++ == 0); + ok(*ranges++ == 11); + ok(*ranges++ == 90); + ok(*ranges == 10); + } + + { + testrange = h2o_iovec_init(H2O_STRLIT("bytes=0-0, 20-89")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ret == 2); + ok(*ranges++ == 0); + ok(*ranges++ == 1); + ok(*ranges++ == 20); + ok(*ranges == 70); + } + + { + testrange = h2o_iovec_init(H2O_STRLIT("bytes=-10,-20")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ret == 2); + ok(*ranges++ == 90); + ok(*ranges++ == 10); + ok(*ranges++ == 80); + ok(*ranges++ == 20); + } + + { /* check ranges entirely out of filesize */ + testrange = h2o_iovec_init(H2O_STRLIT("bytes=100-102")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ranges == NULL); + } + + { /* check ranges with "negative" length */ + testrange = h2o_iovec_init(H2O_STRLIT("bytes=70-21")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ranges == NULL); + } + + { /* check ranges with one side inside filesize */ + testrange = h2o_iovec_init(H2O_STRLIT("bytes=90-102")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ret == 1); + ok(*ranges++ == 90); + ok(*ranges == 10); + } + + { /* check suffix range larger than filesize */ + testrange = h2o_iovec_init(H2O_STRLIT("bytes=-200")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ret == 1); + ok(*ranges++ == 0); + ok(*ranges == 100); + } + + { /* check multiple ranges with unsatisfiable ranges, but also contain satisfiable ranges */ + testrange = h2o_iovec_init(H2O_STRLIT("bytes=100-102, 90-102, 72-30,-22, 95-")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ret == 3); + ok(*ranges++ == 90); + ok(*ranges++ == 10); + ok(*ranges++ == 78); + ok(*ranges++ == 22); + ok(*ranges++ == 95); + ok(*ranges++ == 5); + } + + { /* this and next 6 check malformed ranges */ + testrange = h2o_iovec_init(H2O_STRLIT("bytes 20-1002")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ranges == NULL); + } + + { + testrange = h2o_iovec_init(H2O_STRLIT("bytes=")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ranges == NULL); + } + + { + testrange = h2o_iovec_init(H2O_STRLIT("bsdfeadsfjwleakjf")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ranges == NULL); + } + + { + testrange = h2o_iovec_init(H2O_STRLIT("bytes=100-102, 90-102, -72-30,-22,95-")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ranges == NULL); + } + + { + testrange = h2o_iovec_init(H2O_STRLIT("bytes=10-12-13, 90-102, -72, -22, 95-")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ranges == NULL); + } + + { + testrange = h2o_iovec_init(H2O_STRLIT("bytes=100-102, 90-102, 70-39, -22$")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ranges == NULL); + } + + { + testrange = h2o_iovec_init(H2O_STRLIT("bytes=-0")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ranges == NULL); + } + + { /* check same ranges with different filesize */ + testrange = h2o_iovec_init(H2O_STRLIT("bytes=20-200")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ret == 1); + ok(*ranges++ == 20); + ok(*ranges == 80); + } + + { + ranges = process_range(&testpool, &testrange, 1000, &ret); + ok(ret == 1); + ok(*ranges++ == 20); + ok(*ranges == 181); + } + + { /* check a range with plenty of WS and COMMA */ + testrange = h2o_iovec_init(H2O_STRLIT("bytes=,\t,1-3 ,, ,5-9,")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ret == 2); + ok(*ranges++ == 1); + ok(*ranges++ == 3); + ok(*ranges++ == 5); + ok(*ranges == 5); + } + + { + testrange = h2o_iovec_init(H2O_STRLIT("bytes= 1-3")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ranges == NULL); + } + + { + testrange = h2o_iovec_init(H2O_STRLIT("bytes=1-3 5-10")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ranges == NULL); + } + + { + testrange = h2o_iovec_init(H2O_STRLIT("bytes=1-\t,5-10")); + ranges = process_range(&testpool, &testrange, 100, &ret); + ok(ret == 2); + ok(*ranges++ == 1); + ok(*ranges++ == 99); + ok(*ranges++ == 5); + ok(*ranges == 6); + } + + h2o_mem_clear_pool(&testpool); +} + +static void test_if_modified_since(void) +{ + char lm_date[H2O_TIMESTR_RFC1123_LEN + 1]; + + { /* obtain last-modified */ + h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx, ctx.globalconf->hosts); + ssize_t lm_index; + 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 == 200); + if ((lm_index = h2o_find_header(&conn->req.res.headers, H2O_TOKEN_LAST_MODIFIED, -1)) == -1) { + ok(0); + return; + } + ok(conn->req.res.headers.entries[lm_index].value.len == H2O_TIMESTR_RFC1123_LEN); + memcpy(lm_date, conn->req.res.headers.entries[lm_index].value.base, H2O_TIMESTR_RFC1123_LEN); + lm_date[H2O_TIMESTR_RFC1123_LEN] = '\0'; + h2o_loopback_destroy(conn); + } + + { /* send if-modified-since using the obtained last-modified */ + 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_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_IF_MODIFIED_SINCE, NULL, lm_date, H2O_TIMESTR_RFC1123_LEN); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 304); + ok(conn->body->size == 0); + ok(h2o_find_header(&conn->req.res.headers, H2O_TOKEN_ETAG, -1) != -1); + h2o_loopback_destroy(conn); + } + + { /* send if-modified-since using an old date */ + 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_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_IF_MODIFIED_SINCE, NULL, + H2O_STRLIT("Sun, 06 Nov 1994 08:49:37 GMT")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 200); + h2o_loopback_destroy(conn); + } + + { /* send if-modified-since using a date in the future */ + 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_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_IF_MODIFIED_SINCE, NULL, + H2O_STRLIT("Wed, 18 May 2033 12:33:20 GMT")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 304); + ok(conn->body->size == 0); + ok(h2o_find_header(&conn->req.res.headers, H2O_TOKEN_ETAG, -1) != -1); + h2o_loopback_destroy(conn); + } +} + +static void test_if_match(void) +{ + h2o_iovec_t etag = {NULL}; + + { /* obtain etag */ + h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx, ctx.globalconf->hosts); + ssize_t etag_index; + 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 == 200); + if ((etag_index = h2o_find_header(&conn->req.res.headers, H2O_TOKEN_ETAG, -1)) == -1) { + ok(0); + return; + } + etag = h2o_strdup(NULL, conn->req.res.headers.entries[etag_index].value.base, + conn->req.res.headers.entries[etag_index].value.len); + h2o_loopback_destroy(conn); + } + + { /* send if-non-match using the obtained etag */ + 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_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_IF_NONE_MATCH, NULL, etag.base, etag.len); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 304); + ok(conn->body->size == 0); + h2o_loopback_destroy(conn); + } + + free(etag.base); +} + +static void test_range_req(void) +{ + { /* check if accept-ranges is "bytes" */ + 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("/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")); + if (check_header(&conn->req.res, H2O_TOKEN_ACCEPT_RANGES, "none")) { + ok(1); + return; + } + ok(check_header(&conn->req.res, H2O_TOKEN_ACCEPT_RANGES, "bytes")); + ok(conn->body->size == 1000); + ok(strcmp(sha1sum(conn->body->bytes, conn->body->size), "dfd3ae1f5c475555fad62efe42e07309fa45f2ed") == 0); + h2o_loopback_destroy(conn); + } + { /* check a normal single range */ + 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("/1000.txt")); + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_RANGE, NULL, H2O_STRLIT("bytes=0-10")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 206); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain")); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_RANGE, "bytes 0-10/1000")); + ok(conn->body->size == 11); + ok(memcmp(conn->body->bytes, "123456789\n1", 11) == 0); + h2o_loopback_destroy(conn); + } + { /* check an over range single range */ + 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("/1000.txt")); + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_RANGE, NULL, H2O_STRLIT("bytes=990-1100")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 206); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain")); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_RANGE, "bytes 990-999/1000")); + ok(conn->body->size == 10); + ok(memcmp(conn->body->bytes, "123456789\n", 10) == 0); + h2o_loopback_destroy(conn); + } + { /* check a single range without end */ + 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("/1000.txt")); + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_RANGE, NULL, H2O_STRLIT("bytes=989-")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 206); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain")); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_RANGE, "bytes 989-999/1000")); + ok(conn->body->size == 11); + ok(memcmp(conn->body->bytes, "\n123456789\n", 11) == 0); + h2o_loopback_destroy(conn); + } + { /* check a single suffix range */ + 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("/1000.txt")); + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_RANGE, NULL, H2O_STRLIT("bytes=-21")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 206); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain")); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_RANGE, "bytes 979-999/1000")); + ok(conn->body->size == 21); + ok(memcmp(conn->body->bytes, "\n123456789\n123456789\n", 21) == 0); + h2o_loopback_destroy(conn); + } + { /* check a single suffix range over filesize */ + 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("/1000.txt")); + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_RANGE, NULL, H2O_STRLIT("bytes=-2100")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 206); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain")); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_RANGE, "bytes 0-999/1000")); + ok(conn->body->size == 1000); + ok(strcmp(sha1sum(conn->body->bytes, conn->body->size), "dfd3ae1f5c475555fad62efe42e07309fa45f2ed") == 0); + h2o_loopback_destroy(conn); + } + { /* malformed range */ + 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("/1000.txt")); + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_RANGE, NULL, H2O_STRLIT("bytes=-0-10, 9-, -10")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 416); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain; charset=utf-8")); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_RANGE, "bytes */1000")); + ok(conn->body->size == strlen("requested range not satisfiable")); + ok(h2o_memis(conn->body->bytes, conn->body->size, H2O_STRLIT("requested range not satisfiable"))); + h2o_loopback_destroy(conn); + } + { /* malformed range */ + 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("/1000.txt")); + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_RANGE, NULL, H2O_STRLIT("bytes=0-10-12, 9-, -10")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 416); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain; charset=utf-8")); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_RANGE, "bytes */1000")); + ok(conn->body->size == strlen("requested range not satisfiable")); + ok(h2o_memis(conn->body->bytes, conn->body->size, H2O_STRLIT("requested range not satisfiable"))); + h2o_loopback_destroy(conn); + } + { /* malformed range */ + 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("/1000.txt")); + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_RANGE, NULL, H2O_STRLIT("bytfasdf")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 416); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain; charset=utf-8")); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_RANGE, "bytes */1000")); + ok(conn->body->size == strlen("requested range not satisfiable")); + ok(h2o_memis(conn->body->bytes, conn->body->size, H2O_STRLIT("requested range not satisfiable"))); + h2o_loopback_destroy(conn); + } + { /* half-malformed range */ + 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("/1000.txt")); + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_RANGE, NULL, H2O_STRLIT("bytes=-0")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 416); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain; charset=utf-8")); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_RANGE, "bytes */1000")); + ok(conn->body->size == strlen("requested range not satisfiable")); + ok(h2o_memis(conn->body->bytes, conn->body->size, H2O_STRLIT("requested range not satisfiable"))); + h2o_loopback_destroy(conn); + } + { /* single range over filesize */ + 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("/1000.txt")); + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_RANGE, NULL, H2O_STRLIT("bytes=1000-1001")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 416); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain; charset=utf-8")); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_RANGE, "bytes */1000")); + ok(conn->body->size == strlen("requested range not satisfiable")); + ok(h2o_memis(conn->body->bytes, conn->body->size, H2O_STRLIT("requested range not satisfiable"))); + h2o_loopback_destroy(conn); + } + { /* single range with "negative" length */ + 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("/1000.txt")); + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_RANGE, NULL, H2O_STRLIT("bytes=900-100")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 416); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain; charset=utf-8")); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_RANGE, "bytes */1000")); + ok(conn->body->size == strlen("requested range not satisfiable")); + ok(h2o_memis(conn->body->bytes, conn->body->size, H2O_STRLIT("requested range not satisfiable"))); + h2o_loopback_destroy(conn); + } + { /* check a half-malformed range with a normal range */ + 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("/1000.txt")); + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_RANGE, NULL, H2O_STRLIT("bytes=-0, 0-0")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 206); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain")); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_RANGE, "bytes 0-0/1000")); + ok(conn->body->size == 1); + ok(memcmp(conn->body->bytes, "1", 1) == 0); + h2o_loopback_destroy(conn); + } + { /* multiple ranges */ + h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx, ctx.globalconf->hosts); + ssize_t content_type_index; + h2o_iovec_t content_type, expected[2] = {{NULL}}; + char boundary[BOUNDARY_SIZE + 1]; + size_t mimebaselen = strlen("multipart/byteranges; boundary="); + conn->req.input.method = h2o_iovec_init(H2O_STRLIT("GET")); + conn->req.input.path = h2o_iovec_init(H2O_STRLIT("/1000.txt")); + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_RANGE, NULL, H2O_STRLIT("bytes=-0, 0-9,-11")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 206); + if ((content_type_index = h2o_find_header(&conn->req.res.headers, H2O_TOKEN_CONTENT_TYPE, -1)) == -1) { + ok(0); + return; + } + content_type = conn->req.res.headers.entries[content_type_index].value; + ok(h2o_memis(content_type.base, mimebaselen, "multipart/byteranges; boundary=", mimebaselen)); + memcpy(boundary, content_type.base + mimebaselen, BOUNDARY_SIZE); + boundary[BOUNDARY_SIZE] = 0; + expected[0].base = h2o_mem_alloc_pool(&conn->req.pool, 256); + expected[0].len = + sprintf(expected[0].base, "Content-Type: %s\r\nContent-Range: bytes 0-9/1000\r\n\r\n%s", "text/plain", "123456789\n"); + expected[1].base = h2o_mem_alloc_pool(&conn->req.pool, 256); + expected[1].len = sprintf(expected[1].base, "Content-Type: %s\r\nContent-Range: bytes 989-999/1000\r\n\r\n%s", "text/plain", + "\n123456789\n"); + ok(h2o_find_header(&conn->req.res.headers, H2O_TOKEN_CONTENT_RANGE, -1) == -1); + ok(conn->body->size == conn->req.res.content_length); + ok(check_multirange_body(conn->body->bytes, boundary, expected, 2)); + h2o_loopback_destroy(conn); + } + { /* multiple ranges with plenty of WS and COMMA */ + h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx, ctx.globalconf->hosts); + ssize_t content_type_index; + h2o_iovec_t content_type, expected[2] = {{NULL}}; + char boundary[BOUNDARY_SIZE + 1]; + size_t mimebaselen = strlen("multipart/byteranges; boundary="); + conn->req.input.method = h2o_iovec_init(H2O_STRLIT("GET")); + conn->req.input.path = h2o_iovec_init(H2O_STRLIT("/1000.txt")); + h2o_add_header(&conn->req.pool, &conn->req.headers, H2O_TOKEN_RANGE, NULL, H2O_STRLIT("bytes=,\t,1-3 ,, ,5-9,")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 206); + if ((content_type_index = h2o_find_header(&conn->req.res.headers, H2O_TOKEN_CONTENT_TYPE, -1)) == -1) { + ok(0); + return; + } + content_type = conn->req.res.headers.entries[content_type_index].value; + ok(h2o_memis(content_type.base, mimebaselen, "multipart/byteranges; boundary=", mimebaselen)); + memcpy(boundary, content_type.base + mimebaselen, BOUNDARY_SIZE); + boundary[BOUNDARY_SIZE] = 0; + expected[0].base = h2o_mem_alloc_pool(&conn->req.pool, 256); + expected[0].len = + sprintf(expected[0].base, "Content-Type: %s\r\nContent-Range: bytes 1-3/1000\r\n\r\n%s", "text/plain", "234"); + expected[1].base = h2o_mem_alloc_pool(&conn->req.pool, 256); + expected[1].len = + sprintf(expected[1].base, "Content-Type: %s\r\nContent-Range: bytes 5-9/1000\r\n\r\n%s", "text/plain", "6789\n"); + ok(h2o_find_header(&conn->req.res.headers, H2O_TOKEN_CONTENT_RANGE, -1) == -1); + ok(conn->body->size == conn->req.res.content_length); + ok(check_multirange_body(conn->body->bytes, boundary, expected, 2)); + h2o_loopback_destroy(conn); + } +} + +void test_lib__handler__file_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_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, 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 == 200); + ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/html")); + 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("/")); + 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, ctx.globalconf->hosts); + conn->req.input.method = h2o_iovec_init(H2O_STRLIT("HEAD")); + conn->req.input.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(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("/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, ctx.globalconf->hosts); + conn->req.input.method = h2o_iovec_init(H2O_STRLIT("HEAD")); + conn->req.input.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 == 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("/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, ctx.globalconf->hosts); + conn->req.input.method = h2o_iovec_init(H2O_STRLIT("HEAD")); + conn->req.input.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 == 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("/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, ctx.globalconf->hosts); + conn->req.input.method = h2o_iovec_init(H2O_STRLIT("HEAD")); + conn->req.input.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(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("/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, ctx.globalconf->hosts); + conn->req.input.method = h2o_iovec_init(H2O_STRLIT("HEAD")); + conn->req.input.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, "/index_txt/")); + 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("/index_txt")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 301); + ok(check_header(&conn->req.res, H2O_TOKEN_LOCATION, "/index_txt/")); + 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("/index_txt_as_dir/")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 301); + ok(check_header(&conn->req.res, H2O_TOKEN_LOCATION, "/index_txt_as_dir/index.txt/")); + 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("/index_txt_as_dir/")); + h2o_loopback_run_loop(conn); + ok(conn->req.res.status == 301); + ok(check_header(&conn->req.res, H2O_TOKEN_LOCATION, "/index_txt_as_dir/index.txt/")); + h2o_loopback_destroy(conn); + } + subtest("if-modified-since", test_if_modified_since); + subtest("if-match", test_if_match); + subtest("process_range()", test_process_range); + subtest("range request", test_range_req); + + h2o_context_dispose(&ctx); + h2o_config_dispose(&globalconf); +} diff --git a/web/server/h2o/libh2o/t/00unit/lib/handler/headers.c b/web/server/h2o/libh2o/t/00unit/lib/handler/headers.c new file mode 100644 index 00000000..9ef8386b --- /dev/null +++ b/web/server/h2o/libh2o/t/00unit/lib/handler/headers.c @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2015 DeNA Co., Ltd., Kazuho Oku + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#include "../../test.h" +#include "../../../../lib/handler/headers.c" + +static int headers_are(h2o_mem_pool_t *pool, h2o_headers_t *headers, const char *s, size_t len) +{ + size_t i; + h2o_iovec_t flattened = {NULL}; + + for (i = 0; i != headers->size; ++i) { + flattened = h2o_concat(pool, flattened, *headers->entries[i].name, h2o_iovec_init(H2O_STRLIT(": ")), + headers->entries[i].value, h2o_iovec_init(H2O_STRLIT("\n"))); + } + + return h2o_memis(flattened.base, flattened.len, s, len); +} + +static void setup_headers(h2o_mem_pool_t *pool, h2o_headers_t *headers) +{ + *headers = (h2o_headers_t){NULL}; + h2o_add_header(pool, headers, H2O_TOKEN_CONTENT_TYPE, NULL, H2O_STRLIT("text/plain")); + h2o_add_header(pool, headers, H2O_TOKEN_CACHE_CONTROL, NULL, H2O_STRLIT("public, max-age=86400")); + h2o_add_header(pool, headers, H2O_TOKEN_SET_COOKIE, NULL, H2O_STRLIT("a=b")); + h2o_add_header_by_str(pool, headers, H2O_STRLIT("x-foo"), 0, NULL, H2O_STRLIT("bar")); +} + +void test_lib__handler__headers_c(void) +{ + h2o_mem_pool_t pool; + h2o_headers_t headers; + h2o_headers_command_t cmd; + h2o_iovec_t header_str; + + h2o_mem_init_pool(&pool); + + /* tests using token headers */ + setup_headers(&pool, &headers); + ok(headers_are(&pool, &headers, + H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: bar\n"))); + cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_ADD, &H2O_TOKEN_SET_COOKIE->buf, {H2O_STRLIT("c=d")}}; + h2o_rewrite_headers(&pool, &headers, &cmd); + ok(headers_are( + &pool, &headers, + H2O_STRLIT( + "content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: bar\nset-cookie: c=d\n"))); + + setup_headers(&pool, &headers); + cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_APPEND, &H2O_TOKEN_CACHE_CONTROL->buf, {H2O_STRLIT("public")}}; + h2o_rewrite_headers(&pool, &headers, &cmd); + ok(headers_are( + &pool, &headers, + H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400, public\nset-cookie: a=b\nx-foo: bar\n"))); + + setup_headers(&pool, &headers); + cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_MERGE, &H2O_TOKEN_CACHE_CONTROL->buf, {H2O_STRLIT("public")}}; + h2o_rewrite_headers(&pool, &headers, &cmd); + ok(headers_are(&pool, &headers, + H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: bar\n"))); + + setup_headers(&pool, &headers); + cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_SET, &H2O_TOKEN_CACHE_CONTROL->buf, {H2O_STRLIT("no-cache")}}; + h2o_rewrite_headers(&pool, &headers, &cmd); + ok(headers_are(&pool, &headers, + H2O_STRLIT("content-type: text/plain\nset-cookie: a=b\nx-foo: bar\ncache-control: no-cache\n"))); + + setup_headers(&pool, &headers); + cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_SETIFEMPTY, &H2O_TOKEN_CACHE_CONTROL->buf, {H2O_STRLIT("no-cache")}}; + h2o_rewrite_headers(&pool, &headers, &cmd); + ok(headers_are(&pool, &headers, + H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: bar\n"))); + + /* tests using non-token headers */ + header_str = h2o_iovec_init(H2O_STRLIT("x-foo")); + setup_headers(&pool, &headers); + cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_ADD, &header_str, {H2O_STRLIT("baz")}}; + h2o_rewrite_headers(&pool, &headers, &cmd); + ok(headers_are( + &pool, &headers, + H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: bar\nx-foo: baz\n"))); + + setup_headers(&pool, &headers); + cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_APPEND, &header_str, {H2O_STRLIT("bar")}}; + h2o_rewrite_headers(&pool, &headers, &cmd); + ok(headers_are( + &pool, &headers, + H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: bar, bar\n"))); + + setup_headers(&pool, &headers); + cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_MERGE, &header_str, {H2O_STRLIT("bar")}}; + h2o_rewrite_headers(&pool, &headers, &cmd); + ok(headers_are(&pool, &headers, + H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: bar\n"))); + + setup_headers(&pool, &headers); + cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_SET, &header_str, {H2O_STRLIT("baz")}}; + h2o_rewrite_headers(&pool, &headers, &cmd); + ok(headers_are(&pool, &headers, + H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: baz\n"))); + + setup_headers(&pool, &headers); + cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_SETIFEMPTY, &header_str, {H2O_STRLIT("baz")}}; + h2o_rewrite_headers(&pool, &headers, &cmd); + ok(headers_are(&pool, &headers, + H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: bar\n"))); + + h2o_mem_clear_pool(&pool); +} diff --git a/web/server/h2o/libh2o/t/00unit/lib/handler/mimemap.c b/web/server/h2o/libh2o/t/00unit/lib/handler/mimemap.c new file mode 100644 index 00000000..51ebdff6 --- /dev/null +++ b/web/server/h2o/libh2o/t/00unit/lib/handler/mimemap.c @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2014 DeNA Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#include "../../test.h" +#include "../../../../lib/handler/mimemap.c" + +static void test_default_attributes(void) +{ + h2o_mime_attributes_t attr; + + h2o_mimemap_get_default_attributes("text/plain", &attr); + ok(attr.is_compressible); + ok(attr.priority == H2O_MIME_ATTRIBUTE_PRIORITY_NORMAL); + + h2o_mimemap_get_default_attributes("text/plain; charset=utf-8", &attr); + ok(attr.is_compressible); + ok(attr.priority == H2O_MIME_ATTRIBUTE_PRIORITY_NORMAL); + + h2o_mimemap_get_default_attributes("application/xhtml+xml", &attr); + ok(attr.is_compressible); + ok(attr.priority == H2O_MIME_ATTRIBUTE_PRIORITY_NORMAL); + + h2o_mimemap_get_default_attributes("application/xhtml+xml; charset=utf-8", &attr); + ok(attr.is_compressible); + ok(attr.priority == H2O_MIME_ATTRIBUTE_PRIORITY_NORMAL); + + h2o_mimemap_get_default_attributes("text/css", &attr); + ok(attr.is_compressible); + ok(attr.priority == H2O_MIME_ATTRIBUTE_PRIORITY_HIGHEST); + + h2o_mimemap_get_default_attributes("text/css; charset=utf-8", &attr); + ok(attr.is_compressible); + ok(attr.priority == H2O_MIME_ATTRIBUTE_PRIORITY_HIGHEST); + + h2o_mimemap_get_default_attributes("application/octet-stream", &attr); + ok(!attr.is_compressible); + ok(attr.priority == H2O_MIME_ATTRIBUTE_PRIORITY_NORMAL); +} + +static int is_mimetype(h2o_mimemap_type_t *type, const char *expected) +{ + return type->type == H2O_MIMEMAP_TYPE_MIMETYPE && type->data.mimetype.len == strlen(expected) && + memcmp(type->data.mimetype.base, expected, type->data.mimetype.len) == 0; +} + +static void test_basic() +{ + h2o_mimemap_t *mimemap = h2o_mimemap_create(), *mimemap2; + + subtest("default-attributes", test_default_attributes); + + /* default and set default */ + ok(is_mimetype(h2o_mimemap_get_default_type(mimemap), "application/octet-stream")); + { + char buf[sizeof("text/plain")]; + strcpy(buf, "text/plain"); + h2o_mimemap_set_default_type(mimemap, buf, NULL); + memset(buf, 0, sizeof(buf)); + } + ok(is_mimetype(h2o_mimemap_get_default_type(mimemap), "text/plain")); + + /* set and overwrite */ + h2o_mimemap_define_mimetype(mimemap, "foo", "example/foo", NULL); + ok(is_mimetype(h2o_mimemap_get_type_by_extension(mimemap, h2o_iovec_init(H2O_STRLIT("foo"))), "example/foo")); + ok(is_mimetype(h2o_mimemap_get_type_by_extension(mimemap, h2o_iovec_init(H2O_STRLIT("FOO"))), "example/foo")); + ok(h2o_mimemap_get_type_by_extension(mimemap, h2o_iovec_init(H2O_STRLIT("foo"))) == + h2o_mimemap_get_type_by_mimetype(mimemap, h2o_iovec_init(H2O_STRLIT("example/foo")), 0)); + h2o_mimemap_define_mimetype(mimemap, "foo", "example/overwritten", NULL); + ok(is_mimetype(h2o_mimemap_get_type_by_extension(mimemap, h2o_iovec_init(H2O_STRLIT("foo"))), "example/overwritten")); + ok(h2o_mimemap_get_type_by_extension(mimemap, h2o_iovec_init(H2O_STRLIT("foo"))) == + h2o_mimemap_get_type_by_mimetype(mimemap, h2o_iovec_init(H2O_STRLIT("example/overwritten")), 0)); + ok(h2o_mimemap_get_type_by_mimetype(mimemap, h2o_iovec_init(H2O_STRLIT("example/foo")), 0) == NULL); + + /* clone and release */ + mimemap2 = h2o_mimemap_clone(mimemap); + ok(is_mimetype(h2o_mimemap_get_default_type(mimemap2), "text/plain")); + ok(is_mimetype(h2o_mimemap_get_type_by_extension(mimemap2, h2o_iovec_init(H2O_STRLIT("foo"))), "example/overwritten")); + ok(h2o_mimemap_get_type_by_extension(mimemap, h2o_iovec_init(H2O_STRLIT("foo"))) == + h2o_mimemap_get_type_by_mimetype(mimemap, h2o_iovec_init(H2O_STRLIT("example/overwritten")), 0)); + h2o_mem_release_shared(mimemap2); + + /* check original */ + ok(is_mimetype(h2o_mimemap_get_default_type(mimemap), "text/plain")); + ok(is_mimetype(h2o_mimemap_get_type_by_extension(mimemap, h2o_iovec_init(H2O_STRLIT("foo"))), "example/overwritten")); + + /* remove */ + h2o_mimemap_remove_type(mimemap, "foo"); + ok(is_mimetype(h2o_mimemap_get_type_by_extension(mimemap, h2o_iovec_init(H2O_STRLIT("foo"))), "text/plain")); + ok(h2o_mimemap_get_type_by_mimetype(mimemap, h2o_iovec_init(H2O_STRLIT("example/overwritten")), 0) == NULL); + h2o_mimemap_remove_type(mimemap, "foo"); + ok(is_mimetype(h2o_mimemap_get_type_by_extension(mimemap, h2o_iovec_init(H2O_STRLIT("foo"))), "text/plain")); + + h2o_mem_release_shared(mimemap); +} + +static void test_dynamic() +{ + h2o_mimemap_t *mimemap = h2o_mimemap_create(); + const char *exts[] = {".php", NULL}; + h2o_globalconf_t global = {NULL}; + h2o_mimemap_define_dynamic(mimemap, exts, &global); + h2o_mem_release_shared(mimemap); +} + +void test_lib__handler__mimemap_c() +{ + subtest("basic", test_basic); + subtest("dynamic", test_dynamic); +} diff --git a/web/server/h2o/libh2o/t/00unit/lib/handler/redirect.c b/web/server/h2o/libh2o/t/00unit/lib/handler/redirect.c new file mode 100644 index 00000000..90cb987d --- /dev/null +++ b/web/server/h2o/libh2o/t/00unit/lib/handler/redirect.c @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2015 DeNA Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#include <stdlib.h> +#include "../../test.h" +#include "../../../../lib/handler/redirect.c" + +static h2o_context_t ctx; + +static int check_header(h2o_res_t *res, const h2o_token_t *header_name, const char *expected) +{ + size_t index = h2o_find_header(&res->headers, header_name, SIZE_MAX); + if (index == SIZE_MAX) + return 0; + return h2o_lcstris(res->headers.entries[index].value.base, res->headers.entries[index].value.len, expected, strlen(expected)); +} + +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); +} |