/proc/self/cwd/source/common/http/http1/parser.h
Line | Count | Source |
1 | | #pragma once |
2 | | |
3 | | #include <memory> |
4 | | |
5 | | #include "envoy/common/pure.h" |
6 | | |
7 | | #include "source/common/common/statusor.h" |
8 | | #include "source/common/http/status.h" |
9 | | |
10 | | namespace Envoy { |
11 | | namespace Http { |
12 | | namespace Http1 { |
13 | | |
14 | | /** |
15 | | * Every parser implementation should have a corresponding parser type here. |
16 | | */ |
17 | | enum class ParserType { Legacy }; |
18 | | |
19 | | enum class MessageType { Request, Response }; |
20 | | |
21 | | // CallbackResult is used to send signals to the parser. See |
22 | | // https://github.com/nodejs/http-parser/blob/5c5b3ac62662736de9e71640a8dc16da45b32503/http_parser.h#L72. |
23 | | enum class CallbackResult { |
24 | | // An error has happened. Further data must not be fed to the parser. |
25 | | Error = -1, |
26 | | // Operation successful. |
27 | | Success = 0, |
28 | | // Returned by onHeadersComplete() to indicate that the parser should not |
29 | | // expect a body. |
30 | | NoBody = 1, |
31 | | // Returned by onHeadersComplete() to indicate that the parser should not |
32 | | // expect either a body or any further data on the connection. |
33 | | NoBodyData = 2, |
34 | | }; |
35 | | |
36 | | class ParserCallbacks { |
37 | | public: |
38 | 56.4k | virtual ~ParserCallbacks() = default; |
39 | | /** |
40 | | * Called when a request/response is beginning. |
41 | | * @return CallbackResult representing success or failure. |
42 | | */ |
43 | | virtual CallbackResult onMessageBegin() PURE; |
44 | | |
45 | | /** |
46 | | * Called when URL data is received. |
47 | | * @param data supplies the start address. |
48 | | * @param length supplies the length. |
49 | | * @return CallbackResult representing success or failure. |
50 | | */ |
51 | | virtual CallbackResult onUrl(const char* data, size_t length) PURE; |
52 | | |
53 | | /** |
54 | | * Called when response status data is received. |
55 | | * @param data supplies the start address. |
56 | | * @param length supplies the length. |
57 | | * @return CallbackResult representing success or failure. |
58 | | */ |
59 | | virtual CallbackResult onStatus(const char* data, size_t length) PURE; |
60 | | |
61 | | /** |
62 | | * Called when header field data is received. |
63 | | * @param data supplies the start address. |
64 | | * @param length supplies the length. |
65 | | * @return CallbackResult representing success or failure. |
66 | | */ |
67 | | virtual CallbackResult onHeaderField(const char* data, size_t length) PURE; |
68 | | |
69 | | /** |
70 | | * Called when header value data is received. |
71 | | * @param data supplies the start address. |
72 | | * @param length supplies the length. |
73 | | * @return CallbackResult representing success or failure. |
74 | | */ |
75 | | virtual CallbackResult onHeaderValue(const char* data, size_t length) PURE; |
76 | | |
77 | | /** |
78 | | * Called when headers are complete. A base routine happens first then a |
79 | | * virtual dispatch is invoked. Note that this only applies to headers and NOT |
80 | | * trailers. End of trailers are signaled via onMessageCompleteBase(). |
81 | | * @return CallbackResult::Error, CallbackResult::Success, |
82 | | * CallbackResult::NoBody, or CallbackResult::NoBodyData. |
83 | | */ |
84 | | virtual CallbackResult onHeadersComplete() PURE; |
85 | | |
86 | | /** |
87 | | * Called when body data is received. |
88 | | * @param data supplies the start address. |
89 | | * @param length supplies the length |
90 | | */ |
91 | | virtual void bufferBody(const char* data, size_t length) PURE; |
92 | | |
93 | | /** |
94 | | * Called when the HTTP message has completed parsing. |
95 | | * @return CallbackResult representing success or failure. |
96 | | */ |
97 | | virtual CallbackResult onMessageComplete() PURE; |
98 | | |
99 | | /** |
100 | | * Called when accepting a chunk header. |
101 | | */ |
102 | | virtual void onChunkHeader(bool) PURE; |
103 | | }; |
104 | | |
105 | | // ParserStatus represents the internal state of the parser. |
106 | | enum class ParserStatus { |
107 | | // An error has occurred. |
108 | | Error = -1, |
109 | | // No error. |
110 | | Ok = 0, |
111 | | // The parser is paused. |
112 | | Paused, |
113 | | }; |
114 | | |
115 | | class Parser { |
116 | | public: |
117 | 56.4k | virtual ~Parser() = default; |
118 | | |
119 | | // Executes the parser. |
120 | | // @return the number of parsed bytes. |
121 | | virtual size_t execute(const char* slice, int len) PURE; |
122 | | |
123 | | // Unpauses the parser. |
124 | | virtual void resume() PURE; |
125 | | |
126 | | // Pauses the parser. Returns CallbackResult::Success, which can be returned |
127 | | // directly in ParserCallback implementations for brevity. |
128 | | virtual CallbackResult pause() PURE; |
129 | | |
130 | | // Returns a ParserStatus representing the internal state of the parser. |
131 | | virtual ParserStatus getStatus() const PURE; |
132 | | |
133 | | // Returns the status code stored in the parser structure. For responses only. |
134 | | virtual Http::Code statusCode() const PURE; |
135 | | |
136 | | // Returns whether HTTP version is 1.1. |
137 | | virtual bool isHttp11() const PURE; |
138 | | |
139 | | // Returns the number of bytes in the body. absl::nullopt if no Content-Length header |
140 | | virtual absl::optional<uint64_t> contentLength() const PURE; |
141 | | |
142 | | // Returns whether headers are chunked. |
143 | | virtual bool isChunked() const PURE; |
144 | | |
145 | | // Returns a textual representation of the method. For requests only. |
146 | | virtual absl::string_view methodName() const PURE; |
147 | | |
148 | | // Returns a textual representation of the internal error state of the parser. |
149 | | virtual absl::string_view errorMessage() const PURE; |
150 | | |
151 | | // Returns whether the Transfer-Encoding header is present. |
152 | | virtual int hasTransferEncoding() const PURE; |
153 | | }; |
154 | | |
155 | | using ParserPtr = std::unique_ptr<Parser>; |
156 | | |
157 | | } // namespace Http1 |
158 | | } // namespace Http |
159 | | } // namespace Envoy |