diff --git a/mongoose.c b/mongoose.c index dd9f7c0d..120abbf6 100644 --- a/mongoose.c +++ b/mongoose.c @@ -5307,19 +5307,19 @@ static struct url urlparse(const char *url) { struct url u; memset(&u, 0, sizeof(u)); for (i = 0; url[i] != '\0'; i++) { - if (i > 0 && u.host == 0 && url[i - 1] == '/' && url[i] == '/') { + if (url[i] == '/' && i > 0 && u.host == 0 && url[i - 1] == '/') { u.host = i + 1; u.port = 0; } else if (url[i] == ']') { u.port = 0; // IPv6 URLs, like http://[::1]/bar } else if (url[i] == ':' && u.port == 0 && u.uri == 0) { u.port = i + 1; - } else if (url[i] == '@' && u.user == 0 && u.pass == 0) { + } else if (url[i] == '@' && u.user == 0 && u.pass == 0 && u.uri == 0) { u.user = u.host; u.pass = u.port; u.host = i + 1; u.port = 0; - } else if (u.host && u.uri == 0 && url[i] == '/') { + } else if (url[i] == '/' && u.host && u.uri == 0) { u.uri = i; } } diff --git a/src/url.c b/src/url.c index 8925d8a4..04a6eb01 100644 --- a/src/url.c +++ b/src/url.c @@ -15,19 +15,19 @@ static struct url urlparse(const char *url) { struct url u; memset(&u, 0, sizeof(u)); for (i = 0; url[i] != '\0'; i++) { - if (i > 0 && u.host == 0 && url[i - 1] == '/' && url[i] == '/') { + if (url[i] == '/' && i > 0 && u.host == 0 && url[i - 1] == '/') { u.host = i + 1; u.port = 0; } else if (url[i] == ']') { u.port = 0; // IPv6 URLs, like http://[::1]/bar } else if (url[i] == ':' && u.port == 0 && u.uri == 0) { u.port = i + 1; - } else if (url[i] == '@' && u.user == 0 && u.pass == 0) { + } else if (url[i] == '@' && u.user == 0 && u.pass == 0 && u.uri == 0) { u.user = u.host; u.pass = u.port; u.host = i + 1; u.port = 0; - } else if (u.host && u.uri == 0 && url[i] == '/') { + } else if (url[i] == '/' && u.host && u.uri == 0) { u.uri = i; } } diff --git a/test/unit_test.c b/test/unit_test.c index 9b1d213a..b724ca92 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -219,6 +219,8 @@ static void test_url(void) { ASSERT(vcmp(mg_url_user("p://u:p@foo"), "u")); ASSERT(vcmp(mg_url_pass("p://u:p@foo"), "p")); ASSERT(vcmp(mg_url_pass("p://u:p@foo//a@b"), "p")); + ASSERT(vcmp(mg_url_user("p://foo/q?mail=a@b.c"), "")); + ASSERT(vcmp(mg_url_pass("p://foo/q?mail=a@b.c"), "")); // URI ASSERT(strcmp(mg_url_uri("p://foo"), "/") == 0); @@ -228,6 +230,7 @@ static void test_url(void) { ASSERT(strcmp(mg_url_uri("p://foo:12/a/b/c"), "/a/b/c") == 0); ASSERT(strcmp(mg_url_uri("p://[::1]:12/a/b/c"), "/a/b/c") == 0); ASSERT(strcmp(mg_url_uri("p://[ab::1]:12/a/b/c"), "/a/b/c") == 0); + ASSERT(strcmp(mg_url_uri("p://foo/q?mail=a@b.c"), "/q?mail=a@b.c") == 0); } static void test_base64(void) {