]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpReply.h
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / HttpReply.h
1 /*
2 * Copyright (C) 1996-2020 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();
33
34 virtual void reset();
35
36 /**
37 \retval true on success
38 \retval false and sets *error to zero when needs more data
39 \retval false and sets *error to a positive Http::StatusCode on error
40 */
41 virtual bool sanityCheckStartLine(const char *buf, const size_t hdr_len, Http::StatusCode *error);
42
43 /** \par public, readable; never update these or their .hdr equivalents directly */
44 time_t date;
45
46 time_t last_modified;
47
48 time_t expires;
49
50 String content_type;
51
52 HttpHdrSc *surrogate_control;
53
54 /// \returns parsed Content-Range for a 206 response and nil for others
55 const HttpHdrContRange *contentRange() const;
56
57 short int keep_alive;
58
59 /** \par public, writable, but use httpReply* interfaces when possible */
60 Http::StatusLine sline;
61
62 HttpBody body; /**< for small constant memory-resident text bodies only */
63
64 String protoPrefix; /**< e.g., "HTTP/" */
65
66 bool do_clean;
67
68 public:
69 virtual int httpMsgParseError();
70
71 virtual bool expectingBody(const HttpRequestMethod&, int64_t&) const;
72
73 virtual bool inheritProperties(const Http::Message *);
74
75 /// \returns nil (if no updates are necessary)
76 /// \returns a new reply combining this reply with 304 updates (otherwise)
77 Pointer recreateOnNotModified(const HttpReply &reply304) const;
78
79 /** set commonly used info with one call */
80 void setHeaders(Http::StatusCode status,
81 const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires);
82
83 /** \return a ready to use mem buffer with a packed reply */
84 MemBuf *pack() const;
85
86 /// construct and return an HTTP/200 (Connection Established) response
87 static HttpReplyPointer MakeConnectionEstablished();
88
89 /** construct a 304 reply and return it */
90 HttpReplyPointer make304() const;
91
92 void redirect(Http::StatusCode, const char *);
93
94 int64_t bodySize(const HttpRequestMethod&) const;
95
96 /** Checks whether received body exceeds known maximum size.
97 * Requires a prior call to calcMaxBodySize().
98 */
99 bool receivedBodyTooLarge(HttpRequest&, int64_t receivedBodySize);
100
101 /** Checks whether expected body exceeds known maximum size.
102 * Requires a prior call to calcMaxBodySize().
103 */
104 bool expectedBodyTooLarge(HttpRequest& request);
105
106 int validatorsMatch (HttpReply const *other) const;
107
108 /// adds status line and header to the given Packable
109 /// assumes that `p` can quickly process small additions
110 void packHeadersUsingFastPacker(Packable &p) const;
111 /// same as packHeadersUsingFastPacker() but assumes that `p` cannot quickly process small additions
112 void packHeadersUsingSlowPacker(Packable &p) const;
113
114 /** Clone this reply.
115 * Could be done as a copy-contructor but we do not want to accidently copy a HttpReply..
116 */
117 HttpReply *clone() const;
118
119 /// Remove Warnings with warn-date different from Date value
120 void removeStaleWarnings();
121
122 virtual void hdrCacheInit();
123
124 /// whether our Date header value is smaller than theirs
125 /// \returns false if any information is missing
126 bool olderThan(const HttpReply *them) const;
127
128 /// Some response status codes prohibit sending Content-Length (RFC 7230 section 3.3.2).
129 void removeIrrelevantContentLength();
130
131 virtual void configureContentLengthInterpreter(Http::ContentLengthInterpreter &);
132 /// parses reply header using Parser
133 bool parseHeader(Http1::Parser &hp);
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 String removeStaleWarningValues(const String &value);
158
159 mutable int64_t bodySizeMax; /**< cached result of calcMaxBodySize */
160
161 HttpHdrContRange *content_range; ///< parsed Content-Range; nil for non-206 responses!
162
163 protected:
164 virtual void packFirstLineInto(Packable * p, bool) const { sline.packInto(p); }
165
166 virtual bool parseFirstLine(const char *start, const char *end);
167 };
168
169 #endif /* SQUID_HTTPREPLY_H */
170