SNode.C
Loading...
Searching...
No Matches
Fields.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/decoder/Fields.h"
43
44#include "core/socket/stream/SocketContext.h"
45
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
48#include "web/http/http_utils.h"
49
50#include <cctype>
51#include <utility>
52
53#endif // DOXYGEN_SHOULD_SKIP_THIS
54
55#define MAX_LINE_LENGTH 8192
56// #define MAX_LINE_LENGTH 1
57
58namespace web::http::decoder {
59
60 Fields::Fields(core::socket::stream::SocketContext* socketContext, std::set<std::string> fieldsExpected)
61 : socketContext(socketContext)
62 , fieldsExpected(std::move(fieldsExpected))
64 }
65
66 void Fields::setFieldsExpected(std::set<std::string> fieldsExpected) {
67 this->fieldsExpected = std::move(fieldsExpected);
68 }
69
70 std::size_t Fields::read() {
71 std::size_t consumed = 0;
72
73 if (completed || errorCode != 0) {
74 completed = false;
75 fields.clear();
76 errorCode = 0;
77 errorReason = "";
78 }
79
80 std::size_t ret = 0;
81 do {
82 char ch = '\0';
83
85 consumed += ret;
86
87 if (ret > 0) {
88 if (!line.empty() || ch != ' ') {
89 line += ch;
90
91 if (maxLineLength == 0 || line.size() <= maxLineLength) {
93 last = ch;
94
95 if (lastButOne == '\r' && last == '\n') {
96 line.pop_back(); // Remove \n
97 line.pop_back(); // Remove \r
98
99 completed = line.empty();
100 if (!completed) {
102
103 if (!fieldsExpected.empty() && fields.size() > fieldsExpected.size()) {
104 errorCode = 400;
105 errorReason = "Too many fields";
106 }
107 } else if (!fieldsExpected.empty() && fields.size() < fieldsExpected.size()) {
108 errorCode = 400;
109 errorReason = "Too view fields";
110
111 completed = false;
112 }
113 line.clear();
114 lastButOne = '\0';
115 last = '\0';
116 }
117 } else {
118 errorCode = 431;
119 errorReason = "Line too long: " + line;
120 }
121 } else {
122 errorCode = 400;
123 errorReason = "Header Folding";
124 }
125 }
126 } while (ret > 0 && !completed && errorCode == 0);
127
128 return consumed;
129 }
130
131 void Fields::splitLine(const std::string& line) {
132 auto [headerFieldName, value] = httputils::str_split(line, ':');
133
134 if (headerFieldName.empty()) {
135 errorCode = 400;
136 errorReason = "Header field empty";
137 } else if ((std::isblank(headerFieldName.back()) != 0) || (std::isblank(headerFieldName.front()) != 0)) {
138 errorCode = 400;
139 errorReason = "White space before or after field";
140 } else if (value.empty()) {
141 errorCode = 400;
142 errorReason = "Value of field \"" + headerFieldName + "\" empty";
143 } else {
144 if (fieldsExpected.empty() || fieldsExpected.contains(headerFieldName)) {
146
147 if (!fields.contains(headerFieldName)) {
148 fields.emplace(headerFieldName, value);
149 } else {
150 fields[headerFieldName] += "," + value;
151 }
152 } else if (!fieldsExpected.empty() && !fieldsExpected.contains(headerFieldName)) {
153 errorCode = 400;
154 errorReason = "Field '" + headerFieldName + "' not in expected fields";
155 }
156 }
157 }
158
159 web::http::CiStringMap<std::string>&& Fields::getHeader() {
160 return std::move(fields);
161 }
162
163 bool Fields::isComplete() const {
164 return completed;
165 }
166
167 bool Fields::isError() const {
168 return errorCode != 0;
169 }
170
171 int Fields::getErrorCode() const {
172 return errorCode;
173 }
174
175 std::string Fields::getErrorReason() {
176 return errorReason;
177 }
178
179} // namespace web::http::decoder
#define MAX_LINE_LENGTH
Definition Fields.cpp:55
std::size_t readFromPeer(char *chunk, std::size_t chunklen) const final
std::string getErrorReason()
Definition Fields.cpp:175
Fields(core::socket::stream::SocketContext *socketContext, std::set< std::string > fieldsExpected={})
Definition Fields.cpp:60
core::socket::stream::SocketContext * socketContext
Definition Fields.h:85
web::http::CiStringMap< std::string > fields
Definition Fields.h:87
void splitLine(const std::string &line)
Definition Fields.cpp:131
std::size_t maxLineLength
Definition Fields.h:91
void setFieldsExpected(std::set< std::string > fieldsExpected)
Definition Fields.cpp:66
std::set< std::string > fieldsExpected
Definition Fields.h:88
web::http::CiStringMap< std::string > && getHeader()
Definition Fields.cpp:159
std::string errorReason
Definition Fields.h:95
std::pair< std::string, std::string > str_split(const std::string &base, char c_middle)
std::string & str_trimm(std::string &text)