cpprestsdk icon indicating copy to clipboard operation
cpprestsdk copied to clipboard

HEAD request crashes due to trying to send a body on OSX

Open KiwiJeff opened this issue 6 years ago • 2 comments

In our software we make HEAD requests before downloading files. In order to test this code, I created a small mocking server using cpprest. On Windows, I have no issues when setting the content length in the headers, but on OSX this causes the application to crash with an exception.

libc++abi.dylib: terminating with uncaught exception of type std::logic_error: uninitialized stream object

When use set_body with data instead it works just fine. So it seems that the reply task for a HEAD and GET request are equal, rather than specialized. What I also noticed is that the HEAD request is sent and received by the client, but I don't know if this is due to the async nature and that the body is attempted to be send afterwards.

Since it is just a mocking server, we can use this workaround, but this is not what a HEAD request should be doing if I read the specifications correctly.

code:

void handleHeadRequest(const web::http::http_request &message)
{
    web::http::http_response response;

    response.headers().set_content_length( 13 );
    //works: response.set_body( std::string( 13, '\0' ) );

    response.headers().add( web::http::header_names::accept_ranges, "bytes" );

    response.set_status_code( web::http::status_codes::OK );

    message.reply( response ).wait();
}

KiwiJeff avatar Oct 08 '19 09:10 KiwiJeff

I use

res.body() = concurrency::streams::istream();

https://github.com/sony/nmos-cpp/blob/8b8b269ff9e5d33d8e1b27ec2dec82e84f813b11/Development/nmos/api_utils.cpp#L491-L493

garethsb avatar Oct 08 '19 15:10 garethsb

And the Content-Lenght field turns out to be correct?

robert-akopyan avatar Feb 13 '24 02:02 robert-akopyan