Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
Cookie.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. 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;
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 * \brief Add an attribute, as a Set-Cookie header writes one: one of the above (Path, Secure...) sets its field; any other goes in fOtherAttributes
91 *
92 * The one-string form takes name=value, or a name alone (a valueless attribute, such as Secure).
93 */
94 nonvirtual void AddAttribute (const String& aEqualsBAttributePair);
95 nonvirtual void AddAttribute (const String& key, const String& value); ///< \brief Add an attribute, as a Set-Cookie header writes one: one of the above (Path, Secure...) sets its field; any other goes in fOtherAttributes
96
97 /**
98 * \brief render as a Set-Cookie header's value: name=value, then its attributes (as HTTP::Headers writes Set-Cookie)
99 * @see https://tools.ietf.org/html/rfc6265#section-4.1.1
100 */
101 template <typename T = String>
102 nonvirtual String As () const;
103
104 /**
105 * Parse (decode) an http cookie into an object - a Set-Cookie header's value, as As<String> () writes.
106 * @see https://tools.ietf.org/html/rfc6265#section-4.1.1
107 *
108 * \pre src.IsSeekable () for InputStream overload
109 */
111 static Cookie Parse (const String& src);
112
113 /**
114 * @see Characters::ToString ();
115 */
116 nonvirtual Characters::String ToString () const;
117
118 public:
119 /**
120 */
121 nonvirtual bool operator== (const Cookie& rhs) const = default;
122 };
123 template <>
124 String Cookie::As<String> () const;
125
126 /**
127 * This corresponds to the value of the Cookie, or Set-Cookie in an HTTP Request header
128 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie
129 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
130 */
132 public:
133 /**
134 * \note Cannot use default-ed CTOR due to use of properties.
135 */
136 CookieList ();
137 CookieList (const CookieList& src);
138 CookieList (CookieList&& src);
139 CookieList (const Mapping<String, String>& basicCookies);
141
142 public:
143 /**
144 * \note Cannot use defaulted-op= due to use of properties.
145 */
146 nonvirtual CookieList& operator= (CookieList&& rhs);
147 nonvirtual CookieList& operator= (const CookieList& rhs);
148
149 public:
150 /**
151 * This representation omits the cookie attributes.
152 */
153 Common::Property<Mapping<String, String>> cookies; // key-value-pair, as would appear in HTTP Cookie: header
154
155 public:
156 /**
157 * This representation includes any cookie attributes.
158 */
159 Common::Property<Collection<Cookie>> cookieDetails; // key-value-pair, as would appear in HTTP Cookie: header
160
161 public:
162 /**
163 * \brief render as a string suitable for a cookie header
164 * @see https://tools.ietf.org/html/rfc6265#section-4.2.1
165 */
166 nonvirtual String EncodeForCookieHeader () const;
167
168 public:
169 /**
170 * Parse (decode) the string as a CookieList. The input format can be the value of a Cookie: HTTP header (which can produce multiple cooklies)
171 * or a Set-Cookie: HTTP header (in which case it produces a single entry cookie list, possibly containing attributes)
172 */
173 static CookieList Parse (const String& cookieValueArg);
174
175 public:
176 /**
177 * @see Characters::ToString ();
178 */
179 nonvirtual Characters::String ToString () const;
180
181 public:
182 /**
183 */
184 nonvirtual bool operator== (const CookieList& rhs) const;
185
186 private:
187 Collection<Cookie> fCookieDetails_; // redundant representation
188 };
189
190}
191
192/*
193 ********************************************************************************
194 ***************************** Implementation Details ***************************
195 ********************************************************************************
196 */
197#include "Cookie.inl"
198
199#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 ...
A Mapping uniquely associates two elements: a key and a value (use Assocation to allow multiple value...
InputStream<>::Ptr is Smart pointer (with abstract Rep) class defining the interface to reading from ...