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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
#include "uriParse.h"
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/lexical_cast.hpp>
#include "compileTimeFormatter.h"
namespace AdHoc {
static inline int _is_scheme_char(int c)
{
return (!isalpha(c) && '+' != c && '-' != c && '.' != c) ? 0 : 1;
}
Uri::Uri(const std::string & uri)
{
auto * puri = this;
const char *curstr;
int i;
int userpass_flag;
int bracket_flag;
curstr = uri.c_str();
const char * tmpstr = ::strchr(curstr, ':');
if (!tmpstr) {
throw InvalidUri("Schema marker not found", uri);
}
int len = tmpstr - curstr;
for (i = 0; i < len; i++) {
if (!_is_scheme_char(curstr[i])) {
throw InvalidUri("Invalid format", uri);
}
}
puri->scheme = std::string(curstr, len);
boost::algorithm::to_lower(puri->scheme);
tmpstr++;
curstr = tmpstr;
for (int i = 0; i < 2; i++) {
if ('/' != *curstr) {
throw InvalidUri("Invalid format", uri);
}
curstr++;
}
userpass_flag = 0;
tmpstr = curstr;
while ('\0' != *tmpstr) {
if ('@' == *tmpstr) {
userpass_flag = 1;
break;
} else if ('/' == *tmpstr) {
userpass_flag = 0;
break;
}
tmpstr++;
}
tmpstr = curstr;
if (userpass_flag) {
while ('\0' != *tmpstr && ':' != *tmpstr && '@' != *tmpstr) {
tmpstr++;
}
len = tmpstr - curstr;
puri->username = std::string(curstr, len);
curstr = tmpstr;
if (':' == *curstr) {
curstr++;
tmpstr = curstr;
while ('\0' != *tmpstr && '@' != *tmpstr) {
tmpstr++;
}
len = tmpstr - curstr;
puri->password = std::string(curstr, len);
curstr = tmpstr;
}
if ('@' != *curstr) {
throw InvalidUri("Invalid format", uri);
}
curstr++;
}
if ('[' == *curstr) {
bracket_flag = 1;
} else {
bracket_flag = 0;
}
tmpstr = curstr;
while ('\0' != *tmpstr) {
if (bracket_flag && ']' == *tmpstr) {
tmpstr++;
break;
} else if (!bracket_flag && (':' == *tmpstr || '/' == *tmpstr)) {
break;
}
tmpstr++;
}
if (tmpstr == curstr) {
throw InvalidUri("Host cannot be blank", uri);
}
len = tmpstr - curstr;
puri->host = std::string(curstr, len);
boost::algorithm::to_lower(puri->host);
curstr = tmpstr;
if (':' == *curstr) {
curstr++;
tmpstr = curstr;
while ('\0' != *tmpstr && '/' != *tmpstr) {
tmpstr++;
}
len = tmpstr - curstr;
puri->port = boost::lexical_cast<uint16_t>(std::string(curstr, len));
curstr = tmpstr;
}
if ('\0' == *curstr) {
return;
}
if ('/' != *curstr) {
throw InvalidUri("Invalid format", uri);
}
curstr++;
tmpstr = curstr;
while ('\0' != *tmpstr && '#' != *tmpstr && '?' != *tmpstr) {
tmpstr++;
}
len = tmpstr - curstr;
puri->path = std::string(curstr, len);
curstr = tmpstr;
if ('?' == *curstr) {
curstr++;
tmpstr = curstr;
while ('\0' != *tmpstr && '#' != *tmpstr) {
while ('\0' != *tmpstr && '#' != *tmpstr && '=' != *tmpstr && '&' != *tmpstr) {
tmpstr++;
}
len = tmpstr - curstr;
auto q = puri->query.insert({ std::string(curstr, len), std::string() });
curstr = tmpstr;
if ('=' == *curstr) {
curstr++;
while ('\0' != *tmpstr && '#' != *tmpstr && '&' != *tmpstr) {
tmpstr++;
}
len = tmpstr - curstr;
q->second = std::string(curstr, len);
curstr = tmpstr;
}
if ('&' == *tmpstr) {
tmpstr++;
curstr = tmpstr;
}
else if ('\0' != *tmpstr && '#' != *tmpstr) {
throw InvalidUri("Parse error in query params", uri);
}
}
curstr = tmpstr;
}
if ('#' == *curstr) {
curstr++;
tmpstr = curstr;
while ('\0' != *tmpstr) {
tmpstr++;
}
len = tmpstr - curstr;
puri->fragment = std::string(curstr, len);
curstr = tmpstr;
}
}
InvalidUri::InvalidUri(const std::string & e, const std::string & u) :
Exception<std::invalid_argument>(std::string()),
err(e),
uri(u)
{
}
AdHocFormatter(InvalidUriMsg, "InvalidUri (%?) parsing [%?]");
std::string
InvalidUri::message() const throw()
{
return InvalidUriMsg::get(err, uri);
}
}
|