-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUrlParser.cpp
More file actions
140 lines (122 loc) · 4.15 KB
/
UrlParser.cpp
File metadata and controls
140 lines (122 loc) · 4.15 KB
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
//
// UrlParser.cpp
//
// Created by Jim Park on 12/18/15.
// Copyright © 2015 Jim Park. All rights reserved.
//
#include "UrlParser.h"
#include <sstream>
namespace urlparser {
using namespace std;
using namespace parse;
using namespace parse::u8;
UrlData::UrlData()
: port(-1) {
}
std::string UrlData::ToJSON() const {
ostringstream o;
o << "{\n";
o << "\t\"protocol\":" << "\"" << protocol << "\",\n";
o << "\t\"host\":" << "\"" << host << "\",\n";
o << "\t\"user\":" << "\"" << user << "\",\n";
o << "\t\"password\":" << "\"" << password << "\",\n";
o << "\t\"port\":" << port << "\n";
o << "\t\"path\":" << "\"" << path << "\",\n";
o << "\t\"queries\":" << "{\n";
for (const auto& i : queries) {
o << "\t\t\"" << i.first << "\": \"" << i.second << "\",\n";
}
o << "\t},\n";
o << "\t\"reference\":" << "\"" << reference << "\",\n";
o << "}" << endl;
return o.str();
}
void UrlData::clear() {
protocol.clear();
host.clear();
user.clear();
password.clear();
port = -1;
path.clear();
queries.clear();
reference.clear();
}
UrlParser::UrlParser() {
static auto p_ = [=](){
auto parseProtocol = Capture(protocol, OoM(w())) + str("://");
auto parseUser = Capture(user, OoM(!(cs({':', '@'})))) +
ZoO(c(':') + Capture(password, OoM(!(c('@'))))) + c('@');
auto parsePort = c(':') + Capture(port, OoM(d()));
auto parseHost = Capture(host, OoM(!(cs({'/', ':', '?', '#'}))));
auto parsePathElement = OoM(!(cs({'/', '#', '?'})));
auto parsePath = c('/') + Capture(path, ZoM((parsePathElement + c('/')) | parsePathElement) + ZoO(c('/')));
auto parseQuery = (OoM(!(c('='))) + c('=') + OoM(!(cs({'&', '#'})))) |
(OoM(!(cs({'=', '&', '#'}))));
auto parseQueryList = c('?') + Capture(queries, parseQuery + ZoM(c('&') + parseQuery));
auto parseRef = c('#') + Capture(reference, OoM(any()));
auto parseUrl = parseProtocol + ZoO(parseUser) + parseHost + ZoO(parsePort) +
ZoO(parsePath) + ZoO(parseQueryList) + ZoO(parseRef) + stop();
return parseUrl;
}();
urlParser = &p_;
}
bool UrlParser::parse(const std::string &url, UrlData &data) {
return parse(url.begin(), url.end(), data);
}
bool UrlParser::parse(it i, it end, UrlData &data) {
if ((*urlParser)(i, end)) {
data.protocol = protocol;
data.host = host;
data.user = user;
data.password = password;
if (port.empty()) {
data.port = -1;
} else {
data.port = atoi(port.c_str());
}
data.path = path;
if (!queries.empty()) {
data.queries = parseQueries(queries);
}
data.reference = reference;
return true;
}
return false;
}
unordered_map<string, string> UrlParser::parseQueries(const std::string& queries) {
unordered_map<string, string> result;
char c;
auto i = queries.begin();
auto end = queries.end();
enum State { Key, Value };
State s = Key;
string key;
string value;
while (i != end) {
c = *i;
switch (c) {
case '&':
result[key] = value;
key.clear();
value.clear();
s = Key;
break;
case '=':
s = Value;
break;
default:
if (s == Key) {
key.push_back(c);
} else {
value.push_back(c);
}
break;
}
++i;
}
if (!key.empty()) {
result[key] = value;
}
return result;
};
}