]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpParser.h
Various audit updates
[thirdparty/squid.git] / src / HttpParser.h
1 #ifndef _SQUID_SRC_HTTPPARSER_H
2 #define _SQUID_SRC_HTTPPARSER_H
3
4 #include "http/StatusCode.h"
5
6 // Parser states
7 #define HTTP_PARSE_NONE 0 // nothing. completely unset state.
8 #define HTTP_PARSE_NEW 1 // initialized, but nothing usefully parsed yet.
9
10 /** HTTP protocol parser.
11 *
12 * Works on a raw character I/O buffer and tokenizes the content into
13 * either an error state or, an HTTP procotol request major segments:
14 * 1. Request Line (method, URL, protocol, version)
15 * 2. Mime header block
16 */
17 class HttpParser
18 {
19 public:
20 HttpParser() { clear(); }
21
22 /** Initialize a new parser.
23 * Presenting it a buffer to work on and the current length of available
24 * data.
25 * NOTE: This is *not* the buffer size, just the parse-able data length.
26 * The parse routines may be called again later with more data.
27 */
28 HttpParser(const char *aBuf, int len) { reset(aBuf,len); };
29
30 /// Set this parser back to a default state.
31 /// Will DROP any reference to a buffer (does not free).
32 void clear();
33
34 /// Reset the parser for use on a new buffer.
35 void reset(const char *aBuf, int len);
36
37 /**
38 * Attempt to parse the first line of a new request message.
39 *
40 * Governed by:
41 * RFC 1945 section 5.1
42 * RFC 2616 section 5.1
43 *
44 * Parsing state is stored between calls. However the current implementation
45 * begins parsing from scratch on every call.
46 * The return value tells you whether the parsing state fields are valid or not.
47 *
48 * \retval -1 an error occurred. request_parse_status indicates HTTP status result.
49 * \retval 1 successful parse. member fields contain the request-line items
50 * \retval 0 more data is needed to complete the parse
51 */
52 int parseRequestFirstLine();
53
54 public:
55 uint8_t state;
56 const char *buf;
57 int bufsiz;
58
59 /// Offsets for pieces of the (HTTP request) Request-Line as per RFC 2616
60 struct request_offsets {
61 int start, end;
62 int m_start, m_end; // method
63 int u_start, u_end; // url
64 int v_start, v_end; // version (full text)
65 int v_maj, v_min; // version numerics
66 } req;
67
68 // Offsets for pieces of the MiME Header segment
69 int hdr_start, hdr_end;
70
71 // TODO: Offsets for pieces of the (HTTP reply) Status-Line as per RFC 2616
72
73 /** HTTP status code to be used on the invalid-request error page
74 * Http::scNone indicates incomplete parse, Http::scOkay indicates no error.
75 */
76 Http::StatusCode request_parse_status;
77 };
78
79 // Legacy functions
80 #define HttpParserInit(h,b,l) (h)->reset((b),(l))
81 int HttpParserParseReqLine(HttpParser *hp);
82
83 #define MSGDODEBUG 0
84 #if MSGDODEBUG
85 int HttpParserReqSz(HttpParser *);
86 int HttpParserHdrSz(HttpParser *);
87 const char * HttpParserHdrBuf(HttpParser *);
88 int HttpParserRequestLen(HttpParser *hp);
89 #else
90 #define HttpParserReqSz(hp) ( (hp)->req.end - (hp)->req.start + 1 )
91 #define HttpParserHdrSz(hp) ( (hp)->hdr_end - (hp)->hdr_start + 1 )
92 #define HttpParserHdrBuf(hp) ( (hp)->buf + (hp)->hdr_start )
93 #define HttpParserRequestLen(hp) ( (hp)->hdr_end - (hp)->req.start + 1 )
94 #endif
95
96 #endif /* _SQUID_SRC_HTTPPARSER_H */