diff --git a/include/opendht/http.h b/include/opendht/http.h index 28d1a592882afea4b8c7099a7a3665b69455f619..6eab52c81af21a2fcfefce0f382e72e4509ce05b 100644 --- a/include/opendht/http.h +++ b/include/opendht/http.h @@ -68,6 +68,7 @@ public: std::string service {"80"}; std::string target {"/"}; std::string query; + std::string fragment; }; class OPENDHT_PUBLIC Connection diff --git a/src/http.cpp b/src/http.cpp index 0d68d51916b844b7010a6a44041b8acb8e741641..caa7fd95e393b095a406e3ef06f948212bb77b6e 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -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); } - query = url.substr(query_begin + 1); + 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 diff --git a/tests/httptester.cpp b/tests/httptester.cpp index efc759c75d34b7c344341424cb81c64d6d50d9e7..7021cdc8dfb0cf50861d930713cfa9e55063ad23 100644 --- a/tests/httptester.cpp +++ b/tests/httptester.cpp @@ -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 diff --git a/tests/httptester.h b/tests/httptester.h index cf77258e499000eb5e11c1e3143668a42616d9ae..14cab3872177ce50ff77a889a02dc46476c90870 100644 --- a/tests/httptester.h +++ b/tests/httptester.h @@ -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) */