Boost logo

Boost :

Subject: Re: [boost] [beast] Chunking example
From: Vinnie Falco (vinnie.falco_at_[hidden])
Date: 2017-07-02 13:09:12


On Sun, Jul 2, 2017 at 5:41 AM, Bjorn Reese via Boost
<boost_at_[hidden]> wrote:
> How would an example of receiving a chunked transfer using the
> synchronous API look? In pseudo-code, I want to do as follows:
>
> establish connection
> while not closed
> read chunk
> print chunk

That's going to be be quite involved, you would need to subclass
beast::http::basic_parser and implement on_chunk to remember the chunk
size. It is not something that I recommend nor is it a common
use-case. HTTP applications are not supposed to care about the
boundaries between chunks since intermediates like proxies are allowed
to re-frame chunked message payloads. However, some applications may
wish to decode the chunk-extension and Beast handles that, but you
have to subclass beast::http::basic_parser for it.

Its possible that what you are really asking is how to read a message
payload incrementally? One of the examples performs a similar
operation:
<http://vinniefalco.github.io/beast/beast/more_examples/http_relay.html>

Something like this should achieve your goal (note, untested):

    /* This function reads a message using a fixed size buffer to hold
        portions of the body, and prints the body contents to a `std::ostream`.
    */
    template<
        bool isRequest,
        class SyncReadStream,
        class DynamicBuffer>
    void
    read_and_print_body(
        std::ostream& os,
        SyncReadStream& stream,
        DynamicBuffer& buffer,
        error_code& ec)
    {
        parser<isRequest, buffer_body> p;
        read_header(stream, buffer, p, ec);
        if(ec)
            return;
        while(! p.is_done())
        {
            char buf[512];
            p.get().body.data = buf;
            p.get().body.size = sizeof(buf);
            read(stream, buffer, p, ec);
            if(ec == error::need_buffer)
                ec.assign(0, ec.category());
            if(ec)
                return;
            os.write(buf, sizeof(buf) - p.get().body.size);
        }
    }

Thanks


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk