How would one parse the URI of a request (server side) elegantly in C++?
URI is defined as in the Casablanca Documentation as:
protocol : // server [: port] / path ? query # fragment
Lets say, i want the path (with all elements) as a list and the queries as a list of key/value pairs so
http://server/foo/bar?val1=1&val2=yo
would result in
std::list<string> path;
path.push_back("foo");
path.push_back("bar");
std::list<std::pair<string, string>> query;
query.push_back(std::make_pair("val1", "1"));
query.push_back(std::make_pair("val2", "yo"));
I want to avoid doing the parsing myself like proposed elsewhere, mostly for portability and performance reasons.
cpp-netlib does not seem to be part of boost yet and i'd prefer not to introduce a new library.
Casablanca has an URI object, but i cant find any parsing or iterators.
Is there a way to solve this using only boost, c++, casablanca/cpprestsdk?
It seems that there are static helper functions in the URI class, e.g. uri::split_query and uri::split_path that perform exactly as requested.
I found references to them after looking at this gist which uses
auto http_get_vars = uri::split_query(request.request_uri().query());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With