]> git.ipfire.org Git - thirdparty/squid.git/blob - src/MemObject.h
Remove serialized HTTP headers from storeClientCopy() (#1335)
[thirdparty/squid.git] / src / MemObject.h
1 /*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID_MEMOBJECT_H
10 #define SQUID_MEMOBJECT_H
11
12 #include "base/DelayedAsyncCalls.h"
13 #include "dlink.h"
14 #include "http/RequestMethod.h"
15 #include "RemovalPolicy.h"
16 #include "SquidString.h"
17 #include "stmem.h"
18 #include "store/forward.h"
19 #include "StoreIOBuffer.h"
20 #include "StoreIOState.h"
21 #include "typedefs.h" //for IRCB
22
23 #if USE_DELAY_POOLS
24 #include "DelayId.h"
25 #endif
26
27 typedef void STMCB (void *data, StoreIOBuffer wroteBuffer);
28
29 class store_client;
30 class PeerSelector;
31
32 class MemObject
33 {
34 MEMPROXY_CLASS(MemObject);
35
36 public:
37 static size_t inUseCount();
38
39 void dump() const;
40 MemObject();
41 ~MemObject();
42
43 /// Sets store ID, log URI, and request method (unless already set). Does
44 /// not clobber the method so that, say, a HEAD hit for a GET entry keeps
45 /// the GET method that matches the entry key. Same for the other parts of
46 /// the trio because the entry filling code may expect them to be constant.
47 /// XXX: Avoid this method. We plan to remove it and make the trio constant
48 /// after addressing the XXX in MemStore::get().
49 void setUris(char const *aStoreId, char const *aLogUri, const HttpRequestMethod &aMethod);
50
51 /// whether setUris() has been called
52 bool hasUris() const;
53
54 void write(const StoreIOBuffer &buf);
55 void unlinkRequest() { request = nullptr; }
56
57 /// HTTP response before 304 (Not Modified) updates
58 /// starts "empty"; modified via replaceBaseReply() or adjustableBaseReply()
59 const HttpReply &baseReply() const { return *reply_; }
60
61 /// \returns nil -- if no 304 updates since replaceBaseReply()
62 /// \returns combination of baseReply() and 304 updates -- after updates
63 const HttpReplyPointer &updatedReply() const { return updatedReply_; }
64
65 /// \returns the updated-by-304(s) response (if it exists)
66 /// \returns baseReply() (otherwise)
67 const HttpReply &freshestReply() const {
68 if (updatedReply_)
69 return *updatedReply_;
70 else
71 return baseReply();
72 }
73
74 /// \returns writable base reply for parsing and other initial modifications
75 /// Base modifications can only be done when forming/loading the entry.
76 /// After that, use replaceBaseReply() to reset all of the replies.
77 HttpReply &adjustableBaseReply();
78
79 /// (re)sets base reply, usually just replacing the initial/empty object
80 /// also forgets the updated reply (if any)
81 void replaceBaseReply(const HttpReplyPointer &r);
82
83 /// (re)sets updated reply; \see updatedReply()
84 void updateReply(const HttpReply &r) { updatedReply_ = &r; }
85
86 /// reflects past Controller::updateOnNotModified(old, e304) calls:
87 /// for HTTP 304 entries: whether our entry was used as "e304"
88 /// for other entries: whether our entry was updated as "old"
89 bool appliedUpdates = false;
90
91 void stat (MemBuf * mb) const;
92
93 /// The offset of the last memory-stored HTTP response byte plus one.
94 /// * HTTP response headers (if any) are stored at offset zero.
95 /// * HTTP response body byte[n] usually has offset (hdr_sz + n), where
96 /// hdr_sz is the size of stored HTTP response headers (zero if none); and
97 /// n is the corresponding byte offset in the whole resource body.
98 /// However, some 206 (Partial Content) response bodies are stored (and
99 /// retrieved) as regular 200 response bodies, disregarding offsets of
100 /// their body parts. \sa HttpStateData::decideIfWeDoRanges().
101 int64_t endOffset () const;
102
103 /// sets baseReply().hdr_sz (i.e. written reply headers size) to endOffset()
104 void markEndOfReplyHeaders();
105
106 /// negative if unknown; otherwise, expected object_sz, expected endOffset
107 /// maximum, and stored reply headers+body size (all three are the same)
108 int64_t expectedReplySize() const;
109 int64_t size() const;
110 void reset();
111 int64_t lowestMemReaderOffset() const;
112 bool readAheadPolicyCanRead() const;
113 void addClient(store_client *);
114 /* XXX belongs in MemObject::swapout, once swaphdrsz is managed
115 * better
116 */
117 int64_t objectBytesOnDisk() const;
118 int64_t policyLowestOffsetToKeep(bool swap) const;
119 int64_t availableForSwapOut() const; ///< buffered bytes we have not swapped out yet
120 void trimSwappable();
121 void trimUnSwappable();
122 bool isContiguous() const;
123 int mostBytesWanted(int max, bool ignoreDelayPools) const;
124 void setNoDelay(bool const newValue);
125 #if USE_DELAY_POOLS
126 DelayId mostBytesAllowed() const;
127 #endif
128
129 #if URL_CHECKSUM_DEBUG
130
131 void checkUrlChecksum() const;
132 #endif
133
134 /// Before StoreID, code assumed that MemObject stores Request URI.
135 /// After StoreID, some old code still incorrectly assumes that.
136 /// Use this method to mark that incorrect assumption.
137 const char *urlXXX() const { return storeId(); }
138
139 /// Entry StoreID (usually just Request URI); if a buggy code requests this
140 /// before the information is available, returns an "[unknown_URI]" string.
141 const char *storeId() const;
142
143 /// client request URI used for logging; storeId() by default
144 const char *logUri() const;
145
146 HttpRequestMethod method;
147 mem_hdr data_hdr;
148 int64_t inmem_lo = 0;
149 dlink_list clients;
150
151 size_t clientCount() const {return nclients;}
152
153 bool clientIsFirst(void *sc) const {return (clients.head && sc == clients.head->data);}
154
155 int nclients = 0;
156
157 class SwapOut
158 {
159 public:
160 int64_t queue_offset = 0; ///< number of bytes sent to SwapDir for writing
161 StoreIOState::Pointer sio;
162
163 /// Decision states for StoreEntry::swapoutPossible() and related code.
164 typedef enum { swNeedsCheck = 0, swImpossible = -1, swPossible = +1, swStarted } Decision;
165 Decision decision = swNeedsCheck; ///< current decision state
166 };
167
168 SwapOut swapout;
169
170 /* TODO: Remove this change-minimizing hack */
171 using Io = Store::IoStatus;
172 static constexpr Io ioUndecided = Store::ioUndecided;
173 static constexpr Io ioReading = Store::ioReading;
174 static constexpr Io ioWriting = Store::ioWriting;
175 static constexpr Io ioDone = Store::ioDone;
176
177 /// State of an entry with regards to the [shared] in-transit table.
178 class XitTable
179 {
180 public:
181 /// associate our StoreEntry with a Transients entry at the given index
182 void open(const int32_t anIndex, const Io anIo)
183 {
184 index = anIndex;
185 io = anIo;
186 }
187
188 /// stop associating our StoreEntry with a Transients entry
189 void close()
190 {
191 index = -1;
192 io = Store::ioDone;
193 }
194
195 int32_t index = -1; ///< entry position inside the in-transit table
196 Io io = ioUndecided; ///< current I/O state
197 };
198 XitTable xitTable; ///< current [shared] memory caching state for the entry
199
200 /// State of an entry with regards to the [shared] memory caching.
201 class MemCache
202 {
203 public:
204 int32_t index = -1; ///< entry position inside the memory cache
205 int64_t offset = 0; ///< bytes written/read to/from the memory cache so far
206
207 Io io = ioUndecided; ///< current I/O state
208 };
209 MemCache memCache; ///< current [shared] memory caching state for the entry
210
211 HttpRequestPointer request;
212
213 struct timeval start_ping;
214 IRCB *ping_reply_callback;
215 PeerSelector *ircb_data = nullptr;
216
217 /// used for notifying StoreEntry writers about 3rd-party initiated aborts
218 AsyncCallPointer abortCallback;
219 RemovalPolicyNode repl;
220 int id = 0;
221 int64_t object_sz = -1;
222 size_t swap_hdr_sz = 0;
223 #if URL_CHECKSUM_DEBUG
224 unsigned int chksum = 0;
225 #endif
226
227 SBuf vary_headers;
228
229 void delayRead(const AsyncCallPointer &);
230 void kickReads();
231
232 private:
233 HttpReplyPointer reply_; ///< \see baseReply()
234 HttpReplyPointer updatedReply_; ///< \see updatedReply()
235
236 mutable String storeId_; ///< StoreId for our entry (usually request URI)
237 mutable String logUri_; ///< URI used for logging (usually request URI)
238
239 DelayedAsyncCalls deferredReads;
240 };
241
242 /** global current memory removal policy */
243 extern RemovalPolicy *mem_policy;
244
245 #endif /* SQUID_MEMOBJECT_H */
246