Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to receive 100-continue with async API #2933

Open
xiaoma565 opened this issue Sep 23, 2024 · 1 comment
Open

How to receive 100-continue with async API #2933

xiaoma565 opened this issue Sep 23, 2024 · 1 comment

Comments

@xiaoma565
Copy link

I readed example https://www.boost.org/doc/libs/1_85_0/libs/beast/doc/html/beast/more_examples/expect_100_continue_server.html with how to receive 100-continue message.
But my code was call by async API.
My code is here

void ClientConnection::DoReadHeader()
{
    auto self(shared_from_this());
    p_.emplace();
    p_->body_limit(boost::none);
    if (timeout_ > 0) {
        stream_.expires_after(std::chrono::seconds(timeout_));
    } else {
        stream_.expires_at(boost::asio::steady_timer::time_point::max());
    }
    boost::beast::http::async_read_header(stream_, buffer_, *p_,
        boost::beast::bind_front_handler(&ClientConnection::OnReadHeader, self));
}

void ClientConnection::OnReadHeader(boost::beast::error_code ec, std::size_t bytes)
{
    boost::ignore_unused(bytes);
    auto self(shared_from_this());
    if (ec) {
        if (ec != boost::beast::http::error::end_of_stream) {
            LOG_E("[ClientConnection] OnReadHeader error : %s", ec.what().c_str());
        }
        auto fun = clientHandler_.GetFun(ClientHandlerType::HEADER_RES_ERROR);
        if (fun != nullptr) {
            fun(self);
        }
        return DoClose();
    }
    auto fun = clientHandler_.GetFun(ClientHandlerType::HEADER_RES);
    if (fun != nullptr) {
        fun(self);
    }
    if (p_->chunked()) {
        p_->on_chunk_header(headerCb);
        p_->on_chunk_body(bodyCb);
        if (!p_->is_done()) {
            if (timeout_ > 0) {
                stream_.expires_after(std::chrono::seconds(timeout_));
            } else {
                stream_.expires_at(boost::asio::steady_timer::time_point::max());
            }
            boost::beast::http::async_read(stream_, buffer_, *p_,
                boost::beast::bind_front_handler(&ClientConnection::OnChunkBody, self));
        }
    } else {
        if (!p_->is_done()) {
            if (timeout_ > 0) {
                stream_.expires_after(std::chrono::seconds(timeout_));
            } else {
                stream_.expires_at(boost::asio::steady_timer::time_point::max());
            }
            boost::beast::http::async_read(stream_, buffer_, *p_,
                boost::beast::bind_front_handler(&ClientConnection::OnReadBody, self));
        }
    }
}

ClientConnection.zip

@ashtum
Copy link
Collaborator

ashtum commented Sep 23, 2024

The provided code is incomplete and cannot be compiled or tested. Could you provide more details on the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants