diff --git a/lib/inc/drogon/Cookie.h b/lib/inc/drogon/Cookie.h index 5b4a79b5..65c83a40 100644 --- a/lib/inc/drogon/Cookie.h +++ b/lib/inc/drogon/Cookie.h @@ -36,12 +36,7 @@ class DROGON_EXPORT Cookie * @param key key of the cookie * @param value value of the cookie */ - Cookie(const std::string &key, const std::string &value) - : key_(key), value_(value) - { - } - - Cookie(std::string &&key, std::string &&value) + Cookie(std::string key, std::string value) : key_(std::move(key)), value_(std::move(value)) { } @@ -89,6 +84,9 @@ class DROGON_EXPORT Cookie domain_ = domain; } + /** + * @brief Set the domain of the cookie. + */ void setDomain(std::string &&domain) { domain_ = std::move(domain); @@ -102,6 +100,9 @@ class DROGON_EXPORT Cookie path_ = path; } + /** + * @brief Set the path of the cookie. + */ void setPath(std::string &&path) { path_ = std::move(path); @@ -115,6 +116,9 @@ class DROGON_EXPORT Cookie key_ = key; } + /** + * @brief Set the key of the cookie. + */ void setKey(std::string &&key) { key_ = std::move(key); @@ -128,6 +132,9 @@ class DROGON_EXPORT Cookie value_ = value; } + /** + * @brief Set the value of the cookie. + */ void setValue(std::string &&value) { value_ = std::move(value); @@ -343,21 +350,15 @@ class DROGON_EXPORT Cookie * @brief Converts a string value to its associated enum class SameSite * value */ - static SameSite convertString2SameSite(const std::string_view &sameSite) + static SameSite convertString2SameSite(std::string_view sameSite) { if (stricmp(sameSite, "lax")) - { return Cookie::SameSite::kLax; - } - else if (stricmp(sameSite, "strict")) - { + if (stricmp(sameSite, "strict")) return Cookie::SameSite::kStrict; - } - else if (stricmp(sameSite, "none")) - { + if (stricmp(sameSite, "none")) return Cookie::SameSite::kNone; - } - else if (!stricmp(sameSite, "null")) + if (!stricmp(sameSite, "null")) { LOG_WARN << "'" << sameSite @@ -372,34 +373,20 @@ class DROGON_EXPORT Cookie * @brief Converts an enum class SameSite value to its associated string * value */ - static const std::string_view &convertSameSite2String(SameSite sameSite) + static std::string_view convertSameSite2String(SameSite sameSite) { switch (sameSite) { case SameSite::kLax: - { - static std::string_view sv{"Lax"}; - return sv; - } + return "Lax"; case SameSite::kStrict: - { - static std::string_view sv{"Strict"}; - return sv; - } + return "Strict"; case SameSite::kNone: - { - static std::string_view sv{"None"}; - return sv; - } + return "None"; case SameSite::kNull: - { - static std::string_view sv{"Null"}; - return sv; - } - } - { - static std::string_view sv{"UNDEFINED"}; - return sv; + return "Null"; + default: + return "UNDEFINED"; } } diff --git a/lib/inc/drogon/HttpRequest.h b/lib/inc/drogon/HttpRequest.h index 238a12e2..68c4271b 100644 --- a/lib/inc/drogon/HttpRequest.h +++ b/lib/inc/drogon/HttpRequest.h @@ -449,8 +449,7 @@ class DROGON_EXPORT HttpRequest virtual void setCustomContentTypeString(const std::string &type) = 0; /// Add a cookie - virtual void addCookie(const std::string &key, - const std::string &value) = 0; + virtual void addCookie(std::string key, std::string value) = 0; /** * @brief Set the request object to the pass-through mode or not. It's not diff --git a/lib/src/Cookie.cc b/lib/src/Cookie.cc index 2423395c..1b1c0e0a 100644 --- a/lib/src/Cookie.cc +++ b/lib/src/Cookie.cc @@ -19,9 +19,11 @@ using namespace drogon; std::string Cookie::cookieString() const { - std::string ret = "Set-Cookie: "; + constexpr std::string_view prefix = "Set-Cookie: "; + std::string ret; // reserve space to reduce frequency allocation - ret.reserve(ret.size() + key_.size() + value_.size() + 30); + ret.reserve(prefix.size() + key_.size() + value_.size() + 30); + ret = prefix; ret.append(key_).append("=").append(value_).append("; "); if (expiresDate_.microSecondsSinceEpoch() != (std::numeric_limits::max)() && diff --git a/lib/src/HttpRequestImpl.h b/lib/src/HttpRequestImpl.h index 24274b78..9edf4faa 100644 --- a/lib/src/HttpRequestImpl.h +++ b/lib/src/HttpRequestImpl.h @@ -438,9 +438,9 @@ class HttpRequestImpl : public HttpRequest headers_[std::move(field)] = std::move(value); } - void addCookie(const std::string &key, const std::string &value) override + void addCookie(std::string key, std::string value) override { - cookies_[key] = value; + cookies_[std::move(key)] = std::move(value); } void setPassThrough(bool flag) override diff --git a/lib/src/HttpResponseImpl.cc b/lib/src/HttpResponseImpl.cc index 24440608..163102ce 100644 --- a/lib/src/HttpResponseImpl.cc +++ b/lib/src/HttpResponseImpl.cc @@ -854,7 +854,7 @@ void HttpResponseImpl::addHeader(const char *start, } if (!cookie.key().empty()) { - cookies_[cookie.key()] = cookie; + cookies_[cookie.key()] = std::move(cookie); } } else diff --git a/lib/tests/CookieSameSite.cc b/lib/tests/CookieSameSite.cc index 85988bb9..4e74159d 100644 --- a/lib/tests/CookieSameSite.cc +++ b/lib/tests/CookieSameSite.cc @@ -44,8 +44,7 @@ DROGON_TEST(CookieSameSite) if (seq.sessionCookie.getValue() != "") { // add session cookie - req->addCookie(seq.sessionCookie.getKey(), - seq.sessionCookie.getValue()); + req->addCookie(seq.sessionCookie.key(), seq.sessionCookie.value()); } // endif client->sendRequest(