Skip to content
Permalink
Browse files

http: handle url fragment

  • Loading branch information...
binarytrails authored and aberaud committed Aug 29, 2019
1 parent 5b344ca commit 562bf7124c011d27995ceb0a6fb04a6dc9d691b5
Showing with 27 additions and 2 deletions.
  1. +1 −0 include/opendht/http.h
  2. +8 −2 src/http.cpp
  3. +16 −0 tests/httptester.cpp
  4. +2 −0 tests/httptester.h
@@ -68,6 +68,7 @@ class OPENDHT_PUBLIC Url
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);
}
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
@@ -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 comments on commit 562bf71

Please sign in to comment.
You can’t perform that action at this time.