Browse Source

merge querystring, and include querystring in toString

master
Odilitime 5 years ago
parent
commit
7b6570a717
  1. 15
      src/URL.cpp

15
src/URL.cpp

@ -185,6 +185,7 @@ std::tuple<std::unique_ptr<URL>,enum URIParseError> parseUri(std::string raw) { @@ -185,6 +185,7 @@ std::tuple<std::unique_ptr<URL>,enum URIParseError> parseUri(std::string raw) {
return std::make_tuple(std::move(uri), URI_PARSE_ERROR_PORT);
}
} else if (state == PATH) {
//std::cout << "state == PATH char:" << raw[cursor] << std::endl;
if (raw[cursor] == '?' || raw[cursor] == '#') {
uri->path = raw.substr(last, cursor - last);
last = cursor;
@ -198,12 +199,15 @@ std::tuple<std::unique_ptr<URL>,enum URIParseError> parseUri(std::string raw) { @@ -198,12 +199,15 @@ std::tuple<std::unique_ptr<URL>,enum URIParseError> parseUri(std::string raw) {
break;
}
} else if (state == QUERY) {
//std::cout << "parsing query " << raw[cursor] << " @ " << (cursor + 1) << "/" << raw.length() << std::endl;
if (raw[cursor] == '#') {
uri->query = raw.substr(last + 1, cursor - last - 1);
std::cout << "Parsed query " << uri->query << std::endl;
last = cursor;
state = FRAGMENT;
} else if (cursor + 1 == raw.length()) {
uri->query = raw.substr(last + 1, cursor + 1 - last);
std::cout << "Parsed query " << uri->query << std::endl;
break;
}
} else if (state == FRAGMENT) {
@ -240,7 +244,7 @@ URL::URL(std::string const& url) { @@ -240,7 +244,7 @@ URL::URL(std::string const& url) {
}
std::string URL::toString() const {
//std::cout << "scheme[" << scheme << "] host[" << host << "] path [" << path << "]" << std::endl;
//std::cout << "scheme[" << scheme << "] host[" << host << "] path [" << path << "] query[" << query << "]" << std::endl;
if (isRelative()) {
return path;
}
@ -248,6 +252,9 @@ std::string URL::toString() const { @@ -248,6 +252,9 @@ std::string URL::toString() const {
if (scheme == "file") {
return scheme + "://" + path;
} else {
if (query != "") {
return scheme + "://" + host + path + "?" + query;
}
return scheme + "://" + host + path;
}
}
@ -262,6 +269,8 @@ URL URL::merge(URL const& url) const { @@ -262,6 +269,8 @@ URL URL::merge(URL const& url) const {
}
URL returnURL = copy();
//std::cout << "URL::merge - source " << url.toString() << std::endl;
//std::cout << "URL::merge - copy " << returnURL.toString() << std::endl;
if (url.path[0] == '/' && url.path[1] == '/') {
auto slashPos = url.path.find('/', 2);
@ -273,6 +282,10 @@ URL URL::merge(URL const& url) const { @@ -273,6 +282,10 @@ URL URL::merge(URL const& url) const {
}
} else if (url.path[0] == '/') {
returnURL.path = url.path;
// FIXME: make smarter about merging query strings
if (url.query !="") {
returnURL.query += "&"+url.query;
}
} else {
if (returnURL.path.back() != '/') {
auto finalSlashPos = returnURL.path.find_last_of('/');

Loading…
Cancel
Save