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#include "web/http/server/SocketContext.h" // IWYU pragma: export
21
22#include "core/EventReceiver.h"
23#include "core/socket/stream/SocketConnection.h"
24#include "web/http/server/Response.h"
25
26#ifndef DOXYGEN_SHOULD_SKIP_THIS
27
28#include "log/Logger.h"
29
30#include <string>
31#include <utility>
32
33#endif /* DOXYGEN_SHOULD_SKIP_THIS */
34
35namespace web::http::server {
36
38 core::socket::stream::SocketConnection* socketConnection,
39 const std::function<void(const std::shared_ptr<Request>&, const std::shared_ptr<Response>&)>& onRequestReady)
43 , parser(
44 this,
45 [this]() {
46 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP Request started";
47 },
48 [this](web::http::server::Request&& request) {
49 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP Request parsed: " << request.method << " "
50 << request.url << " HTTP/" << request.httpMajor << "." << request.httpMinor;
51
53
54 if (pendingRequests.size() == 1) {
56 }
57 },
58 [this](int status, const std::string& reason) {
59 LOG(ERROR) << getSocketConnection()->getConnectionName() << " HTTP Request parse error: " << reason << " (" << status
60 << ") ";
61
62 shutdownWrite(true);
63 }) {
64 }
65
67 if (!pendingRequests.empty()) {
68 const std::shared_ptr<Request>& pendingRequest = pendingRequests.front();
69
70 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP Request ready: " << pendingRequest->method << " "
71 << pendingRequest->url << " HTTP/" << pendingRequest->httpMajor << "." << pendingRequest->httpMinor;
72
73 masterResponse->init();
74 masterResponse->httpMajor = pendingRequest->httpMajor;
75 masterResponse->httpMinor = pendingRequest->httpMinor;
76
77 const std::string connection = pendingRequest->get("Connection");
78 if (!connection.empty()) {
79 masterResponse->set("Connection", connection);
80 }
81
82 onRequestReady(pendingRequest, masterResponse);
83 } else {
84 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP Request: No more pending";
85 }
86 }
87
89 const std::shared_ptr<Request>& pendingRequest = pendingRequests.front();
90
91 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP Response started: " << pendingRequest->method << " "
92 << pendingRequest->url << " HTTP/" << pendingRequest->httpMajor << "." << pendingRequest->httpMinor;
93 }
94
95 void SocketContext::responseCompleted(bool success) {
96 if (success) {
97 const std::shared_ptr<Request>& pendingRequest = pendingRequests.front();
98
99 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP Response completed: " << pendingRequest->method << " "
100 << pendingRequest->url << " HTTP/" << pendingRequest->httpMajor << "." << pendingRequest->httpMinor;
101
102 httpClose = masterResponse->connectionState == ConnectionState::Close ||
103 (masterResponse->connectionState == ConnectionState::Default &&
104 ((masterResponse->httpMajor == 0 && masterResponse->httpMinor == 9) ||
105 (masterResponse->httpMajor == 1 && masterResponse->httpMinor == 0)));
106
108 } else {
109 LOG(WARNING) << getSocketConnection()->getConnectionName() << " HTTP Response wrong content length";
110
111 shutdownWrite(true);
112 }
113 }
114
116 const std::shared_ptr<Request>& pendingRequest = pendingRequests.front();
117
118 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP Request completed: " << pendingRequest->method << " "
119 << pendingRequest->url << " HTTP/" << pendingRequest->httpMajor << "." << pendingRequest->httpMinor;
120
121 if (httpClose) {
122 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: Connection = Close";
123
124 shutdownWrite();
125 } else {
126 LOG(DEBUG) << getSocketConnection()->getConnectionName() << " HTTP: Connection = Keep-Alive";
127
128 core::EventReceiver::atNextTick([this, response = static_cast<std::weak_ptr<Response>>(this->masterResponse)]() {
129 if (!response.expired()) {
130 pendingRequests.pop_front();
132 }
133 });
134 }
135 }
136
138 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: Connected";
139
140 masterResponse->init();
141 }
142
144 std::size_t consumed = 0;
145
146 if (!httpClose) {
147 consumed = parser.parse();
148 }
149
150 return consumed;
151 }
152
154 if (masterResponse != nullptr) {
155 masterResponse->stopResponse();
156 }
157
158 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: Disconnected";
159 }
160
161 bool SocketContext::onSignal(int signum) {
162 LOG(INFO) << getSocketConnection()->getConnectionName() << " HTTP: Received signal " << signum;
163
164 return true;
165 }
166
168 // Do nothing in case of an write error
169 }
170
171} // namespace web::http::server
bool onSignal(int signum) override
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)