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