SNode.C
Loading...
Searching...
No Matches
RequestParser.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#include "web/http/server/RequestParser.h"
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#include "web/http/http_utils.h"
25
26#include <regex>
27#include <tuple>
28#include <utility>
29
30#endif /* DOXYGEN_SHOULD_SKIP_THIS */
31
32namespace web::http::server {
33
34 RequestParser::RequestParser(core::socket::stream::SocketContext* socketContext,
35 const std::function<void()>& onRequestStart,
36 const std::function<void(Request&&)>& onRequestParsed,
37 const std::function<void(int, const std::string&)>& onRequestParseError)
42 }
43
44 bool RequestParser::methodSupported(const std::string& method) const {
45 return supportedMethods.contains(method);
46 }
47
49 onRequestStart();
50 }
51
52 void RequestParser::parseStartLine(const std::string& line) {
54
55 if (!line.empty()) {
56 std::string remaining;
57
58 std::tie(request.method, remaining) = httputils::str_split(line, ' ');
59 std::tie(request.url, request.httpVersion) = httputils::str_split(remaining, ' ');
60
61 std::string queriesLine;
62 std::tie(std::ignore, queriesLine) = httputils::str_split(request.url, '?');
63
64 if (!methodSupported(request.method)) {
65 parseError(400, "Bad request method: " + request.method);
66 } else if (request.url.empty() || request.url.front() != '/') {
67 parseError(400, "Malformed request");
68 } else {
69 std::smatch httpVersionMatch;
70 if (!std::regex_match(request.httpVersion, httpVersionMatch, httpVersionRegex)) {
71 parseError(400, "Wrong protocol-version: " + request.httpVersion);
72 } else {
73 httpMajor = std::stoi(httpVersionMatch.str(1));
74 httpMinor = std::stoi(httpVersionMatch.str(2));
75
76 while (!queriesLine.empty()) {
77 std::string query;
78
79 std::tie(query, queriesLine) = httputils::str_split(queriesLine, '&');
80 request.queries.insert(httputils::str_split(query, '='));
81 }
82 }
83 }
84 } else {
85 parseError(400, "Request-line empty");
86 }
87 }
88
91
92 if (headers.contains("Connection")) {
93 const std::string& connection = headers["Connection"];
94 if (web::http::ciContains(connection, "keep-alive")) {
95 request.connectionState = ConnectionState::Keep;
96 } else if (web::http::ciContains(connection, "close")) {
97 request.connectionState = ConnectionState::Close;
98 }
99 }
100
101 if (headers.contains("Cookie")) {
102 std::string& cookiesLine = headers["Cookie"];
103
104 while (!cookiesLine.empty()) {
105 std::string cookieLine;
106 std::tie(cookieLine, cookiesLine) = httputils::str_split(cookiesLine, ',');
107
108 while (!cookieLine.empty()) {
109 std::string cookie;
110 std::tie(cookie, cookieLine) = httputils::str_split(cookieLine, ';');
111
112 std::string cookieName;
113 std::string cookieValue;
114 std::tie(cookieName, cookieValue) = httputils::str_split(cookie, '=');
115
116 httputils::str_trimm(cookieName);
117 httputils::str_trimm(cookieValue);
118
119 request.cookies.emplace(cookieName, cookieValue);
120 }
121 }
122
123 headers.erase("Cookie");
124 }
125
126 if (transferEncoding != TransferEncoding::Chunked && contentLength == 0) {
128 } else {
130 }
131 }
132
134 request.httpMajor = httpMajor;
135 request.httpMinor = httpMinor;
136 request.headers = std::move(Parser::headers);
137 request.body = std::move(content);
138
139 onRequestParsed(std::move(request));
140
141 reset();
142
144 }
145
146 void RequestParser::parseError(int code, const std::string& reason) {
147 onRequestParseError(code, reason);
148
149 reset();
150
152 }
153
154} // namespace web::http::server
ParserState parserState
Definition Parser.h:78
virtual void analyzeHeader()
Definition Parser.cpp:138
void parseStartLine(const std::string &line) override
void parseError(int code, const std::string &reason) override
virtual bool methodSupported(const std::string &method) const
RequestParser(core::socket::stream::SocketContext *socketContext, const std::function< void()> &onRequestStart, const std::function< void(Request &&)> &onRequestParsed, const std::function< void(int, const std::string &)> &onRequestParseError)