Skip to content

Commit ccaa825

Browse files
parse for http url
1 parent 2d0c87b commit ccaa825

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

example.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ int main()
66
{
77
using namespace lighturl;
88
url hu, fu;
9-
std::string http = "http://localhost:8080/index.html#p3";
9+
std::string http = "http://localhost:8080/index.html?a==b&c==d#p3";
1010
std::string ftp = "ftp://pink:1234567@localhost:2000/lover.mp3";
1111
hu.parse(http);
1212
fu.parse(ftp);
13-
13+
std::cout << hu.query() << " " << hu.params();
1414
std::cout << fu.print() << std::endl;
1515
std::cout << hu.print() << std::endl;
1616
std::string s = hu.print();
@@ -33,5 +33,8 @@ int main()
3333
}
3434

3535
std::cout << u.print() << std::endl;
36-
}
3736

37+
auto mv_u = std::move(u);
38+
std::cout << u.print() << std::endl;
39+
std::cout << mv_u.print() << std::endl;
40+
}

url.hpp

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,14 @@ enum {
105105
url_password,
106106
url_host,
107107
url_port,
108-
url_abs_path
108+
url_abs_path,
109+
url_query,
110+
url_params
109111
};
110112

111113
} // namespace imple
112114

113-
enum { Invalid = -1, Ok };
115+
enum { Invalid = -1, Ok = 1 };
114116

115117
// Best for http...
116118
class url {
@@ -121,6 +123,8 @@ class url {
121123
std::string host_;
122124
std::size_t port_;
123125
std::string abs_path_;
126+
std::string query_;
127+
std::string params_;
124128

125129
int state_;
126130

@@ -133,6 +137,7 @@ class url {
133137
: scheme_(std::move(u.scheme_)), account_(std::move(u.account_)),
134138
password_(std::move(u.password_)), host_(std::move(u.host_)),
135139
port_(std::move(u.port_)), abs_path_(std::move(u.abs_path_)),
140+
query_(std::move(u.query_)), params_(std::move(u.params_)),
136141
state_(u.state_) {
137142
u.port_ = 0;
138143
u.state_ = 0;
@@ -146,6 +151,8 @@ class url {
146151
port_ = u.port_;
147152
u.port_ = 0;
148153
abs_path_ = std::move(u.abs_path_);
154+
query_ = std::move(u.query_);
155+
params_ = std::move(u.params_);
149156
state_ = u.state_;
150157
u.state_ = 0;
151158
return *this;
@@ -178,6 +185,16 @@ class url {
178185
s.append(abs_path_);
179186
}
180187

188+
if (query_.size() > 0) {
189+
s.push_back('?');
190+
s.append(query_);
191+
}
192+
193+
if (params_.size() > 0) {
194+
s.push_back('#');
195+
s.append(params_);
196+
}
197+
181198
return s;
182199
}
183200

@@ -208,6 +225,10 @@ class url {
208225
std::size_t &port_ref() { return port_; }
209226
std::string &abs_path_ref() { return abs_path_; }
210227
std::string abs_path() { return abs_path_; }
228+
std::string &query_ref() { return query_; }
229+
std::string query() { return query_; }
230+
std::string &params_ref() { return params_; }
231+
std::string params() { return params_; }
211232

212233
void reset() {
213234
scheme_.clear();
@@ -216,7 +237,8 @@ class url {
216237
host_.clear();
217238
port_ = 0;
218239
abs_path_.clear();
219-
240+
query_.clear();
241+
params_.clear();
220242
state_ = 0;
221243
}
222244
};
@@ -350,11 +372,41 @@ inline int url::parse(char ch) {
350372
break;
351373

352374
case url_abs_path:
375+
if (ch == '?') {
376+
state_ = url_query;
377+
break;
378+
}
379+
380+
if (ch == '#') {
381+
state_ = url_params;
382+
break;
383+
}
384+
353385
if (!allow_in_path(ch)) {
354386
return Invalid;
355387
}
356388
abs_path_.push_back(ch);
357389
break;
390+
391+
case url_query:
392+
if (ch == '#') {
393+
state_ = url_params;
394+
break;
395+
}
396+
397+
if (!allow_in_path(ch)) {
398+
return Invalid;
399+
}
400+
401+
query_.push_back(ch);
402+
break;
403+
404+
case url_params:
405+
if (!allow_in_path(ch)) {
406+
return Invalid;
407+
}
408+
params_.push_back(ch);
409+
break;
358410
}
359411

360412
return Ok;

0 commit comments

Comments
 (0)