SNode.C
Loading...
Searching...
No Matches
SocketContext.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/server/SocketContext.h" // IWYU pragma: export
43
44#include "core/EventReceiver.h"
45#include "core/socket/stream/SocketConnection.h"
46#include "web/http/StatusCodes.h"
47#include "web/http/server/Response.h"
48
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
50
51#include "log/Logger.h"
52#include "utils/Timeval.h"
53
54#include <string>
55#include <utility>
56
57#endif /* DOXYGEN_SHOULD_SKIP_THIS */
58
59namespace web::http::server {
60
62 core::socket::stream::SocketConnection* socketConnection,
63 const std::function<void(const std::shared_ptr<Request>&, const std::shared_ptr<Response>&)>& onRequestReady)
64 : Super(socketConnection)
65 , onRequestReady(onRequestReady)
66 , masterResponse(std::make_shared<Response>(this))
67 , parser(
68 this,
69 [this]() {
70 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: Request start";
71 },
72 [this](web::http::server::Request&& request) {
73 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: Request parse success: " << request.method << " "
74 << request.url << " HTTP/" << request.httpMajor << "." << request.httpMinor;
75
76 pendingRequests.emplace_back(std::make_shared<Request>(std::move(request)));
77
78 if (pendingRequests.size() == 1) {
80 }
81 },
82 [this](int status, const std::string& reason) {
83 LOG(ERROR) << getSocketConnection()->getConnectionName() << " HTTP: Request parse error: " << reason << " (" << status
84 << ") ";
86
87 pendingRequests.emplace_back(std::make_shared<Request>(Request(status, reason)));
88
89 if (pendingRequests.size() == 1) {
91 }
92 }) {
93 }
94
95 SocketContext* SocketContext::onConnected(std::function<void()> onConnectEventReceiver) {
96 onConnectEventReceiverList.push_back(std::move(onConnectEventReceiver));
97
98 return this;
99 }
100
101 SocketContext* SocketContext::onDisconnected(std::function<void()> onDisconnectEventReceiver) {
102 onDisconnectEventReceiverList.push_back(std::move(onDisconnectEventReceiver));
103
104 return this;
105 }
106
108 if (!pendingRequests.empty()) {
109 const std::shared_ptr<Request>& pendingRequest = pendingRequests.front();
110
111 if (pendingRequest->status == 0) {
112 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: Request deliver: " << pendingRequest->method << " "
113 << pendingRequest->url << " HTTP/" << pendingRequest->httpMajor << "." << pendingRequest->httpMinor;
114
116 masterResponse->requestMethod = pendingRequest->method;
117 masterResponse->httpMajor = pendingRequest->httpMajor;
118 masterResponse->httpMinor = pendingRequest->httpMinor;
119
120 const std::string connection = pendingRequest->get("Connection");
121 if (!connection.empty()) {
122 masterResponse->set("Connection", connection);
123 }
124
125 onRequestReady(pendingRequest, masterResponse);
126 } else {
130
131 masterResponse->status(pendingRequest->status).send(pendingRequest->reason);
132 }
133 } else {
134 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: No more pending request";
135 }
136 }
137
138 void SocketContext::responseStarted(const Response& response) {
139 if (!pendingRequests.empty()) {
140 const std::shared_ptr<Request>& pendingRequest = pendingRequests.front();
141
142 serverSentEvent = web::http::ciContains(pendingRequest->get("Accept"), "text/event-stream");
143
144 if (serverSentEvent) {
146 }
147
148 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: Response start for request: " << pendingRequest->method
149 << " " << pendingRequest->url << " HTTP/" << pendingRequest->httpMajor << "." << pendingRequest->httpMinor;
151 << "HTTP/" + std::to_string(response.httpMajor)
152 .append(".")
153 .append(std::to_string(response.httpMinor))
154 .append(" ")
155 .append(std::to_string(response.statusCode))
156 .append(" ")
157 .append(StatusCode::reason(response.statusCode));
158 }
159 }
160
161 void SocketContext::responseCompleted(const Response& response, bool success) {
162 if (success) {
163 requestCompleted(response);
164 } else {
165 LOG(WARNING) << getSocketConnection()->getConnectionName() << " HTTP: Response completed with error: " << response.statusCode
166 << " " << StatusCode::reason(response.statusCode);
167
168 shutdownWrite(true);
169 }
170 }
171
172 void SocketContext::requestCompleted(const Response& response) {
173 const std::shared_ptr<Request> request = std::move(pendingRequests.front());
174 pendingRequests.pop_front();
175
176 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: Response completed for request: " << request->method << " "
177 << request->url << " HTTP/" << request->httpMajor << "." << request->httpMinor;
179 << "HTTP/" + std::to_string(response.httpMajor)
180 .append(".")
181 .append(std::to_string(response.httpMinor))
182 .append(" ")
183 .append(std::to_string(response.statusCode))
184 .append(" ")
185 .append(StatusCode::reason(response.statusCode));
186
189 ((response.httpMajor == 0 && response.httpMinor == 9) || (response.httpMajor == 1 && response.httpMinor == 0)));
190
191 if (httpClose) {
192 LOG(DEBUG) << getSocketConnection()->getConnectionName() << " HTTP: Connection = Close";
193
194 shutdownWrite(true);
195 } else {
196 LOG(DEBUG) << getSocketConnection()->getConnectionName() << " HTTP: Connection = Keep-Alive";
197
198 if (!pendingRequests.empty()) {
199 core::EventReceiver::atNextTick([this, response = std::weak_ptr<Response>(masterResponse)]() {
200 if (!response.expired()) {
202 }
203 });
204 }
205 }
206 }
207
209 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: Connected";
210
211 for (auto& onConnectEventReceiver : onConnectEventReceiverList) {
212 onConnectEventReceiver();
213 }
214 }
215
217 std::size_t consumed = 0;
218
219 if (!serverSentEvent && !httpClose) {
220 consumed = parser.parse();
221 }
222
223 return consumed;
224 }
225
228
229 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: Received disconnect";
230
231 for (auto& onDisconnectEventReceiver : onDisconnectEventReceiverList) {
232 onDisconnectEventReceiver();
233 }
234 }
235
236 bool SocketContext::onSignal(int signum) {
237 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: Received signal " << signum;
238
239 return true;
240 }
241
243 // Do nothing in case of an write error
244 }
245
246} // namespace web::http::server
static void atNextTick(const std::function< void(void)> &callBack)
virtual void setReadTimeout(const utils::Timeval &timeout)=0
const std::string & getConnectionName() const
SocketConnection * getSocketConnection() const
SocketContext(core::socket::stream::SocketConnection *socketConnection)
void shutdownWrite(bool forceClose=false)
std::size_t parse()
Definition Parser.cpp:92
static std::string reason(int status)
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)
Request(int status, const std::string &reason="")
Definition Request.cpp:55
const std::string & get(const std::string &key, int i=0) const
Definition Request.cpp:60
Response & status(int statusCode)
Definition Response.cpp:104
void send(const std::string &chunk)
Definition Response.cpp:228
Response & set(const std::string &field, const std::string &value, bool overwrite=true)
Definition Response.cpp:130
ConnectionState connectionState
Definition Response.h:137
bool onSignal(int signum) override
std::list< std::function< void()> > onDisconnectEventReceiverList
void responseCompleted(const Response &response, bool success)
std::shared_ptr< Response > masterResponse
std::size_t onReceivedFromPeer() override
void onWriteError(int errnum) override
std::list< std::function< void()> > onConnectEventReceiverList
void responseStarted(const Response &response)
void requestCompleted(const Response &response)
SocketContext(core::socket::stream::SocketConnection *socketConnection, const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &onRequestReady)
std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> onRequestReady
SocketContext * onConnected(std::function< void()> onConnectEventReceiver)
SocketContext * onDisconnected(std::function< void()> onDisconnectEventReceiver)
std::list< std::shared_ptr< Request > > pendingRequests
bool ciContains(const std::string &str1, const std::string &str2)