2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_SRC_HTTP_H
10 #define SQUID_SRC_HTTP_H
12 #include "clients/Client.h"
14 #include "http/forward.h"
15 #include "http/StateFlags.h"
16 #include "sbuf/SBuf.h"
24 class HttpStateData
: public Client
26 CBDATA_CHILD(HttpStateData
);
30 /// assists in making and relaying entry caching/sharing decision
34 enum Answers
{ reuseNot
= 0, cachePositively
, cacheNegatively
, doNotCacheButShare
};
36 ReuseDecision(const StoreEntry
*e
, const Http::StatusCode code
);
37 /// stores the corresponding decision
38 Answers
make(const Answers ans
, const char *why
);
40 Answers answer
; ///< the decision id
41 const char *reason
; ///< the decision reason
42 const StoreEntry
*entry
; ///< entry for debugging
43 const Http::StatusCode statusCode
; ///< HTTP status for debugging
46 HttpStateData(FwdState
*);
47 ~HttpStateData() override
;
49 static void httpBuildRequestHeader(HttpRequest
* request
,
51 const AccessLogEntryPointer
&al
,
53 const Http::StateFlags
&flags
);
55 const Comm::ConnectionPointer
& dataConnection() const override
;
56 /* should be private */
58 void processReplyHeader();
59 void processReplyBody() override
;
60 void readReply(const CommIoCbParams
&io
);
61 void maybeReadVirginBody() override
; // read response data from the network
63 // Checks whether the response is cacheable/shareable.
64 ReuseDecision::Answers
reusableReply(ReuseDecision
&decision
);
66 CachePeer
*_peer
= nullptr; /* CachePeer request made to */
67 int eof
= 0; /* reached end-of-object? */
68 int lastChunk
= 0; /* reached last chunk of a chunk-encoded reply */
69 Http::StateFlags flags
;
70 SBuf inBuf
; ///< I/O buffer for receiving server responses
71 bool ignoreCacheControl
= false;
72 bool surrogateNoStore
= false;
74 /// Upgrade header value sent to the origin server or cache peer.
75 String
*upgradeHeaderOut
= nullptr;
77 void processSurrogateControl(HttpReply
*);
81 void noteDelayAwareReadChance() override
;
84 void proceedAfter1xx();
85 void handle1xx(const HttpReplyPointer
&);
86 void drop1xx(const char *reason
);
90 * The current server connection.
91 * Maybe open, closed, or NULL.
92 * Use doneWithServer() to check if the server is available for use.
94 Comm::ConnectionPointer serverConnection
;
95 AsyncCall::Pointer closeHandler
;
96 enum ConnectionStatus
{
98 COMPLETE_PERSISTENT_MSG
,
99 COMPLETE_NONPERSISTENT_MSG
101 ConnectionStatus
statusIfComplete() const;
102 ConnectionStatus
persistentConnStatus() const;
103 void keepaliveAccounting(HttpReply
*);
104 void checkDateSkew(HttpReply
*);
106 bool continueAfterParsingHeader();
107 void truncateVirginBody();
109 void start() override
;
110 void haveParsedReplyHeaders() override
;
111 bool getMoreRequestBody(MemBuf
&buf
) override
;
112 void closeServer() override
; // end communication with the server
113 bool doneWithServer() const override
; // did we end communication?
114 void abortAll(const char *reason
) override
; // abnormal termination
115 bool mayReadVirginReplyBody() const override
;
117 void abortTransaction(const char *reason
) { abortAll(reason
); } // abnormal termination
119 size_t calcReadBufferCapacityLimit() const;
120 std::optional
<size_t> canBufferMoreReplyBytes() const;
121 size_t maybeMakeSpaceAvailable(size_t maxReadSize
);
123 // consuming request body
124 virtual void handleMoreRequestBodyAvailable();
125 void handleRequestBodyProducerAborted() override
;
127 void writeReplyBody();
128 bool decodeAndWriteReplyBody();
129 bool finishingBrokenPost();
130 bool finishingChunkedRequest();
131 void doneSendingRequestBody() override
;
132 void requestBodyHandler(MemBuf
&);
133 void sentRequestBody(const CommIoCbParams
&io
) override
;
134 void wroteLast(const CommIoCbParams
&io
);
136 void httpStateConnClosed(const CommCloseCbParams
¶ms
);
137 void httpTimeout(const CommTimeoutCbParams
¶ms
);
138 void markPrematureReplyBodyEofFailure();
140 mb_size_t
buildRequestPrefix(MemBuf
* mb
);
141 void forwardUpgrade(HttpHeader
&);
142 static bool decideIfWeDoRanges (HttpRequest
* orig_request
);
143 bool peerSupportsConnectionPinning() const;
144 const char *blockSwitchingProtocols(const HttpReply
&) const;
146 /// Parser being used at present to parse the HTTP/ICY server response.
147 Http1::ResponseParserPointer hp
;
148 Http1::TeChunkedParser
*httpChunkDecoder
= nullptr;
150 /// amount of message payload/body received so far.
151 int64_t payloadSeen
= 0;
152 /// positive when we read more than we wanted
153 int64_t payloadTruncated
= 0;
155 /// whether we are waiting for our Comm::Read() handler to be called
156 bool waitingForCommRead
= false;
158 /// Whether we received a Date header older than that of a matching
160 bool sawDateGoBack
= false;
163 std::ostream
&operator <<(std::ostream
&os
, const HttpStateData::ReuseDecision
&d
);
165 int httpCachable(const HttpRequestMethod
&);
166 void httpStart(FwdState
*);
167 SBuf
httpMakeVaryMark(HttpRequest
* request
, HttpReply
const * reply
);
169 #endif /* SQUID_SRC_HTTP_H */