Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse an URI elegantly in Casablanca

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?

like image 403
x29a Avatar asked Nov 19 '15 13:11

x29a


1 Answers

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());
like image 141
x29a Avatar answered Oct 09 '22 18:10

x29a