I am having some problems implementing HEAD requests in a way that keeps the connections alive. I am unsure how to properly implement HEAD requests, so as to send the client a correct "Content-Length" and not breaking keep-alive. I tried two options:
(1) using MHD_create_response_from_callback(), i.e., maintaining almost the same application code as for GET. For HEAD, microhttpd calls the callback with max=0, for which my callback simply returns MHD_CONTENT_READER_END_OF_STREAM.
The problem with this approach is that the HTTP connection is dropped after sending the HEAD headers. This is perfectly legal and clients can deal with it, but incurs a large performance overhead. I'm not sure if this is bad usage of microhttpd on my side or a bug in microhttpd.
(2) using MHD_create_response_from_buffer(). Since I sometimes return large files (1GB is not uncommon) it does not make sense to create a large, empty buffer to pass to MHD_create_response_from_buffer, so I have two options:
(2a) MHD_create_response_from_buffer(0, "", MHD_RESPMEM_PERSISTENT) -> the client gets an incorrect "Content-Length"
(2b) MHD_create_response_from_buffer(contentSize, "", MHD_RESPMEM_PERSISTENT) -> scary, looks like the largest buffer overflow ever. :) Valgrind does not seem to complain, so it seems that microhttpd does not actually access that buffer.
Tested with libmicrohttpd 0.9.33 and 0.9.42.
Can somebody point out an example HEAD implementation? Which of the above usages is recommended?