TrinityCore
HttpSslSocket.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITYCORE_HTTP_SSL_SOCKET_H
19#define TRINITYCORE_HTTP_SSL_SOCKET_H
20
21#include "BaseHttpSocket.h"
22#include "SslSocket.h"
23#include <boost/beast/core/stream_traits.hpp>
24#include <boost/beast/core/tcp_stream.hpp>
25#include <boost/beast/ssl/ssl_stream.hpp>
26
27namespace Trinity::Net::Http
28{
29namespace Impl
30{
31class BoostBeastSslSocketWrapper : public ::SslSocket<boost::beast::ssl_stream<boost::beast::tcp_stream>>
32{
33public:
35
36 void shutdown(boost::asio::socket_base::shutdown_type what, boost::system::error_code& shutdownError)
37 {
38 _sslSocket.shutdown(shutdownError);
39 boost::beast::get_lowest_layer(_sslSocket).socket().shutdown(what, shutdownError);
40 }
41
42 void close(boost::system::error_code& /*error*/)
43 {
44 boost::beast::get_lowest_layer(_sslSocket).close();
45 }
46
47 boost::asio::ip::tcp::socket::endpoint_type remote_endpoint() const
48 {
49 return boost::beast::get_lowest_layer(_sslSocket).socket().remote_endpoint();
50 }
51};
52}
53
54template <typename Derived>
55class SslSocket : public BaseSocket<Derived, Impl::BoostBeastSslSocketWrapper>
56{
58
59public:
60 explicit SslSocket(boost::asio::ip::tcp::socket&& socket, boost::asio::ssl::context& sslContext)
61 : SocketBase(std::move(socket), sslContext) { }
62
63 SslSocket(SslSocket const& other) = delete;
64 SslSocket(SslSocket&& other) = delete;
65 SslSocket& operator=(SslSocket const& other) = delete;
66 SslSocket& operator=(SslSocket&& other) = delete;
67
68 ~SslSocket() = default;
69
70 void Start() override
71 {
72 this->AsyncHandshake();
73 }
74
76 {
77 this->underlying_stream().async_handshake(boost::asio::ssl::stream_base::server,
78 [self = this->shared_from_this()](boost::system::error_code const& error) { self->HandshakeHandler(error); });
79 }
80
81 void HandshakeHandler(boost::system::error_code const& error)
82 {
83 if (error)
84 {
85 TC_LOG_ERROR("server.http.session.ssl", "{} SSL Handshake failed {}", this->GetClientInfo(), error.message());
86 this->CloseSocket();
87 return;
88 }
89
90 this->ResetHttpParser();
91
92 this->AsyncRead();
93 }
94};
95}
96
97#endif // TRINITYCORE_HTTP_SSL_SOCKET_H
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:165
Impl::BoostBeastSslSocketWrapper & underlying_stream()
Definition: Socket.h:210
boost::beast::ssl_stream< boost::beast::tcp_stream > _sslSocket
Definition: SslSocket.h:85
void shutdown(boost::asio::socket_base::shutdown_type what, boost::system::error_code &shutdownError)
Definition: HttpSslSocket.h:36
boost::asio::ip::tcp::socket::endpoint_type remote_endpoint() const
Definition: HttpSslSocket.h:47
SslSocket & operator=(SslSocket const &other)=delete
SslSocket(boost::asio::ip::tcp::socket &&socket, boost::asio::ssl::context &sslContext)
Definition: HttpSslSocket.h:60
SslSocket(SslSocket &&other)=delete
SslSocket & operator=(SslSocket &&other)=delete
SslSocket(SslSocket const &other)=delete
void HandshakeHandler(boost::system::error_code const &error)
Definition: HttpSslSocket.h:81
STL namespace.