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