]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 1996-2017 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 | /* DEBUG: section 74 HTTP Message */ | |
10 | ||
11 | #include "squid.h" | |
12 | #include "Debug.h" | |
13 | #include "http/one/Parser.h" | |
14 | #include "HttpHdrCc.h" | |
15 | #include "HttpHeaderTools.h" | |
16 | #include "HttpMsg.h" | |
17 | #include "MemBuf.h" | |
18 | #include "mime_header.h" | |
19 | #include "profiler/Profiler.h" | |
20 | #include "SquidConfig.h" | |
21 | ||
22 | HttpMsg::HttpMsg(http_hdr_owner_type owner): | |
23 | http_ver(Http::ProtocolVersion()), | |
24 | header(owner), | |
25 | cache_control(NULL), | |
26 | hdr_sz(0), | |
27 | content_length(0), | |
28 | pstate(psReadyToParseStartLine), | |
29 | sources(0) | |
30 | {} | |
31 | ||
32 | HttpMsg::~HttpMsg() | |
33 | { | |
34 | assert(!body_pipe); | |
35 | } | |
36 | ||
37 | void | |
38 | HttpMsg::putCc(const HttpHdrCc *otherCc) | |
39 | { | |
40 | // get rid of the old CC, if any | |
41 | if (cache_control) { | |
42 | delete cache_control; | |
43 | cache_control = nullptr; | |
44 | if (!otherCc) | |
45 | header.delById(Http::HdrType::CACHE_CONTROL); | |
46 | // else it will be deleted inside putCc() below | |
47 | } | |
48 | ||
49 | // add new CC, if any | |
50 | if (otherCc) { | |
51 | cache_control = new HttpHdrCc(*otherCc); | |
52 | header.putCc(cache_control); | |
53 | } | |
54 | } | |
55 | ||
56 | HttpMsgParseState &operator++ (HttpMsgParseState &aState) | |
57 | { | |
58 | int tmp = (int)aState; | |
59 | aState = (HttpMsgParseState)(++tmp); | |
60 | return aState; | |
61 | } | |
62 | ||
63 | /* find first CRLF */ | |
64 | static int | |
65 | httpMsgIsolateStart(const char **parse_start, const char **blk_start, const char **blk_end) | |
66 | { | |
67 | int slen = strcspn(*parse_start, "\r\n"); | |
68 | ||
69 | if (!(*parse_start)[slen]) /* no CRLF found */ | |
70 | return 0; | |
71 | ||
72 | *blk_start = *parse_start; | |
73 | ||
74 | *blk_end = *blk_start + slen; | |
75 | ||
76 | while (**blk_end == '\r') /* CR */ | |
77 | ++(*blk_end); | |
78 | ||
79 | if (**blk_end == '\n') /* LF */ | |
80 | ++(*blk_end); | |
81 | ||
82 | *parse_start = *blk_end; | |
83 | ||
84 | return 1; | |
85 | } | |
86 | ||
87 | // negative return is the negated Http::StatusCode error code | |
88 | // zero return means need more data | |
89 | // positive return is the size of parsed headers | |
90 | bool | |
91 | HttpMsg::parse(const char *buf, const size_t sz, bool eof, Http::StatusCode *error) | |
92 | { | |
93 | assert(error); | |
94 | *error = Http::scNone; | |
95 | ||
96 | // find the end of headers | |
97 | const size_t hdr_len = headersEnd(buf, sz); | |
98 | ||
99 | // sanity check the start line to see if this is in fact an HTTP message | |
100 | if (!sanityCheckStartLine(buf, hdr_len, error)) { | |
101 | // NP: sanityCheck sets *error and sends debug warnings on syntax errors. | |
102 | // if we have seen the connection close, this is an error too | |
103 | if (eof && *error == Http::scNone) | |
104 | *error = Http::scInvalidHeader; | |
105 | ||
106 | return false; | |
107 | } | |
108 | ||
109 | if (hdr_len > Config.maxReplyHeaderSize || (hdr_len <= 0 && sz > Config.maxReplyHeaderSize)) { | |
110 | debugs(58, DBG_IMPORTANT, "HttpMsg::parse: Too large reply header (" << hdr_len << " > " << Config.maxReplyHeaderSize); | |
111 | *error = Http::scHeaderTooLarge; | |
112 | return false; | |
113 | } | |
114 | ||
115 | if (hdr_len <= 0) { | |
116 | debugs(58, 3, "HttpMsg::parse: failed to find end of headers (eof: " << eof << ") in '" << buf << "'"); | |
117 | ||
118 | if (eof) // iff we have seen the end, this is an error | |
119 | *error = Http::scInvalidHeader; | |
120 | ||
121 | return false; | |
122 | } | |
123 | ||
124 | const int res = httpMsgParseStep(buf, sz, eof); | |
125 | ||
126 | if (res < 0) { // error | |
127 | debugs(58, 3, "HttpMsg::parse: cannot parse isolated headers in '" << buf << "'"); | |
128 | *error = Http::scInvalidHeader; | |
129 | return false; | |
130 | } | |
131 | ||
132 | if (res == 0) { | |
133 | debugs(58, 2, "HttpMsg::parse: strange, need more data near '" << buf << "'"); | |
134 | *error = Http::scInvalidHeader; | |
135 | return false; // but this should not happen due to headersEnd() above | |
136 | } | |
137 | ||
138 | assert(res > 0); | |
139 | debugs(58, 9, "HttpMsg::parse success (" << hdr_len << " bytes) near '" << buf << "'"); | |
140 | ||
141 | if (hdr_sz != (int)hdr_len) { | |
142 | debugs(58, DBG_IMPORTANT, "internal HttpMsg::parse vs. headersEnd error: " << | |
143 | hdr_sz << " != " << hdr_len); | |
144 | hdr_sz = (int)hdr_len; // because old http.cc code used hdr_len | |
145 | } | |
146 | ||
147 | return true; | |
148 | } | |
149 | ||
150 | /* | |
151 | * parseCharBuf() takes character buffer of HTTP headers (buf), | |
152 | * which may not be NULL-terminated, and fills in an HttpMsg | |
153 | * structure. The parameter 'end' specifies the offset to | |
154 | * the end of the reply headers. The caller may know where the | |
155 | * end is, but is unable to NULL-terminate the buffer. This function | |
156 | * returns true on success. | |
157 | */ | |
158 | bool | |
159 | HttpMsg::parseCharBuf(const char *buf, ssize_t end) | |
160 | { | |
161 | MemBuf mb; | |
162 | int success; | |
163 | /* reset current state, because we are not used in incremental fashion */ | |
164 | reset(); | |
165 | mb.init(); | |
166 | mb.append(buf, end); | |
167 | mb.terminate(); | |
168 | success = httpMsgParseStep(mb.buf, mb.size, 0); | |
169 | mb.clean(); | |
170 | return success == 1; | |
171 | } | |
172 | ||
173 | /* | |
174 | * parses a 0-terminating buffer into HttpMsg. | |
175 | * Returns: | |
176 | * 1 -- success | |
177 | * 0 -- need more data (partial parse) | |
178 | * -1 -- parse error | |
179 | */ | |
180 | int | |
181 | HttpMsg::httpMsgParseStep(const char *buf, int len, int atEnd) | |
182 | { | |
183 | const char *parse_start = buf; | |
184 | int parse_len = len; | |
185 | const char *blk_start, *blk_end; | |
186 | const char **parse_end_ptr = &blk_end; | |
187 | assert(parse_start); | |
188 | assert(pstate < psParsed); | |
189 | ||
190 | *parse_end_ptr = parse_start; | |
191 | ||
192 | PROF_start(HttpMsg_httpMsgParseStep); | |
193 | ||
194 | if (pstate == psReadyToParseStartLine) { | |
195 | if (!httpMsgIsolateStart(&parse_start, &blk_start, &blk_end)) { | |
196 | PROF_stop(HttpMsg_httpMsgParseStep); | |
197 | return 0; | |
198 | } | |
199 | ||
200 | if (!parseFirstLine(blk_start, blk_end)) { | |
201 | PROF_stop(HttpMsg_httpMsgParseStep); | |
202 | return httpMsgParseError(); | |
203 | } | |
204 | ||
205 | *parse_end_ptr = parse_start; | |
206 | ||
207 | hdr_sz = *parse_end_ptr - buf; | |
208 | parse_len = parse_len - hdr_sz; | |
209 | ||
210 | ++pstate; | |
211 | } | |
212 | ||
213 | /* | |
214 | * XXX This code uses parse_start; but if we're incrementally parsing then | |
215 | * this code might not actually be given parse_start at the right spot (just | |
216 | * after headers.) Grr. | |
217 | */ | |
218 | if (pstate == psReadyToParseHeaders) { | |
219 | size_t hsize = 0; | |
220 | const int parsed = header.parse(parse_start, parse_len, atEnd, hsize); | |
221 | if (parsed <= 0) { | |
222 | PROF_stop(HttpMsg_httpMsgParseStep); | |
223 | return !parsed ? 0 : httpMsgParseError(); | |
224 | } | |
225 | hdr_sz += hsize; | |
226 | hdrCacheInit(); | |
227 | ++pstate; | |
228 | } | |
229 | ||
230 | PROF_stop(HttpMsg_httpMsgParseStep); | |
231 | return 1; | |
232 | } | |
233 | ||
234 | bool | |
235 | HttpMsg::parseHeader(Http1::Parser &hp) | |
236 | { | |
237 | // HTTP/1 message contains "zero or more header fields" | |
238 | // zero does not need parsing | |
239 | // XXX: c_str() reallocates. performance regression. | |
240 | if (hp.headerBlockSize() && !header.parse(hp.mimeHeader().c_str(), hp.headerBlockSize())) { | |
241 | pstate = psError; | |
242 | return false; | |
243 | } | |
244 | ||
245 | // XXX: we are just parsing HTTP headers, not the whole message prefix here | |
246 | hdr_sz = hp.messageHeaderSize(); | |
247 | pstate = psParsed; | |
248 | hdrCacheInit(); | |
249 | return true; | |
250 | } | |
251 | ||
252 | /* handy: resets and returns -1 */ | |
253 | int | |
254 | HttpMsg::httpMsgParseError() | |
255 | { | |
256 | reset(); | |
257 | return -1; | |
258 | } | |
259 | ||
260 | void | |
261 | HttpMsg::setContentLength(int64_t clen) | |
262 | { | |
263 | header.delById(Http::HdrType::CONTENT_LENGTH); // if any | |
264 | header.putInt64(Http::HdrType::CONTENT_LENGTH, clen); | |
265 | content_length = clen; | |
266 | } | |
267 | ||
268 | bool | |
269 | HttpMsg::persistent() const | |
270 | { | |
271 | if (http_ver > Http::ProtocolVersion(1,0)) { | |
272 | /* | |
273 | * for modern versions of HTTP: persistent unless there is | |
274 | * a "Connection: close" header. | |
275 | */ | |
276 | return !httpHeaderHasConnDir(&header, "close"); | |
277 | } else { | |
278 | /* for old versions of HTTP: persistent if has "keep-alive" */ | |
279 | return httpHeaderHasConnDir(&header, "keep-alive"); | |
280 | } | |
281 | } | |
282 | ||
283 | void HttpMsg::packInto(Packable *p, bool full_uri) const | |
284 | { | |
285 | packFirstLineInto(p, full_uri); | |
286 | header.packInto(p); | |
287 | p->append("\r\n", 2); | |
288 | } | |
289 | ||
290 | void HttpMsg::hdrCacheInit() | |
291 | { | |
292 | content_length = header.getInt64(Http::HdrType::CONTENT_LENGTH); | |
293 | assert(NULL == cache_control); | |
294 | cache_control = header.getCc(); | |
295 | } | |
296 | ||
297 | /* | |
298 | * useful for debugging | |
299 | */ | |
300 | void HttpMsg::firstLineBuf(MemBuf& mb) | |
301 | { | |
302 | packFirstLineInto(&mb, true); | |
303 | } | |
304 |