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#include "web/websocket/server/SocketContextUpgradeFactory.h"
21
22#include "web/http/server/Request.h"
23#include "web/http/server/Response.h"
24#include "web/websocket/server/SocketContextUpgrade.h"
25
26#ifndef DOXYGEN_SHOULD_SKIP_THIS
27
28#include "utils/base64.h"
29#include "web/http/http_utils.h"
30
31#include <list>
32#include <tuple>
33
34#endif /* DOXYGEN_SHOULD_SKIP_THIS */
35
36namespace web::websocket::server {
37
38 std::string SocketContextUpgradeFactory::name() {
39 return "websocket";
40 }
41
42 http::SocketContextUpgrade<web::http::server::Request, web::http::server::Response>*
43 SocketContextUpgradeFactory::create(core::socket::stream::SocketConnection* socketConnection,
44 web::http::server::Request* request,
45 web::http::server::Response* response) {
46 SocketContextUpgrade* socketContext = nullptr;
47
48 if (request->get("Sec-WebSocket-Version") == "13") {
49 std::string requestedSubProtocolNames = request->get("sec-websocket-protocol");
50
51 std::list<std::string> subProtocolNamesList;
52 do {
53 std::string subProtocolName;
54 std::tie(subProtocolName, requestedSubProtocolNames) = httputils::str_split(requestedSubProtocolNames, ',');
55 httputils::str_trimm(subProtocolName);
56 subProtocolNamesList.push_back(subProtocolName);
57 } while (!requestedSubProtocolNames.empty());
58
59 if (!subProtocolNamesList.empty()) {
60 std::string selectedSubProtocolName;
61
62 socketContext = new SocketContextUpgrade(socketConnection, this);
63 selectedSubProtocolName = socketContext->loadSubProtocol(subProtocolNamesList);
64
65 if (!selectedSubProtocolName.empty()) {
66 response->set("Upgrade", "websocket");
67 response->set("Connection", "Upgrade");
68 response->set("Sec-WebSocket-Protocol", selectedSubProtocolName);
69 response->set("Sec-WebSocket-Accept", base64::serverWebSocketKey(request->get("sec-websocket-key")));
70
71 response->status(101); // Switch Protocol
72 } else {
73 delete socketContext;
74 socketContext = nullptr;
75
76 response->set("Connection", "close");
77 response->status(400);
78 }
79 } else {
80 checkRefCount();
81
82 response->set("Connection", "close");
83 response->status(400);
84 }
85 } else {
86 checkRefCount();
87
88 response->set("Sec-WebSocket-Version", "13");
89 response->set("Connection", "close");
90 response->status(426);
91 }
92
93 return socketContext;
94 }
95
96 void SocketContextUpgradeFactory::link() {
97 static bool linked = false;
98
99 if (!linked) {
100 web::http::server::SocketContextUpgradeFactory::link("websocket", websocketServerSocketContextUpgradeFactory);
101 linked = true;
102 }
103 }
104
106 return new SocketContextUpgradeFactory();
107 }
108
109} // namespace web::websocket::server
web::http::server::SocketContextUpgradeFactory * websocketServerSocketContextUpgradeFactory()