http Range and reading the bytes out of the response body

Mar 21, 2014 at 11:11 AM
Edited Mar 21, 2014 at 11:55 AM
Hi,

I am trying to get a range of bytes from IIS and for some reason the Range header is not being passed to the server at all. Is this functionality not ready yet?
I am looking at the development branch.

The other question is, once I get the binary body how do I read the bytes out from the istream into, say, uint8_t vector.
http_client client(U("http://server/binaryfile.bin"));

        http_request msg(methods::GET);
        msg.headers().add(U("Range"),U("bytes=0-99"));
         
        client.request(msg).then([](http_response response)
        {
            auto status_code = response.status_code();
            if(response.status_code() == status_codes::OK)
            {
                auto stream = response.body();
                // how do I get the 100 bytes out of the body
            }
        }).wait();
Update Range header actually works, my mistake, The second question is still open, not sure how to read from the istream.
Coordinator
Mar 21, 2014 at 9:35 PM
Hi cosminonea,

You can use concurrency::streams::container_buffer to read from the istream.
 client.request(msg).then([](http_response response)
    {
        auto status_code = response.status_code();
        concurrency::streams::container_buffer<std::vector<uint8_t>> rwbuf;
        if (response.status_code() == status_codes::OK)
        {
            auto stream = response.body();
            stream.read_to_end(rwbuf).get();
            auto result = rwbuf.collection();
        }
    }).wait();
You may get more details from here: http://casablanca.codeplex.com/wikipage?title=Asynchronous%20Streams&referringTitle=Documentation
Mar 21, 2014 at 10:18 PM
That's great. Thanks.

In terms of performance, is there any buffer copying involved in this?

If I have an already allocated buffer,say uint8_t[100] buffer, how can I give it to the library to copy the data into it?

Sorry if the questions look trivial but I am new to c++ development.
Coordinator
Mar 24, 2014 at 6:50 PM
Edited Mar 24, 2014 at 10:31 PM
Hi cosminonea,

To avoid the extra buffer copying, you can use the "set_response_stream" when you create the http request. When the response body data is ready, it will save to the stream buffer you provided.
 concurrency::streams::container_buffer<std::vector<uint8_t>> buf;
    msg.set_response_stream(buf.create_ostream());

    client.request(msg).then([](http_response response)
    {
        return response.content_ready();
    }).wait();

    auto result = std::move(buf.collection());