Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Cookie.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_IO_Network_HTTP_Cookie_h_
5#define _Stroika_Foundation_IO_Network_HTTP_Cookie_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
11#include "Stroika/Foundation/Common/KeyValuePair.h"
12#include "Stroika/Foundation/Common/Property.h"
13#include "Stroika/Foundation/Containers/Collection.h"
14#include "Stroika/Foundation/Containers/Mapping.h"
17
18/**
19 */
20
22
23 using Characters::Character;
24 using Characters::String;
25 using Common::KeyValuePair;
26 using Containers::Collection;
27 using Containers::Mapping;
28
29 /**
30 * \brief object representing an HTTP cookie - from https://tools.ietf.org/html/rfc6265
31 */
32 struct Cookie : KeyValuePair<String, String> {
33 /**
34 */
35 Cookie () = default;
36 Cookie (const String& name, const String& value);
37 Cookie (const String& name, const String& value, const Mapping<String, String>& attributes);
38
39 /*
40 * https://tools.ietf.org/html/rfc6265#section-4.1.2.1
41 */
42 static constexpr string_view kExpiresAttributeLabel = "Expires"sv;
43 optional<Time::DateTime> fExpires;
44
45 /*
46 * https://tools.ietf.org/html/rfc6265#section-4.1.2.2
47 */
48 static constexpr string_view kMaxAgeAttributeLabel = "Max-Age"sv;
49 optional<int> fMaxAge;
50
51 /*
52 * https://tools.ietf.org/html/rfc6265#section-4.1.2.3
53 */
54 static constexpr string_view kDomainAttributeLabel = "Domain"sv;
55 optional<String> fDomain;
56
57 /*
58 * https://tools.ietf.org/html/rfc6265#section-4.1.2.4
59 */
60 static constexpr string_view kPathAttributeLabel = "Path"sv;
61 optional<String> fPath;
62
63 /*
64 * https://tools.ietf.org/html/rfc6265#section-4.1.2.5
65 *
66 * valueless attribute- just presence/absense of this attribute counts
67 */
68 static constexpr string_view kSecureAttributeLabel = "Secure"sv;
69 bool fSecure{false};
70
71 /*
72 * https://tools.ietf.org/html/rfc6265#section-4.1.2.6
73 *
74 * valueless attribute- just presence/absense of this attribute counts
75 */
76 static constexpr string_view kHttpOnlyAttributeLabel = "HttpOnly"sv;
77 bool fHttpOnly{false};
78
79 /*
80 * https://tools.ietf.org/html/rfc6265#section-4.1.1
81 */
82 optional<Mapping<String, String>> fOtherAttributes;
83
84 /**
85 * Return a combined mapping of the other attributes with the expicit (known name) attributes)
86 */
87 nonvirtual Mapping<String, String> GetAttributes () const;
88
89 /**
90 */
91 nonvirtual void AddAttribute (const String& aEqualsBAttributePair);
92 nonvirtual void AddAttribute (const String& key, const String& value);
93
94 /**
95 * \brief render as a string suitable for a cookie header
96 * @see https://tools.ietf.org/html/rfc6265#section-4.2.1
97 */
98 template <typename T = String>
99 nonvirtual String As () const;
100
101 /**
102 * Parse (decode) an http cookie into an object.
103 * @see https://tools.ietf.org/html/rfc6265#section-4.2.1
104 *
105 * \pre src.IsSeekable () for InputStream overload
106 */
108 static Cookie Parse (const String& src);
109
110 /**
111 * @see Characters::ToString ();
112 */
113 nonvirtual Characters::String ToString () const;
114
115 public:
116 /**
117 */
118 nonvirtual bool operator== (const Cookie& rhs) const = default;
119 };
120 template <>
121 String Cookie::As<String> () const;
122
123 /**
124 * This corresponds to the value of the Cookie, or Set-Cookie in an HTTP Request header
125 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie
126 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
127 */
129 public:
130 /**
131 * \note Cannot use default-ed CTOR due to use of properties.
132 */
133 CookieList ();
134 CookieList (const CookieList& src);
135 CookieList (CookieList&& src);
136 CookieList (const Mapping<String, String>& basicCookies);
138
139 public:
140 /**
141 * \note Cannot use defaulted-op= due to use of properties.
142 */
143 nonvirtual CookieList& operator= (CookieList&& rhs);
144 nonvirtual CookieList& operator= (const CookieList& rhs);
145
146 public:
147 /**
148 * This representation omits the cookie attributes.
149 */
150 Common::Property<Mapping<String, String>> cookies; // key-value-pair, as would appear in HTTP Cookie: header
151
152 public:
153 /**
154 * This representation includes any cookie attributes.
155 */
156 Common::Property<Collection<Cookie>> cookieDetails; // key-value-pair, as would appear in HTTP Cookie: header
157
158 public:
159 /**
160 * \brief render as a string suitable for a cookie header
161 * @see https://tools.ietf.org/html/rfc6265#section-4.2.1
162 */
163 nonvirtual String EncodeForCookieHeader () const;
164
165 public:
166 /**
167 * Parse (decode) the string as a CookieList. The input format can be the value of a Cookie: HTTP header (which can produce multiple cooklies)
168 * or a Set-Cookie: HTTP header (in which case it produces a single entry cookie list, possibly containing attributes)
169 */
170 static CookieList Parse (const String& cookieValueArg);
171
172 public:
173 /**
174 * @see Characters::ToString ();
175 */
176 nonvirtual Characters::String ToString () const;
177
178 public:
179 /**
180 */
181 nonvirtual bool operator== (const CookieList& rhs) const;
182
183 private:
184 Collection<Cookie> fCookieDetails_; // redundant representation
185 };
186
187}
188
189/*
190 ********************************************************************************
191 ***************************** Implementation Details ***************************
192 ********************************************************************************
193 */
194#include "Cookie.inl"
195
196#endif /*_Stroika_Foundation_IO_Network_HTTP_Cookie_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
A Collection<T> is a container to manage an un-ordered collection of items, without equality defined ...
Definition Collection.h:102
InputStream<>::Ptr is Smart pointer (with abstract Rep) class defining the interface to reading from ...