2021-03-26 23:18:51 +01:00
|
|
|
#include "cpr/cookies.h"
|
2023-08-05 20:31:33 +02:00
|
|
|
#include <ctime>
|
|
|
|
#include <iomanip>
|
2021-03-26 23:18:51 +01:00
|
|
|
|
|
|
|
namespace cpr {
|
2023-08-05 20:31:33 +02:00
|
|
|
const std::string Cookie::GetDomain() const {
|
|
|
|
return domain_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Cookie::IsIncludingSubdomains() const {
|
|
|
|
return includeSubdomains_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string Cookie::GetPath() const {
|
|
|
|
return path_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Cookie::IsHttpsOnly() const {
|
|
|
|
return httpsOnly_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::chrono::system_clock::time_point Cookie::GetExpires() const {
|
|
|
|
return expires_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string Cookie::GetExpiresString() const {
|
|
|
|
std::stringstream ss;
|
|
|
|
std::tm tm{};
|
|
|
|
const std::time_t tt = std::chrono::system_clock::to_time_t(expires_);
|
|
|
|
#ifdef _WIN32
|
|
|
|
gmtime_s(&tm, &tt);
|
|
|
|
#else
|
|
|
|
gmtime_r(&tt, &tm);
|
|
|
|
#endif
|
|
|
|
ss << std::put_time(&tm, "%a, %d %b %Y %H:%M:%S GMT");
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string Cookie::GetName() const {
|
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string Cookie::GetValue() const {
|
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string Cookies::GetEncoded(const CurlHolder& holder) const {
|
2021-03-26 23:18:51 +01:00
|
|
|
std::stringstream stream;
|
2023-08-05 20:31:33 +02:00
|
|
|
for (const cpr::Cookie& item : cookies_) {
|
2021-03-26 23:18:51 +01:00
|
|
|
// Depending on if encoding is set to "true", we will URL-encode cookies
|
2023-08-05 20:31:33 +02:00
|
|
|
stream << (encode ? holder.urlEncode(item.GetName()) : item.GetName()) << "=";
|
2021-03-26 23:18:51 +01:00
|
|
|
|
|
|
|
// special case version 1 cookies, which can be distinguished by
|
|
|
|
// beginning and trailing quotes
|
2023-08-05 20:31:33 +02:00
|
|
|
if (!item.GetValue().empty() && item.GetValue().front() == '"' && item.GetValue().back() == '"') {
|
|
|
|
stream << item.GetValue();
|
2021-03-26 23:18:51 +01:00
|
|
|
} else {
|
|
|
|
// Depending on if encoding is set to "true", we will URL-encode cookies
|
2023-08-05 20:31:33 +02:00
|
|
|
stream << (encode ? holder.urlEncode(item.GetValue()) : item.GetValue());
|
2021-03-26 23:18:51 +01:00
|
|
|
}
|
|
|
|
stream << "; ";
|
|
|
|
}
|
|
|
|
return stream.str();
|
|
|
|
}
|
|
|
|
|
2023-08-05 20:31:33 +02:00
|
|
|
cpr::Cookie& Cookies::operator[](size_t pos) {
|
|
|
|
return cookies_[pos];
|
2021-03-26 23:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Cookies::iterator Cookies::begin() {
|
2023-08-05 20:31:33 +02:00
|
|
|
return cookies_.begin();
|
2021-03-26 23:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Cookies::iterator Cookies::end() {
|
2023-08-05 20:31:33 +02:00
|
|
|
return cookies_.end();
|
2021-03-26 23:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Cookies::const_iterator Cookies::begin() const {
|
2023-08-05 20:31:33 +02:00
|
|
|
return cookies_.begin();
|
2021-03-26 23:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Cookies::const_iterator Cookies::end() const {
|
2023-08-05 20:31:33 +02:00
|
|
|
return cookies_.end();
|
2021-03-26 23:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Cookies::const_iterator Cookies::cbegin() const {
|
2023-08-05 20:31:33 +02:00
|
|
|
return cookies_.cbegin();
|
2021-03-26 23:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Cookies::const_iterator Cookies::cend() const {
|
2023-08-05 20:31:33 +02:00
|
|
|
return cookies_.cend();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cookies::emplace_back(const Cookie& str) {
|
|
|
|
cookies_.emplace_back(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cookies::push_back(const Cookie& str) {
|
|
|
|
cookies_.push_back(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cookies::pop_back() {
|
|
|
|
cookies_.pop_back();
|
2021-03-26 23:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cpr
|