]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpReply.h
CI: Remove unnecessary test-functionality test wrappers (#1393)
[thirdparty/squid.git] / src / HttpReply.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_HTTPREPLY_H
10 #define SQUID_HTTPREPLY_H
11
12 #include "http/StatusLine.h"
13 #include "HttpBody.h"
14 #include "HttpRequest.h"
15
16 void httpReplyInitModule(void);
17
18 /* Sync changes here with HttpReply.cc */
19
20 class HttpHdrContRange;
21
22 class HttpHdrSc;
23
24 class HttpReply: public Http::Message
25 {
26 MEMPROXY_CLASS(HttpReply);
27
28 public:
29 typedef RefCount<HttpReply> Pointer;
30
31 HttpReply();
32 ~HttpReply() override;
33
34 void reset() override;
35
36 /* Http::Message API */
37 bool sanityCheckStartLine(const char *buf, const size_t hdr_len, Http::StatusCode *error) override;
38
39 /** \par public, readable; never update these or their .hdr equivalents directly */
40 time_t date;
41
42 time_t last_modified;
43
44 time_t expires;
45
46 String content_type;
47
48 HttpHdrSc *surrogate_control;
49
50 /// \returns parsed Content-Range for a 206 response and nil for others
51 const HttpHdrContRange *contentRange() const;
52
53 short int keep_alive;
54
55 /** \par public, writable, but use httpReply* interfaces when possible */
56 Http::StatusLine sline;
57
58 HttpBody body; /**< for small constant memory-resident text bodies only */
59
60 String protoPrefix; /**< e.g., "HTTP/" */
61
62 bool do_clean;
63
64 public:
65 int httpMsgParseError() override;
66
67 bool expectingBody(const HttpRequestMethod&, int64_t&) const override;
68
69 bool inheritProperties(const Http::Message *) override;
70
71 /// \returns nil (if no updates are necessary)
72 /// \returns a new reply combining this reply with 304 updates (otherwise)
73 Pointer recreateOnNotModified(const HttpReply &reply304) const;
74
75 /** set commonly used info with one call */
76 void setHeaders(Http::StatusCode status,
77 const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires);
78
79 /** \return a ready to use mem buffer with a packed reply */
80 MemBuf *pack() const;
81
82 /// construct and return an HTTP/200 (Connection Established) response
83 static HttpReplyPointer MakeConnectionEstablished();
84
85 /** construct a 304 reply and return it */
86 HttpReplyPointer make304() const;
87
88 void redirect(Http::StatusCode, const char *);
89
90 int64_t bodySize(const HttpRequestMethod&) const;
91
92 /** Checks whether received body exceeds known maximum size.
93 * Requires a prior call to calcMaxBodySize().
94 */
95 bool receivedBodyTooLarge(HttpRequest&, int64_t receivedBodySize);
96
97 /** Checks whether expected body exceeds known maximum size.
98 * Requires a prior call to calcMaxBodySize().
99 */
100 bool expectedBodyTooLarge(HttpRequest& request);
101
102 int validatorsMatch (HttpReply const *other) const;
103
104 /// adds status line and header to the given Packable
105 /// assumes that `p` can quickly process small additions
106 void packHeadersUsingFastPacker(Packable &p) const;
107 /// same as packHeadersUsingFastPacker() but assumes that `p` cannot quickly process small additions
108 void packHeadersUsingSlowPacker(Packable &p) const;
109
110 /** Clone this reply.
111 * Could be done as a copy-contructor but we do not want to accidentally copy a HttpReply..
112 */
113 HttpReply *clone() const override;
114
115 void hdrCacheInit() override;
116
117 /// whether our Date header value is smaller than theirs
118 /// \returns false if any information is missing
119 bool olderThan(const HttpReply *them) const;
120
121 /// Some response status codes prohibit sending Content-Length (RFC 7230 section 3.3.2).
122 void removeIrrelevantContentLength();
123
124 void configureContentLengthInterpreter(Http::ContentLengthInterpreter &) override;
125 /// parses reply header using Parser
126 bool parseHeader(Http1::Parser &hp);
127
128 /// Parses response status line and headers at the start of the given
129 /// NUL-terminated buffer of the given size. Respects reply_header_max_size.
130 /// Assures pstate becomes Http::Message::psParsed on (and only on) success.
131 /// \returns the number of bytes in a successfully parsed prefix (or zero)
132 /// \retval 0 implies that more data is needed to parse the response prefix
133 size_t parseTerminatedPrefix(const char *, size_t);
134
135 private:
136 /** initialize */
137 void init();
138
139 void clean();
140
141 void hdrCacheClean();
142
143 void packInto(MemBuf &) const;
144
145 /* ez-routines */
146 /** \return construct 304 reply and pack it into a MemBuf */
147 MemBuf *packed304Reply() const;
148
149 /* header manipulation */
150 time_t hdrExpirationTime();
151
152 /** Calculates and stores maximum body size if needed.
153 * Used by receivedBodyTooLarge() and expectedBodyTooLarge().
154 */
155 void calcMaxBodySize(HttpRequest& request) const;
156
157 mutable int64_t bodySizeMax; /**< cached result of calcMaxBodySize */
158
159 HttpHdrContRange *content_range; ///< parsed Content-Range; nil for non-206 responses!
160
161 protected:
162 void packFirstLineInto(Packable * p, bool) const override { sline.packInto(p); }
163
164 bool parseFirstLine(const char *start, const char *end) override;
165 };
166
167 #endif /* SQUID_HTTPREPLY_H */
168