From 98c54b22797a362d031f56e16c1f3713fef1f98c Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 22 Mar 2022 11:55:59 +0100 Subject: [PATCH] Handle multiple arguments in RequestContext. --- src/server/request_context.cpp | 20 ++++++++++++++------ src/server/request_context.h | 9 +++++++-- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/server/request_context.cpp b/src/server/request_context.cpp index 6b435e5ad..c479a6201 100644 --- a/src/server/request_context.cpp +++ b/src/server/request_context.cpp @@ -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(__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; } diff --git a/src/server/request_context.h b/src/server/request_context.h index 2c4c8902e..6172bcdf0 100644 --- a/src/server/request_context.h +++ b/src/server/request_context.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "byte_range.h" @@ -69,7 +70,11 @@ class RequestContext { std::string get_header(const std::string& name) const; template T get_argument(const std::string& name) const { - return extractFromString(arguments.at(name)); + return extractFromString(get_argument(name)); + } + + std::vector get_arguments(const std::string& name) const { + return arguments.at(name); } template @@ -105,7 +110,7 @@ class RequestContext { ByteRange byteRange_; std::map headers; - std::map arguments; + std::map> arguments; private: // functions static MHD_Result fill_header(void *, enum MHD_ValueKind, const char*, const char*);