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
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 <tuple>
54#include <utility>
55
56#endif /* DOXYGEN_SHOULD_SKIP_THIS */
57
58namespace web::http {
59
60 // HTTP/1.0 and HTTP/1.1
61 const std::regex Parser::httpVersionRegex("^HTTP/([1])[.]([0-1])$");
62
63 Parser::Parser(core::socket::stream::SocketContext* socketContext, const enum Parser::HTTPCompliance& compliance)
64 : hTTPCompliance(compliance)
65 , socketContext(socketContext)
66 , headerDecoder(socketContext)
67 , trailerDecoder(socketContext) {
68 }
69
71 reset();
72 }
73
74 void Parser::reset() {
76 headers.clear();
77 content.clear();
78 httpMinor = 0;
79 httpMajor = 0;
80 line.clear();
81 contentLength = 0;
82 contentLengthRead = 0;
83
84 for (ContentDecoder* contentDecoder : decoderQueue) {
85 delete contentDecoder;
86 }
87 decoderQueue.clear();
88
89 trailerFieldsExpected.clear();
90 }
91
93 std::size_t ret = 0;
94 std::size_t consumed = 0;
95
96 do {
97 switch (parserState) {
98 case ParserState::BEGIN:
99 begin();
100 parserState = ParserState::FIRSTLINE;
101 [[fallthrough]];
102 case ParserState::FIRSTLINE:
103 ret = readStartLine();
104 break;
105 case ParserState::HEADER:
106 ret = readHeader();
107 break;
108 case ParserState::BODY:
109 ret = readContent();
110 break;
111 case ParserState::TRAILER:
112 ret = readTrailer();
113 break;
114 case ParserState::ERROR:
115 break;
116 }
117 consumed += ret;
118 } while (ret > 0 && parserState != ParserState::BEGIN && parserState != ParserState::ERROR);
119
120 return consumed;
121 }
122
124 std::size_t consumed = 0;
125 std::size_t ret = 0;
126
127 do {
128 char ch = 0;
129 ret = socketContext->readFromPeer(&ch, 1);
130
131 if (ret > 0) {
132 consumed += ret;
133 if (ch == '\r' || ch == '\n') {
134 if (ch == '\n') {
135 parseStartLine(line);
136 line.clear();
137 }
138 } else {
139 line += ch;
140 }
141 }
142 } while (ret > 0 && parserState == ParserState::FIRSTLINE);
143
144 return consumed;
145 }
146
148 const std::size_t consumed = headerDecoder.read();
149
150 if (headerDecoder.isError()) {
151 parseError(headerDecoder.getErrorCode(), headerDecoder.getErrorReason());
152 } else if (headerDecoder.isComplete()) {
153 headers = std::move(headerDecoder.getHeader());
155 }
156
157 return consumed;
158 }
159
161 if (headers.contains("Content-Length")) {
162 contentLength = std::stoul(headers["Content-Length"]);
163 transferEncoding = TransferEncoding::Identity;
164 decoderQueue.emplace_back(new web::http::decoder::Identity(socketContext, contentLength));
165 }
166 if (headers.contains("Transfer-Encoding")) {
167 const std::string& encoding = headers["Transfer-Encoding"];
168
169 if (web::http::ciContains(encoding, "chunked")) {
170 transferEncoding = TransferEncoding::Chunked;
171 decoderQueue.emplace_back(new web::http::decoder::Chunked(socketContext));
172
173 if (headers.contains("Trailer")) {
174 std::string trailers = headers["Trailer"];
175
176 while (!trailers.empty()) {
177 std::string trailerField;
178 std::tie(trailerField, trailers) = httputils::str_split(trailers, ',');
179 httputils::str_trimm(trailerField);
180 trailerFieldsExpected.insert(trailerField);
181 trailerField.clear();
182 }
183 trailerDecoder.setFieldsExpected(trailerFieldsExpected);
184 }
185 }
186 if (web::http::ciContains(encoding, "compressed")) {
187 // decoderQueue.emplace_back(new web::http::decoder::Compress(socketContext));
188 }
189 if (web::http::ciContains(encoding, "deflate")) {
190 // decoderQueue.emplace_back(new web::http::decoder::Deflate(socketContext));
191 }
192 if (web::http::ciContains(encoding, "gzip")) {
193 // decoderQueue.emplace_back(new web::http::decoder::GZip(socketContext));
194 }
195 }
196 if (decoderQueue.empty()) {
197 transferEncoding = TransferEncoding::HTTP10;
198 decoderQueue.emplace_back(new web::http::decoder::HTTP10Response(socketContext));
199 }
200
201 if (headers.contains("Content-Encoding")) {
202 const std::string& encoding = headers["Content-Encoding"];
203
204 if (web::http::ciContains(encoding, "compressed")) {
205 // decoderQueue.emplace_back(new web::http::decoder::Compress(socketContext));
206 }
207 if (web::http::ciContains(encoding, "deflate")) {
208 // decoderQueue.emplace_back(new web::http::decoder::Deflate(socketContext));
209 }
210 if (web::http::ciContains(encoding, "gzip")) {
211 // decoderQueue.emplace_back(new web::http::decoder::GZip(socketContext));
212 }
213 if (web::http::ciContains(encoding, "br")) {
214 // decoderQueue.emplace_back(new web::http::decoder::Br(socketContext));
215 }
216 }
217 }
218
220 ContentDecoder* contentDecoder = decoderQueue.front();
221
222 const std::size_t consumed = contentDecoder->read();
223
224 if (contentDecoder->isComplete()) {
225 contentDecoder = decoderQueue.back();
226
227 std::vector<char> chunk = contentDecoder->getContent();
228 content.insert(content.end(), chunk.begin(), chunk.end());
229
230 if (transferEncoding == TransferEncoding::Chunked && headers.contains("Trailer")) {
232 } else {
234 }
235 } else if (contentDecoder->isError()) {
236 parseError(400, "Wrong content encoding");
237 }
238
239 return consumed;
240 }
241
243 const std::size_t consumed = trailerDecoder.read();
244
245 if (trailerDecoder.isError()) {
246 parseError(trailerDecoder.getErrorCode(), trailerDecoder.getErrorReason());
247 } else if (trailerDecoder.isComplete()) {
248 web::http::CiStringMap<std::string>&& trailer = trailerDecoder.getHeader();
249 headers.insert(trailer.begin(), trailer.end());
251 }
252
253 return consumed;
254 }
255
256 enum Parser::HTTPCompliance operator|(const enum Parser::HTTPCompliance& c1, const enum Parser::HTTPCompliance& c2) {
257 return static_cast<enum Parser::HTTPCompliance>(static_cast<unsigned short>(c1) | static_cast<unsigned short>(c2));
258 }
259
260 enum Parser::HTTPCompliance operator&(const enum Parser::HTTPCompliance& c1, const enum Parser::HTTPCompliance& c2) {
261 return static_cast<enum Parser::HTTPCompliance>(static_cast<unsigned short>(c1) & static_cast<unsigned short>(c2));
262 }
263
264} // namespace web::http
ParserState parserState
Definition Parser.h:100
std::size_t parse()
Definition Parser.cpp:92
friend enum HTTPCompliance operator&(const enum HTTPCompliance &c1, const enum HTTPCompliance &c2)
Definition Parser.cpp:260
virtual ~Parser()
Definition Parser.cpp:70
static const std::regex httpVersionRegex
Definition Parser.h:104
friend enum HTTPCompliance operator|(const enum HTTPCompliance &c1, const enum HTTPCompliance &c2)
Definition Parser.cpp:256
std::size_t readContent()
Definition Parser.cpp:219
virtual void parsingFinished()=0
std::size_t readTrailer()
Definition Parser.cpp:242
std::size_t readStartLine()
Definition Parser.cpp:123
virtual void analyzeHeader()
Definition Parser.cpp:160
std::size_t readHeader()
Definition Parser.cpp:147