summaryrefslogtreecommitdiff
path: root/crow/include/crow/socket_adaptors.h
blob: a47697f2a5d8141220ccf5ff48f5d1e12c8fb924 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#pragma once
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>

#include "crow/logging.h"

#ifdef BMCWEB_ENABLE_SSL
#include <boost/asio/ssl.hpp>
#endif
namespace crow
{
using namespace boost;
using tcp = asio::ip::tcp;

struct SocketAdaptor
{
    using streamType = tcp::socket;
    using secure = std::false_type;
    using context = void;
    SocketAdaptor(boost::asio::io_service& ioService, context* /*unused*/) :
        socketCls(ioService)
    {
    }

    boost::asio::io_service& getIoService()
    {
        return socketCls.get_io_service();
    }

    tcp::socket& rawSocket()
    {
        return socketCls;
    }

    tcp::socket& socket()
    {
        return socketCls;
    }

    std::string remoteEndpoint()
    {
        boost::system::error_code ec;
        tcp::endpoint ep = socketCls.remote_endpoint(ec);
        if (ec)
        {
            return "";
        }
        return boost::lexical_cast<std::string>(ep);
    }

    bool isOpen()
    {
        return socketCls.is_open();
    }

    void close()
    {
        socketCls.close();
    }

    template <typename F> void start(F f)
    {
        boost::system::error_code ec;
        f(ec);
    }

    tcp::socket socketCls;
};

struct TestSocketAdaptor
{
    using secure = std::false_type;
    using context = void;
    TestSocketAdaptor(boost::asio::io_service& ioService, context* /*unused*/) :
        socketCls(ioService)
    {
    }

    boost::asio::io_service& getIoService()
    {
        return socketCls.get_io_service();
    }

    tcp::socket& rawSocket()
    {
        return socketCls;
    }

    tcp::socket& socket()
    {
        return socketCls;
    }

    std::string remoteEndpoint()
    {
        return "Testhost";
    }

    bool isOpen()
    {
        return socketCls.is_open();
    }

    void close()
    {
        socketCls.close();
    }

    template <typename F> void start(F f)
    {
        f(boost::system::error_code());
    }

    tcp::socket socketCls;
};

#ifdef BMCWEB_ENABLE_SSL
struct SSLAdaptor
{
    using streamType = boost::asio::ssl::stream<tcp::socket>;
    using secure = std::true_type;
    using context = boost::asio::ssl::context;
    using ssl_socket_t = boost::asio::ssl::stream<tcp::socket>;
    SSLAdaptor(boost::asio::io_service& ioService, context* ctx) :
        sslSocket(new ssl_socket_t(ioService, *ctx))
    {
    }

    boost::asio::ssl::stream<tcp::socket>& socket()
    {
        return *sslSocket;
    }

    tcp::socket::lowest_layer_type& rawSocket()
    {
        return sslSocket->lowest_layer();
    }

    std::string remoteEndpoint()
    {
        boost::system::error_code ec;
        tcp::endpoint ep = rawSocket().remote_endpoint(ec);
        if (ec)
        {
            return "";
        }
        return boost::lexical_cast<std::string>(ep);
    }

    bool isOpen()
    {
        /*TODO(ed) this is a bit of a cheat.
         There are cases  when running a websocket where sslSocket might have
        std::move() called on it (to transfer ownership to
        websocket::Connection) and be empty.  This (and the check on close()) is
        a cheat to do something sane in this scenario. the correct fix would
        likely involve changing the http parser to return a specific code
        meaning "has been upgraded" so that the doRead function knows not to try
        to close the Connection which would fail, because the adapter is gone.
        As is, doRead believes the parse failed, because isOpen now returns
        False (which could also mean the client disconnected during parse)
        UPdate: The parser does in fact have an "isUpgrade" method that is
        intended for exactly this purpose.  Todo is now to make doRead obey the
        flag appropriately so this code can be changed back.
        */
        if (sslSocket != nullptr)
        {
            return sslSocket->lowest_layer().is_open();
        }
        return false;
    }

    void close()
    {
        if (sslSocket == nullptr)
        {
            return;
        }
        boost::system::error_code ec;

        // Shut it down
        this->sslSocket->lowest_layer().close();
    }

    boost::asio::io_service& getIoService()
    {
        return rawSocket().get_io_service();
    }

    template <typename F> void start(F f)
    {
        sslSocket->async_handshake(
            boost::asio::ssl::stream_base::server,
            [f](const boost::system::error_code& ec) { f(ec); });
    }

    std::unique_ptr<boost::asio::ssl::stream<tcp::socket>> sslSocket;
};
#endif
} // namespace crow