Added setters for fr::URL components + ability to get whole URL

This commit is contained in:
Fred Nicolson 2019-06-25 14:53:34 +01:00
parent 134940138a
commit a515cf7c92
No known key found for this signature in database
GPG Key ID: 78C1DD87B47797D2
3 changed files with 88 additions and 4 deletions

View File

@ -50,6 +50,14 @@ namespace fr
return scheme; return scheme;
} }
/*
* Set the URL host
*/
inline void set_scheme(Scheme scheme_)
{
scheme = scheme_;
}
/* /*
* Get the URL host * Get the URL host
*/ */
@ -58,6 +66,14 @@ namespace fr
return host; return host;
} }
/*
* Set the URL host
*/
inline void set_host(std::string host_)
{
host = std::move(host_);
}
/* /*
* Get the URL port * Get the URL port
*/ */
@ -66,6 +82,14 @@ namespace fr
return port; return port;
} }
/*!
* Set the port
*/
inline void set_port(std::string port_)
{
port = std::move(port_);
}
/* /*
* Get the URL path * Get the URL path
*/ */
@ -74,6 +98,15 @@ namespace fr
return path; return path;
} }
/*!
* Set the path
*/
inline void set_path(std::string path_)
{
path = std::move(path_);
}
/* /*
* Get the URL query * Get the URL query
*/ */
@ -82,6 +115,14 @@ namespace fr
return query; return query;
} }
/*!
* Set the query
*/
inline void set_query(std::string query_)
{
query = std::move(query_);
}
/* /*
* Get the URL fragment * Get the URL fragment
*/ */
@ -90,6 +131,14 @@ namespace fr
return fragment; 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. * Returns the combination of other URL elements into a single URI string.
* *
@ -100,7 +149,6 @@ namespace fr
inline std::string get_uri() const inline std::string get_uri() const
{ {
std::string result; std::string result;
if(!get_path().empty())
result += get_path(); result += get_path();
if(!get_query().empty()) if(!get_query().empty())
result += "?" + get_query(); result += "?" + get_query();
@ -109,6 +157,26 @@ namespace fr
return result; 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. * Converts a string to a scheme enum.
* *

View File

@ -7,10 +7,10 @@
#include <algorithm> #include <algorithm>
#include "frnetlib/NetworkEncoding.h" #include "frnetlib/NetworkEncoding.h"
constexpr bool is_little_endian() bool is_little_endian()
{ {
unsigned short x=0x0001; unsigned short x=0x0001;
auto p = reinterpret_cast<unsigned char*>(&x); auto p = (unsigned char*)(&x);
return *p != 0; return *p != 0;
} }

View File

@ -73,3 +73,19 @@ TEST(URLTest, schema_parse_test)
url2.parse("127.0.0.1:2020"); url2.parse("127.0.0.1:2020");
ASSERT_TRUE(url == url2); 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");
}