diff --git a/include/frnetlib/URL.h b/include/frnetlib/URL.h index 3ea245c..ebae4a7 100644 --- a/include/frnetlib/URL.h +++ b/include/frnetlib/URL.h @@ -50,6 +50,14 @@ namespace fr return scheme; } + /* + * Set the URL host + */ + inline void set_scheme(Scheme scheme_) + { + scheme = scheme_; + } + /* * Get the URL host */ @@ -58,6 +66,14 @@ namespace fr return host; } + /* + * Set the URL host + */ + inline void set_host(std::string host_) + { + host = std::move(host_); + } + /* * Get the URL port */ @@ -66,6 +82,14 @@ namespace fr return port; } + /*! + * Set the port + */ + inline void set_port(std::string port_) + { + port = std::move(port_); + } + /* * Get the URL path */ @@ -74,6 +98,15 @@ namespace fr return path; } + /*! + * Set the path + */ + inline void set_path(std::string path_) + { + path = std::move(path_); + } + + /* * Get the URL query */ @@ -82,6 +115,14 @@ namespace fr return query; } + /*! + * Set the query + */ + inline void set_query(std::string query_) + { + query = std::move(query_); + } + /* * Get the URL fragment */ @@ -90,6 +131,14 @@ namespace fr return fragment; } + /*! + * Set the fragment + */ + inline void set_fragment(std::string fragment_) + { + fragment = std::move(fragment_); + } + /*! * Returns the combination of other URL elements into a single URI string. * @@ -100,8 +149,7 @@ namespace fr inline std::string get_uri() const { std::string result; - if(!get_path().empty()) - result += get_path(); + result += get_path(); if(!get_query().empty()) result += "?" + get_query(); if(!get_fragment().empty()) @@ -109,6 +157,26 @@ namespace fr return result; } + /*! + * Gets the *whole* URL, including every single element + * + * @return Whole URL + */ + inline std::string get_url() const + { + std::string ret; + if(!get_host().empty()) + { + if(scheme != Scheme::Unknown) + ret.append(scheme_to_string(scheme)).append("://"); + ret.append(get_host()); + if(!get_port().empty()) + ret.append(":").append(port); + } + ret.append(get_uri()); + return ret; + } + /*! * Converts a string to a scheme enum. * diff --git a/tests/NetworkEncodingTest.cpp b/tests/NetworkEncodingTest.cpp index 55addaf..f0f5b88 100644 --- a/tests/NetworkEncodingTest.cpp +++ b/tests/NetworkEncodingTest.cpp @@ -7,10 +7,10 @@ #include #include "frnetlib/NetworkEncoding.h" -constexpr bool is_little_endian() +bool is_little_endian() { unsigned short x=0x0001; - auto p = reinterpret_cast(&x); + auto p = (unsigned char*)(&x); return *p != 0; } diff --git a/tests/URLTest.cpp b/tests/URLTest.cpp index 76cb576..0cf3cc9 100644 --- a/tests/URLTest.cpp +++ b/tests/URLTest.cpp @@ -72,4 +72,20 @@ TEST(URLTest, schema_parse_test) fr::URL url2; url2.parse("127.0.0.1:2020"); ASSERT_TRUE(url == url2); +} + +TEST(URLTest, get_url_test) +{ + ASSERT_EQ(fr::URL("127.0.0.1:2020").get_url(), "127.0.0.1:2020"); + ASSERT_EQ(fr::URL("https://127.0.0.1:2020").get_url(), "https://127.0.0.1:2020"); + ASSERT_EQ(fr::URL("https://127.0.0.1").get_url(), "https://127.0.0.1:443"); + ASSERT_EQ(fr::URL("127.0.0.1/hello.php?x=10").get_url(), "127.0.0.1/hello.php?x=10"); + ASSERT_EQ(fr::URL("/hello.php").get_url(), "/hello.php"); +} + +TEST(URLTest, test_modify_url) +{ + fr::URL url("https://example.com:2020"); + url.set_host("example.co.uk"); + ASSERT_EQ(url.get_url(), "https://example.co.uk:2020"); } \ No newline at end of file