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