1 | #include "cpr/cookies.h" |
---|---|
2 | #include <ctime> |
3 | #include <iomanip> |
4 | |
5 | namespace cpr { |
6 | const std::string Cookie::GetDomain() const { |
7 | return domain_; |
8 | } |
9 | |
10 | bool Cookie::IsIncludingSubdomains() const { |
11 | return includeSubdomains_; |
12 | } |
13 | |
14 | const std::string Cookie::GetPath() const { |
15 | return path_; |
16 | } |
17 | |
18 | bool Cookie::IsHttpsOnly() const { |
19 | return httpsOnly_; |
20 | } |
21 | |
22 | const std::chrono::system_clock::time_point Cookie::GetExpires() const { |
23 | return expires_; |
24 | } |
25 | |
26 | const std::string Cookie::GetExpiresString() const { |
27 | std::stringstream ss; |
28 | std::tm tm{}; |
29 | const std::time_t tt = std::chrono::system_clock::to_time_t(t: expires_); |
30 | #ifdef _WIN32 |
31 | gmtime_s(&tm, &tt); |
32 | #else |
33 | gmtime_r(timer: &tt, tp: &tm); |
34 | #endif |
35 | ss << std::put_time(tmb: &tm, fmt: "%a, %d %b %Y %H:%M:%S GMT"); |
36 | return ss.str(); |
37 | } |
38 | |
39 | const std::string Cookie::GetName() const { |
40 | return name_; |
41 | } |
42 | |
43 | const std::string Cookie::GetValue() const { |
44 | return value_; |
45 | } |
46 | |
47 | const std::string Cookies::GetEncoded(const CurlHolder& holder) const { |
48 | std::stringstream stream; |
49 | for (const cpr::Cookie& item : cookies_) { |
50 | // Depending on if encoding is set to "true", we will URL-encode cookies |
51 | stream << (encode ? holder.urlEncode(s: item.GetName()) : item.GetName()) << "="; |
52 | |
53 | // special case version 1 cookies, which can be distinguished by |
54 | // beginning and trailing quotes |
55 | if (!item.GetValue().empty() && item.GetValue().front() == '"' && item.GetValue().back() == '"') { |
56 | stream << item.GetValue(); |
57 | } else { |
58 | // Depending on if encoding is set to "true", we will URL-encode cookies |
59 | stream << (encode ? holder.urlEncode(s: item.GetValue()) : item.GetValue()); |
60 | } |
61 | stream << "; "; |
62 | } |
63 | return stream.str(); |
64 | } |
65 | |
66 | cpr::Cookie& Cookies::operator[](size_t pos) { |
67 | return cookies_[pos]; |
68 | } |
69 | |
70 | Cookies::iterator Cookies::begin() { |
71 | return cookies_.begin(); |
72 | } |
73 | |
74 | Cookies::iterator Cookies::end() { |
75 | return cookies_.end(); |
76 | } |
77 | |
78 | Cookies::const_iterator Cookies::begin() const { |
79 | return cookies_.begin(); |
80 | } |
81 | |
82 | Cookies::const_iterator Cookies::end() const { |
83 | return cookies_.end(); |
84 | } |
85 | |
86 | Cookies::const_iterator Cookies::cbegin() const { |
87 | return cookies_.cbegin(); |
88 | } |
89 | |
90 | Cookies::const_iterator Cookies::cend() const { |
91 | return cookies_.cend(); |
92 | } |
93 | |
94 | void Cookies::emplace_back(const Cookie& str) { |
95 | cookies_.emplace_back(args: str); |
96 | } |
97 | |
98 | void Cookies::push_back(const Cookie& str) { |
99 | cookies_.push_back(x: str); |
100 | } |
101 | |
102 | void Cookies::pop_back() { |
103 | cookies_.pop_back(); |
104 | } |
105 | |
106 | } // namespace cpr |
107 |