diff --git a/server_http.hpp b/server_http.hpp index ad2b65b9..23f11955 100644 --- a/server_http.hpp +++ b/server_http.hpp @@ -1034,6 +1034,61 @@ namespace SimpleWeb { return result; } + std::unordered_multimap parse_post() + { + std::unordered_multimap result; + + auto it = this->header.find("Content-Type"); + if (it == this->header.end()) return result; + + std::string type = it->second; + if(type == "application/x-www-form-urlencoded") + { + std::string cont = content.string(); + static regex::regex pattern("([\\w+%]+)=?([^&]*)"); + int submatches[] = { 1, 2 }; + auto it_begin = regex::sregex_token_iterator(cont.begin(), cont.end(), pattern, submatches); + auto it_end = regex::sregex_token_iterator(); + for (auto it = it_begin; it != it_end; ++it) { + auto submatch1 = it->str(); + auto submatch2 = url_decode((++it)->str()); + result.emplace(submatch1, submatch2); + } + } + else if(type == "application/json") + { + throw std::runtime_error("could not parse json data"); + } + else if(type == "application/xml") + { + throw std::runtime_error("could not parse xml data"); + } + else if(type.find("multipart/form-data")!=std::string::npos) + { + auto st = type.find("boundary="); + auto boundary = type.substr(st+9); + std::vector conts(std::istream_iterator(content), {}); + for(auto it =conts.begin();it < conts.end() ; ++it) + { + while(it->find("name") == std::string::npos) + { + ++it; + if (it == conts.end()) + return result; + } + auto name = it->substr(6,it->length() - 7); + ++it; + result.emplace(name, *it); + } + } + else + { + throw std::runtime_error("unknown type"); + } + + return result; + } + private: Request(const socket_type &socket): content(streambuf) { try {