2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 74 HTTP Message */
13 #include "HttpHeaderTools.h"
16 #include "mime_header.h"
17 #include "profiler/Profiler.h"
18 #include "SquidConfig.h"
20 HttpMsg::HttpMsg(http_hdr_owner_type owner
): header(owner
),
21 cache_control(NULL
), hdr_sz(0), content_length(0),
22 pstate(psReadyToParseStartLine
)
30 HttpMsgParseState
&operator++ (HttpMsgParseState
&aState
)
32 int tmp
= (int)aState
;
33 aState
= (HttpMsgParseState
)(++tmp
);
37 /* find end of headers */
39 httpMsgIsolateHeaders(const char **parse_start
, int l
, const char **blk_start
, const char **blk_end
)
42 * parse_start points to the first line of HTTP message *headers*,
43 * not including the request or status lines
45 size_t end
= headersEnd(*parse_start
, l
);
49 *blk_start
= *parse_start
;
50 *blk_end
= *parse_start
+ end
- 1;
52 * leave blk_end pointing to the first character after the
53 * first newline which terminates the headers
55 assert(**blk_end
== '\n');
57 while (*(*blk_end
- 1) == '\r')
60 assert(*(*blk_end
- 1) == '\n');
68 * If we didn't find the end of headers, and parse_start does
69 * NOT point to a CR or NL character, then return failure
71 if (**parse_start
!= '\r' && **parse_start
!= '\n')
72 return 0; /* failure */
75 * If we didn't find the end of headers, and parse_start does point
76 * to an empty line, then we have empty headers. Skip all CR and
77 * NL characters up to the first NL. Leave parse_start pointing at
78 * the first character after the first NL.
80 *blk_start
= *parse_start
;
82 *blk_end
= *blk_start
;
84 for (nnl
= 0; nnl
== 0; ++(*parse_start
)) {
85 if (**parse_start
== '\r')
87 else if (**parse_start
== '\n')
98 httpMsgIsolateStart(const char **parse_start
, const char **blk_start
, const char **blk_end
)
100 int slen
= strcspn(*parse_start
, "\r\n");
102 if (!(*parse_start
)[slen
]) /* no CRLF found */
105 *blk_start
= *parse_start
;
107 *blk_end
= *blk_start
+ slen
;
109 while (**blk_end
== '\r') /* CR */
112 if (**blk_end
== '\n') /* LF */
115 *parse_start
= *blk_end
;
120 // negative return is the negated Http::StatusCode error code
121 // zero return means need more data
122 // positive return is the size of parsed headers
124 HttpMsg::parse(MemBuf
*buf
, bool eof
, Http::StatusCode
*error
)
127 *error
= Http::scNone
;
129 // httpMsgParseStep() and debugging require 0-termination, unfortunately
130 buf
->terminate(); // does not affect content size
132 // find the end of headers
133 const size_t hdr_len
= headersEnd(buf
->content(), buf
->contentSize());
135 // sanity check the start line to see if this is in fact an HTTP message
136 if (!sanityCheckStartLine(buf
, hdr_len
, error
)) {
137 // NP: sanityCheck sets *error and sends debug warnings on syntax errors.
138 // if we have seen the connection close, this is an error too
139 if (eof
&& *error
== Http::scNone
)
140 *error
= Http::scInvalidHeader
;
145 // TODO: move to httpReplyParseStep()
146 if (hdr_len
> Config
.maxReplyHeaderSize
|| (hdr_len
<= 0 && (size_t)buf
->contentSize() > Config
.maxReplyHeaderSize
)) {
147 debugs(58, DBG_IMPORTANT
, "HttpMsg::parse: Too large reply header (" << hdr_len
<< " > " << Config
.maxReplyHeaderSize
);
148 *error
= Http::scHeaderTooLarge
;
153 debugs(58, 3, "HttpMsg::parse: failed to find end of headers (eof: " << eof
<< ") in '" << buf
->content() << "'");
155 if (eof
) // iff we have seen the end, this is an error
156 *error
= Http::scInvalidHeader
;
161 const int res
= httpMsgParseStep(buf
->content(), buf
->contentSize(), eof
);
163 if (res
< 0) { // error
164 debugs(58, 3, "HttpMsg::parse: cannot parse isolated headers in '" << buf
->content() << "'");
165 *error
= Http::scInvalidHeader
;
170 debugs(58, 2, "HttpMsg::parse: strange, need more data near '" << buf
->content() << "'");
171 *error
= Http::scInvalidHeader
;
172 return false; // but this should not happen due to headersEnd() above
176 debugs(58, 9, "HttpMsg::parse success (" << hdr_len
<< " bytes) near '" << buf
->content() << "'");
178 if (hdr_sz
!= (int)hdr_len
) {
179 debugs(58, DBG_IMPORTANT
, "internal HttpMsg::parse vs. headersEnd error: " <<
180 hdr_sz
<< " != " << hdr_len
);
181 hdr_sz
= (int)hdr_len
; // because old http.cc code used hdr_len
188 * parseCharBuf() takes character buffer of HTTP headers (buf),
189 * which may not be NULL-terminated, and fills in an HttpMsg
190 * structure. The parameter 'end' specifies the offset to
191 * the end of the reply headers. The caller may know where the
192 * end is, but is unable to NULL-terminate the buffer. This function
193 * returns true on success.
196 HttpMsg::parseCharBuf(const char *buf
, ssize_t end
)
200 /* reset current state, because we are not used in incremental fashion */
205 success
= httpMsgParseStep(mb
.buf
, mb
.size
, 0);
211 * parses a 0-terminating buffer into HttpMsg.
214 * 0 -- need more data (partial parse)
218 HttpMsg::httpMsgParseStep(const char *buf
, int len
, int atEnd
)
220 const char *parse_start
= buf
;
222 const char *blk_start
, *blk_end
;
223 const char **parse_end_ptr
= &blk_end
;
225 assert(pstate
< psParsed
);
227 *parse_end_ptr
= parse_start
;
229 PROF_start(HttpMsg_httpMsgParseStep
);
231 if (pstate
== psReadyToParseStartLine
) {
232 if (!httpMsgIsolateStart(&parse_start
, &blk_start
, &blk_end
)) {
233 PROF_stop(HttpMsg_httpMsgParseStep
);
237 if (!parseFirstLine(blk_start
, blk_end
)) {
238 PROF_stop(HttpMsg_httpMsgParseStep
);
239 return httpMsgParseError();
242 *parse_end_ptr
= parse_start
;
244 hdr_sz
= *parse_end_ptr
- buf
;
245 parse_len
= parse_len
- hdr_sz
;
251 * XXX This code uses parse_start; but if we're incrementally parsing then
252 * this code might not actually be given parse_start at the right spot (just
253 * after headers.) Grr.
255 if (pstate
== psReadyToParseHeaders
) {
256 if (!httpMsgIsolateHeaders(&parse_start
, parse_len
, &blk_start
, &blk_end
)) {
258 blk_start
= parse_start
;
259 blk_end
= blk_start
+ strlen(blk_start
);
261 PROF_stop(HttpMsg_httpMsgParseStep
);
266 if (!header
.parse(blk_start
, blk_end
-blk_start
)) {
267 PROF_stop(HttpMsg_httpMsgParseStep
);
268 return httpMsgParseError();
273 *parse_end_ptr
= parse_start
;
275 hdr_sz
= *parse_end_ptr
- buf
;
280 PROF_stop(HttpMsg_httpMsgParseStep
);
284 /* handy: resets and returns -1 */
286 HttpMsg::httpMsgParseError()
293 HttpMsg::setContentLength(int64_t clen
)
295 header
.delById(HDR_CONTENT_LENGTH
); // if any
296 header
.putInt64(HDR_CONTENT_LENGTH
, clen
);
297 content_length
= clen
;
301 HttpMsg::persistent() const
303 if (http_ver
> Http::ProtocolVersion(1, 0)) {
305 * for modern versions of HTTP: persistent unless there is
306 * a "Connection: close" header.
308 return !httpHeaderHasConnDir(&header
, "close");
310 /* for old versions of HTTP: persistent if has "keep-alive" */
311 return httpHeaderHasConnDir(&header
, "keep-alive");
315 void HttpMsg::packInto(Packer
*p
, bool full_uri
) const
317 packFirstLineInto(p
, full_uri
);
319 packerAppend(p
, "\r\n", 2);
322 void HttpMsg::hdrCacheInit()
324 content_length
= header
.getInt64(HDR_CONTENT_LENGTH
);
325 assert(NULL
== cache_control
);
326 cache_control
= header
.getCc();
330 * useful for debugging
332 void HttpMsg::firstLineBuf(MemBuf
& mb
)
335 packerToMemInit(&p
, &mb
);
336 packFirstLineInto(&p
, true);