From 1bede7e28b7c50a3136e4fdb9db35af8a1f0f812 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka <valenok@gmail.com> Date: Mon, 19 May 2014 12:56:07 +0100 Subject: [PATCH] Added test for parse_url() --- examples/unit_test.c | 16 ++++++++++++++++ mongoose.c | 7 ++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/examples/unit_test.c b/examples/unit_test.c index 64e350cc6..d0bcdda38 100644 --- a/examples/unit_test.c +++ b/examples/unit_test.c @@ -502,6 +502,21 @@ static const char *test_rewrites(void) { return NULL; } +static const char *test_mg_parse_url(void) { + unsigned short port; + char a[100], b[100]; + ASSERT(parse_url("", a, sizeof(a), b, sizeof(b), &port) == 0); + ASSERT(parse_url("ws://foo ", a, sizeof(a), b, sizeof(b), &port) == 8); + ASSERT(strcmp(a, "ws") == 0 && strcmp(b, "foo") == 0 && port == 80); + ASSERT(parse_url("xx://y:123 ", a, sizeof(a), b, sizeof(b), &port) == 10); + ASSERT(strcmp(a, "xx") == 0 && strcmp(b, "y") == 0 && port == 123); + ASSERT(parse_url(" foo ", a, sizeof(a), b, sizeof(b), &port) == 0); + ASSERT(parse_url(" foo:44 ", a, sizeof(a), b, sizeof(b), &port) == 0); + ASSERT(parse_url("foo:44 ", a, sizeof(a), b, sizeof(b), &port) == 6); + ASSERT(strcmp(a, "") == 0 && strcmp(b, "foo") == 0 && port == 44); + return NULL; +} + static const char *run_all_tests(void) { RUN_TEST(test_should_keep_alive); RUN_TEST(test_match_prefix); @@ -518,6 +533,7 @@ static const char *run_all_tests(void) { RUN_TEST(test_mg_set_option); RUN_TEST(test_server); RUN_TEST(test_rewrites); + RUN_TEST(test_mg_parse_url); return NULL; } diff --git a/mongoose.c b/mongoose.c index ad058a7f4..ac4ce2d68 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3992,11 +3992,12 @@ static int parse_url(const char *url, char *proto, size_t plen, snprintf(fmt3, sizeof(fmt3), "%%%zu[^: ]:%%hu%%n", hlen); if (sscanf(url, fmt1, proto, host, port, &n) == 3 || - sscanf(url, fmt2, proto, host, &n) == 2 || - sscanf(url, fmt3, host, port, &n) == 2) { + sscanf(url, fmt2, proto, host, &n) == 2) { + return n; + } else if (sscanf(url, fmt3, host, port, &n) == 2) { + proto[0] = '\0'; return n; } - return 0; } -- GitLab