forked from jspotter/evhttpclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.h
110 lines (97 loc) · 2.04 KB
/
url.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef URL_H_
#define URL_H_
#include <string>
#include <algorithm>
#include <cctype>
#include <functional>
#include <sstream>
using namespace std;
typedef struct Url_
{
Url_()
{
}
Url_(const string& url_s)
{
parse(url_s);
}
string protocol()
{
return protocol_;
}
string host()
{
return host_;
}
int port()
{
return port_;
}
string path()
{
return path_;
}
string query()
{
return query_;
}
void parse(const string& url_s)
{
const string prot_end("://");
string::const_iterator prot_i = search(url_s.begin(), url_s.end(),
prot_end.begin(), prot_end.end());
protocol_.reserve(distance(url_s.begin(), prot_i));
transform(url_s.begin(), prot_i,
back_inserter(protocol_),
ptr_fun<int,int>(tolower)); // protocol is icase
if( prot_i == url_s.end() )
return;
advance(prot_i, prot_end.length());
string::const_iterator port_i = find(prot_i, url_s.end(), ':');
string::const_iterator path_i = min(find(prot_i, url_s.end(), '/'), find(prot_i, url_s.end(), '?'));
string::const_iterator next_i = (port_i == url_s.end() ? path_i : port_i);
if(port_i != url_s.end())
{
++port_i;
std::stringstream portstream;
while(port_i != path_i)
{
portstream << *port_i;
++port_i;
}
portstream >> port_;
}
else
{
port_ = 80;
}
host_.reserve(distance(prot_i, next_i));
transform(prot_i, next_i,
back_inserter(host_),
ptr_fun<int,int>(tolower)); // host is icase
string::const_iterator query_i = find(path_i, url_s.end(), '?');
path_.assign(path_i, query_i);
if( query_i != url_s.end() )
++query_i;
query_.assign(query_i, url_s.end());
if(path_.size() == 0)
{
path_ = "/";
}
}
string toString()
{
stringstream ss;
ss
<< "protocol: " << protocol_ << endl
<< "host: " << host_ << endl
<< "port: " << port_ << endl
<< "path: " << path_ << endl
<< "query: " << query_ << endl;
return ss.str();
}
private:
string protocol_, host_, path_, query_;
short port_;
} Url;
#endif /* URL_H_ */