]>
Commit | Line | Data |
---|---|---|
48a37aee | 1 | /* |
bde978a6 | 2 | * Copyright (C) 1996-2015 The Squid Software Foundation and contributors |
48a37aee AJ |
3 | * |
4 | * Squid software is distributed under GPLv2+ license and includes | |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
7 | */ | |
8 | ||
83aacd9a AJ |
9 | #ifndef _SQUID_SRC_HTTP_ONE_PARSER_H |
10 | #define _SQUID_SRC_HTTP_ONE_PARSER_H | |
4c14658e | 11 | |
c99510dd AJ |
12 | #include "anyp/ProtocolVersion.h" |
13 | #include "http/one/forward.h" | |
f1d5359e | 14 | #include "http/StatusCode.h" |
eb1bd364 | 15 | #include "SBuf.h" |
4c14658e | 16 | |
b8f86fd2 AJ |
17 | namespace Parser { |
18 | class Tokenizer; | |
19 | } | |
20 | ||
bb86dcd4 | 21 | namespace Http { |
1b51ee7b | 22 | namespace One { |
bb86dcd4 | 23 | |
4c14658e | 24 | // Parser states |
678451c0 | 25 | enum ParseState { |
36a9c964 AJ |
26 | HTTP_PARSE_NONE, ///< initialized, but nothing usefully parsed yet |
27 | HTTP_PARSE_FIRST, ///< HTTP/1 message first-line | |
28 | HTTP_PARSE_MIME, ///< HTTP/1 mime-header block | |
7a4fa6a0 | 29 | HTTP_PARSE_DONE ///< parsed a message header, or reached a terminal syntax error |
678451c0 | 30 | }; |
4c14658e | 31 | |
36a9c964 | 32 | /** HTTP/1.x protocol parser |
4c14658e | 33 | * |
00589b8e | 34 | * Works on a raw character I/O buffer and tokenizes the content into |
36a9c964 | 35 | * the major CRLF delimited segments of an HTTP/1 procotol message: |
4c14658e | 36 | * |
7322c9dd | 37 | * \item first-line (request-line / simple-request / status-line) |
36a9c964 | 38 | * \item mime-header 0*( header-name ':' SP field-value CRLF) |
4c14658e | 39 | */ |
7322c9dd | 40 | class Parser : public RefCountable |
4c14658e AJ |
41 | { |
42 | public: | |
8e677087 AJ |
43 | typedef SBuf::size_type size_type; |
44 | ||
f1d5359e | 45 | Parser() : parseStatusCode(Http::scNone), parsingStage_(HTTP_PARSE_NONE) {} |
f9688132 | 46 | virtual ~Parser() {} |
4c14658e AJ |
47 | |
48 | /// Set this parser back to a default state. | |
49 | /// Will DROP any reference to a buffer (does not free). | |
f9688132 | 50 | virtual void clear() = 0; |
4c14658e | 51 | |
36a9c964 AJ |
52 | /// attempt to parse a message from the buffer |
53 | /// \retval true if a full message was found and parsed | |
54 | /// \retval false if incomplete, invalid or no message was found | |
55 | virtual bool parse(const SBuf &aBuf) = 0; | |
f9daf571 | 56 | |
36a9c964 AJ |
57 | /** Whether the parser is waiting on more data to complete parsing a message. |
58 | * Use to distinguish between incomplete data and error results | |
59 | * when parse() returns false. | |
87abd755 | 60 | */ |
36a9c964 | 61 | bool needsMoreData() const {return parsingStage_!=HTTP_PARSE_DONE;} |
f9daf571 AJ |
62 | |
63 | /// size in bytes of the first line including CRLF terminator | |
8e677087 | 64 | virtual size_type firstLineSize() const = 0; |
7e1d6c48 | 65 | |
f4880526 | 66 | /// size in bytes of the message headers including CRLF terminator(s) |
7322c9dd | 67 | /// but excluding first-line bytes |
8e677087 | 68 | size_type headerBlockSize() const {return mimeHeaderBlock_.length();} |
7e1d6c48 | 69 | |
7322c9dd | 70 | /// size in bytes of HTTP message block, includes first-line and mime headers |
7e1d6c48 | 71 | /// excludes any body/entity/payload bytes |
7322c9dd | 72 | /// excludes any garbage prefix before the first-line |
8e677087 | 73 | size_type messageHeaderSize() const {return firstLineSize() + headerBlockSize();} |
7e1d6c48 | 74 | |
7322c9dd | 75 | /// buffer containing HTTP mime headers, excluding message first-line. |
36a9c964 | 76 | SBuf mimeHeader() const {return mimeHeaderBlock_;} |
7322c9dd AJ |
77 | |
78 | /// the protocol label for this message | |
79 | const AnyP::ProtocolVersion & messageProtocol() const {return msgProtocol_;} | |
afff15b2 | 80 | |
a4181565 | 81 | /** |
f1d5359e | 82 | * Scan the mime header block (badly) for a header with the given name. |
687696c1 AJ |
83 | * |
84 | * BUG: omits lines when searching for headers with obs-fold or multiple entries. | |
85 | * | |
86 | * BUG: limits output to just 1KB when Squid accepts up to 64KB line length. | |
87 | * | |
a4181565 AJ |
88 | * \return A pointer to a field-value of the first matching field-name, or NULL. |
89 | */ | |
90 | char *getHeaderField(const char *name); | |
91 | ||
b749de75 AJ |
92 | /// the remaining unprocessed section of buffer |
93 | const SBuf &remaining() const {return buf_;} | |
94 | ||
f1d5359e AJ |
95 | /** |
96 | * HTTP status code resulting from the parse process. | |
97 | * to be used on the invalid message handling. | |
98 | * | |
99 | * Http::scNone indicates incomplete parse, | |
100 | * Http::scOkay indicates no error, | |
101 | * other codes represent a parse error. | |
102 | */ | |
103 | Http::StatusCode parseStatusCode; | |
104 | ||
b749de75 | 105 | protected: |
b8f86fd2 AJ |
106 | /// detect and skip the CRLF or (if tolerant) LF line terminator |
107 | /// consume from the tokenizer and return true only if found | |
108 | bool skipLineTerminator(::Parser::Tokenizer &tok) const; | |
109 | ||
110 | /** | |
111 | * Parse scan to find the mime headers block for current message. | |
112 | * | |
113 | * \retval true if mime block (or a blocks non-existence) has been | |
114 | * identified accurately within limit characters. | |
115 | * mimeHeaderBlock_ has been updated and buf_ consumed. | |
116 | * \retval false an error occured, or no MIME terminator found within limit. | |
117 | */ | |
118 | bool findMimeBlock(const char *which, const size_t limit); | |
f1d5359e | 119 | |
9651320a AJ |
120 | /// RFC 7230 section 2.6 - 7 magic octets |
121 | static const SBuf Http1magic; | |
122 | ||
b749de75 AJ |
123 | /// bytes remaining to be parsed |
124 | SBuf buf_; | |
74f478f8 | 125 | |
7322c9dd | 126 | /// what stage the parser is currently up to |
cbcd99df | 127 | ParseState parsingStage_; |
7322c9dd | 128 | |
7a4fa6a0 AJ |
129 | /// what protocol label has been found in the first line (if any) |
130 | AnyP::ProtocolVersion msgProtocol_; | |
7322c9dd | 131 | |
7a4fa6a0 | 132 | /// buffer holding the mime headers (if any) |
7322c9dd AJ |
133 | SBuf mimeHeaderBlock_; |
134 | }; | |
135 | ||
1b51ee7b | 136 | } // namespace One |
bb86dcd4 AJ |
137 | } // namespace Http |
138 | ||
83aacd9a | 139 | #endif /* _SQUID_SRC_HTTP_ONE_PARSER_H */ |
f53969cc | 140 |