Skip to content
This repository was archived by the owner on Jun 12, 2018. It is now read-only.

Commit a4b5e55

Browse files
committed
prase post data
Signed-off-by: manageryzy <manageryzy@gmail.com>
1 parent 937370c commit a4b5e55

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

server_http.hpp

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ namespace SimpleWeb {
858858

859859
void response(int status)
860860
{
861-
if (http_status_codes.find(status == http_status_codes.end()))
861+
if (http_status_codes.find(status) == http_status_codes.end())
862862
throw std::runtime_error("error status code: " + status);
863863
this->status = status;
864864
}
@@ -1034,6 +1034,61 @@ namespace SimpleWeb {
10341034
return result;
10351035
}
10361036

1037+
std::unordered_multimap<std::string, std::string, CaseInsensitiveHash, CaseInsensitiveEqual> parse_post()
1038+
{
1039+
std::unordered_multimap<std::string, std::string, CaseInsensitiveHash, CaseInsensitiveEqual> result;
1040+
1041+
auto it = this->header.find("Content-Type");
1042+
if (it == this->header.end()) return result;
1043+
1044+
std::string type = it->second;
1045+
if(type == "application/x-www-form-urlencoded")
1046+
{
1047+
std::string cont = content.string();
1048+
static regex::regex pattern("([\\w+%]+)=?([^&]*)");
1049+
int submatches[] = { 1, 2 };
1050+
auto it_begin = regex::sregex_token_iterator(cont.begin(), cont.end(), pattern, submatches);
1051+
auto it_end = regex::sregex_token_iterator();
1052+
for (auto it = it_begin; it != it_end; ++it) {
1053+
auto submatch1 = it->str();
1054+
auto submatch2 = url_decode((++it)->str());
1055+
result.emplace(submatch1, submatch2);
1056+
}
1057+
}
1058+
else if(type == "application/json")
1059+
{
1060+
throw std::runtime_error("could not parse json data");
1061+
}
1062+
else if(type == "application/xml")
1063+
{
1064+
throw std::runtime_error("could not parse xml data");
1065+
}
1066+
else if(type.find("multipart/form-data")!=std::string::npos)
1067+
{
1068+
auto st = type.find("boundary=");
1069+
auto boundary = type.substr(st+9);
1070+
std::vector<std::string> conts(std::istream_iterator<std::string>(content), {});
1071+
for(auto it =conts.begin();it < conts.end() ; ++it)
1072+
{
1073+
while(it->find("name") == std::string::npos)
1074+
{
1075+
++it;
1076+
if (it == conts.end())
1077+
return result;
1078+
}
1079+
auto name = it->substr(6,it->length() - 7);
1080+
++it;
1081+
result.emplace(name, *it);
1082+
}
1083+
}
1084+
else
1085+
{
1086+
throw std::runtime_error("unknown type");
1087+
}
1088+
1089+
return result;
1090+
}
1091+
10371092
private:
10381093
Request(const socket_type &socket): content(streambuf) {
10391094
try {

0 commit comments

Comments
 (0)