Allows the parsing of full URLs into easy chunks. Added as a helper class for users of the library dealing with URLs, and to potentially replace some of the HTTP parsing code in the future.
124 lines
2.2 KiB
C++
124 lines
2.2 KiB
C++
//
|
|
// Created by fred.nicolson on 23/05/17.
|
|
//
|
|
|
|
#ifndef FRNETLIB_URLPARSER_H
|
|
#define FRNETLIB_URLPARSER_H
|
|
#include <string>
|
|
#include <unordered_map>
|
|
|
|
//Note, a URL looks like this: scheme:[//host[:port]][/path][?query][#fragment]
|
|
|
|
class URL
|
|
{
|
|
public:
|
|
enum Scheme
|
|
{
|
|
HTTP = 0,
|
|
HTTPS = 1,
|
|
FTP = 2,
|
|
MAILTO = 3,
|
|
IRC = 4,
|
|
SFTP = 5,
|
|
Unknown = 6,
|
|
};
|
|
|
|
/*!
|
|
* Constructors
|
|
*/
|
|
URL() = default;
|
|
URL(const std::string &url);
|
|
|
|
/*!
|
|
* Parses a given URL, extracting its various components
|
|
*
|
|
* @param url The URL to parse
|
|
*/
|
|
void parse(std::string url);
|
|
|
|
/*!
|
|
* Get the URL scheme
|
|
*/
|
|
inline Scheme get_scheme()
|
|
{
|
|
return scheme;
|
|
}
|
|
|
|
/*
|
|
* Get the URL host
|
|
*/
|
|
inline const std::string &get_host()
|
|
{
|
|
return host;
|
|
}
|
|
|
|
/*
|
|
* Get the URL port
|
|
*/
|
|
inline const std::string &get_port()
|
|
{
|
|
return port;
|
|
}
|
|
|
|
/*
|
|
* Get the URL path
|
|
*/
|
|
inline const std::string &get_path()
|
|
{
|
|
return path;
|
|
}
|
|
|
|
/*
|
|
* Get the URL query
|
|
*/
|
|
inline const std::string &get_query()
|
|
{
|
|
return query;
|
|
}
|
|
|
|
/*
|
|
* Get the URL fragment
|
|
*/
|
|
inline const std::string &get_fragment()
|
|
{
|
|
return fragment;
|
|
}
|
|
|
|
/*!
|
|
* Converts a string to a scheme enum.
|
|
*
|
|
* @param scheme The string scheme to convert
|
|
* @return The associated scheme enum value. Scheme::Unknown on failure.
|
|
*/
|
|
static Scheme string_to_scheme(const std::string &scheme);
|
|
|
|
/*!
|
|
* Converts a scheme enum value to a string
|
|
*
|
|
* @param scheme The scheme value to convert
|
|
* @return The string version
|
|
*/
|
|
static const std::string &scheme_to_string(Scheme scheme);
|
|
|
|
private:
|
|
/*!
|
|
* Converts a string to lower case
|
|
*
|
|
* @param str The string to convert
|
|
* @return The converted string
|
|
*/
|
|
static std::string to_lower(const std::string &str);
|
|
|
|
//State
|
|
Scheme scheme;
|
|
std::string host;
|
|
std::string port;
|
|
std::string path;
|
|
std::string query;
|
|
std::string fragment;
|
|
static std::unordered_map<std::string, URL::Scheme> scheme_string_map;
|
|
};
|
|
|
|
|
|
#endif //FRNETLIB_URLPARSER_H
|