From 31a118b91767e8951b14366d24a436f4cbffb7b1 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 28 Dec 2010 14:43:57 +0100 Subject: Parse query strings. --- http.cpp | 22 ++++++++++++++++++++-- http.h | 5 ++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/http.cpp b/http.cpp index 46f8e40..d7b1dc9 100644 --- a/http.cpp +++ b/http.cpp @@ -7,7 +7,7 @@ #include HTTPRequest::HTTPRequest(std::istream& is) { - std::string firstline; + std::string firstline, query_str; std::getline(is, firstline); std::vector splitvec; @@ -15,8 +15,26 @@ HTTPRequest::HTTPRequest(std::istream& is) { type = splitvec[0]; path = splitvec[1]; - url_decode(path); httpver = splitvec[2]; + + if(path.find("?") != std::string::npos) { + boost::algorithm::split(splitvec, path, boost::is_any_of("?")); + path = splitvec[0]; + query_str = splitvec[1]; + boost::algorithm::split(splitvec, query_str, boost::is_any_of("&")); + for(std::vector::iterator it = splitvec.begin(); it != splitvec.end(); it++) { + std::vector sv; + boost::algorithm::split(sv, *it, boost::is_any_of("=")); + std::string key = sv[0]; + std::string value = sv[1]; + url_decode(key); + url_decode(value); + query[key] = value; + std::cout << boost::format("%s: %s") % key % value << std::endl; + } + } + url_decode(path); + std::cout << boost::format("%s %s %s\n") % type % path % httpver; while(is.good()) { diff --git a/http.h b/http.h index ef6e43c..29add9a 100644 --- a/http.h +++ b/http.h @@ -7,11 +7,14 @@ #include #include -typedef std::map HTTPHeaders; +typedef std::map stringmap; +typedef stringmap HTTPHeaders; +typedef stringmap HTTPQuery; class HTTPRequest { public: std::string type, path, httpver; + HTTPQuery query; HTTPHeaders headers; HTTPRequest(std::istream& is); static void url_decode(std::string& str); -- cgit v1.2.3