Skip to content

Commit d556574

Browse files
committed
Fixed a bug in url-parsing where the url-arguments.
Closes 55
1 parent 1e02544 commit d556574

3 files changed

Lines changed: 58 additions & 12 deletions

File tree

include/restc-cpp/Url.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ namespace restc_cpp {
2424
boost::string_ref GetHost() const { return host_; }
2525
boost::string_ref GetPort() const { return port_; }
2626
boost::string_ref GetPath() const { return path_; }
27+
boost::string_ref GetArgs() const { return args_; }
2728
Protocol GetProtocol() const { return protocol_; }
2829

2930
private:
3031
boost::string_ref protocol_name_;
3132
boost::string_ref host_;
3233
boost::string_ref port_;
3334
boost::string_ref path_ = "/";
35+
boost::string_ref args_;
3436
Protocol protocol_ = Protocol::UNKNOWN;
3537
};
3638

src/Url.cpp

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,42 @@ Url& Url::operator = (const char *url) {
3636
throw ParseException("Invalid protocol in url. Must be 'http[s]://'");
3737
}
3838

39-
host_ = boost::string_ref(protocol_name_.end());
40-
const auto port_start = host_.find(':');
41-
if (port_start != host_.npos) {
42-
if (host_.length() <= static_cast<decltype(host_.length())>(port_start + 2)) {
39+
auto remains = boost::string_ref(protocol_name_.end());
40+
const auto args_start = remains.find('?');
41+
if (args_start != remains.npos) {
42+
args_ = {remains.begin() + args_start + 1,
43+
remains.size() - (args_start + 1)};
44+
remains = {remains.begin(), args_start};
45+
}
46+
const auto port_start = remains.find(':');
47+
if (port_start != remains.npos) {
48+
if (remains.length() <= static_cast<decltype(host_.length())>(port_start + 2)) {
4349
throw ParseException("Invalid host (no port after column)");
4450
}
45-
port_ = boost::string_ref(&host_[port_start+1]);
46-
host_ = boost::string_ref(host_.data(), port_start);
51+
//port_ = boost::string_ref(&remains[port_start+1]);
52+
//host_ = boost::string_ref(host_.data(), port_start);
53+
host_ = {remains.begin(), port_start};
54+
remains = {remains.begin() + port_start + 1, remains.size() - (port_start + 1)};
4755

48-
const auto path_start = port_.find('/');
56+
const auto path_start = remains.find('/');
4957
if (path_start != port_.npos) {
50-
path_ = &port_[path_start];
51-
port_ = boost::string_ref(port_.data(), path_start);
58+
path_ = {remains.begin() + path_start, remains.size() - path_start};// &port_[path_start];
59+
port_ = {remains.begin(), path_start};
60+
remains = {};
61+
} else {
62+
port_ = remains;
5263
}
5364
} else {
54-
const auto path_start = host_.find('/');
65+
const auto path_start = remains.find('/');
5566
if (path_start != host_.npos) {
56-
path_ = &host_[path_start];
57-
host_ = boost::string_ref(host_.data(), path_start);
67+
//path_ = &host_[path_start];
68+
//host_ = boost::string_ref(host_.data(), path_start);
69+
70+
host_ = {remains.begin(), path_start};
71+
path_ = {remains.begin() + host_.size(), remains.size() - host_.size()};
72+
remains = {};
73+
} else {
74+
host_ = remains;
5875
}
5976
}
6077

tests/unit/UrlTests.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,34 @@ STARTCASE(HttpsUrlSimple)
188188
CHECK_EQUAL("443"s, url.GetPort());
189189
CHECK_EQUAL_ENUM(Url::Protocol::HTTPS, url.GetProtocol());
190190
CHECK_EQUAL("/"s, url.GetPath());
191+
CHECK_EQUAL(""s, url.GetArgs());
191192
} ENDCASE
193+
194+
195+
STARTCASE(HttpsWithPortAndPathAndArgs)
196+
{
197+
Url url("https://github.com:12345/jgaa?arg=abc:5432");
198+
CHECK_EQUAL("github.com"s, url.GetHost());
199+
CHECK_EQUAL("12345"s, url.GetPort());
200+
CHECK_EQUAL_ENUM(Url::Protocol::HTTPS, url.GetProtocol());
201+
CHECK_EQUAL("/jgaa"s, url.GetPath());
202+
CHECK_EQUAL("arg=abc:5432"s, url.GetArgs());
203+
} ENDCASE
204+
205+
STARTCASE(HttpsWithArgsOnly)
206+
{
207+
Url url("https://github.com?arg=abc:123");
208+
CHECK_EQUAL("github.com"s, url.GetHost());
209+
CHECK_EQUAL("443"s, url.GetPort());
210+
CHECK_EQUAL_ENUM(Url::Protocol::HTTPS, url.GetProtocol());
211+
CHECK_EQUAL("/"s, url.GetPath());
212+
CHECK_EQUAL("arg=abc:123"s, url.GetArgs());
213+
const string args{"arg=abc:123"};
214+
CHECK_EQUAL(args, url.GetArgs());
215+
CHECK_EQUAL(args.size(), url.GetArgs().size());
216+
} ENDCASE
217+
218+
192219
}; // lest
193220

194221

0 commit comments

Comments
 (0)