From a4dd2e64448de2a5468a5ed4ebc2102ee064cc58 Mon Sep 17 00:00:00 2001 From: ProTrack Date: Sun, 5 Feb 2017 16:01:00 +0200 Subject: [PATCH] Added query string parsing and member to request --- server_http.hpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/server_http.hpp b/server_http.hpp index 4045708..1013bb2 100644 --- a/server_http.hpp +++ b/server_http.hpp @@ -107,6 +107,7 @@ namespace SimpleWeb { Content content; std::unordered_multimap header; + std::unordered_multimap query_string; REGEX_NS::smatch path_match; @@ -320,6 +321,24 @@ namespace SimpleWeb { request->method=line.substr(0, method_end); request->path=line.substr(method_end+1, path_end-method_end-1); + //search and populte query_string + size_t qs_start; + if ((qs_start = request->path.find('?')) != std::string::npos) + { + string qs = request->path.substr(qs_start, request->path.size() - qs_start - 1); + REGEX_NS::regex pattern("([\\w+%]+)=?([^&]*)"); + int submatches[] = { 1, 2 }; + auto qs_begin = REGEX_NS::sregex_token_iterator(qs.begin(), qs.end(), pattern, submatches); + auto qs_end = REGEX_NS::sregex_token_iterator(); + + for (auto i = qs_begin; i != qs_end; i++) + { + string key = i->str(); + string value = (++i)->str(); + request->query_string.emplace(std::make_pair(key, value)); + } + } + size_t protocol_end; if((protocol_end=line.find('/', path_end+1))!=std::string::npos) { if(line.compare(path_end+1, protocol_end-path_end-1, "HTTP")!=0)