Better URL parsing

This commit is contained in:
Fred Nicolson 2019-06-26 13:50:01 +01:00
parent a515cf7c92
commit a4adac8bf7
No known key found for this signature in database
GPG Key ID: 78C1DD87B47797D2
2 changed files with 30 additions and 3 deletions

View File

@ -61,9 +61,12 @@ namespace fr
pos = url.find('/', parse_offset); pos = url.find('/', parse_offset);
pos = (pos != std::string::npos) ? pos : url.find('?', parse_offset); pos = (pos != std::string::npos) ? pos : url.find('?', parse_offset);
pos = (pos != std::string::npos) ? pos : url.find('#', parse_offset); pos = (pos != std::string::npos) ? pos : url.find('#', parse_offset);
pos = (pos != std::string::npos) ? pos : url.size(); if(pos != std::string::npos || parse_offset != 0)
host = url.substr(parse_offset, pos - parse_offset); {
parse_offset = pos + 1; pos = (pos != std::string::npos) ? pos : url.size();
host = url.substr(parse_offset, pos - parse_offset);
parse_offset = pos + 1;
}
//Guess port based on scheme, if it's not explicitly provided. //Guess port based on scheme, if it's not explicitly provided.
switch(scheme) switch(scheme)

View File

@ -89,3 +89,27 @@ TEST(URLTest, test_modify_url)
url.set_host("example.co.uk"); url.set_host("example.co.uk");
ASSERT_EQ(url.get_url(), "https://example.co.uk:2020"); ASSERT_EQ(url.get_url(), "https://example.co.uk:2020");
} }
TEST(URLTest, test_path_only_parse)
{
{
fr::URL url("/bob");
ASSERT_TRUE(url.get_host().empty());
ASSERT_EQ(url.get_path(), "/bob");
}
{
fr::URL url("bob");
ASSERT_TRUE(url.get_host().empty());
ASSERT_EQ(url.get_path(), "/bob");
}
{
fr::URL url("http://bob");
ASSERT_EQ(url.get_host(), "bob");
ASSERT_TRUE(url.get_path().empty());
}
{
fr::URL url("bob.com/trob");
ASSERT_EQ(url.get_host(), "bob.com");
ASSERT_EQ(url.get_path(), "/trob");
}
}