From 6d16877e7ba6c64a423a45b7cb99b487624c24b1 Mon Sep 17 00:00:00 2001 From: antao Date: Wed, 27 Feb 2019 14:29:50 +0800 Subject: [PATCH] Add the getParameter() method into the HttpRequest class --- lib/inc/drogon/HttpRequest.h | 3 +++ lib/src/HttpRequestImpl.h | 10 +++++++++- lib/src/MultiPart.cc | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/inc/drogon/HttpRequest.h b/lib/inc/drogon/HttpRequest.h index 2ac3b3c2..50798fae 100755 --- a/lib/inc/drogon/HttpRequest.h +++ b/lib/inc/drogon/HttpRequest.h @@ -85,6 +85,9 @@ class HttpRequest /// Get parameters of the request. virtual const std::unordered_map &getParameters() const = 0; + /// Get a parameter identified by the @param key + virtual const std::string &getParameter(const std::string &key, const std::string &defaultVal = std::string()) const = 0; + /// Return the remote IP address and port virtual const trantor::InetAddress &peerAddr() const = 0; diff --git a/lib/src/HttpRequestImpl.h b/lib/src/HttpRequestImpl.h index f09b28d9..3f4f13ea 100755 --- a/lib/src/HttpRequestImpl.h +++ b/lib/src/HttpRequestImpl.h @@ -157,10 +157,18 @@ class HttpRequestImpl : public HttpRequest _path = path; } - const std::unordered_map &getParameters() const override + virtual const std::unordered_map &getParameters() const override { return _parameters; } + + virtual const std::string &getParameter(const std::string &key, const std::string &defaultVal = std::string()) const override + { + auto iter = _parameters.find(key); + if (iter != _parameters.end()) + return iter->second; + return defaultVal; + } const std::string &path() const override { diff --git a/lib/src/MultiPart.cc b/lib/src/MultiPart.cc index ac58bc60..18a023d5 100755 --- a/lib/src/MultiPart.cc +++ b/lib/src/MultiPart.cc @@ -39,7 +39,7 @@ const std::vector &MultiPartParser::getFiles() const std::map &MultiPartParser::getParameters() const { return _parameters; -}; +} int MultiPartParser::parse(const HttpRequestPtr &req) {