-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHttpUrl.cpp
168 lines (143 loc) · 3.46 KB
/
CHttpUrl.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "CHttpUrl.h"
#include "CUrlParsingError.h"
#include <algorithm>
#include <regex>
#include <string>
using namespace std;
CHttpUrl::CHttpUrl(string const& url)
{
regex ex("([\\w]*)://([^/ :]+):?([^/ ]*)(/?[^ #?]*)\\x3f?([^ #]*)#?([^ ]*)");
cmatch what;
string protocol, domain, port;
if (regex_match(url.c_str(), what, ex))
{
protocol = string(what[1]);
m_domain = string(what[2]);
m_document = string(what[4]);
port = string(what[3]);
m_protocol = ConvertStringToProtocol(protocol);
m_port = StringToUnsignedShort(port, m_protocol);
}
else
{
throw CUrlParsingError("ERROR: wrong url! URL must consist of protocol://domain:port/documen\n");
}
}
CHttpUrl::CHttpUrl(string const& domain, string const& document, Protocol protocol)
: m_protocol(protocol)
{
if (domain.empty() || document.empty())
{
throw CUrlParsingError("ERROR: wrong url! URL must consist of [domain], [/documen], [protocol]\n");
}
m_document = TransformDocumentString(document);
m_domain = domain;
if (protocol == Protocol::HTTPS)
{
m_port = 443;
}
else
{
m_port = 80;
}
}
CHttpUrl::CHttpUrl(string const& domain, string const& document, Protocol protocol, int port)
: m_protocol(protocol)
{
if (domain.empty() || document.empty())
{
throw CUrlParsingError("ERROR: wrong url! URL must consist of [domain], [/documen], [protocol]\n");
}
m_port = CheckPortRange(port);
m_document = TransformDocumentString(document);
m_domain = domain;
}
string TransformDocumentString(std::string const& document)
{
string::const_iterator it = document.begin();
string transform;
return (*it != '/') ? transform = '/' + document : transform = document;
}
unsigned short StringToUnsignedShort(string& port, Protocol protocol)
{
if (port.empty())
{
return protocol == Protocol::HTTP ? 80 : 443;
}
else
{
try
{
int p = static_cast<int>(strtoul(port.c_str(), NULL, 10));
return CheckPortRange(p);
}
catch (CUrlParsingError error)
{
throw error;
}
}
}
unsigned short CheckPortRange(const int port)
{
if (port < 1 || port > USHRT_MAX)
{
throw CUrlParsingError("ERROR: wrong port! Port must be in the range of 1 to 65535\n");
}
return (unsigned short)port;
}
string CHttpUrl::GetDomain() const
{
return m_domain;
}
string CHttpUrl::GetDocument() const
{
return m_document;
}
Protocol CHttpUrl::GetProtocol() const
{
return m_protocol;
}
unsigned short CHttpUrl::GetPort() const
{
return m_port;
}
void CHttpUrl::AppendProperties(std::ostream& strm) const
{
strm << "\n\tDomain: " << GetDomain()
<< "\n\tDocument: " << GetDocument()
<< "\n\tProtocol: " << ConvertProtocolToString(GetProtocol())
<< "\n\tPort: " << ConvertPortToString(GetPort());
}
string CHttpUrl::GetUrl() const
{
ostringstream strm;
strm << ConvertProtocolToString(m_protocol) + "://" + m_domain + m_document;
AppendProperties(strm);
return strm.str();
}
Protocol ConvertStringToProtocol(string& inpString)
{
transform(inpString.begin(), inpString.end(), inpString.begin(), ::tolower);
Protocol protocol;
if (inpString == "https" || inpString == "https://")
{
protocol = Protocol::HTTPS;
}
else if (inpString == "http" || inpString == "http://")
{
protocol = Protocol::HTTP;
}
else
{
throw CUrlParsingError("ERROR: wrong protocol value! Use http or https protocol\n");
}
return protocol;
}
string ConvertProtocolToString(const Protocol protocol)
{
return protocol == Protocol::HTTP ? "http" : "https";
}
string ConvertPortToString(unsigned short port)
{
return to_string(port);
}