For HTTP calls, you can do:
http::async_read(m_stream, m_buffer, m_res, beast::bind_front_handler(&RestSession::on_read, shared_from_this()));
// ... then
void on_read(beast::error_code ec, std::size_t /*bytes_transferred*/)
{
if (ec)
return ; // handle error_code
if (m_res.result() == http::status::not_found)
return ; // handle target not found
}
but I can't see how to do the equivalent for a websocket stream. If the stream target does not exist the on_read() is never called.
I tried getting the next stream from the websocket stream (m_websocketStream), but the lambda is never called, presumably because the websocket layer has already swallowed the HTTP response:
void on_handshake(beast::error_code ec)
{
if(ec)
return fail(ec, "handshake", m_callback);
http::async_read(m_websockStream.next_layer(), m_buffer, m_httpRes, [this, self = shared_from_this()](beast::error_code ec, std::size_t)
{
if (m_httpResponse.result() == http::status::not_found)
return fail("path not found", m_callback);
else
m_ws.async_read(m_buffer, beast::bind_front_handler(&WsSession::on_read, self->shared_from_this()));
});
}
Is this possible?
That makes a lot of sense, since the read won't ever be started. Instead the handshake fails, and the
ec
infail(ec, "handshake", m_callback);
will reflect that.Getting HTTP response details
This is also supported: docs:
You can pass an additional response object to recive the details:
here's a very simple test:
Prints
I suppose most servers will simply decline, not send HTTP Not Found.