有没有办法回应boost :: asio :: ssl :: stream的握手失败?

时间:2018-12-21 09:04:06

标签: http ssl boost boost-asio

假设某人正在尝试使用https访问我的http路由,或者他们的客户没有正确的证书。我希望我的服务器能够发送回错误代码。那不可能吗?

当客户端具有正确的证书时,握手成功后,我可以向他们发送任何想要的响应。但是,当握手失败后尝试在套接字上进行写操作时,客户端不会收到任何消息,而在尝试关闭连接之后尝试关闭连接似乎并不会通知仍在“刷新”等待所述响应的客户端。如果在握手失败而没有写入后尝试关闭套接字,则客户端会看到它并只是说它得到了ERR_EMPTY_RESPONSE

因此,如果握手失败,没有办法告诉客户端吗?

void Server::Accept()
{
    std::shared_ptr<socket> sk = CreateSocket();
    acceptor_.async_accept(sk->lowest_layer(),
    [this, sk](boost::system::error_code ec)
    {
        if(ec) { return; }
        Connection connection = std::make_shared<Connection>(sk, *this);
        if (!connection.IsValid())
        {
            return;
        }

        boost::asio::ip::tcp::no_delay option(true);
        boost::system::error_code resultCode;
        connection->GetSocket().lowest_layer().set_option(option, resultCode);
        if (resultCode)
        {
            StopConnection(_connection);
            return;
        }

        connection->GetSocket().async_handshake(boost::asio::ssl::stream_base::server, 
[this, connection](const boost::system::error_code &resultCode) {
        if (resultCode)
        {
            // Failed handshake! Doing a Write here, then a close behaves weird on the client.
            // If I just close here, the client just says he didn't get a response.
            connection->Write(errorCode512);
        }
        else
        {
            // Success, I can write whatever I want.
            connection->Start();
        }

    });
}

void Connection::Write(HttpReply reply)
{
    auto self(shared_from_this());
    boost::asio::async_write(*m_Socket, ToBuffers(reply),
        [this, self](boost::system::error_code ec, std::size_t)
    {

        boost::system::error_code errorCode;
        m_Socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both,
        errorCode);
        if(errorCode)
        {
            Log(errorCode.data());
        }

        m_Socket->lowest_layer().close(errorCode);
        if(errorCode)
        {
             Log(errorCode.data());
        }
    });
}

0 个答案:

没有答案