@@ -27,6 +27,8 @@ using boost::asio::make_strand;
2727using tcp_acceptor = boost::asio::basic_socket_acceptor<tcp, boost::asio::io_context::executor_type>;
2828using tcp_socket = boost::asio::basic_stream_socket<tcp, boost::asio::strand<boost::asio::io_context::executor_type>>;
2929using tls_socket = boost::asio::ssl::stream<tcp_socket>;
30+ using http_socket = http::stream<tcp_socket>;
31+ using https_socket = http::stream<tls_socket>;
3032using awaitable = boost::asio::awaitable<void , boost::asio::io_context::executor_type>;
3133using awaitable_strand = boost::asio::awaitable<void , boost::asio::strand<boost::asio::io_context::executor_type>>;
3234namespace fs = std::filesystem;
@@ -273,7 +275,7 @@ awaitable_strand websocket_write_loop(std::shared_ptr<websocket_impl<Socket>> ws
273275 {
274276 auto buf = std::move (ws->buf_write_queue .front ());
275277 ws->buf_write_queue .erase (begin (ws->buf_write_queue ));
276- co_await http::async_ws_write (ws->sock , buf.data , buf.is_text , true );
278+ co_await http::async_ws_write (ws->sock , buf.data , buf.is_text );
277279 }
278280 }
279281 catch (const std::exception& e)
@@ -285,15 +287,15 @@ awaitable_strand websocket_write_loop(std::shared_ptr<websocket_impl<Socket>> ws
285287
286288template <class Socket >
287289awaitable_strand websocket_session (
288- Socket sock,
290+ http::stream< Socket> sock,
289291 http::request req,
290292 const ws_handlers_t & handlers,
291293 std::shared_ptr<void > ctx
292294)
293295{
294296 // Move into shared state
295- auto state = std::make_shared<websocket_impl<Socket>>(std::move (sock), ctx);
296- std::vector<char > buf ;
297+ auto state = std::make_shared<websocket_impl<http::stream< Socket> >>(std::move (sock), ctx);
298+ std::vector<char > msg ;
297299
298300 try
299301 {
@@ -304,8 +306,8 @@ awaitable_strand websocket_session (
304306 for (;;)
305307 {
306308 // Read
307- const bool is_text = co_await http::async_ws_read (state->sock , buf, true );
308- handlers.on_data (state, buf .data (), buf .size (), is_text);
309+ const bool is_text = co_await http::async_ws_read (state->sock , msg );
310+ handlers.on_data (state, msg .data (), msg .size (), is_text);
309311 }
310312 }
311313 catch (const std::exception& e)
@@ -325,7 +327,7 @@ awaitable_strand websocket_session (
325327
326328template <class Socket >
327329awaitable_strand http_session (
328- Socket sock,
330+ http::stream< Socket> sock,
329331 const api_options& options,
330332 std::shared_ptr<void > ctx
331333)
@@ -334,16 +336,15 @@ awaitable_strand http_session (
334336 {
335337 http::request req;
336338 http::response resp;
337- std::string buf;
338339
339340 // Complete TLS handshake if SSL
340341 if constexpr (std::is_same_v<Socket, tls_socket>)
341- co_await sock.async_handshake (boost::asio::ssl::stream_base::server);
342+ co_await sock.next_layer (). async_handshake (boost::asio::ssl::stream_base::server);
342343
343344 for (;;)
344345 {
345346 // Read request
346- size_t res = co_await http::async_http_read (sock, req, buf );
347+ size_t res = co_await http::async_http_read (sock, req);
347348
348349 // Manage websocket
349350 if (req.is_websocket_req ())
@@ -362,13 +363,13 @@ awaitable_strand http_session (
362363 handle_request (options.docroot , options.username , options.password , options.http_handlers , req, resp);
363364
364365 // Write response
365- res = co_await async_http_write (sock, resp, buf );
366+ res = co_await async_http_write (sock, resp);
366367
367368 // Shutdown if necessary
368369 if (!keep_alive)
369370 {
370371 if constexpr (std::is_same_v<Socket, tls_socket>)
371- co_await sock.async_shutdown ();
372+ co_await sock.next_layer (). async_shutdown ();
372373 sock.lowest_layer ().shutdown (tcp_socket::shutdown_both);
373374 break ;
374375 }
@@ -418,14 +419,9 @@ awaitable listen (
418419 tcp_socket sock = co_await acceptor.async_accept (make_strand (ioc));
419420
420421 if (options.use_tls )
421- {
422- tls_socket tls_sock{std::move (sock), *ssl};
423- co_spawn (sock.get_executor (), http_session (std::move (tls_sock), options, ssl), detached);
424- }
422+ co_spawn (sock.get_executor (), http_session (https_socket (tls_socket (std::move (sock), *ssl), true ), options, ssl), detached);
425423 else
426- {
427- co_spawn (sock.get_executor (), http_session (std::move (sock), options, nullptr ), detached);
428- }
424+ co_spawn (sock.get_executor (), http_session (http_socket (std::move (sock), true ), options, nullptr ), detached);
429425 }
430426}
431427
0 commit comments