]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/MemObject.h
Maintenance: automate header guards 2/3 (#1655)
[thirdparty/squid.git] / src / MemObject.h
index 5fc53b73bc1834ff03c0e8ccc7139cff32fcf113..4479e4029951afdf39877559e5415102f7ada106 100644 (file)
@@ -1,43 +1,25 @@
 /*
+ * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
  *
- * SQUID Web Proxy Cache          http://www.squid-cache.org/
- * ----------------------------------------------------------
- *
- *  Squid is the result of efforts by numerous individuals from
- *  the Internet community; see the CONTRIBUTORS file for full
- *  details.   Many organizations have provided support for Squid's
- *  development; see the SPONSORS file for full details.  Squid is
- *  Copyrighted (C) 2001 by the Regents of the University of
- *  California; see the COPYRIGHT file for full details.  Squid
- *  incorporates software developed and/or copyrighted by other
- *  sources; see the CREDITS file for full details.
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
- *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
-#ifndef SQUID_MEMOBJECT_H
-#define SQUID_MEMOBJECT_H
+#ifndef SQUID_SRC_MEMOBJECT_H
+#define SQUID_SRC_MEMOBJECT_H
 
-#include "CommRead.h"
+#include "base/DelayedAsyncCalls.h"
 #include "dlink.h"
-#include "HttpRequestMethod.h"
+#include "http/RequestMethod.h"
+#include "HttpReply.h"
 #include "RemovalPolicy.h"
+#include "sbuf/SBuf.h"
 #include "stmem.h"
+#include "store/forward.h"
 #include "StoreIOBuffer.h"
 #include "StoreIOState.h"
+#include "typedefs.h" //for IRCB
 
 #if USE_DELAY_POOLS
 #include "DelayId.h"
 typedef void STMCB (void *data, StoreIOBuffer wroteBuffer);
 
 class store_client;
-class HttpRequest;
-class HttpReply;
+class PeerSelector;
 
 class MemObject
 {
+    MEMPROXY_CLASS(MemObject);
 
 public:
     static size_t inUseCount();
-    MEMPROXY_CLASS(MemObject);
 
     void dump() const;
     MemObject();
     ~MemObject();
 
-    /// sets store ID, log URI, and request method; TODO: find a better name
+    /// Sets store ID, log URI, and request method (unless already set). Does
+    /// not clobber the method so that, say, a HEAD hit for a GET entry keeps
+    /// the GET method that matches the entry key. Same for the other parts of
+    /// the trio because the entry filling code may expect them to be constant.
+    /// XXX: Avoid this method. We plan to remove it and make the trio constant
+    /// after addressing the XXX in MemStore::get().
     void setUris(char const *aStoreId, char const *aLogUri, const HttpRequestMethod &aMethod);
 
     /// whether setUris() has been called
     bool hasUris() const;
 
     void write(const StoreIOBuffer &buf);
-    void unlinkRequest();
-    HttpReply const *getReply() const;
-    void replaceHttpReply(HttpReply *newrep);
+    void unlinkRequest() { request = nullptr; }
+
+    /// HTTP response before 304 (Not Modified) updates
+    /// starts "empty"; modified via replaceBaseReply() or adjustableBaseReply()
+    const HttpReply &baseReply() const { return *reply_; }
+
+    /// \returns nil -- if no 304 updates since replaceBaseReply()
+    /// \returns combination of baseReply() and 304 updates -- after updates
+    const HttpReplyPointer &updatedReply() const { return updatedReply_; }
+
+    /// \returns the updated-by-304(s) response (if it exists)
+    /// \returns baseReply() (otherwise)
+    const HttpReply &freshestReply() const {
+        if (updatedReply_)
+            return *updatedReply_;
+        else
+            return baseReply();
+    }
+
+    /// \returns writable base reply for parsing and other initial modifications
+    /// Base modifications can only be done when forming/loading the entry.
+    /// After that, use replaceBaseReply() to reset all of the replies.
+    HttpReply &adjustableBaseReply();
+
+    /// (re)sets base reply, usually just replacing the initial/empty object
+    /// also forgets the updated reply (if any)
+    void replaceBaseReply(const HttpReplyPointer &r);
+
+    /// (re)sets updated reply; \see updatedReply()
+    void updateReply(const HttpReply &r) { updatedReply_ = &r; }
+
+    /// reflects past Controller::updateOnNotModified(old, e304) calls:
+    /// for HTTP 304 entries: whether our entry was used as "e304"
+    /// for other entries: whether our entry was updated as "old"
+    bool appliedUpdates = false;
+
     void stat (MemBuf * mb) const;
+
+    /// The offset of the last memory-stored HTTP response byte plus one.
+    /// * HTTP response headers (if any) are stored at offset zero.
+    /// * HTTP response body byte[n] usually has offset (hdr_sz + n), where
+    ///   hdr_sz is the size of stored HTTP response headers (zero if none); and
+    ///   n is the corresponding byte offset in the whole resource body.
+    ///   However, some 206 (Partial Content) response bodies are stored (and
+    ///   retrieved) as regular 200 response bodies, disregarding offsets of
+    ///   their body parts. \sa HttpStateData::decideIfWeDoRanges().
     int64_t endOffset () const;
-    void markEndOfReplyHeaders(); ///< sets _reply->hdr_sz to endOffset()
+
+    /// sets baseReply().hdr_sz (i.e. written reply headers size) to endOffset()
+    void markEndOfReplyHeaders();
+
     /// negative if unknown; otherwise, expected object_sz, expected endOffset
     /// maximum, and stored reply headers+body size (all three are the same)
     int64_t expectedReplySize() const;
@@ -115,41 +146,55 @@ public:
 
     HttpRequestMethod method;
     mem_hdr data_hdr;
-    int64_t inmem_lo;
+    int64_t inmem_lo = 0;
     dlink_list clients;
 
-    /** \todo move into .cc or .cci */
     size_t clientCount() const {return nclients;}
 
     bool clientIsFirst(void *sc) const {return (clients.head && sc == clients.head->data);}
 
-    int nclients;
+    int nclients = 0;
 
     class SwapOut
     {
-
     public:
-        int64_t queue_offset; ///< number of bytes sent to SwapDir for writing
+        int64_t queue_offset = 0; ///< number of bytes sent to SwapDir for writing
         StoreIOState::Pointer sio;
 
         /// Decision states for StoreEntry::swapoutPossible() and related code.
         typedef enum { swNeedsCheck = 0, swImpossible = -1, swPossible = +1, swStarted } Decision;
-        Decision decision; ///< current decision state
+        Decision decision = swNeedsCheck; ///< current decision state
     };
 
     SwapOut swapout;
 
-    /// cache "I/O" direction and status
-    typedef enum { ioUndecided, ioWriting, ioReading, ioDone } Io;
+    /* TODO: Remove this change-minimizing hack */
+    using Io = Store::IoStatus;
+    static constexpr Io ioUndecided = Store::ioUndecided;
+    static constexpr Io ioReading = Store::ioReading;
+    static constexpr Io ioWriting = Store::ioWriting;
+    static constexpr Io ioDone = Store::ioDone;
 
     /// State of an entry with regards to the [shared] in-transit table.
     class XitTable
     {
     public:
-        XitTable(): index(-1), io(ioUndecided) {}
-
-        int32_t index; ///< entry position inside the in-transit table
-        Io io; ///< current I/O state
+        /// associate our StoreEntry with a Transients entry at the given index
+        void open(const int32_t anIndex, const Io anIo)
+        {
+            index = anIndex;
+            io = anIo;
+        }
+
+        /// stop associating our StoreEntry with a Transients entry
+        void close()
+        {
+            index = -1;
+            io = Store::ioDone;
+        }
+
+        int32_t index = -1; ///< entry position inside the in-transit table
+        Io io = ioUndecided; ///< current I/O state
     };
     XitTable xitTable; ///< current [shared] memory caching state for the entry
 
@@ -157,55 +202,46 @@ public:
     class MemCache
     {
     public:
-        MemCache(): index(-1), offset(0), io(ioUndecided) {}
-
-        int32_t index; ///< entry position inside the memory cache
-        int64_t offset; ///< bytes written/read to/from the memory cache so far
+        int32_t index = -1; ///< entry position inside the memory cache
+        int64_t offset = 0; ///< bytes written/read to/from the memory cache so far
 
-        Io io; ///< current I/O state
+        Io io = ioUndecided; ///< current I/O state
     };
     MemCache memCache; ///< current [shared] memory caching state for the entry
 
-    bool smpCollapsed; ///< whether this entry gets data from another worker
-
-    /* Read only - this reply must be preserved by store clients */
-    /* The original reply. possibly with updated metadata. */
-    HttpRequest *request;
+    HttpRequestPointer request;
 
     struct timeval start_ping;
     IRCB *ping_reply_callback;
-    void *ircb_data;
+    PeerSelector *ircb_data = nullptr;
 
-    struct {
-        STABH *callback;
-        void *data;
-    } abort;
+    /// used for notifying StoreEntry writers about 3rd-party initiated aborts
+    AsyncCallPointer abortCallback;
     RemovalPolicyNode repl;
-    int id;
-    int64_t object_sz;
-    size_t swap_hdr_sz;
+    int id = 0;
+    int64_t object_sz = -1;
+    size_t swap_hdr_sz = 0;
 #if URL_CHECKSUM_DEBUG
-
-    unsigned int chksum;
+    unsigned int chksum = 0;
 #endif
 
-    const char *vary_headers;
+    SBuf vary_headers;
 
-    void delayRead(DeferredRead const &);
+    void delayRead(const AsyncCallPointer &);
     void kickReads();
 
 private:
-    HttpReply *_reply;
+    HttpReplyPointer reply_; ///< \see baseReply()
+    HttpReplyPointer updatedReply_; ///< \see updatedReply()
 
-    mutable String storeId_; ///< StoreId for our entry (usually request URI)
-    mutable String logUri_;  ///< URI used for logging (usually request URI)
+    mutable SBuf storeId_; ///< StoreId for our entry (usually request URI)
+    mutable SBuf logUri_;  ///< URI used for logging (usually request URI)
 
-    DeferredReadManager deferredReads;
+    DelayedAsyncCalls deferredReads;
 };
 
-MEMPROXY_CLASS_INLINE(MemObject);
-
 /** global current memory removal policy */
 extern RemovalPolicy *mem_policy;
 
-#endif /* SQUID_MEMOBJECT_H */
+#endif /* SQUID_SRC_MEMOBJECT_H */
+