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;
83
84 for (ContentDecoder* contentDecoder : decoderQueue) {
85 delete contentDecoder;
86 }
87 decoderQueue.clear();
88
90 }
91
92 std::size_t Parser::parse() {
93 std::size_t ret = 0;
94 std::size_t consumed = 0;
95
96 do {
97 switch (parserState) {
99 begin();
101 [[fallthrough]];
103 ret = readStartLine();
104 break;
106 ret = readHeader();
107 break;
109 ret = readContent();
110 break;
112 ret = readTrailer();
113 break;
115 break;
116 }
117 consumed += ret;
118 } while (ret > 0 && parserState != ParserState::BEGIN && parserState != ParserState::ERROR);
119
120 return consumed;
121 }
122
123 std::size_t Parser::readStartLine() {
124 std::size_t consumed = 0;
125 std::size_t ret = 0;
126
127 do {
128 char ch = 0;
130
131 if (ret > 0) {
132 consumed += ret;
133 if (ch == '\r' || ch == '\n') {
134 if (ch == '\n') {
136 line.clear();
137 }
138 } else {
139 line += ch;
140 }
141 }
142 } while (ret > 0 && parserState == ParserState::FIRSTLINE);
143
144 return consumed;
145 }
146
147 std::size_t Parser::readHeader() {
148 const std::size_t consumed = headerDecoder.read();
149
152 } else if (headerDecoder.isComplete()) {
155 }
156
157 return consumed;
158 }
159
161 if (headers.contains("Content-Length")) {
162 contentLength = std::stoul(headers["Content-Length"]);
163 decoderQueue.emplace_back(new web::http::decoder::Identity(socketContext, contentLength));
164 }
165 if (headers.contains("Transfer-Encoding")) {
166 const std::string& encoding = headers["Transfer-Encoding"];
167
168 if (web::http::ciContains(encoding, "chunked")) {
170 decoderQueue.emplace_back(new web::http::decoder::Chunked(socketContext));
171
172 if (headers.contains("Trailer")) {
173 std::string trailers = headers["Trailer"];
174
175 while (!trailers.empty()) {
176 std::string trailerField;
177 std::tie(trailerField, trailers) = httputils::str_split(trailers, ',');
178 httputils::str_trimm(trailerField);
179 trailerFieldsExpected.insert(trailerField);
180 trailerField.clear();
181 }
183 }
184 }
185 if (web::http::ciContains(encoding, "compressed")) {
186 // decoderQueue.emplace_back(new web::http::decoder::Compress(socketContext));
187 }
188 if (web::http::ciContains(encoding, "deflate")) {
189 // decoderQueue.emplace_back(new web::http::decoder::Deflate(socketContext));
190 }
191 if (web::http::ciContains(encoding, "gzip")) {
192 // decoderQueue.emplace_back(new web::http::decoder::GZip(socketContext));
193 }
194 }
195 if (decoderQueue.empty()) {
196 decoderQueue.emplace_back(new web::http::decoder::HTTP10Response(socketContext));
197 }
198
199 if (headers.contains("Content-Encoding")) {
200 const std::string& encoding = headers["Content-Encoding"];
201
202 if (web::http::ciContains(encoding, "compressed")) {
203 // decoderQueue.emplace_back(new web::http::decoder::Compress(socketContext));
204 }
205 if (web::http::ciContains(encoding, "deflate")) {
206 // decoderQueue.emplace_back(new web::http::decoder::Deflate(socketContext));
207 }
208 if (web::http::ciContains(encoding, "gzip")) {
209 // decoderQueue.emplace_back(new web::http::decoder::GZip(socketContext));
210 }
211 if (web::http::ciContains(encoding, "br")) {
212 // decoderQueue.emplace_back(new web::http::decoder::Br(socketContext));
213 }
214 }
215 }
216
217 std::size_t Parser::readContent() {
218 ContentDecoder* contentDecoder = decoderQueue.front();
219
220 const std::size_t consumed = contentDecoder->read();
221
222 if (contentDecoder->isComplete()) {
223 contentDecoder = decoderQueue.back();
224
225 std::vector<char> chunk = contentDecoder->getContent();
226 content.insert(content.end(), chunk.begin(), chunk.end());
227
228 if (transferEncoding == TransferEncoding::Chunked && headers.contains("Trailer")) {
230 } else {
232 }
233 } else if (contentDecoder->isError()) {
234 parseError(501, "Wrong content encoding");
235 }
236
237 return consumed;
238 }
239
240 std::size_t Parser::readTrailer() {
241 const std::size_t consumed = trailerDecoder.read();
242
245 } else if (trailerDecoder.isComplete()) {
246 web::http::CiStringMap<std::string>&& trailer = trailerDecoder.getHeader();
247 headers.insert(trailer.begin(), trailer.end());
249 }
250
251 return consumed;
252 }
253
254 enum Parser::HTTPCompliance operator|(const enum Parser::HTTPCompliance& c1, const enum Parser::HTTPCompliance& c2) {
255 return static_cast<enum Parser::HTTPCompliance>(static_cast<unsigned short>(c1) | static_cast<unsigned short>(c2));
256 }
257
258 enum Parser::HTTPCompliance operator&(const enum Parser::HTTPCompliance& c1, const enum Parser::HTTPCompliance& c2) {
259 return static_cast<enum Parser::HTTPCompliance>(static_cast<unsigned short>(c1) & static_cast<unsigned short>(c2));
260 }
261
262} // 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:92
friend enum HTTPCompliance operator&(const enum HTTPCompliance &c1, const enum HTTPCompliance &c2)
Definition Parser.cpp:258
virtual void parseError(int code, const std::string &reason)=0
virtual ~Parser()
Definition Parser.cpp:70
Parser(core::socket::stream::SocketContext *socketContext, const enum HTTPCompliance &compliance=HTTPCompliance::RFC2616|HTTPCompliance::RFC7230)
Definition Parser.cpp:63
static const std::regex httpVersionRegex
Definition Parser.h:104
friend enum HTTPCompliance operator|(const enum HTTPCompliance &c1, const enum HTTPCompliance &c2)
Definition Parser.cpp:254
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:217
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:240
std::vector< char > content
Definition Parser.h:120
std::size_t readStartLine()
Definition Parser.cpp:123
virtual void analyzeHeader()
Definition Parser.cpp:160
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:147
std::string line
Definition Parser.h:142
Chunked(const core::socket::stream::SocketContext *socketContext)
Definition Chunked.cpp:54
std::string getErrorReason()
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)