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