]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpMsg.cc
Moved some MIME header function prototypes to newly-created mime_header.h
[thirdparty/squid.git] / src / HttpMsg.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 74 HTTP Message
6 * AUTHOR: Alex Rousskov
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "squid.h"
37 #include "Debug.h"
38 #include "HttpHeaderTools.h"
39 #include "HttpMsg.h"
40 #include "MemBuf.h"
41 #include "mime_header.h"
42 #include "profiler/Profiler.h"
43 #include "protos.h"
44
45 HttpMsg::HttpMsg(http_hdr_owner_type owner): header(owner),
46 cache_control(NULL), hdr_sz(0), content_length(0), protocol(AnyP::PROTO_NONE),
47 pstate(psReadyToParseStartLine), lock_count(0)
48 {}
49
50 HttpMsg::~HttpMsg()
51 {
52 assert(lock_count == 0);
53 assert(!body_pipe);
54 }
55
56 HttpMsgParseState &operator++ (HttpMsgParseState &aState)
57 {
58 int tmp = (int)aState;
59 aState = (HttpMsgParseState)(++tmp);
60 return aState;
61 }
62
63 /* find end of headers */
64 int
65 httpMsgIsolateHeaders(const char **parse_start, int l, const char **blk_start, const char **blk_end)
66 {
67 /*
68 * parse_start points to the first line of HTTP message *headers*,
69 * not including the request or status lines
70 */
71 size_t end = headersEnd(*parse_start, l);
72 int nnl;
73
74 if (end) {
75 *blk_start = *parse_start;
76 *blk_end = *parse_start + end - 1;
77 /*
78 * leave blk_end pointing to the first character after the
79 * first newline which terminates the headers
80 */
81 assert(**blk_end == '\n');
82
83 while (*(*blk_end - 1) == '\r')
84 --(*blk_end);
85
86 assert(*(*blk_end - 1) == '\n');
87
88 *parse_start += end;
89
90 return 1;
91 }
92
93 /*
94 * If we didn't find the end of headers, and parse_start does
95 * NOT point to a CR or NL character, then return failure
96 */
97 if (**parse_start != '\r' && **parse_start != '\n')
98 return 0; /* failure */
99
100 /*
101 * If we didn't find the end of headers, and parse_start does point
102 * to an empty line, then we have empty headers. Skip all CR and
103 * NL characters up to the first NL. Leave parse_start pointing at
104 * the first character after the first NL.
105 */
106 *blk_start = *parse_start;
107
108 *blk_end = *blk_start;
109
110 for (nnl = 0; nnl == 0; ++(*parse_start)) {
111 if (**parse_start == '\r')
112 (void) 0;
113 else if (**parse_start == '\n')
114 ++nnl;
115 else
116 break;
117 }
118
119 return 1;
120 }
121
122 /* find first CRLF */
123 static int
124 httpMsgIsolateStart(const char **parse_start, const char **blk_start, const char **blk_end)
125 {
126 int slen = strcspn(*parse_start, "\r\n");
127
128 if (!(*parse_start)[slen]) /* no CRLF found */
129 return 0;
130
131 *blk_start = *parse_start;
132
133 *blk_end = *blk_start + slen;
134
135 while (**blk_end == '\r') /* CR */
136 ++(*blk_end);
137
138 if (**blk_end == '\n') /* LF */
139 ++(*blk_end);
140
141 *parse_start = *blk_end;
142
143 return 1;
144 }
145
146 // negative return is the negated HTTP_ error code
147 // zero return means need more data
148 // positive return is the size of parsed headers
149 bool HttpMsg::parse(MemBuf *buf, bool eof, http_status *error)
150 {
151 assert(error);
152 *error = HTTP_STATUS_NONE;
153
154 // httpMsgParseStep() and debugging require 0-termination, unfortunately
155 buf->terminate(); // does not affect content size
156
157 // find the end of headers
158 const size_t hdr_len = headersEnd(buf->content(), buf->contentSize());
159
160 // sanity check the start line to see if this is in fact an HTTP message
161 if (!sanityCheckStartLine(buf, hdr_len, error)) {
162 // NP: sanityCheck sets *error and sends debug warnings on syntax errors.
163 // if we have seen the connection close, this is an error too
164 if (eof && *error==HTTP_STATUS_NONE)
165 *error = HTTP_INVALID_HEADER;
166
167 return false;
168 }
169
170 // TODO: move to httpReplyParseStep()
171 if (hdr_len > Config.maxReplyHeaderSize || (hdr_len <= 0 && (size_t)buf->contentSize() > Config.maxReplyHeaderSize)) {
172 debugs(58, DBG_IMPORTANT, "HttpMsg::parse: Too large reply header (" << hdr_len << " > " << Config.maxReplyHeaderSize);
173 *error = HTTP_HEADER_TOO_LARGE;
174 return false;
175 }
176
177 if (hdr_len <= 0) {
178 debugs(58, 3, "HttpMsg::parse: failed to find end of headers (eof: " << eof << ") in '" << buf->content() << "'");
179
180 if (eof) // iff we have seen the end, this is an error
181 *error = HTTP_INVALID_HEADER;
182
183 return false;
184 }
185
186 const int res = httpMsgParseStep(buf->content(), buf->contentSize(), eof);
187
188 if (res < 0) { // error
189 debugs(58, 3, "HttpMsg::parse: cannot parse isolated headers in '" << buf->content() << "'");
190 *error = HTTP_INVALID_HEADER;
191 return false;
192 }
193
194 if (res == 0) {
195 debugs(58, 2, "HttpMsg::parse: strange, need more data near '" << buf->content() << "'");
196 *error = HTTP_INVALID_HEADER;
197 return false; // but this should not happen due to headersEnd() above
198 }
199
200 assert(res > 0);
201 debugs(58, 9, "HttpMsg::parse success (" << hdr_len << " bytes) near '" << buf->content() << "'");
202
203 if (hdr_sz != (int)hdr_len) {
204 debugs(58, DBG_IMPORTANT, "internal HttpMsg::parse vs. headersEnd error: " <<
205 hdr_sz << " != " << hdr_len);
206 hdr_sz = (int)hdr_len; // because old http.cc code used hdr_len
207 }
208
209 return true;
210 }
211
212 /*
213 * parseCharBuf() takes character buffer of HTTP headers (buf),
214 * which may not be NULL-terminated, and fills in an HttpMsg
215 * structure. The parameter 'end' specifies the offset to
216 * the end of the reply headers. The caller may know where the
217 * end is, but is unable to NULL-terminate the buffer. This function
218 * returns true on success.
219 */
220 bool
221 HttpMsg::parseCharBuf(const char *buf, ssize_t end)
222 {
223 MemBuf mb;
224 int success;
225 /* reset current state, because we are not used in incremental fashion */
226 reset();
227 mb.init();
228 mb.append(buf, end);
229 mb.terminate();
230 success = httpMsgParseStep(mb.buf, mb.size, 0);
231 mb.clean();
232 return success == 1;
233 }
234
235 /*
236 * parses a 0-terminating buffer into HttpMsg.
237 * Returns:
238 * 1 -- success
239 * 0 -- need more data (partial parse)
240 * -1 -- parse error
241 */
242 int
243 HttpMsg::httpMsgParseStep(const char *buf, int len, int atEnd)
244 {
245 const char *parse_start = buf;
246 int parse_len = len;
247 const char *blk_start, *blk_end;
248 const char **parse_end_ptr = &blk_end;
249 assert(parse_start);
250 assert(pstate < psParsed);
251
252 *parse_end_ptr = parse_start;
253
254 PROF_start(HttpMsg_httpMsgParseStep);
255
256 if (pstate == psReadyToParseStartLine) {
257 if (!httpMsgIsolateStart(&parse_start, &blk_start, &blk_end)) {
258 PROF_stop(HttpMsg_httpMsgParseStep);
259 return 0;
260 }
261
262 if (!parseFirstLine(blk_start, blk_end)) {
263 PROF_stop(HttpMsg_httpMsgParseStep);
264 return httpMsgParseError();
265 }
266
267 *parse_end_ptr = parse_start;
268
269 hdr_sz = *parse_end_ptr - buf;
270 parse_len = parse_len - hdr_sz;
271
272 ++pstate;
273 }
274
275 /*
276 * XXX This code uses parse_start; but if we're incrementally parsing then
277 * this code might not actually be given parse_start at the right spot (just
278 * after headers.) Grr.
279 */
280 if (pstate == psReadyToParseHeaders) {
281 if (!httpMsgIsolateHeaders(&parse_start, parse_len, &blk_start, &blk_end)) {
282 if (atEnd) {
283 blk_start = parse_start, blk_end = blk_start + strlen(blk_start);
284 } else {
285 PROF_stop(HttpMsg_httpMsgParseStep);
286 return 0;
287 }
288 }
289
290 if (!header.parse(blk_start, blk_end)) {
291 PROF_stop(HttpMsg_httpMsgParseStep);
292 return httpMsgParseError();
293 }
294
295 hdrCacheInit();
296
297 *parse_end_ptr = parse_start;
298
299 hdr_sz = *parse_end_ptr - buf;
300
301 ++pstate;
302 }
303
304 PROF_stop(HttpMsg_httpMsgParseStep);
305 return 1;
306 }
307
308 /* handy: resets and returns -1 */
309 int
310 HttpMsg::httpMsgParseError()
311 {
312 reset();
313 return -1;
314 }
315
316 void
317 HttpMsg::setContentLength(int64_t clen)
318 {
319 header.delById(HDR_CONTENT_LENGTH); // if any
320 header.putInt64(HDR_CONTENT_LENGTH, clen);
321 content_length = clen;
322 }
323
324 bool
325 HttpMsg::persistent() const
326 {
327 if (http_ver > HttpVersion(1, 0)) {
328 /*
329 * for modern versions of HTTP: persistent unless there is
330 * a "Connection: close" header.
331 */
332 return !httpHeaderHasConnDir(&header, "close");
333 } else {
334 /* for old versions of HTTP: persistent if has "keep-alive" */
335 return httpHeaderHasConnDir(&header, "keep-alive");
336 }
337 }
338
339 void HttpMsg::packInto(Packer *p, bool full_uri) const
340 {
341 packFirstLineInto(p, full_uri);
342 header.packInto(p);
343 packerAppend(p, "\r\n", 2);
344 }
345
346 void HttpMsg::hdrCacheInit()
347 {
348 content_length = header.getInt64(HDR_CONTENT_LENGTH);
349 assert(NULL == cache_control);
350 cache_control = header.getCc();
351 }
352
353 /*
354 * useful for debugging
355 */
356 void HttpMsg::firstLineBuf(MemBuf& mb)
357 {
358 Packer p;
359 packerToMemInit(&p, &mb);
360 packFirstLineInto(&p, true);
361 packerClean(&p);
362 }
363
364 // use HTTPMSGLOCK() instead of calling this directly
365 HttpMsg *
366 HttpMsg::_lock()
367 {
368 ++lock_count;
369 return this;
370 }
371
372 // use HTTPMSGUNLOCK() instead of calling this directly
373 void
374 HttpMsg::_unlock()
375 {
376 assert(lock_count > 0);
377 --lock_count;
378
379 if (0 == lock_count)
380 delete this;
381 }