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