Handle multiple arguments in RequestContext.

This commit is contained in:
Matthieu Gautier 2022-03-22 11:55:59 +01:00
parent 854623618c
commit 98c54b2279
2 changed files with 21 additions and 8 deletions

View File

@ -106,7 +106,7 @@ MHD_Result RequestContext::fill_argument(void *__this, enum MHD_ValueKind kind,
const char *key, const char* value)
{
RequestContext *_this = static_cast<RequestContext*>(__this);
_this->arguments[key] = value == nullptr ? "" : value;
_this->arguments[key].push_back(value == nullptr ? "" : value);
return MHD_YES;
}
@ -121,8 +121,14 @@ void RequestContext::print_debug_info() const {
printf(" - %s : '%s'\n", it->first.c_str(), it->second.c_str());
}
printf("arguments :\n");
for (auto it=arguments.begin(); it!=arguments.end(); it++) {
printf(" - %s : '%s'\n", it->first.c_str(), it->second.c_str());
for (auto& pair:arguments) {
printf(" - %s :", pair.first.c_str());
bool first = true;
for (auto& v: pair.second) {
printf("%s %s", first?"":",", v.c_str());
first = false;
}
printf("\n");
}
printf("Parsed : \n");
printf("full_url: %s\n", full_url.c_str());
@ -176,7 +182,7 @@ ByteRange RequestContext::get_range() const {
template<>
std::string RequestContext::get_argument(const std::string& name) const {
return arguments.at(name);
return arguments.at(name)[0];
}
std::string RequestContext::get_header(const std::string& name) const {
@ -187,8 +193,10 @@ std::string RequestContext::get_query() const {
std::string q;
const char* sep = "";
for ( const auto& a : arguments ) {
q += sep + a.first + '=' + a.second;
sep = "&";
for (const auto& v: a.second) {
q += sep + a.first + '=' + v;
sep = "&";
}
}
return q;
}

View File

@ -25,6 +25,7 @@
#include <string>
#include <sstream>
#include <map>
#include <vector>
#include <stdexcept>
#include "byte_range.h"
@ -69,7 +70,11 @@ class RequestContext {
std::string get_header(const std::string& name) const;
template<typename T=std::string>
T get_argument(const std::string& name) const {
return extractFromString<T>(arguments.at(name));
return extractFromString<T>(get_argument(name));
}
std::vector<std::string> get_arguments(const std::string& name) const {
return arguments.at(name);
}
template<class T>
@ -105,7 +110,7 @@ class RequestContext {
ByteRange byteRange_;
std::map<std::string, std::string> headers;
std::map<std::string, std::string> arguments;
std::map<std::string, std::vector<std::string>> arguments;
private: // functions
static MHD_Result fill_header(void *, enum MHD_ValueKind, const char*, const char*);