Skip to content

Commit fb38b48

Browse files
authored
fix(url): correct URL prefix removal (alibaba#1206)
Signed-off-by: huangzhaoxiang.hzx <huangzhaoxiang.hzx@alibaba-inc.com>
1 parent 8cc9b32 commit fb38b48

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

net/http/test/client_function_test.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,47 @@ TEST(url, path_fix) {
712712
EXPECT_EQ(u2.query(), "a=b");
713713
}
714714

715+
TEST(url, no_scheme) {
716+
// Relative paths
717+
URL u0("/");
718+
EXPECT_EQ(u0.path(), "/");
719+
EXPECT_EQ(u0.target(), "/");
720+
721+
URL u1("/echo");
722+
EXPECT_EQ(u1.path(), "/echo");
723+
EXPECT_EQ(u1.target(), "/echo");
724+
EXPECT_EQ(u1.host(), "");
725+
726+
URL u2("/path?query=1");
727+
EXPECT_EQ(u2.path(), "/path");
728+
EXPECT_EQ(u2.target(), "/path?query=1");
729+
EXPECT_EQ(u2.query(), "query=1");
730+
EXPECT_EQ(u2.host(), "");
731+
732+
// Bare hostnames (no scheme)
733+
URL u3("example.com");
734+
EXPECT_EQ(u3.host(), "example.com");
735+
EXPECT_EQ(u3.port(), 80);
736+
EXPECT_EQ(u3.path(), "/");
737+
738+
URL u4("example.com:8080");
739+
EXPECT_EQ(u4.host(), "example.com");
740+
EXPECT_EQ(u4.port(), 8080);
741+
EXPECT_EQ(u4.path(), "/");
742+
743+
URL u5("example.com/path");
744+
EXPECT_EQ(u5.host(), "example.com");
745+
EXPECT_EQ(u5.path(), "/path");
746+
EXPECT_EQ(u5.target(), "/path");
747+
748+
URL u6("example.com:8080/path?q=1");
749+
EXPECT_EQ(u6.host(), "example.com");
750+
EXPECT_EQ(u6.port(), 8080);
751+
EXPECT_EQ(u6.path(), "/path");
752+
EXPECT_EQ(u6.query(), "q=1");
753+
EXPECT_EQ(u6.target(), "/path?q=1");
754+
}
755+
715756
TEST(url, utils) {
716757
estring_view u1 = "http://www.taobao.com", u2 = "https://www.taobao.com";
717758
estring_view u3 = "HTTPS://www.taobao.com/", u4 = "www.taobao.com";

net/http/url.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ bool URL::from_string(std::string_view url_) {
5050
if (protocol.icmp("http") == 0) { }
5151
else if (protocol.icmp("https") == 0) { m_port = 443; m_secure = true; }
5252
else LOG_ERROR_RETURN(EINVAL, false, "invalid protocol: ", protocol);
53+
url.remove_prefix(p + 3);
5354
}
5455

55-
url.remove_prefix(p + 3);
5656
// hashtag should be dropped, since it is pure client-side mark
5757
url = url.substr(0, url.find_first_of('#'));
5858

0 commit comments

Comments
 (0)