1 #ifndef _SQUID_SRC_HTTPPARSER_H
2 #define _SQUID_SRC_HTTPPARSER_H
4 #include "http/StatusCode.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:
14 * 1. Request Line (method, URL, protocol, version)
15 * 2. Mime header block
20 HttpParser() { clear(); }
22 /** Initialize a new parser.
23 * Presenting it a buffer to work on and the current length of available
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.
28 HttpParser(const char *aBuf
, int len
) { reset(aBuf
,len
); };
30 /// Set this parser back to a default state.
31 /// Will DROP any reference to a buffer (does not free).
34 /// Reset the parser for use on a new buffer.
35 void reset(const char *aBuf
, int len
);
38 * Attempt to parse the first line of a new request message.
41 * RFC 1945 section 5.1
42 * RFC 2616 section 5.1
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.
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
52 int parseRequestFirstLine();
59 /// Offsets for pieces of the (HTTP request) Request-Line as per RFC 2616
60 struct request_offsets
{
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
68 // Offsets for pieces of the MiME Header segment
69 int hdr_start
, hdr_end
;
71 // TODO: Offsets for pieces of the (HTTP reply) Status-Line as per RFC 2616
73 /** HTTP status code to be used on the invalid-request error page
74 * Http::scNone indicates incomplete parse, Http::scOkay indicates no error.
76 Http::StatusCode request_parse_status
;
80 #define HttpParserInit(h,b,l) (h)->reset((b),(l))
81 int HttpParserParseReqLine(HttpParser
*hp
);
85 int HttpParserReqSz(HttpParser
*);
86 int HttpParserHdrSz(HttpParser
*);
87 const char * HttpParserHdrBuf(HttpParser
*);
88 int HttpParserRequestLen(HttpParser
*hp
);
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 )
96 #endif /* _SQUID_SRC_HTTPPARSER_H */