Moved URL class into fr namespace
It should not have been in the global namespace, as it's a part of the library.
This commit is contained in:
parent
c110fb6c81
commit
431f646bae
@ -8,10 +8,11 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
//Note, a URL looks like this: scheme:[//host[:port]][/path][?query][#fragment]
|
//Note, a URL looks like this: scheme:[//host[:port]][/path][?query][#fragment]
|
||||||
|
namespace fr
|
||||||
class URL
|
|
||||||
{
|
{
|
||||||
public:
|
class URL
|
||||||
|
{
|
||||||
|
public:
|
||||||
enum Scheme
|
enum Scheme
|
||||||
{
|
{
|
||||||
HTTP = 0,
|
HTTP = 0,
|
||||||
@ -27,6 +28,7 @@ public:
|
|||||||
* Constructors
|
* Constructors
|
||||||
*/
|
*/
|
||||||
URL() = default;
|
URL() = default;
|
||||||
|
|
||||||
URL(const std::string &url);
|
URL(const std::string &url);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -100,7 +102,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
static const std::string &scheme_to_string(Scheme scheme);
|
static const std::string &scheme_to_string(Scheme scheme);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/*!
|
/*!
|
||||||
* Converts a string to lower case
|
* Converts a string to lower case
|
||||||
*
|
*
|
||||||
@ -117,7 +119,7 @@ private:
|
|||||||
std::string query;
|
std::string query;
|
||||||
std::string fragment;
|
std::string fragment;
|
||||||
static std::unordered_map<std::string, URL::Scheme> scheme_string_map;
|
static std::unordered_map<std::string, URL::Scheme> scheme_string_map;
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#endif //FRNETLIB_URLPARSER_H
|
#endif //FRNETLIB_URLPARSER_H
|
||||||
|
|||||||
39
src/URL.cpp
39
src/URL.cpp
@ -7,7 +7,10 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "frnetlib/URL.h"
|
#include "frnetlib/URL.h"
|
||||||
|
|
||||||
std::unordered_map<std::string, URL::Scheme> URL::scheme_string_map = {
|
namespace fr
|
||||||
|
{
|
||||||
|
|
||||||
|
std::unordered_map<std::string, URL::Scheme> URL::scheme_string_map = {
|
||||||
{"http", URL::HTTP},
|
{"http", URL::HTTP},
|
||||||
{"https", URL::HTTPS},
|
{"https", URL::HTTPS},
|
||||||
{"sftp", URL::FTP},
|
{"sftp", URL::FTP},
|
||||||
@ -15,16 +18,16 @@ std::unordered_map<std::string, URL::Scheme> URL::scheme_string_map = {
|
|||||||
{"irc", URL::IRC},
|
{"irc", URL::IRC},
|
||||||
{"sftp", URL::SFTP},
|
{"sftp", URL::SFTP},
|
||||||
{"unknown", URL::Unknown}
|
{"unknown", URL::Unknown}
|
||||||
};
|
};
|
||||||
|
|
||||||
URL::URL(const std::string &url)
|
URL::URL(const std::string &url)
|
||||||
{
|
{
|
||||||
parse(url);
|
parse(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void URL::parse(std::string url)
|
void URL::parse(std::string url)
|
||||||
{
|
{
|
||||||
size_t parse_offset = 0;
|
size_t parse_offset = 0;
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
|
|
||||||
@ -98,30 +101,32 @@ void URL::parse(std::string url)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
URL::Scheme URL::string_to_scheme(const std::string &scheme)
|
URL::Scheme URL::string_to_scheme(const std::string &scheme)
|
||||||
{
|
{
|
||||||
auto iter = scheme_string_map.find(to_lower(scheme));
|
auto iter = scheme_string_map.find(to_lower(scheme));
|
||||||
if(iter == scheme_string_map.end())
|
if(iter == scheme_string_map.end())
|
||||||
return URL::Unknown;
|
return URL::Unknown;
|
||||||
return iter->second;
|
return iter->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string URL::to_lower(const std::string &str)
|
std::string URL::to_lower(const std::string &str)
|
||||||
{
|
{
|
||||||
std::string out = str;
|
std::string out = str;
|
||||||
std::transform(out.begin(), out.end(), out.begin(), ::tolower);
|
std::transform(out.begin(), out.end(), out.begin(), ::tolower);
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string &URL::scheme_to_string(URL::Scheme scheme)
|
const std::string &URL::scheme_to_string(URL::Scheme scheme)
|
||||||
{
|
{
|
||||||
auto iter = std::find_if(scheme_string_map.begin(), scheme_string_map.end(), [&](const auto &i){
|
auto iter = std::find_if(scheme_string_map.begin(), scheme_string_map.end(), [&](const auto &i)
|
||||||
|
{
|
||||||
return i.second == scheme;
|
return i.second == scheme;
|
||||||
});
|
});
|
||||||
|
|
||||||
if(iter == scheme_string_map.end())
|
if(iter == scheme_string_map.end())
|
||||||
throw std::logic_error("Unknown URL::Scheme value " + std::to_string(scheme));
|
throw std::logic_error("Unknown URL::Scheme value " + std::to_string(scheme));
|
||||||
return iter->first;
|
return iter->first;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user