Skip to content

Commit

Permalink
Option to parse a raw cookie string
Browse files Browse the repository at this point in the history
  • Loading branch information
COM8 committed Sep 22, 2023
1 parent e341350 commit a011366
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
34 changes: 19 additions & 15 deletions cpr/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,31 @@ enum class CurlHTTPCookieField : size_t {
};

Cookies parseCookies(curl_slist* raw_cookies) {
const int CURL_HTTP_COOKIE_SIZE = static_cast<int>(CurlHTTPCookieField::Value) + 1;
Cookies cookies;
for (curl_slist* nc = raw_cookies; nc; nc = nc->next) {
std::vector<std::string> tokens = cpr::util::split(nc->data, '\t');
while (tokens.size() < CURL_HTTP_COOKIE_SIZE) {
tokens.emplace_back("");
}
const std::time_t expires = static_cast<time_t>(std::stoul(tokens.at(static_cast<size_t>(CurlHTTPCookieField::Expires))));
cookies.emplace_back(Cookie{
tokens.at(static_cast<size_t>(CurlHTTPCookieField::Name)),
tokens.at(static_cast<size_t>(CurlHTTPCookieField::Value)),
tokens.at(static_cast<size_t>(CurlHTTPCookieField::Domain)),
isTrue(tokens.at(static_cast<size_t>(CurlHTTPCookieField::IncludeSubdomains))),
tokens.at(static_cast<size_t>(CurlHTTPCookieField::Path)),
isTrue(tokens.at(static_cast<size_t>(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<int>(CurlHTTPCookieField::Value) + 1;
std::vector<std::string> tokens = cpr::util::split(raw_cookie, '\t');
while (tokens.size() < CURL_HTTP_COOKIE_SIZE) {
tokens.emplace_back("");
}
const std::time_t expires = static_cast<time_t>(std::stoul(tokens.at(static_cast<size_t>(CurlHTTPCookieField::Expires))));
return Cookie{
tokens.at(static_cast<size_t>(CurlHTTPCookieField::Name)),
tokens.at(static_cast<size_t>(CurlHTTPCookieField::Value)),
tokens.at(static_cast<size_t>(CurlHTTPCookieField::Domain)),
isTrue(tokens.at(static_cast<size_t>(CurlHTTPCookieField::IncludeSubdomains))),
tokens.at(static_cast<size_t>(CurlHTTPCookieField::Path)),
isTrue(tokens.at(static_cast<size_t>(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<std::string> lines;
Expand Down
1 change: 1 addition & 0 deletions include/cpr/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a011366

Please sign in to comment.