Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
CacheControl.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_IO_Network_HTTP_CacheControl_h_
5#define _Stroika_Foundation_IO_Network_HTTP_CacheControl_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <optional>
10
13
14/**
15 */
16
18
19 /**
20 * Support Cache-Control headers
21 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
22 * @see https://tools.ietf.org/html/rfc7234 (Hypertext Transfer Protocol (HTTP/1.1): Caching)
23 *
24 * \par Example Usage
25 * \code
26 * auto cc1 = CacheControl{CacheControl::eNoStore}; // Cache-Control: no-store
27 * auto cc2 = CacheControl{.fStoreRestriction=CacheControl::eNoStore, .fMaxAge=0}; // Cache-Control: no-store, max-age=0
28 * auto cc3 = CacheControl{.fMaxAge=1234}; // Cache-Control: max-age=1234
29 * auto cc3 = CacheControl{.fMaxAge=604800}; // Cache-Control: public, max-age=604800
30 * \endcode
31 *
32 * \par Example Usage (@see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#caching_static_assets)
33 * \code
34 * auto cc = CacheControl{.fVisibility=CacheControl::ePublic, .fImmutable=true, .fMaxAge=CacheControl::kMaximumAgeValue}; // Cache-Control: public, max-age=2147483647, immutable
35 * see kImmutable
36 * \endcode
37 *
38 * \par Example Usage (@see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#preventing_caching)
39 * \code
40 * auto cc1 = CacheControl{.fStoreRestriction=CacheControl::StoreRestriction::eNoStore}; // Cache-Control: no-store
41 * auto cc2 = CacheControl{.fStoreRestriction=CacheControl::StoreRestriction::eNoStore, .fMaxAge=0}; // Cache-Control: no-store max-age=0; like above but forces clear of existing cached item
42 * see kDisableCaching
43 * \endcode
44 *
45 * \par Example Usage (Web-Services) - just a rough example cuz this will depend alot on the web-service
46 * \code
47 * auto cc1 = CacheControl{.fVisibility=CacheControl::ePrivate, .fMaxAge=Duration{30s}.As<int> ()}; // Cache-Control: private, max-age=30
48 * auto cc2 = CacheControl::kDisableCaching;
49 * // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#requiring_revalidation
50 * auto cc3 = CacheControl{.fStoreRestriction=CacheControl::StoreRestriction::eNoCache}; // Cache-Control: no-cache
51 * auto cc3 = CacheControl{.fMaxAge=0, .fMustRevalidate=true}; // Cache-Control: max-age=0, must-revalidate
52 * auto cc4 = CacheControl::kMustRevalidate;
53 * auto cc5 = CacheControl::kPrivateMustRevalidate; // ** probably best to use for most webservice calls, except those that have side-effects, and those should use kDisableCaching **
54 * \endcode
55 */
56 struct CacheControl {
57
58 /**
59 * The Parse the header, SILENTLY ignorning anything in the header which is unrecognized.
60 */
61 static CacheControl Parse (const Characters::String& headerValue);
62
63 /**
64 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#cacheability
65 *
66 * \note Common::DefaultNames<> supported
67 */
68 enum class Cacheability {
69 ePublic, // The response may be stored by any cache, even if the response is normally non-cacheable
70 ePrivate, // The response may be stored only by a browser's cache, even if the response is normally non-cacheable
71 eNoCache, // The response may be stored by any cache, even if the response is normally non-cacheable. However, the stored response MUST always go through validation with the origin server first
72 eNoStore, // The response may not be stored in any cache (can also set max-age=0 to also clear existing cache responses)
73
74 Stroika_Define_Enum_Bounds (ePublic, eNoStore)
75 };
76 static constexpr Cacheability ePublic = Cacheability::ePublic;
77 static constexpr Cacheability ePrivate = Cacheability::ePrivate;
78 static constexpr Cacheability eNoStore = Cacheability::eNoStore;
79 static constexpr Cacheability eNoCache = Cacheability::eNoCache;
80
81 /**
82 */
83 optional<Cacheability> fCacheability;
84
85 /**
86 * The number of seconds a resource is considered fresh (**very commonly used**)
87 * \note req fMaxAge <= kMaximumAgeValue
88 */
89 optional<uint32_t> fMaxAge;
90
91 static constexpr uint32_t kMaximumAgeValue = numeric_limits<int32_t>::max (); // see https://tools.ietf.org/html/rfc7234#section-1.2.1
92
93 /**
94 * \brief Probably not useful in most cases. Just affects behavior of cached values when expired and disconnected.
95 *
96 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#requiring_revalidation
97 */
98 bool fMustRevalidate{false};
99
100 /**
101 * \brief Very useful if/when true.
102 *
103 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#revalidation_and_reloading
104 */
105 bool fImmutable{false};
106
107 /**
108 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#other
109 */
110 bool fNoTransform{false};
111
112 /**
113 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#other
114 */
115 bool fOnlyIfCached{false};
116
117 /**
118 * Used by servers just in proxying (otherwise no reason to return something with a non-zero age)
119 * \note req fMaxAge <= kMaximumAgeValue
120 */
121 optional<uint32_t> fAge;
122
123 /**
124 * overrides fMaxAge & Expires, but ONLY for shared caches(e.g. proxies)
125 * \note req fMaxAge <= kMaximumAgeValue
126 */
127 optional<int> fSharedMaxAge;
128
129 /**
130 * Indicates the client will accept a stale response
131 */
132 struct MaxStale {
133 //An optional value in seconds indicates the upper limit of staleness the client will accept.
134 optional<int> fAmount;
135 nonvirtual strong_ordering operator<=> (const MaxStale&) const = default;
136 };
137 optional<MaxStale> fMaxStale;
138
139 /**
140 * Indicates the client wants a response that will still be fresh for at least the specified number of seconds
141 */
142 optional<int> fMinFresh;
143
144 /**
145 * Supported values for T include:
146 * String
147 */
148 template <typename T>
149 T As () const;
150
151 /**
152 * @see Characters::ToString ();
153 */
154 nonvirtual Characters::String ToString () const;
155
156 public:
157 /**
158 */
159 nonvirtual strong_ordering operator<=> (const CacheControl&) const = default;
160
161 public:
162 /**
163 */
164 nonvirtual bool operator== (const CacheControl&) const = default;
165
166 public:
167 /**
168 * \par Example Usage (@see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#caching_static_assets)
169 * \code
170 * kImmutable = CacheControl{.fCacheability=CacheControl::ePublic, .fMaxAge=CacheControl::kMaximumAgeValue, .fImmutable=true}; // Cache-Control: public, max-age=2147483647, immutable
171 * \endcode
172 */
174
175 public:
176 /**
177 * \par Example Usage (@see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#preventing_caching)
178 * \code
179 * auto cc = CacheControl{.fCacheability=CacheControl::eNoStore}; // Cache-Control: no-store
180 * \endcode
181 */
183
184 public:
185 /**
186 * \brief this means you CAN cache the value, but should revalidate each time before use (so etags can be used etc) - but it should not be re-used from user to user
187 */
188 [[deprecated ("Since v2.1.11 - deprecated - probably use CacheControl{.fCacheability=CacheControl::ePrivate, "
189 ".fMaxAge=3600}")]] static const CacheControl kMustRevalidatePrivate;
190 };
191 template <>
193
194 inline constexpr const CacheControl CacheControl::kImmutable{
195 .fCacheability = CacheControl::ePublic, .fMaxAge = CacheControl::kMaximumAgeValue, .fImmutable = true};
196 inline constexpr const CacheControl CacheControl::kDisableCaching{.fCacheability = CacheControl::eNoStore};
197 inline constexpr const CacheControl CacheControl::kMustRevalidatePrivate{.fCacheability = CacheControl::ePrivate, .fMustRevalidate = true};
198
199}
200
201/*
202 ********************************************************************************
203 ***************************** Implementation Details ***************************
204 ********************************************************************************
205 */
206#include "CacheControl.inl"
207
208#endif /*_Stroika_Foundation_IO_Network_HTTP_CacheControl_h_*/
#define Stroika_Define_Enum_Bounds(FIRST_ITEM, LAST_ITEM)
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
bool fMustRevalidate
Probably not useful in most cases. Just affects behavior of cached values when expired and disconnect...
static const CacheControl kMustRevalidatePrivate
this means you CAN cache the value, but should revalidate each time before use (so etags can be used ...
nonvirtual Characters::String ToString() const
static CacheControl Parse(const Characters::String &headerValue)