]> git.ipfire.org Git - thirdparty/squid.git/blob - src/http.h
Maintenance: Consistent use of C++11 "override" specifier (#1224)
[thirdparty/squid.git] / src / http.h
1 /*
2 * Copyright (C) 1996-2022 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_HTTP_H
10 #define SQUID_HTTP_H
11
12 #include "clients/Client.h"
13 #include "comm.h"
14 #include "http/forward.h"
15 #include "http/StateFlags.h"
16 #include "sbuf/SBuf.h"
17
18 class FwdState;
19 class HttpHeader;
20 class String;
21
22 class HttpStateData : public Client
23 {
24 CBDATA_CHILD(HttpStateData);
25
26 public:
27
28 /// assists in making and relaying entry caching/sharing decision
29 class ReuseDecision
30 {
31 public:
32 enum Answers { reuseNot = 0, cachePositively, cacheNegatively, doNotCacheButShare };
33
34 ReuseDecision(const StoreEntry *e, const Http::StatusCode code);
35 /// stores the corresponding decision
36 Answers make(const Answers ans, const char *why);
37
38 Answers answer; ///< the decision id
39 const char *reason; ///< the decision reason
40 const StoreEntry *entry; ///< entry for debugging
41 const Http::StatusCode statusCode; ///< HTTP status for debugging
42 };
43
44 HttpStateData(FwdState *);
45 ~HttpStateData() override;
46
47 static void httpBuildRequestHeader(HttpRequest * request,
48 StoreEntry * entry,
49 const AccessLogEntryPointer &al,
50 HttpHeader * hdr_out,
51 const Http::StateFlags &flags);
52
53 const Comm::ConnectionPointer & dataConnection() const override;
54 /* should be private */
55 bool sendRequest();
56 void processReplyHeader();
57 void processReplyBody() override;
58 void readReply(const CommIoCbParams &io);
59 void maybeReadVirginBody() override; // read response data from the network
60
61 // Checks whether the response is cacheable/shareable.
62 ReuseDecision::Answers reusableReply(ReuseDecision &decision);
63
64 CachePeer *_peer = nullptr; /* CachePeer request made to */
65 int eof = 0; /* reached end-of-object? */
66 int lastChunk = 0; /* reached last chunk of a chunk-encoded reply */
67 Http::StateFlags flags;
68 SBuf inBuf; ///< I/O buffer for receiving server responses
69 bool ignoreCacheControl = false;
70 bool surrogateNoStore = false;
71
72 /// Upgrade header value sent to the origin server or cache peer.
73 String *upgradeHeaderOut = nullptr;
74
75 void processSurrogateControl(HttpReply *);
76
77 protected:
78 /* Client API */
79 void noteDelayAwareReadChance() override;
80
81 void processReply();
82 void proceedAfter1xx();
83 void handle1xx(HttpReply *msg);
84 void drop1xx(const char *reason);
85
86 private:
87 /**
88 * The current server connection.
89 * Maybe open, closed, or NULL.
90 * Use doneWithServer() to check if the server is available for use.
91 */
92 Comm::ConnectionPointer serverConnection;
93 AsyncCall::Pointer closeHandler;
94 enum ConnectionStatus {
95 INCOMPLETE_MSG,
96 COMPLETE_PERSISTENT_MSG,
97 COMPLETE_NONPERSISTENT_MSG
98 };
99 ConnectionStatus statusIfComplete() const;
100 ConnectionStatus persistentConnStatus() const;
101 void keepaliveAccounting(HttpReply *);
102 void checkDateSkew(HttpReply *);
103
104 bool continueAfterParsingHeader();
105 void truncateVirginBody();
106
107 void start() override;
108 void haveParsedReplyHeaders() override;
109 bool getMoreRequestBody(MemBuf &buf) override;
110 void closeServer() override; // end communication with the server
111 bool doneWithServer() const override; // did we end communication?
112 void abortAll(const char *reason) override; // abnormal termination
113 bool mayReadVirginReplyBody() const override;
114
115 void abortTransaction(const char *reason) { abortAll(reason); } // abnormal termination
116
117 /**
118 * determine if read buffer can have space made available
119 * for a read.
120 *
121 * \param grow whether to actually expand the buffer
122 *
123 * \return whether the buffer can be grown to provide space
124 * regardless of whether the grow actually happened.
125 */
126 bool maybeMakeSpaceAvailable(bool grow);
127
128 // consuming request body
129 virtual void handleMoreRequestBodyAvailable();
130 void handleRequestBodyProducerAborted() override;
131
132 void writeReplyBody();
133 bool decodeAndWriteReplyBody();
134 bool finishingBrokenPost();
135 bool finishingChunkedRequest();
136 void doneSendingRequestBody() override;
137 void requestBodyHandler(MemBuf &);
138 void sentRequestBody(const CommIoCbParams &io) override;
139 void wroteLast(const CommIoCbParams &io);
140 void sendComplete();
141 void httpStateConnClosed(const CommCloseCbParams &params);
142 void httpTimeout(const CommTimeoutCbParams &params);
143
144 mb_size_t buildRequestPrefix(MemBuf * mb);
145 void forwardUpgrade(HttpHeader&);
146 static bool decideIfWeDoRanges (HttpRequest * orig_request);
147 bool peerSupportsConnectionPinning() const;
148 const char *blockSwitchingProtocols(const HttpReply&) const;
149
150 /// Parser being used at present to parse the HTTP/ICY server response.
151 Http1::ResponseParserPointer hp;
152 Http1::TeChunkedParser *httpChunkDecoder = nullptr;
153
154 /// amount of message payload/body received so far.
155 int64_t payloadSeen = 0;
156 /// positive when we read more than we wanted
157 int64_t payloadTruncated = 0;
158
159 /// Whether we received a Date header older than that of a matching
160 /// cached response.
161 bool sawDateGoBack = false;
162 };
163
164 std::ostream &operator <<(std::ostream &os, const HttpStateData::ReuseDecision &d);
165
166 int httpCachable(const HttpRequestMethod&);
167 void httpStart(FwdState *);
168 SBuf httpMakeVaryMark(HttpRequest * request, HttpReply const * reply);
169
170 #endif /* SQUID_HTTP_H */
171