From c3346a481538f01748e5a60b158ac18aac6197ac Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 5 Dec 2017 23:19:39 -0500 Subject: [PATCH] Changed to use std::multimap for params --- httplib.h | 45 +++++++++++++++++++++++++++------------------ test/test.cc | 26 +++++++++++++------------- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/httplib.h b/httplib.h index ab22db1..4f1f225 100644 --- a/httplib.h +++ b/httplib.h @@ -69,9 +69,8 @@ namespace httplib enum class HttpVersion { v1_0 = 0, v1_1 }; -typedef std::map Map; -typedef std::multimap MultiMap; -typedef std::smatch Match; +typedef std::multimap MultiMap; +typedef std::smatch Match; typedef std::function Progress; struct MultipartFile { @@ -87,7 +86,7 @@ struct Request { std::string path; MultiMap headers; std::string body; - Map params; + MultiMap params; MultipartFiles files; Match matches; Progress progress; @@ -97,6 +96,7 @@ struct Request { void set_header(const char* key, const char* val); bool has_param(const char* key) const; + std::string get_param_value(const char* key) const; bool has_file(const char* key) const; MultipartFile get_file_value(const char* key) const; @@ -189,7 +189,7 @@ public: std::shared_ptr get(const char* path, Progress callback = [](int64_t,int64_t){}); std::shared_ptr head(const char* path); std::shared_ptr post(const char* path, const std::string& body, const char* content_type); - std::shared_ptr post(const char* path, const Map& params); + std::shared_ptr post(const char* path, const MultiMap& params); bool send(const Request& req, Response& res); @@ -540,19 +540,19 @@ inline const char* status_message(int status) } } -inline const char* get_header_value(const MultiMap& map, const char* key, const char* def) +inline const char* get_header_value(const MultiMap& headers, const char* key, const char* def) { - auto it = map.find(key); - if (it != map.end()) { + auto it = headers.find(key); + if (it != headers.end()) { return it->second.c_str(); } return def; } -inline int get_header_value_int(const MultiMap& map, const char* key, int def) +inline int get_header_value_int(const MultiMap& headers, const char* key, int def) { - auto it = map.find(key); - if (it != map.end()) { + auto it = headers.find(key); + if (it != headers.end()) { return std::stoi(it->second); } return def; @@ -576,7 +576,7 @@ inline bool read_headers(Stream& strm, MultiMap& headers) if (std::regex_match(buf, m, re)) { auto key = std::string(m[1]); auto val = std::string(m[2]); - headers.insert(std::make_pair(key, val)); + headers.emplace(key, val); } } @@ -856,7 +856,7 @@ inline void write_request(Stream& strm, const Request& req, const char* ver) } } -inline void parse_query_text(const std::string& s, Map& params) +inline void parse_query_text(const std::string& s, MultiMap& params) { split(&s[0], &s[s.size()], '&', [&](const char* b, const char* e) { std::string key; @@ -868,7 +868,7 @@ inline void parse_query_text(const std::string& s, Map& params) val.assign(b, e); } }); - params[key] = detail::decode_url(val); + params.emplace(key, detail::decode_url(val)); }); } @@ -959,7 +959,7 @@ inline bool parse_multipart_formdata( return false; } - files.insert(std::make_pair(name, file)); + files.emplace(name, file); pos = next_pos + crlf.size(); } @@ -998,7 +998,7 @@ inline std::string Request::get_header_value(const char* key) const inline void Request::set_header(const char* key, const char* val) { - headers.insert(std::make_pair(key, val)); + headers.emplace(key, val); } inline bool Request::has_param(const char* key) const @@ -1006,6 +1006,15 @@ inline bool Request::has_param(const char* key) const return params.find(key) != params.end(); } +inline std::string Request::get_param_value(const char* key) const +{ + auto it = params.find(key); + if (it != params.end()) { + return it->second; + } + return std::string(); +} + inline bool Request::has_file(const char* key) const { return files.find(key) != files.end(); @@ -1033,7 +1042,7 @@ inline std::string Response::get_header_value(const char* key) const inline void Response::set_header(const char* key, const char* val) { - headers.insert(std::make_pair(key, val)); + headers.emplace(key, val); } inline void Response::set_redirect(const char* url) @@ -1421,7 +1430,7 @@ inline std::shared_ptr Client::post( } inline std::shared_ptr Client::post( - const char* path, const Map& params) + const char* path, const MultiMap& params) { std::string query; for (auto it = params.begin(); it != params.end(); ++it) { diff --git a/test/test.cc b/test/test.cc index b38d08d..bb71f00 100644 --- a/test/test.cc +++ b/test/test.cc @@ -32,7 +32,7 @@ TEST(StartupTest, WSAStartup) TEST(SplitTest, ParseQueryString) { string s = "key1=val1&key2=val2&key3=val3"; - map dic; + MultiMap dic; detail::split(s.c_str(), s.c_str() + s.size(), '&', [&](const char* b, const char* e) { string key, val; @@ -43,24 +43,24 @@ TEST(SplitTest, ParseQueryString) val.assign(b, e); } }); - dic[key] = val; + dic.emplace(key, val); }); - EXPECT_EQ("val1", dic["key1"]); - EXPECT_EQ("val2", dic["key2"]); - EXPECT_EQ("val3", dic["key3"]); + EXPECT_EQ("val1", dic.find("key1")->second); + EXPECT_EQ("val2", dic.find("key2")->second); + EXPECT_EQ("val3", dic.find("key3")->second); } TEST(ParseQueryTest, ParseQueryString) { string s = "key1=val1&key2=val2&key3=val3"; - map dic; + MultiMap dic; detail::parse_query_text(s, dic); - EXPECT_EQ("val1", dic["key1"]); - EXPECT_EQ("val2", dic["key2"]); - EXPECT_EQ("val3", dic["key3"]); + EXPECT_EQ("val1", dic.find("key1")->second); + EXPECT_EQ("val2", dic.find("key2")->second); + EXPECT_EQ("val3", dic.find("key3")->second); } TEST(SocketTest, OpenClose) @@ -159,7 +159,7 @@ protected: }) .post("/person", [&](const Request& req, Response& res) { if (req.has_param("name") && req.has_param("note")) { - persons_[req.params.at("name")] = req.params.at("note"); + persons_[req.get_param_value("name")] = req.get_param_value("note"); } else { res.status = 400; } @@ -310,9 +310,9 @@ TEST_F(ServerTest, PostMethod2) ASSERT_TRUE(res != nullptr); ASSERT_EQ(404, res->status); - Map params; - params["name"] = "john2"; - params["note"] = "coder"; + MultiMap params; + params.emplace("name", "john2"); + params.emplace("note", "coder"); res = cli_.post("/person", params); ASSERT_TRUE(res != nullptr);