From 562bf7124c011d27995ceb0a6fb04a6dc9d691b5 Mon Sep 17 00:00:00 2001
From: Seva <seva@binarytrails.net>
Date: Thu, 29 Aug 2019 00:51:19 -0400
Subject: [PATCH] http: handle url fragment

---
 include/opendht/http.h |  1 +
 src/http.cpp           | 10 ++++++++--
 tests/httptester.cpp   | 16 ++++++++++++++++
 tests/httptester.h     |  2 ++
 4 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/include/opendht/http.h b/include/opendht/http.h
index 28d1a592..6eab52c8 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 0d68d519..caa7fd95 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 efc759c7..7021cdc8 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 cf77258e..14cab387 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)
      */
-- 
GitLab