SNode.C
Loading...
Searching...
No Matches
SocketContextUpgradeFactory.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/websocket/server/SocketContextUpgradeFactory.h"
43
44#include "web/http/server/Request.h"
45#include "web/http/server/Response.h"
46#include "web/websocket/server/SocketContextUpgrade.h"
47
48#ifndef DOXYGEN_SHOULD_SKIP_THIS
49
50#include "utils/base64.h"
51#include "web/http/http_utils.h"
52
53#include <list>
54#include <tuple>
55
56#endif /* DOXYGEN_SHOULD_SKIP_THIS */
57
58namespace web::websocket::server {
59
61 return "websocket";
62 }
63
64 http::SocketContextUpgrade<web::http::server::Request, web::http::server::Response>*
65 SocketContextUpgradeFactory::create(core::socket::stream::SocketConnection* socketConnection,
66 web::http::server::Request* request,
67 web::http::server::Response* response) {
68 SocketContextUpgrade* socketContext = nullptr;
69
70 if (request->get("Sec-WebSocket-Version") == "13") {
71 std::string requestedSubProtocolNames = request->get("sec-websocket-protocol");
72
73 std::list<std::string> subProtocolNamesList;
74 do {
75 std::string subProtocolName;
76 std::tie(subProtocolName, requestedSubProtocolNames) = httputils::str_split(requestedSubProtocolNames, ',');
77 httputils::str_trimm(subProtocolName);
78 subProtocolNamesList.push_back(subProtocolName);
79 } while (!requestedSubProtocolNames.empty());
80
81 if (!subProtocolNamesList.empty()) {
82 std::string selectedSubProtocolName;
83
84 socketContext = new SocketContextUpgrade(socketConnection, this);
85 selectedSubProtocolName = socketContext->loadSubProtocol(subProtocolNamesList);
86
87 if (!selectedSubProtocolName.empty()) {
88 response->set("Upgrade", "websocket");
89 response->set("Connection", "Upgrade");
90 response->set("Sec-WebSocket-Protocol", selectedSubProtocolName);
91 response->set("Sec-WebSocket-Accept", base64::serverWebSocketKey(request->get("sec-websocket-key")));
92
93 response->status(101); // Switch Protocol
94 } else {
95 delete socketContext;
96 socketContext = nullptr;
97
98 response->set("Connection", "close");
99 response->status(400);
100 }
101 } else {
103
104 response->set("Connection", "close");
105 response->status(400);
106 }
107 } else {
109
110 response->set("Sec-WebSocket-Version", "13");
111 response->set("Connection", "close");
112 response->status(426);
113 }
114
115 return socketContext;
116 }
117
119 static bool linked = false;
120
121 if (!linked) {
123 linked = true;
124 }
125 }
126
129 }
130
131} // namespace web::websocket::server
const std::string & get(const std::string &key, int i=0) const
Definition Request.cpp:53
Response & status(int statusCode)
Definition Response.cpp:102
Response & set(const std::string &field, const std::string &value, bool overwrite=true)
Definition Response.cpp:128
static void link(const std::string &upgradeContextName, SocketContextUpgradeFactory *(*linkedPlugin)())
http::SocketContextUpgrade< web::http::server::Request, web::http::server::Response > * create(core::socket::stream::SocketConnection *socketConnection, web::http::server::Request *request, web::http::server::Response *response) override
SocketContextUpgrade(core::socket::stream::SocketConnection *socketConnection, web::http::SocketContextUpgradeFactory< web::http::server::Request, web::http::server::Response > *socketContextUpgradeFactory)
std::string loadSubProtocol(const std::list< std::string > &subProtocolNames)
std::string serverWebSocketKey(const std::string &clientWebSocketKey)
Definition base64.cpp:56
std::pair< std::string, std::string > str_split(const std::string &base, char c_middle)
std::string & str_trimm(std::string &text)
web::http::server::SocketContextUpgradeFactory * websocketServerSocketContextUpgradeFactory()