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