microsoft / cpprestsdk

The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
Other
7.97k stars 1.65k forks source link

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

Open KiwiJeff opened 4 years ago

KiwiJeff commented 4 years ago

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();
}
garethsb commented 4 years ago

I use

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

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

robert-akopyan commented 7 months ago

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