SNode.C
Loading...
Searching...
No Matches
Parser.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025, 2026
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "web/http/Parser.h"
43
44#include "core/socket/stream/SocketContext.h"
45#include "web/http/decoder/Chunked.h"
46#include "web/http/decoder/HTTP10Response.h"
47#include "web/http/decoder/Identity.h"
48
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
50
51#include "web/http/http_utils.h"
52
53#include <charconv>
54#include <system_error>
55#include <tuple>
56#include <utility>
57
58#endif /* DOXYGEN_SHOULD_SKIP_THIS */
59
60namespace web::http {
61
62 static bool parseContentLengthStrict(const std::string& s, std::size_t& out) {
63 bool success = false;
64
65 if (!s.empty()) {
66 unsigned long long value = 0;
67
68 const char* first = s.data();
69 const char* last = s.data() + s.size();
70
71 const auto [ptr, ec] = std::from_chars(first, last, value, 10);
72
73 if (ec == std::errc{} && ptr == last) {
74 out = static_cast<std::size_t>(value);
75 success = true;
76 }
77 }
78
79 return success;
80 }
81
82 static bool transferEncodingHasChunked(CiStringMap<std::string>& headers) {
83 bool hasChunked = false;
84
85 if (headers.contains("Transfer-Encoding")) {
86 const std::string& encoding = headers["Transfer-Encoding"];
87 hasChunked = web::http::ciContains(encoding, "chunked");
88 }
89
90 return hasChunked;
91 }
92
93 // HTTP/1.0 and HTTP/1.1
94 const std::regex Parser::httpVersionRegex("^HTTP/([1])[.]([0-1])$");
95
96 Parser::Parser(core::socket::stream::SocketContext* socketContext, const enum Parser::HTTPCompliance& compliance)
97 : hTTPCompliance(compliance)
98 , socketContext(socketContext)
99 , headerDecoder(socketContext)
100 , trailerDecoder(socketContext) {
101 }
102
104 reset();
105 }
106
107 void Parser::reset() {
109 headers.clear();
110 content.clear();
111 httpMinor = 0;
112 httpMajor = 0;
113 line.clear();
114 contentLength = 0;
116
117 for (const ContentDecoder* contentDecoder : decoderQueue) {
118 delete contentDecoder;
119 }
120 decoderQueue.clear();
121
122 trailerFieldsExpected.clear();
123 }
124
125 std::size_t Parser::parse() {
126 std::size_t ret = 0;
127 std::size_t consumed = 0;
128
129 do {
130 switch (parserState) {
132 begin();
134 [[fallthrough]];
136 ret = readStartLine();
137 break;
139 ret = readHeader();
140 break;
142 ret = readContent();
143 break;
145 ret = readTrailer();
146 break;
148 break;
149 }
150 consumed += ret;
151 } while (ret > 0 && parserState != ParserState::BEGIN && parserState != ParserState::ERROR);
152
153 return consumed;
154 }
155
156 std::size_t Parser::readStartLine() {
157 std::size_t consumed = 0;
158 std::size_t ret = 0;
159
160 do {
161 char ch = 0;
163
164 if (ret > 0) {
165 consumed += ret;
166 if (ch == '\r' || ch == '\n') {
167 if (ch == '\n') {
169 line.clear();
170 }
171 } else {
172 line += ch;
173 }
174 }
175 } while (ret > 0 && parserState == ParserState::FIRSTLINE);
176
177 return consumed;
178 }
179
180 std::size_t Parser::readHeader() {
181 const std::size_t consumed = headerDecoder.read();
182
185 } else if (headerDecoder.isComplete()) {
188 }
189
190 return consumed;
191 }
192
194 bool success = true;
195
196 // Determine message framing.
197 // RFC 9112 ยง6.3: Transfer-Encoding (chunked) overrides Content-Length.
198 const bool hasChunked = transferEncodingHasChunked(headers);
199
200 if (hasChunked) {
202 decoderQueue.emplace_back(new web::http::decoder::Chunked(socketContext));
203
204 if (headers.contains("Trailer")) {
205 std::string trailers = headers["Trailer"];
206
207 while (!trailers.empty()) {
208 std::string trailerField;
209 std::tie(trailerField, trailers) = httputils::str_split(trailers, ',');
210 httputils::str_trimm(trailerField);
211 trailerFieldsExpected.insert(trailerField);
212 trailerField.clear();
213 }
215 }
216 } else if (headers.contains("Content-Length")) {
217 std::size_t length = 0;
218
219 if (!parseContentLengthStrict(headers["Content-Length"], length)) {
220 parseError(400, "Invalid Content-Length");
221 success = false;
222 } else {
223 contentLength = length;
224 decoderQueue.emplace_back(new web::http::decoder::Identity(socketContext, contentLength));
225 }
226 }
227
228 if (success) {
229 // Transfer-Encoding (other than chunked) is currently not implemented, but we keep the
230 // existing behavior of not altering the decoder queue here.
231 if (headers.contains("Transfer-Encoding")) {
232 const std::string& encoding = headers["Transfer-Encoding"];
233 if (web::http::ciContains(encoding, "compressed")) {
234 // decoderQueue.emplace_back(new web::http::decoder::Compress(socketContext));
235 }
236 if (web::http::ciContains(encoding, "deflate")) {
237 // decoderQueue.emplace_back(new web::http::decoder::Deflate(socketContext));
238 }
239 if (web::http::ciContains(encoding, "gzip")) {
240 // decoderQueue.emplace_back(new web::http::decoder::GZip(socketContext));
241 }
242 }
243
244 if (decoderQueue.empty()) {
245 decoderQueue.emplace_back(new web::http::decoder::HTTP10Response(socketContext));
246 }
247
248 if (headers.contains("Content-Encoding")) {
249 const std::string& encoding = headers["Content-Encoding"];
250
251 if (web::http::ciContains(encoding, "compressed")) {
252 // decoderQueue.emplace_back(new web::http::decoder::Compress(socketContext));
253 }
254 if (web::http::ciContains(encoding, "deflate")) {
255 // decoderQueue.emplace_back(new web::http::decoder::Deflate(socketContext));
256 }
257 if (web::http::ciContains(encoding, "gzip")) {
258 // decoderQueue.emplace_back(new web::http::decoder::GZip(socketContext));
259 }
260 if (web::http::ciContains(encoding, "br")) {
261 // decoderQueue.emplace_back(new web::http::decoder::Br(socketContext));
262 }
263 }
264 }
265 }
266
267 std::size_t Parser::readContent() {
268 ContentDecoder* contentDecoder = decoderQueue.front();
269
270 const std::size_t consumed = contentDecoder->read();
271
272 if (contentDecoder->isComplete()) {
273 contentDecoder = decoderQueue.back();
274
275 std::vector<char> chunk = contentDecoder->getContent();
276 content.insert(content.end(), chunk.begin(), chunk.end());
277
278 if (transferEncoding == TransferEncoding::Chunked && headers.contains("Trailer")) {
280 } else {
282 }
283 } else if (contentDecoder->isError()) {
284 parseError(501, "Wrong content encoding");
285 }
286
287 return consumed;
288 }
289
290 std::size_t Parser::readTrailer() {
291 const std::size_t consumed = trailerDecoder.read();
292
295 } else if (trailerDecoder.isComplete()) {
296 web::http::CiStringMap<std::string>&& trailer = trailerDecoder.getHeader();
297 headers.insert(trailer.begin(), trailer.end());
299 }
300
301 return consumed;
302 }
303
304 enum Parser::HTTPCompliance operator|(const enum Parser::HTTPCompliance& c1, const enum Parser::HTTPCompliance& c2) {
305 return static_cast<enum Parser::HTTPCompliance>(static_cast<unsigned short>(c1) | static_cast<unsigned short>(c2));
306 }
307
308 enum Parser::HTTPCompliance operator&(const enum Parser::HTTPCompliance& c1, const enum Parser::HTTPCompliance& c2) {
309 return static_cast<enum Parser::HTTPCompliance>(static_cast<unsigned short>(c1) & static_cast<unsigned short>(c2));
310 }
311
312} // namespace web::http
std::size_t readFromPeer(char *chunk, std::size_t chunklen) const final
virtual std::size_t read()=0
std::vector< char > && getContent()
ParserState parserState
Definition Parser.h:100
std::size_t parse()
Definition Parser.cpp:125
friend enum HTTPCompliance operator&(const enum HTTPCompliance &c1, const enum HTTPCompliance &c2)
Definition Parser.cpp:308
virtual void parseError(int code, const std::string &reason)=0
virtual ~Parser()
Definition Parser.cpp:103
Parser(core::socket::stream::SocketContext *socketContext, const enum HTTPCompliance &compliance=HTTPCompliance::RFC2616|HTTPCompliance::RFC7230)
Definition Parser.cpp:96
static const std::regex httpVersionRegex
Definition Parser.h:104
friend enum HTTPCompliance operator|(const enum HTTPCompliance &c1, const enum HTTPCompliance &c2)
Definition Parser.cpp:304
std::size_t contentLength
Definition Parser.h:143
virtual void parseStartLine(const std::string &line)=0
std::size_t contentLengthRead
Definition Parser.h:144
web::http::decoder::Fields headerDecoder
Definition Parser.h:130
core::socket::stream::SocketContext * socketContext
Definition Parser.h:127
CiStringMap< std::string > headers
Definition Parser.h:119
std::size_t readContent()
Definition Parser.cpp:267
std::list< web::http::ContentDecoder * > decoderQueue
Definition Parser.h:125
virtual void parsingFinished()=0
virtual void begin()=0
TransferEncoding transferEncoding
Definition Parser.h:102
std::size_t readTrailer()
Definition Parser.cpp:290
std::vector< char > content
Definition Parser.h:120
std::size_t readStartLine()
Definition Parser.cpp:156
virtual void analyzeHeader()
Definition Parser.cpp:193
std::set< std::string > trailerFieldsExpected
Definition Parser.h:132
web::http::decoder::Fields trailerDecoder
Definition Parser.h:133
std::size_t readHeader()
Definition Parser.cpp:180
std::string line
Definition Parser.h:142
Chunked(const core::socket::stream::SocketContext *socketContext)
Definition Chunked.cpp:54
std::string getErrorReason() const
Definition Fields.cpp:175
Fields(core::socket::stream::SocketContext *socketContext, std::set< std::string > fieldsExpected={})
Definition Fields.cpp:60
void setFieldsExpected(std::set< std::string > fieldsExpected)
Definition Fields.cpp:66
web::http::CiStringMap< std::string > && getHeader()
Definition Fields.cpp:159
HTTP10Response(const core::socket::stream::SocketContext *socketContext)
Identity(const core::socket::stream::SocketContext *socketContext, std::size_t contentLengthExpected)
Definition Identity.cpp:54
std::pair< std::string, std::string > str_split(const std::string &base, char c_middle)
std::string & str_trimm(std::string &text)
bool ciContains(const std::string &str1, const std::string &str2)
static bool transferEncodingHasChunked(CiStringMap< std::string > &headers)
Definition Parser.cpp:82
static bool parseContentLengthStrict(const std::string &s, std::size_t &out)
Definition Parser.cpp:62