diff --git a/tests/test.c b/tests/test.c index c6f9dbe..dd5a315 100644 --- a/tests/test.c +++ b/tests/test.c @@ -202,15 +202,21 @@ static int json_to_url(cJSON *json, URL *url) url->fragment.len--; } - char tmp[8]; - if (port.len >= (int) sizeof(tmp)) { - test_printf(" JSON object's port field is longer than expected\n"); - return -1; - } - memcpy(tmp, port.ptr, port.len); - tmp[port.len] = 0; + if (port.len == 0) { + url->no_port = 1; + url->port = 0; + } else { + char tmp[8]; + if (port.len >= (int) sizeof(tmp)) { + test_printf(" JSON object's port field is longer than expected\n"); + return -1; + } + memcpy(tmp, port.ptr, port.len); + tmp[port.len] = 0; - url->port = atoi(tmp); + url->no_port = 0; + url->port = atoi(tmp); + } return 0; } @@ -269,6 +275,20 @@ static int compare_urls(URL parsed, URL expected) ok = false; } + if (parsed.no_port != expected.no_port) { + if (ok) test_printf("\n"); + if (parsed.no_port) { + test_printf(" port mismatch\n"); + test_printf(" parsed (empty)\n"); + test_printf(" expected [%d]\n", expected.port); + } else { + test_printf(" port mismatch\n"); + test_printf(" parsed [%d]\n", parsed.port); + test_printf(" expected (empty)\n"); + } + ok = false; + } + if (parsed.port != expected.port) { if (ok) test_printf("\n"); test_printf(" port mismatch\n"); diff --git a/url.c b/url.c index 73d01f8..31f4887 100644 --- a/url.c +++ b/url.c @@ -580,25 +580,26 @@ int url_parse(char *src, int len, int *pcur, URL *out, int flags) out->no_port = 1; out->port = 0; if (cur < len && src[cur] == ':') { - - // The WHATWG standard forbids port numbers with the - // "file" protocol. - if ((flags & URL_FLAG_RFC3986) == 0) { - if (streqcase(out->scheme, S("file"))) - return -1; - } - cur++; // Consume the ':' - out->no_port = 0; - while (cur < len && is_digit(src[cur])) { + if (cur < len && is_digit(src[cur])) { - int n = src[cur] - '0'; - cur++; + // The WHATWG standard forbids port numbers with the + // "file" protocol. + if ((flags & URL_FLAG_RFC3986) == 0) { + if (streqcase(out->scheme, S("file"))) + return -1; + } - if (out->port > (UINT16_MAX - n) / 10) - return -1; // Overflow - out->port = out->port * 10 + n; + out->no_port = 0; + do { + int n = src[cur] - '0'; + cur++; + + if (out->port > (UINT16_MAX - n) / 10) + return -1; // Overflow + out->port = out->port * 10 + n; + } while (cur < len && is_digit(src[cur])); } }