Message.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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/Stream.h"
14 #include "http/Message.h"
15 #include "http/one/Parser.h"
16 #include "HttpHdrCc.h"
17 #include "HttpHeaderTools.h"
18 #include "MemBuf.h"
19 #include "mime_header.h"
20 #include "SquidConfig.h"
21 
23  http_ver(Http::ProtocolVersion()),
24  header(owner)
25 {}
26 
28 {
29  assert(!body_pipe);
30 }
31 
32 void
34 {
35  delete cache_control;
36  cache_control = new HttpHdrCc(otherCc);
37  header.putCc(*cache_control);
38 }
39 
40 /* find first CRLF */
41 static int
42 httpMsgIsolateStart(const char **parse_start, const char **blk_start, const char **blk_end)
43 {
44  int slen = strcspn(*parse_start, "\r\n");
45 
46  if (!(*parse_start)[slen]) /* no CRLF found */
47  return 0;
48 
49  *blk_start = *parse_start;
50 
51  *blk_end = *blk_start + slen;
52 
53  while (**blk_end == '\r') /* CR */
54  ++(*blk_end);
55 
56  if (**blk_end == '\n') /* LF */
57  ++(*blk_end);
58 
59  *parse_start = *blk_end;
60 
61  return 1;
62 }
63 
64 // negative return is the negated Http::StatusCode error code
65 // zero return means need more data
66 // positive return is the size of parsed headers
67 bool
68 Http::Message::parse(const char *buf, const size_t sz, bool eof, Http::StatusCode *error)
69 {
70  assert(error);
72 
73  // find the end of headers
74  const size_t hdr_len = headersEnd(buf, sz);
75 
76  if (hdr_len > Config.maxReplyHeaderSize || (hdr_len == 0 && sz > Config.maxReplyHeaderSize)) {
77  debugs(58, 3, "input too large: " << hdr_len << " or " << sz << " > " << Config.maxReplyHeaderSize);
79  return false;
80  }
81 
82  // sanity check the start line to see if this is in fact an HTTP message
83  if (!sanityCheckStartLine(buf, hdr_len, error)) {
84  // NP: sanityCheck sets *error and sends debug warnings on syntax errors.
85  // if we have seen the connection close, this is an error too
86  if (eof && *error == Http::scNone)
88 
89  return false;
90  }
91 
92  assert(hdr_len > 0); // sanityCheckStartLine() rejects buffers that cannot be parsed
93 
94  const int res = httpMsgParseStep(buf, sz, eof);
95 
96  if (res < 0) { // error
97  debugs(58, 3, "cannot parse isolated headers in '" << buf << "'");
99  return false;
100  }
101 
102  if (res == 0) {
103  debugs(58, 2, "strange, need more data near '" << buf << "'");
105  return false; // but this should not happen due to headersEnd() above
106  }
107 
108  assert(res > 0);
109  debugs(58, 9, "success (" << hdr_len << " bytes) near '" << buf << "'");
110 
111  if (hdr_sz != (int)hdr_len) {
112  debugs(58, DBG_IMPORTANT, "ERROR: internal Http::Message::parse vs. headersEnd failure: " <<
113  hdr_sz << " != " << hdr_len);
114  hdr_sz = (int)hdr_len; // because old http.cc code used hdr_len
115  }
116 
117  return true;
118 }
119 
128 bool
129 Http::Message::parseCharBuf(const char *buf, ssize_t end)
130 {
131  MemBuf mb;
132  int success;
133  /* reset current state, because we are not used in incremental fashion */
134  reset();
135  mb.init();
136  mb.append(buf, end);
137  mb.terminate();
138  success = httpMsgParseStep(mb.buf, mb.size, 0);
139  mb.clean();
140  return success == 1;
141 }
142 
150 int
151 Http::Message::httpMsgParseStep(const char *buf, int len, int atEnd)
152 {
153  const char *parse_start = buf;
154  int parse_len = len;
155  const char *blk_start, *blk_end;
156  const char **parse_end_ptr = &blk_end;
157  assert(parse_start);
159 
160  *parse_end_ptr = parse_start;
161 
163  if (!httpMsgIsolateStart(&parse_start, &blk_start, &blk_end)) {
164  return 0;
165  }
166 
167  if (!parseFirstLine(blk_start, blk_end)) {
168  return httpMsgParseError();
169  }
170 
171  *parse_end_ptr = parse_start;
172 
173  hdr_sz = *parse_end_ptr - buf;
174  parse_len = parse_len - hdr_sz;
175 
177  }
178 
179  /*
180  * XXX This code uses parse_start; but if we're incrementally parsing then
181  * this code might not actually be given parse_start at the right spot (just
182  * after headers.) Grr.
183  */
184  if (pstate == Http::Message::psReadyToParseHeaders) {
185  size_t hsize = 0;
186  Http::ContentLengthInterpreter interpreter;
187  configureContentLengthInterpreter(interpreter);
188  const int parsed = header.parse(parse_start, parse_len, atEnd, hsize, interpreter);
189  if (parsed <= 0) {
190  return !parsed ? 0 : httpMsgParseError();
191  }
192  hdr_sz += hsize;
193  hdrCacheInit();
194  pstate = Http::Message::psParsed;
195  }
196 
197  return 1;
198 }
199 
200 bool
202 {
203  // HTTP/1 message contains "zero or more header fields"
204  // zero does not need parsing
205  // XXX: c_str() reallocates. performance regression.
206  configureContentLengthInterpreter(clen);
207  if (hp.headerBlockSize() && !header.parse(hp.mimeHeader().c_str(), hp.headerBlockSize(), clen)) {
208  pstate = Http::Message::psError;
209  return false;
210  }
211 
212  // XXX: we are just parsing HTTP headers, not the whole message prefix here
213  hdr_sz = hp.messageHeaderSize();
214  pstate = Http::Message::psParsed;
215  hdrCacheInit();
216  return true;
217 }
218 
219 /* handy: resets and returns -1 */
220 int
222 {
223  reset();
224  return -1;
225 }
226 
227 void
229 {
230  header.delById(Http::HdrType::CONTENT_LENGTH); // if any
231  header.putInt64(Http::HdrType::CONTENT_LENGTH, clen);
232  content_length = clen;
233 }
234 
235 bool
237 {
238  if (http_ver > Http::ProtocolVersion(1,0)) {
239  /*
240  * for modern versions of HTTP: persistent unless there is
241  * a "Connection: close" header.
242  */
243  static SBuf close("close", 5);
244  return !httpHeaderHasConnDir(&header, close);
245  } else {
246  /* for old versions of HTTP: persistent if has "keep-alive" */
247  static SBuf keepAlive("keep-alive", 10);
248  return httpHeaderHasConnDir(&header, keepAlive);
249  }
250 }
251 
252 void
253 Http::Message::packInto(Packable *p, bool full_uri) const
254 {
255  packFirstLineInto(p, full_uri);
256  header.packInto(p);
257  p->append("\r\n", 2);
258 }
259 
260 void
262 {
263  content_length = header.getInt64(Http::HdrType::CONTENT_LENGTH);
264  assert(nullptr == cache_control);
265  cache_control = header.getCc();
266 }
267 
269 void
271 {
272  packFirstLineInto(&mb, true);
273 }
274 
char * buf
Definition: MemBuf.h:134
void terminate()
Definition: MemBuf.cc:241
void packInto(Packable *, bool full_uri) const
produce a message copy, except for a few connection-specific settings
Definition: Message.cc:253
AnyP::ProtocolVersion ProtocolVersion()
Protocol version to use in Http::Message structures wrapping FTP messages.
Definition: Elements.cc:24
@ scNone
Definition: StatusCode.h:21
mb_size_t size
Definition: MemBuf.h:135
void error(char *format,...)
size_type messageHeaderSize() const
Definition: Parser.h:78
void init(mb_size_t szInit, mb_size_t szMax)
Definition: MemBuf.cc:93
Definition: SBuf.h:93
size_t maxReplyHeaderSize
Definition: SquidConfig.h:137
virtual void append(const char *buf, int size)=0
Appends a c-string to existing packed data.
StatusCode
Definition: StatusCode.h:20
size_type headerBlockSize() const
Definition: Parser.h:73
Definition: forward.h:17
SBuf mimeHeader() const
buffer containing HTTP mime headers, excluding message first-line.
Definition: Parser.h:81
bool parseCharBuf(const char *buf, ssize_t end)
Definition: Message.cc:129
int httpMsgParseStep(const char *buf, int len, int atEnd)
Definition: Message.cc:151
@ CONTENT_LENGTH
bool persistent() const
Definition: Message.cc:236
virtual void hdrCacheInit()
Definition: Message.cc:261
bool httpHeaderHasConnDir(const HttpHeader *hdr, const SBuf &directive)
void setContentLength(int64_t)
[re]sets Content-Length header and cached value
Definition: Message.cc:228
void append(const char *c, int sz) override
Definition: MemBuf.cc:209
Definition: MemBuf.h:23
void clean()
Definition: MemBuf.cc:110
static int httpMsgIsolateStart(const char **parse_start, const char **blk_start, const char **blk_end)
Definition: Message.cc:42
~Message() override
Definition: Message.cc:27
#define assert(EX)
Definition: assert.h:17
const char * c_str()
Definition: SBuf.cc:516
void putCc(const HttpHdrCc &)
Definition: Message.cc:33
@ psReadyToParseHeaders
Definition: Message.h:88
#define DBG_IMPORTANT
Definition: Stream.h:38
@ scInvalidHeader
Squid header parsing error.
Definition: StatusCode.h:88
Message(http_hdr_owner_type)
Definition: Message.cc:22
virtual int httpMsgParseError()
Definition: Message.cc:221
void firstLineBuf(MemBuf &)
useful for debugging
Definition: Message.cc:270
bool parseHeader(Http1::Parser &, Http::ContentLengthInterpreter &)
Definition: Message.cc:201
@ scHeaderTooLarge
Header too large to process.
Definition: StatusCode.h:89
size_t headersEnd(const char *mime, size_t l, bool &containsObsFold)
Definition: mime_header.cc:17
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
class SquidConfig Config
Definition: SquidConfig.cc:12
http_hdr_owner_type
Definition: HttpHeader.h:31
int unsigned int
Definition: stub_fd.cc:19
AnyP::ProtocolVersion ProtocolVersion(unsigned int aMajor, unsigned int aMinor)
HTTP version label information.
bool parse(const char *buf, const size_t sz, bool eol, Http::StatusCode *error)
Definition: Message.cc:68
@ psReadyToParseStartLine
Definition: Message.h:87

 

Introduction

Documentation

Support

Miscellaneous