From 9731a51daeeae3b202f29932b16ef4a75bff078b Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Mon, 29 Nov 2021 13:57:33 +0000 Subject: [PATCH] Recognise :PORT as a valid URL --- mongoose.c | 10 +++++++++- src/net.c | 10 +++++++++- test/unit_test.c | 2 ++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/mongoose.c b/mongoose.c index 21e90888..8d701aaa 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2398,6 +2398,13 @@ static bool mg_atonl(struct mg_str str, struct mg_addr *addr) { return true; } +static bool mg_atone(struct mg_str str, struct mg_addr *addr) { + if (str.len > 0) return false; + addr->ip = 0; + addr->is_ip6 = false; + return true; +} + static bool mg_aton4(struct mg_str str, struct mg_addr *addr) { uint8_t data[4] = {0, 0, 0, 0}; size_t i, num_dots = 0; @@ -2474,7 +2481,8 @@ static bool mg_aton6(struct mg_str str, struct mg_addr *addr) { bool mg_aton(struct mg_str str, struct mg_addr *addr) { // LOG(LL_INFO, ("[%.*s]", (int) str.len, str.ptr)); - return mg_atonl(str, addr) || mg_aton4(str, addr) || mg_aton6(str, addr); + return mg_atone(str, addr) || mg_atonl(str, addr) || mg_aton4(str, addr) || + mg_aton6(str, addr); } void mg_mgr_free(struct mg_mgr *mgr) { diff --git a/src/net.c b/src/net.c index 5104b953..304844be 100644 --- a/src/net.c +++ b/src/net.c @@ -49,6 +49,13 @@ static bool mg_atonl(struct mg_str str, struct mg_addr *addr) { return true; } +static bool mg_atone(struct mg_str str, struct mg_addr *addr) { + if (str.len > 0) return false; + addr->ip = 0; + addr->is_ip6 = false; + return true; +} + static bool mg_aton4(struct mg_str str, struct mg_addr *addr) { uint8_t data[4] = {0, 0, 0, 0}; size_t i, num_dots = 0; @@ -125,7 +132,8 @@ static bool mg_aton6(struct mg_str str, struct mg_addr *addr) { bool mg_aton(struct mg_str str, struct mg_addr *addr) { // LOG(LL_INFO, ("[%.*s]", (int) str.len, str.ptr)); - return mg_atonl(str, addr) || mg_aton4(str, addr) || mg_aton6(str, addr); + return mg_atone(str, addr) || mg_atonl(str, addr) || mg_aton4(str, addr) || + mg_aton6(str, addr); } void mg_mgr_free(struct mg_mgr *mgr) { diff --git a/test/unit_test.c b/test/unit_test.c index aaba8426..83add9d0 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -121,6 +121,7 @@ static void test_url(void) { ASSERT(vcmp(mg_url_host("foo"), "foo")); ASSERT(vcmp(mg_url_host("//foo"), "foo")); ASSERT(vcmp(mg_url_host("foo:1234"), "foo")); + ASSERT(vcmp(mg_url_host(":1234"), "")); ASSERT(vcmp(mg_url_host("//foo:1234"), "foo")); ASSERT(vcmp(mg_url_host("p://foo"), "foo")); ASSERT(vcmp(mg_url_host("p://foo/"), "foo")); @@ -141,6 +142,7 @@ static void test_url(void) { // Port ASSERT(mg_url_port("foo:1234") == 1234); + ASSERT(mg_url_port(":1234") == 1234); ASSERT(mg_url_port("x://foo:1234") == 1234); ASSERT(mg_url_port("x://foo:1234/") == 1234); ASSERT(mg_url_port("x://foo:1234/xx") == 1234);