]> git.ipfire.org Git - thirdparty/squid.git/blob - src/http.h
cb4ae061d4329371ab2139df8da55180ca2d0a3d
[thirdparty/squid.git] / src / http.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_SRC_HTTP_H
10 #define SQUID_SRC_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 #include <optional>
19
20 class FwdState;
21 class HttpHeader;
22 class String;
23
24 class HttpStateData : public Client
25 {
26 CBDATA_CHILD(HttpStateData);
27
28 public:
29
30 /// assists in making and relaying entry caching/sharing decision
31 class ReuseDecision
32 {
33 public:
34 enum Answers { reuseNot = 0, cachePositively, cacheNegatively, doNotCacheButShare };
35
36 ReuseDecision(const StoreEntry *e, const Http::StatusCode code);
37 /// stores the corresponding decision
38 Answers make(const Answers ans, const char *why);
39
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
44 };
45
46 HttpStateData(FwdState *);
47 ~HttpStateData() override;
48
49 static void httpBuildRequestHeader(HttpRequest * request,
50 StoreEntry * entry,
51 const AccessLogEntryPointer &al,
52 HttpHeader * hdr_out,
53 const Http::StateFlags &flags);
54
55 const Comm::ConnectionPointer & dataConnection() const override;
56 /* should be private */
57 bool sendRequest();
58 void processReplyHeader();
59 void processReplyBody() override;
60 void readReply(const CommIoCbParams &io);
61 void maybeReadVirginBody() override; // read response data from the network
62
63 // Checks whether the response is cacheable/shareable.
64 ReuseDecision::Answers reusableReply(ReuseDecision &decision);
65
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;
73
74 /// Upgrade header value sent to the origin server or cache peer.
75 String *upgradeHeaderOut = nullptr;
76
77 void processSurrogateControl(HttpReply *);
78
79 protected:
80 /* Client API */
81 void noteDelayAwareReadChance() override;
82
83 void processReply();
84 void proceedAfter1xx();
85 void handle1xx(HttpReply *msg);
86 void drop1xx(const char *reason);
87
88 private:
89 /**
90 * The current server connection.
91 * Maybe open, closed, or NULL.
92 * Use doneWithServer() to check if the server is available for use.
93 */
94 Comm::ConnectionPointer serverConnection;
95 AsyncCall::Pointer closeHandler;
96 enum ConnectionStatus {
97 INCOMPLETE_MSG,
98 COMPLETE_PERSISTENT_MSG,
99 COMPLETE_NONPERSISTENT_MSG
100 };
101 ConnectionStatus statusIfComplete() const;
102 ConnectionStatus persistentConnStatus() const;
103 void keepaliveAccounting(HttpReply *);
104 void checkDateSkew(HttpReply *);
105
106 bool continueAfterParsingHeader();
107 void truncateVirginBody();
108
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;
116
117 void abortTransaction(const char *reason) { abortAll(reason); } // abnormal termination
118
119 size_t calcReadBufferCapacityLimit() const;
120 std::optional<size_t> canBufferMoreReplyBytes() const;
121 size_t maybeMakeSpaceAvailable(size_t maxReadSize);
122
123 // consuming request body
124 virtual void handleMoreRequestBodyAvailable();
125 void handleRequestBodyProducerAborted() override;
126
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);
135 void sendComplete();
136 void httpStateConnClosed(const CommCloseCbParams &params);
137 void httpTimeout(const CommTimeoutCbParams &params);
138 void markPrematureReplyBodyEofFailure();
139
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;
145
146 /// Parser being used at present to parse the HTTP/ICY server response.
147 Http1::ResponseParserPointer hp;
148 Http1::TeChunkedParser *httpChunkDecoder = nullptr;
149
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;
154
155 /// Whether we received a Date header older than that of a matching
156 /// cached response.
157 bool sawDateGoBack = false;
158 };
159
160 std::ostream &operator <<(std::ostream &os, const HttpStateData::ReuseDecision &d);
161
162 int httpCachable(const HttpRequestMethod&);
163 void httpStart(FwdState *);
164 SBuf httpMakeVaryMark(HttpRequest * request, HttpReply const * reply);
165
166 #endif /* SQUID_SRC_HTTP_H */
167