]>
Commit | Line | Data |
---|---|---|
83aacd9a AJ |
1 | #ifndef _SQUID_SRC_HTTP_ONE_PARSER_H |
2 | #define _SQUID_SRC_HTTP_ONE_PARSER_H | |
4c14658e | 3 | |
c99510dd AJ |
4 | #include "anyp/ProtocolVersion.h" |
5 | #include "http/one/forward.h" | |
eb1bd364 | 6 | #include "SBuf.h" |
4c14658e | 7 | |
bb86dcd4 | 8 | namespace Http { |
1b51ee7b | 9 | namespace One { |
bb86dcd4 | 10 | |
4c14658e | 11 | // Parser states |
678451c0 | 12 | enum ParseState { |
36a9c964 AJ |
13 | HTTP_PARSE_NONE, ///< initialized, but nothing usefully parsed yet |
14 | HTTP_PARSE_FIRST, ///< HTTP/1 message first-line | |
15 | HTTP_PARSE_MIME, ///< HTTP/1 mime-header block | |
7a4fa6a0 | 16 | HTTP_PARSE_DONE ///< parsed a message header, or reached a terminal syntax error |
678451c0 | 17 | }; |
4c14658e | 18 | |
36a9c964 | 19 | /** HTTP/1.x protocol parser |
4c14658e | 20 | * |
00589b8e | 21 | * Works on a raw character I/O buffer and tokenizes the content into |
36a9c964 | 22 | * the major CRLF delimited segments of an HTTP/1 procotol message: |
4c14658e | 23 | * |
7322c9dd | 24 | * \item first-line (request-line / simple-request / status-line) |
36a9c964 | 25 | * \item mime-header 0*( header-name ':' SP field-value CRLF) |
4c14658e | 26 | */ |
7322c9dd | 27 | class Parser : public RefCountable |
4c14658e | 28 | { |
c99510dd | 29 | explicit Parser(const Parser&); // do not implement |
36a9c964 AJ |
30 | Parser& operator =(const Parser&); // do not implement |
31 | ||
4c14658e | 32 | public: |
8e677087 AJ |
33 | typedef SBuf::size_type size_type; |
34 | ||
7322c9dd | 35 | Parser() { clear(); } |
36a9c964 | 36 | virtual ~Parser() {} |
4c14658e AJ |
37 | |
38 | /// Set this parser back to a default state. | |
39 | /// Will DROP any reference to a buffer (does not free). | |
7322c9dd | 40 | virtual void clear(); |
4c14658e | 41 | |
36a9c964 AJ |
42 | /// attempt to parse a message from the buffer |
43 | /// \retval true if a full message was found and parsed | |
44 | /// \retval false if incomplete, invalid or no message was found | |
45 | virtual bool parse(const SBuf &aBuf) = 0; | |
f9daf571 | 46 | |
36a9c964 AJ |
47 | /** Whether the parser is waiting on more data to complete parsing a message. |
48 | * Use to distinguish between incomplete data and error results | |
49 | * when parse() returns false. | |
87abd755 | 50 | */ |
36a9c964 | 51 | bool needsMoreData() const {return parsingStage_!=HTTP_PARSE_DONE;} |
f9daf571 AJ |
52 | |
53 | /// size in bytes of the first line including CRLF terminator | |
8e677087 | 54 | virtual size_type firstLineSize() const = 0; |
7e1d6c48 | 55 | |
f4880526 | 56 | /// size in bytes of the message headers including CRLF terminator(s) |
7322c9dd | 57 | /// but excluding first-line bytes |
8e677087 | 58 | size_type headerBlockSize() const {return mimeHeaderBlock_.length();} |
7e1d6c48 | 59 | |
7322c9dd | 60 | /// size in bytes of HTTP message block, includes first-line and mime headers |
7e1d6c48 | 61 | /// excludes any body/entity/payload bytes |
7322c9dd | 62 | /// excludes any garbage prefix before the first-line |
8e677087 | 63 | size_type messageHeaderSize() const {return firstLineSize() + headerBlockSize();} |
7e1d6c48 | 64 | |
7322c9dd | 65 | /// buffer containing HTTP mime headers, excluding message first-line. |
36a9c964 | 66 | SBuf mimeHeader() const {return mimeHeaderBlock_;} |
7322c9dd AJ |
67 | |
68 | /// the protocol label for this message | |
69 | const AnyP::ProtocolVersion & messageProtocol() const {return msgProtocol_;} | |
afff15b2 | 70 | |
a4181565 AJ |
71 | /** |
72 | * \return A pointer to a field-value of the first matching field-name, or NULL. | |
73 | */ | |
74 | char *getHeaderField(const char *name); | |
75 | ||
b749de75 AJ |
76 | /// the remaining unprocessed section of buffer |
77 | const SBuf &remaining() const {return buf_;} | |
78 | ||
79 | protected: | |
9651320a AJ |
80 | /// RFC 7230 section 2.6 - 7 magic octets |
81 | static const SBuf Http1magic; | |
82 | ||
b749de75 AJ |
83 | /// bytes remaining to be parsed |
84 | SBuf buf_; | |
74f478f8 | 85 | |
7322c9dd | 86 | /// what stage the parser is currently up to |
cbcd99df | 87 | ParseState parsingStage_; |
7322c9dd | 88 | |
7a4fa6a0 AJ |
89 | /// what protocol label has been found in the first line (if any) |
90 | AnyP::ProtocolVersion msgProtocol_; | |
7322c9dd | 91 | |
7a4fa6a0 | 92 | /// buffer holding the mime headers (if any) |
7322c9dd AJ |
93 | SBuf mimeHeaderBlock_; |
94 | }; | |
95 | ||
1b51ee7b | 96 | } // namespace One |
bb86dcd4 AJ |
97 | } // namespace Http |
98 | ||
83aacd9a | 99 | #endif /* _SQUID_SRC_HTTP_ONE_PARSER_H */ |