1 #ifndef _SQUID_SRC_HTTPPARSER_H
2 #define _SQUID_SRC_HTTPPARSER_H
4 #include "HttpStatusCode.h"
7 #define HTTP_PARSE_NONE 0 // nothing. completely unset state.
8 #define HTTP_PARSE_NEW 1 // initialized, but nothing usefully parsed yet.
10 /** HTTP protocol parser.
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:
15 * \item Request Line (method, URL, protocol, version)
16 * \item Mime header block
21 HttpParser() { clear(); }
23 /** Initialize a new parser.
24 * Presenting it a buffer to work on and the current length of available
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.
29 HttpParser(const char *aBuf
, int len
) { reset(aBuf
,len
); };
31 /// Set this parser back to a default state.
32 /// Will DROP any reference to a buffer (does not free).
35 /// Reset the parser for use on a new buffer.
36 void reset(const char *aBuf
, int len
);
39 * Attempt to parse the first line of a new request message.
42 * RFC 1945 section 5.1
43 * RFC 2616 section 5.1
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.
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
53 int parseRequestFirstLine();
60 /// Offsets for pieces of the (HTTP request) Request-Line as per RFC 2616
61 struct request_offsets
{
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
69 // Offsets for pieces of the MiME Header segment
70 int hdr_start
, hdr_end
;
72 // TODO: Offsets for pieces of the (HTTP reply) Status-Line as per RFC 2616
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.
77 http_status request_parse_status
;
81 #define HttpParserInit(h,b,l) (h)->reset((b),(l))
82 extern int HttpParserParseReqLine(HttpParser
*hp
);
86 extern int HttpParserReqSz(HttpParser
*);
87 extern int HttpParserHdrSz(HttpParser
*);
88 extern const char * HttpParserHdrBuf(HttpParser
*);
89 extern int HttpParserRequestLen(HttpParser
*hp
);
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 )
97 #endif /* _SQUID_SRC_HTTPPARSER_H */