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/server/Response.h"
47
48#ifndef DOXYGEN_SHOULD_SKIP_THIS
49
50#include "log/Logger.h"
51
52#include <string>
53#include <utility>
54
55#endif /* DOXYGEN_SHOULD_SKIP_THIS */
56
57namespace web::http::server {
58
60 core::socket::stream::SocketConnection* socketConnection,
61 const std::function<void(const std::shared_ptr<Request>&, const std::shared_ptr<Response>&)>& onRequestReady)
62 : Super(socketConnection)
63 , onRequestReady(onRequestReady)
64 , masterResponse(std::make_shared<Response>(this))
65 , parser(
66 this,
67 [this]() {
68 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP Request started";
69 },
70 [this](web::http::server::Request&& request) {
71 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP Request parsed: " << request.method << " "
72 << request.url << " HTTP/" << request.httpMajor << "." << request.httpMinor;
73
74 pendingRequests.emplace_back(std::make_shared<Request>(std::move(request)));
75
76 if (pendingRequests.size() == 1) {
78 }
79 },
80 [this](int status, const std::string& reason) {
81 LOG(ERROR) << getSocketConnection()->getConnectionName() << " HTTP Request parse error: " << reason << " (" << status
82 << ") ";
83
85 }) {
86 }
87
89 if (!pendingRequests.empty()) {
90 const std::shared_ptr<Request>& pendingRequest = pendingRequests.front();
91
92 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP Request ready: " << pendingRequest->method << " "
93 << pendingRequest->url << " HTTP/" << pendingRequest->httpMajor << "." << pendingRequest->httpMinor;
94
96 masterResponse->httpMajor = pendingRequest->httpMajor;
97 masterResponse->httpMinor = pendingRequest->httpMinor;
98
99 const std::string connection = pendingRequest->get("Connection");
100 if (!connection.empty()) {
101 masterResponse->set("Connection", connection);
102 }
103
104 onRequestReady(pendingRequest, masterResponse);
105 } else {
106 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP Request: No more pending";
107 }
108 }
109
111 const std::shared_ptr<Request>& pendingRequest = pendingRequests.front();
112
113 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP Response started: " << pendingRequest->method << " "
114 << pendingRequest->url << " HTTP/" << pendingRequest->httpMajor << "." << pendingRequest->httpMinor;
115 }
116
117 void SocketContext::responseCompleted(bool success) {
118 if (success) {
119 const std::shared_ptr<Request>& pendingRequest = pendingRequests.front();
120
121 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP Response completed: " << pendingRequest->method << " "
122 << pendingRequest->url << " HTTP/" << pendingRequest->httpMajor << "." << pendingRequest->httpMinor;
123
128
130 } else {
131 LOG(WARNING) << getSocketConnection()->getConnectionName() << " HTTP Response wrong content length";
132
133 shutdownWrite(true);
134 }
135 }
136
138 const std::shared_ptr<Request>& pendingRequest = pendingRequests.front();
139
140 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP Request completed: " << pendingRequest->method << " "
141 << pendingRequest->url << " HTTP/" << pendingRequest->httpMajor << "." << pendingRequest->httpMinor;
142
143 if (httpClose) {
144 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: Connection = Close";
145
147 } else {
148 LOG(DEBUG) << getSocketConnection()->getConnectionName() << " HTTP: Connection = Keep-Alive";
149
150 core::EventReceiver::atNextTick([this, response = static_cast<std::weak_ptr<Response>>(this->masterResponse)]() {
151 if (!response.expired()) {
152 pendingRequests.pop_front();
154 }
155 });
156 }
157 }
158
160 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: Connected";
161
163 }
164
166 std::size_t consumed = 0;
167
168 if (!httpClose) {
169 consumed = parser.parse();
170 }
171
172 return consumed;
173 }
174
176 if (masterResponse != nullptr) {
178 }
179
180 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: Disconnected";
181 }
182
183 bool SocketContext::onSignal(int signum) {
184 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: Received signal " << signum;
185
186 return true;
187 }
188
190 // Do nothing in case of an write error
191 }
192
193} // namespace web::http::server
static void atNextTick(const std::function< void(void)> &callBack)
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
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)
const std::string & get(const std::string &key, int i=0) const
Definition Request.cpp:53
Response & set(const std::string &field, const std::string &value, bool overwrite=true)
Definition Response.cpp:128
ConnectionState connectionState
Definition Response.h:140
bool onSignal(int signum) override
std::shared_ptr< Response > masterResponse
std::size_t onReceivedFromPeer() override
void onWriteError(int errnum) override
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
std::list< std::shared_ptr< Request > > pendingRequests