]> git.ipfire.org Git - thirdparty/squid.git/blame - src/http/one/Parser.h
Polish: constify new local variables
[thirdparty/squid.git] / src / http / one / Parser.h
CommitLineData
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 8namespace Http {
1b51ee7b 9namespace One {
bb86dcd4 10
4c14658e 11// Parser states
678451c0 12enum 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 27class 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 32public:
7322c9dd 33 Parser() { clear(); }
36a9c964 34 virtual ~Parser() {}
4c14658e
AJ
35
36 /// Set this parser back to a default state.
37 /// Will DROP any reference to a buffer (does not free).
7322c9dd 38 virtual void clear();
4c14658e 39
36a9c964
AJ
40 /// attempt to parse a message from the buffer
41 /// \retval true if a full message was found and parsed
42 /// \retval false if incomplete, invalid or no message was found
43 virtual bool parse(const SBuf &aBuf) = 0;
f9daf571 44
36a9c964
AJ
45 /** Whether the parser is waiting on more data to complete parsing a message.
46 * Use to distinguish between incomplete data and error results
47 * when parse() returns false.
87abd755 48 */
36a9c964 49 bool needsMoreData() const {return parsingStage_!=HTTP_PARSE_DONE;}
f9daf571
AJ
50
51 /// size in bytes of the first line including CRLF terminator
7322c9dd 52 virtual int64_t firstLineSize() const = 0;
7e1d6c48 53
f4880526 54 /// size in bytes of the message headers including CRLF terminator(s)
7322c9dd 55 /// but excluding first-line bytes
eb1bd364 56 int64_t headerBlockSize() const {return mimeHeaderBlock_.length();}
7e1d6c48 57
7322c9dd 58 /// size in bytes of HTTP message block, includes first-line and mime headers
7e1d6c48 59 /// excludes any body/entity/payload bytes
7322c9dd 60 /// excludes any garbage prefix before the first-line
f4880526 61 int64_t messageHeaderSize() const {return firstLineSize() + headerBlockSize();}
7e1d6c48 62
7322c9dd 63 /// buffer containing HTTP mime headers, excluding message first-line.
36a9c964 64 SBuf mimeHeader() const {return mimeHeaderBlock_;}
7322c9dd
AJ
65
66 /// the protocol label for this message
67 const AnyP::ProtocolVersion & messageProtocol() const {return msgProtocol_;}
afff15b2 68
a4181565
AJ
69 /**
70 * \return A pointer to a field-value of the first matching field-name, or NULL.
71 */
72 char *getHeaderField(const char *name);
73
4c14658e 74public:
9651320a
AJ
75 /// RFC 7230 section 2.6 - 7 magic octets
76 static const SBuf Http1magic;
77
7a4fa6a0 78 SBuf buf;
74f478f8 79
7322c9dd
AJ
80protected:
81 /// what stage the parser is currently up to
cbcd99df 82 ParseState parsingStage_;
7322c9dd 83
7a4fa6a0
AJ
84 /// what protocol label has been found in the first line (if any)
85 AnyP::ProtocolVersion msgProtocol_;
7322c9dd 86
7a4fa6a0 87 /// buffer holding the mime headers (if any)
7322c9dd
AJ
88 SBuf mimeHeaderBlock_;
89};
90
1b51ee7b 91} // namespace One
bb86dcd4
AJ
92} // namespace Http
93
83aacd9a 94#endif /* _SQUID_SRC_HTTP_ONE_PARSER_H */