Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 2 | #include "crow/logging.h" |
| 3 | #include "crow/settings.h" |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 4 | #include <boost/asio.hpp> |
| 5 | #ifdef CROW_ENABLE_SSL |
| 6 | #include <boost/asio/ssl.hpp> |
| 7 | #endif |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 8 | namespace crow { |
| 9 | using namespace boost; |
| 10 | using tcp = asio::ip::tcp; |
| 11 | |
| 12 | struct SocketAdaptor { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 13 | using secure = std::false_type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 14 | using context = void; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 15 | SocketAdaptor(boost::asio::io_service& io_service, context* /*unused*/) |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 16 | : socket_(io_service) {} |
| 17 | |
| 18 | boost::asio::io_service& get_io_service() { return socket_.get_io_service(); } |
| 19 | |
| 20 | tcp::socket& raw_socket() { return socket_; } |
| 21 | |
| 22 | tcp::socket& socket() { return socket_; } |
| 23 | |
| 24 | tcp::endpoint remote_endpoint() { return socket_.remote_endpoint(); } |
| 25 | |
| 26 | bool is_open() { return socket_.is_open(); } |
| 27 | |
| 28 | void close() { socket_.close(); } |
| 29 | |
| 30 | template <typename F> |
| 31 | void start(F f) { |
| 32 | f(boost::system::error_code()); |
| 33 | } |
| 34 | |
| 35 | tcp::socket socket_; |
| 36 | }; |
| 37 | |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 38 | struct TestSocketAdaptor { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 39 | using secure = std::false_type; |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 40 | using context = void; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 41 | TestSocketAdaptor(boost::asio::io_service& io_service, context* /*unused*/) |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 42 | : socket_(io_service) {} |
| 43 | |
| 44 | boost::asio::io_service& get_io_service() { return socket_.get_io_service(); } |
| 45 | |
| 46 | tcp::socket& raw_socket() { return socket_; } |
| 47 | |
| 48 | tcp::socket& socket() { return socket_; } |
| 49 | |
| 50 | tcp::endpoint remote_endpoint() { return socket_.remote_endpoint(); } |
| 51 | |
| 52 | bool is_open() { return socket_.is_open(); } |
| 53 | |
| 54 | void close() { socket_.close(); } |
| 55 | |
| 56 | template <typename F> |
| 57 | void start(F f) { |
| 58 | f(boost::system::error_code()); |
| 59 | } |
| 60 | |
| 61 | tcp::socket socket_; |
| 62 | }; |
| 63 | |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 64 | #ifdef CROW_ENABLE_SSL |
| 65 | struct SSLAdaptor { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 66 | using secure = std::true_type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 67 | using context = boost::asio::ssl::context; |
| 68 | using ssl_socket_t = boost::asio::ssl::stream<tcp::socket>; |
| 69 | SSLAdaptor(boost::asio::io_service& io_service, context* ctx) |
| 70 | : ssl_socket_(new ssl_socket_t(io_service, *ctx)) {} |
| 71 | |
| 72 | boost::asio::ssl::stream<tcp::socket>& socket() { return *ssl_socket_; } |
| 73 | |
| 74 | tcp::socket::lowest_layer_type& raw_socket() { |
| 75 | return ssl_socket_->lowest_layer(); |
| 76 | } |
| 77 | |
| 78 | tcp::endpoint remote_endpoint() { return raw_socket().remote_endpoint(); } |
| 79 | |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 80 | bool is_open() { |
| 81 | /*TODO(ed) this is a bit of a cheat. |
| 82 | There are cases when running a websocket where ssl_socket_ might have |
| 83 | std::move() called on it (to transfer ownership to websocket::Connection) |
| 84 | and be empty. This (and the check on close()) is a cheat to do something |
| 85 | sane in this scenario. the correct fix would likely involve changing the |
| 86 | http parser to return a specific code meaning "has been upgraded" so that |
| 87 | the do_read function knows not to try to close the connection which would |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 88 | fail, because the adapter is gone. As is, do_read believes the parse |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 89 | failed, because is_open now returns False (which could also mean the client |
| 90 | disconnected during parse) |
Ed Tanous | 4c3cbc6 | 2017-05-16 09:17:42 -0700 | [diff] [blame] | 91 | UPdate: The parser does in fact have an "is_upgrade" method that is intended |
Ed Tanous | 0d485ef | 2017-05-23 09:23:53 -0700 | [diff] [blame] | 92 | for exactly this purpose. Todo is now to make do_read obey the flag |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 93 | appropriately so this code can be changed back. |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 94 | */ |
| 95 | if (ssl_socket_ != nullptr) { |
| 96 | return ssl_socket_->lowest_layer().is_open(); |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 97 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 98 | return false; |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 99 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 100 | |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 101 | void close() { |
| 102 | if (ssl_socket_ == nullptr) { |
| 103 | return; |
| 104 | } |
Ed Tanous | 0d485ef | 2017-05-23 09:23:53 -0700 | [diff] [blame] | 105 | boost::system::error_code ec; |
| 106 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 107 | // Shut it down |
Ed Tanous | 0d485ef | 2017-05-23 09:23:53 -0700 | [diff] [blame] | 108 | this->ssl_socket_->lowest_layer().close(); |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 109 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 110 | |
| 111 | boost::asio::io_service& get_io_service() { |
| 112 | return raw_socket().get_io_service(); |
| 113 | } |
| 114 | |
| 115 | template <typename F> |
| 116 | void start(F f) { |
| 117 | ssl_socket_->async_handshake( |
| 118 | boost::asio::ssl::stream_base::server, |
| 119 | [f](const boost::system::error_code& ec) { f(ec); }); |
| 120 | } |
| 121 | |
| 122 | std::unique_ptr<boost::asio::ssl::stream<tcp::socket>> ssl_socket_; |
| 123 | }; |
| 124 | #endif |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 125 | } // namespace crow |