From 01af51308e621a381654e9de912c1e0f64494833 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Wed, 7 Aug 2013 22:41:59 -0700 Subject: [PATCH] Add SslContext abstraction --- Builds/VisualStudio2012/beast.vcxproj | 8 +- Builds/VisualStudio2012/beast.vcxproj.filters | 9 +- modules/beast_asio/beast_asio.cpp | 2 + modules/beast_asio/beast_asio.h | 2 +- .../sockets/beast_SocketInterface.h | 78 +++++++++++++- .../beast_asio/sockets/beast_SocketWrapper.h | 2 +- .../sockets/beast_SocketWrapperBase.h | 101 ------------------ .../beast_asio/sockets/beast_SslContext.cpp | 18 ++++ modules/beast_asio/sockets/beast_SslContext.h | 41 +++++++ .../beast_asio/system/beast_BoostIncludes.h | 11 +- 10 files changed, 155 insertions(+), 117 deletions(-) delete mode 100644 modules/beast_asio/sockets/beast_SocketWrapperBase.h create mode 100644 modules/beast_asio/sockets/beast_SslContext.cpp create mode 100644 modules/beast_asio/sockets/beast_SslContext.h diff --git a/Builds/VisualStudio2012/beast.vcxproj b/Builds/VisualStudio2012/beast.vcxproj index cdc39ec662..a66eabb50e 100644 --- a/Builds/VisualStudio2012/beast.vcxproj +++ b/Builds/VisualStudio2012/beast.vcxproj @@ -75,7 +75,7 @@ - + @@ -279,6 +279,12 @@ + + true + true + true + true + true diff --git a/Builds/VisualStudio2012/beast.vcxproj.filters b/Builds/VisualStudio2012/beast.vcxproj.filters index a2c09e4306..c443c1bd32 100644 --- a/Builds/VisualStudio2012/beast.vcxproj.filters +++ b/Builds/VisualStudio2012/beast.vcxproj.filters @@ -752,9 +752,6 @@ beast_asio\sockets - - beast_asio\sockets - beast_asio\sockets @@ -770,6 +767,9 @@ beast_asio\system + + beast_asio\sockets + @@ -1189,6 +1189,9 @@ beast_asio + + beast_asio\sockets + diff --git a/modules/beast_asio/beast_asio.cpp b/modules/beast_asio/beast_asio.cpp index d0855e1a88..b9e45eae60 100644 --- a/modules/beast_asio/beast_asio.cpp +++ b/modules/beast_asio/beast_asio.cpp @@ -24,4 +24,6 @@ namespace beast { +#include "sockets/beast_SslContext.cpp" + } diff --git a/modules/beast_asio/beast_asio.h b/modules/beast_asio/beast_asio.h index 930cb4268a..12646080c7 100644 --- a/modules/beast_asio/beast_asio.h +++ b/modules/beast_asio/beast_asio.h @@ -51,9 +51,9 @@ namespace beast #include "sockets/beast_SocketBase.h" #include "sockets/beast_Socket.h" #include "sockets/beast_SocketInterface.h" -#include "sockets/beast_SocketWrapperBase.h" #include "sockets/beast_SocketWrapper.h" #include "sockets/beast_SharedSocket.h" +#include "sockets/beast_SslContext.h" } diff --git a/modules/beast_asio/sockets/beast_SocketInterface.h b/modules/beast_asio/sockets/beast_SocketInterface.h index c320408484..947ae8e50f 100644 --- a/modules/beast_asio/sockets/beast_SocketInterface.h +++ b/modules/beast_asio/sockets/beast_SocketInterface.h @@ -34,7 +34,7 @@ struct SocketInterface struct SyncStream { }; struct AsyncStream { }; struct Stream : SyncStream, AsyncStream { }; - + /** Tags for compatibility with asio::ssl::stream http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/ssl__stream.html */ @@ -43,6 +43,82 @@ struct SocketInterface struct AsyncHandshake { }; struct AsyncBufferedHandshake : AsyncHandshake{ }; struct Handshake : SyncBufferedHandshake, AsyncBufferedHandshake { }; + +protected: + //-------------------------------------------------------------------------- + + /** Template specialization to determine available interfaces. */ + template + struct InterfacesOf + { + /** Intrusive tag support. + + To use this, add a struct called SocketInterfaces to your + class and derive it from the interfaces that you support. + For example: + + @code + + struct MyHandshakingStream + { + struct SocketInterfaces + : SocketInterface::Stream + , SocketInterface::Handshake + { + }; + } + + @endcode + */ + typedef typename Object::SocketInterfaces type; + typedef type value; + }; + + // Specialization for boost::asio::basic_socket + template + struct InterfacesOf > + { + struct value : SocketInterface::Socket { }; + typedef value type; + }; + + // Specialization for boost::asio::basic_stream_socket + template + struct InterfacesOf > + { + struct value : SocketInterface::Socket, SocketInterface::Stream { }; + typedef value type; + }; + + // Specialization for boost::asio::ssl::stream + template + struct InterfacesOf > + { + struct value : SocketInterface::Stream , SocketInterface::Handshake { }; + typedef value type; + }; + +#if 1 + // Less elegant, but works. + // Determines if Object supports the specified Interface + template + struct HasInterface : boost::false_type { }; + + template + struct HasInterface ::type> >::type > + : boost::true_type { }; +#else + // This should work, but doesn't. + // K-ballo from #boost suggested it. + // + // Determines if Object supports the specified Interface + template + struct HasInterface : boost::is_base_of > + { + }; +#endif }; #endif diff --git a/modules/beast_asio/sockets/beast_SocketWrapper.h b/modules/beast_asio/sockets/beast_SocketWrapper.h index 72da7f5ff1..4bb7c70c29 100644 --- a/modules/beast_asio/sockets/beast_SocketWrapper.h +++ b/modules/beast_asio/sockets/beast_SocketWrapper.h @@ -34,7 +34,7 @@ */ template class SocketWrapper - : public SocketWrapperBase + : public SocketInterface , public virtual Socket { public: diff --git a/modules/beast_asio/sockets/beast_SocketWrapperBase.h b/modules/beast_asio/sockets/beast_SocketWrapperBase.h deleted file mode 100644 index 60ee33fb59..0000000000 --- a/modules/beast_asio/sockets/beast_SocketWrapperBase.h +++ /dev/null @@ -1,101 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2013, Vinnie Falco - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ -//============================================================================== - -#ifndef BEAST_SOCKETWRAPPERBASE_H_INCLUDED -#define BEAST_SOCKETWRAPPERBASE_H_INCLUDED - -/** Base class of wrappper provides the interface tags. */ -struct SocketWrapperBase -{ - /** Template specialization to determine available interfaces. */ - template - struct InterfacesOf - { - /** Intrusive tag support. - - To use this, add a struct called SocketInterfaces to your - class and derive it from the interfaces that you support. - For example: - - @code - - struct MyHandshakingStream - { - struct SocketInterfaces - : SocketInterface::Stream - , SocketInterface::Handshake - { - }; - } - - @endcode - */ - typedef typename Object::SocketInterfaces type; - typedef type value; - }; - - // Specialization for boost::asio::basic_socket - template - struct InterfacesOf > - { - struct value : SocketInterface::Socket { }; - typedef value type; - }; - - // Specialization for boost::asio::basic_stream_socket - template - struct InterfacesOf > - { - struct value : SocketInterface::Socket, SocketInterface::Stream { }; - typedef value type; - }; - - // Specialization for boost::asio::ssl::stream - template - struct InterfacesOf > - { - struct value : SocketInterface::Stream , SocketInterface::Handshake { }; - typedef value type; - }; - -#if 1 - // Less elegant, but works. - // Determines if Object supports the specified Interface - template - struct HasInterface : boost::false_type { }; - - template - struct HasInterface ::type> >::type > - : boost::true_type { }; -#else - // This should work, but doesn't. - // K-ballo from #boost suggested it. - // - // Determines if Object supports the specified Interface - template - struct HasInterface : boost::is_base_of > - { - }; -#endif - -}; - -#endif diff --git a/modules/beast_asio/sockets/beast_SslContext.cpp b/modules/beast_asio/sockets/beast_SslContext.cpp new file mode 100644 index 0000000000..b6865169f0 --- /dev/null +++ b/modules/beast_asio/sockets/beast_SslContext.cpp @@ -0,0 +1,18 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== diff --git a/modules/beast_asio/sockets/beast_SslContext.h b/modules/beast_asio/sockets/beast_SslContext.h new file mode 100644 index 0000000000..f9a48952d5 --- /dev/null +++ b/modules/beast_asio/sockets/beast_SslContext.h @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_SSLCONTEXT_H_INCLUDED +#define BEAST_SSLCONTEXT_H_INCLUDED + +/** An SSL context that wraps a boost::asio::ssl::context. */ +class SslContextBase +{ +public: + typedef boost::asio::ssl::context BoostContextType; + virtual ~SslContextBase () { } + + /** Conversion to boost::asio::ssl::context + This lets you pass this object where the real thing is expected. + */ + operator BoostContextType& () + { + return getBoostContext (); + } + + virtual BoostContextType& getBoostContext () noexcept = 0; +}; + +#endif diff --git a/modules/beast_asio/system/beast_BoostIncludes.h b/modules/beast_asio/system/beast_BoostIncludes.h index b3de32c528..4db3e957dd 100644 --- a/modules/beast_asio/system/beast_BoostIncludes.h +++ b/modules/beast_asio/system/beast_BoostIncludes.h @@ -44,13 +44,6 @@ #include #include -//#include -//#include -//#include -//#include -//#include -//#include - //------------------------------------------------------------------------------ // Configure some options based on the version of boost @@ -72,10 +65,10 @@ void_or_deduced # elif defined(_MSC_VER) && (_MSC_VER < 1500) # define BOOST_ASIO_INITFN_RESULT_TYPE_MEMBER(h, sig) \ - ::boost::asio::detail::async_result_type_helper::type + boost::asio::detail::async_result_type_helper::type # else # define BOOST_ASIO_INITFN_RESULT_TYPE_MEMBER(h, sig) \ - ::boost::asio::async_result <::boost::asio::handler_type::type>::type + boost::asio::async_result ::type>::type # endif #endif