1 #ifndef _YAHTTP_URL_HPP
2 #define _YAHTTP_URL_HPP 1
8 #ifndef YAHTTP_MAX_URL_LENGTH
9 #define YAHTTP_MAX_URL_LENGTH 2048
18 if (pos >= url.size())
return false;
19 if ( (pos1 = url.find_first_of(
":",pos)) == std::string::npos )
return false;
20 protocol = url.substr(pos, pos1-pos);
24 if (url.compare(pos, 2,
"//") == 0) {
31 bool parseHost(
const std::string& url,
size_t &pos) {
33 if (pos >= url.size())
return true;
34 if ( (pos1 = url.find_first_of(
"/", pos)) == std::string::npos ) {
35 host = url.substr(pos);
39 host = url.substr(pos, pos1-pos);
42 if ( (pos1 =
host.find_first_of(
":")) != std::string::npos ) {
43 std::istringstream tmp(
host.substr(pos1+1));
52 if (pos >= url.size())
return true;
54 if ( (pos1 = url.find_first_of(
"@",pos)) == std::string::npos )
return true;
55 pos2 = url.find_first_of(
":",pos);
57 if (pos2 != std::string::npos) {
58 username = url.substr(pos, pos2 - pos);
59 password = url.substr(pos2+1, pos1 - pos2 - 1);
62 username = url.substr(pos+1, pos1 - pos);
69 bool parsePath(
const std::string& url,
size_t &pos) {
71 if (pos >= url.size())
return true;
72 if (url[pos] !=
'/')
return false;
73 if ( (pos1 = url.find_first_of(
"?", pos)) == std::string::npos ) {
74 path = url.substr(pos);
77 path = url.substr(pos, pos1-pos);
85 if (pos >= url.size())
return true;
86 if (url[pos] ==
'#')
return true;
87 if (url[pos] !=
'?')
return false;
88 if ( (pos1 = url.find_first_of(
"#", pos)) == std::string::npos ) {
100 if (pos >= url.size())
return true;
101 if (url[pos] !=
'#')
return false;
102 anchor = url.substr(pos+1);
113 std::ostringstream oss;
117 if (
host.empty() ==
false) {
128 if (
host.empty() ==
false)
141 if (
anchor.empty() ==
false)
157 URL(
const std::string& url) {
162 parse(std::string(url));
165 bool parse(
const std::string& url) {
171 if (*(url.begin()) !=
'/') {
178 if (
parseHost(url, pos) ==
false)
return false;
180 if (
parsePath(url, pos) ==
false)
return false;
std::string password
Definition: url.hpp:150
static std::string encodeURL(const std::string &component, bool asUrl=true)
Definition: utility.hpp:233
bool parse(const std::string &url)
Definition: url.hpp:165
std::string host
Definition: url.hpp:147
bool pathless
Definition: url.hpp:154
static std::string decodeURL(const std::string &component)
Definition: utility.hpp:204
bool parsePath(const std::string &url, size_t &pos)
Definition: url.hpp:69
bool parseSchema(const std::string &url, size_t &pos)
Definition: url.hpp:16
bool parseAnchor(const std::string &url, size_t &pos)
Definition: url.hpp:99
int port
Definition: url.hpp:148
URL(const char *url)
Definition: url.hpp:161
friend std::ostream & operator<<(std::ostream &os, const URL &url)
Definition: url.hpp:185
std::string parameters
Definition: url.hpp:152
bool parseParameters(const std::string &url, size_t &pos)
Definition: url.hpp:83
bool parseUserPass(const std::string &url, size_t &pos)
Definition: url.hpp:50
std::string protocol
Definition: url.hpp:144
#define YAHTTP_MAX_URL_LENGTH
Definition: url.hpp:9
URL()
Definition: url.hpp:156
std::string anchor
Definition: url.hpp:153
std::string path
Definition: url.hpp:151
bool parseHost(const std::string &url, size_t &pos)
Definition: url.hpp:31
URL(const std::string &url)
Definition: url.hpp:157
std::string username
Definition: url.hpp:149
std::string to_string() const
Definition: url.hpp:111
void initialize()
Definition: url.hpp:106