From a011366b74e897cd499c079ee3a2fd39a53a255b Mon Sep 17 00:00:00 2001 From: Fabian Sauter Date: Fri, 22 Sep 2023 19:56:04 +0200 Subject: [PATCH] Option to parse a raw cookie string --- cpr/util.cpp | 34 +++++++++++++++++++--------------- include/cpr/util.h | 1 + 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/cpr/util.cpp b/cpr/util.cpp index ff0b6c280..f879969dd 100644 --- a/cpr/util.cpp +++ b/cpr/util.cpp @@ -44,27 +44,31 @@ enum class CurlHTTPCookieField : size_t { }; Cookies parseCookies(curl_slist* raw_cookies) { - const int CURL_HTTP_COOKIE_SIZE = static_cast(CurlHTTPCookieField::Value) + 1; Cookies cookies; for (curl_slist* nc = raw_cookies; nc; nc = nc->next) { - std::vector tokens = cpr::util::split(nc->data, '\t'); - while (tokens.size() < CURL_HTTP_COOKIE_SIZE) { - tokens.emplace_back(""); - } - const std::time_t expires = static_cast(std::stoul(tokens.at(static_cast(CurlHTTPCookieField::Expires)))); - cookies.emplace_back(Cookie{ - tokens.at(static_cast(CurlHTTPCookieField::Name)), - tokens.at(static_cast(CurlHTTPCookieField::Value)), - tokens.at(static_cast(CurlHTTPCookieField::Domain)), - isTrue(tokens.at(static_cast(CurlHTTPCookieField::IncludeSubdomains))), - tokens.at(static_cast(CurlHTTPCookieField::Path)), - isTrue(tokens.at(static_cast(CurlHTTPCookieField::HttpsOnly))), - std::chrono::system_clock::from_time_t(expires), - }); + cookies.emplace_back(parseCookie(nc->data)); } return cookies; } +Cookie parseCookie(const std::string& raw_cookie) { + static const int CURL_HTTP_COOKIE_SIZE = static_cast(CurlHTTPCookieField::Value) + 1; + std::vector tokens = cpr::util::split(raw_cookie, '\t'); + while (tokens.size() < CURL_HTTP_COOKIE_SIZE) { + tokens.emplace_back(""); + } + const std::time_t expires = static_cast(std::stoul(tokens.at(static_cast(CurlHTTPCookieField::Expires)))); + return Cookie{ + tokens.at(static_cast(CurlHTTPCookieField::Name)), + tokens.at(static_cast(CurlHTTPCookieField::Value)), + tokens.at(static_cast(CurlHTTPCookieField::Domain)), + isTrue(tokens.at(static_cast(CurlHTTPCookieField::IncludeSubdomains))), + tokens.at(static_cast(CurlHTTPCookieField::Path)), + isTrue(tokens.at(static_cast(CurlHTTPCookieField::HttpsOnly))), + std::chrono::system_clock::from_time_t(expires), + }; +} + Header parseHeader(const std::string& headers, std::string* status_line, std::string* reason) { Header header; std::vector lines; diff --git a/include/cpr/util.h b/include/cpr/util.h index d851e23a8..640bf84c0 100644 --- a/include/cpr/util.h +++ b/include/cpr/util.h @@ -14,6 +14,7 @@ namespace cpr::util { Header parseHeader(const std::string& headers, std::string* status_line = nullptr, std::string* reason = nullptr); Cookies parseCookies(curl_slist* raw_cookies); +Cookie parseCookie(const std::string& raw_cookie); size_t readUserFunction(char* ptr, size_t size, size_t nitems, const ReadCallback* read); size_t headerUserFunction(char* ptr, size_t size, size_t nmemb, const HeaderCallback* header); size_t writeFunction(char* ptr, size_t size, size_t nmemb, std::string* data);