Skip to content
Snippets Groups Projects
Commit 562bf712 authored by Seva's avatar Seva Committed by Adrien Béraud
Browse files

http: handle url fragment

parent 5b344cad
Branches
No related tags found
No related merge requests found
......@@ -68,6 +68,7 @@ public:
std::string service {"80"};
std::string target {"/"};
std::string query;
std::string fragment;
};
class OPENDHT_PUBLIC Connection
......
......@@ -52,7 +52,7 @@ Url::Url(const std::string& url): url(url)
host = host_service.first;
if (!host_service.second.empty())
service = host_service.second;
// target, query
// target, query fragment
size_t query_begin = url.find("?");
auto addr_end = addr_begin + addr_size;
if (addr_end < url.size()){
......@@ -61,7 +61,13 @@ Url::Url(const std::string& url): url(url)
else
target = url.substr(addr_end, query_begin - addr_end);
}
size_t fragment_begin = url.find("#");
if (fragment_begin == std::string::npos)
query = url.substr(query_begin + 1);
else{
query = url.substr(query_begin + 1, fragment_begin - query_begin - 1);
fragment = url.substr(fragment_begin);
}
}
// connection
......
......@@ -96,6 +96,22 @@ HttpTester::test_parse_url_query() {
CPPUNIT_ASSERT(parsed.query == "key=1");
}
void
HttpTester::test_parse_url_fragment() {
// Arrange
std::string url = "http://google.com/?key=1#some-important-id";
// Act
dht::http::Url parsed (url);
// Assert
CPPUNIT_ASSERT(parsed.url == url);
CPPUNIT_ASSERT(parsed.protocol == "http");
CPPUNIT_ASSERT(parsed.host == "google.com");
CPPUNIT_ASSERT(parsed.service == "80");
CPPUNIT_ASSERT(parsed.target == "/");
CPPUNIT_ASSERT(parsed.query == "key=1");
CPPUNIT_ASSERT(parsed.fragment == "#some-important-id");
}
void
HttpTester::test_parse_url_ipv4() {
// Arrange
......
......@@ -34,6 +34,7 @@ class HttpTester : public CppUnit::TestFixture {
CPPUNIT_TEST(test_parse_url_no_prefix_no_target);
CPPUNIT_TEST(test_parse_url_target);
CPPUNIT_TEST(test_parse_url_query);
CPPUNIT_TEST(test_parse_url_fragment);
CPPUNIT_TEST(test_parse_url_ipv4);
CPPUNIT_TEST(test_parse_url_no_prefix_no_target_ipv4);
CPPUNIT_TEST(test_parse_url_target_ipv4);
......@@ -59,6 +60,7 @@ class HttpTester : public CppUnit::TestFixture {
void test_parse_url_no_prefix_no_target();
void test_parse_url_target();
void test_parse_url_query();
void test_parse_url_fragment();
/**
* Test parse urls (ipv4)
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment