libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [libmicrohttpd] How to specify content-length in a HEAD request


From: Evgeny Grin
Subject: Re: [libmicrohttpd] How to specify content-length in a HEAD request
Date: Mon, 6 Nov 2023 10:18:49 +0300
User-agent: Mozilla Thunderbird

Hi Rohit,

On 05.11.2023 19:15, Rohit Kota wrote:
Hello,
We have a use case where content-length needs to be set in the response headers for a HEAD request. See for example:- https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadObject.html#API_HeadObject_Examples <https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadObject.html#API_HeadObject_Examples>

According to rfc-9110 section 8.6.6 <https://www.rfc-editor.org/rfc/rfc9110.html#section-8.6-6> this is valid behaviour A server MAY send a Content-Length header field in a response to a HEAD request (Section 9.3.2 <https://www.rfc-editor.org/rfc/rfc9110.html#HEAD>); a server MUST NOT send Content-Length in such a response unless its field value equals the decimal number of octets that would have been sent in the content of a response if the same request had used the GET method.

Your quote from RFC is absolutely correct and MHD fully follow these recommendations. See below for more details.

But from what I gathered here <https://lists.nongnu.org/archive/html/libmicrohttpd/2019-10/msg00037.html>, I can set MHD_RF_INSANITY_HEADER_CONTENT_LENGTH to allow this behaviour.

This is incorrect interpretation of the email discussion. The name of the option MHD_RF_INSANITY_HEADER_CONTENT_LENGTH suggests incorrect behaviour.

For HEAD requests, I suspect libmicrohttpd is always setting content-length to 0.

This is not what is written in the documentation.
For *any* request libmicrohttpd *always* set correct Content-Length that matches application-provided response, if RFCs allow the Content-Length header. Exceptions: 1xx and 204 response codes, where no reply body nor Content-Length are used.

So for example when i set content-length to 24 manually I am getting this.
< HTTP/1.1 200 OK
< Date: Sun, 05 Nov 2023 16:10:44 GMT
< Connection: close
< Last-Modified: Thu, 26 Oct 2023 04:42:56 GMT
< Content-Length: 24
< Content-Length: 0

I suspect the extra content-length is being set by mhd.

No. The extra "Content-Length" header with size 24 as added by application. The application allowed to add this header because of MHD_RF_INSANITY_HEADER_CONTENT_LENGTH flag. See https://git.gnunet.org/libmicrohttpd.git/tree/src/include/microhttpd.h?h=v0.9.77#n3882 for manually adding wrong "Content-Length" header. The "Context-Length" with number zero is used because application provided response with zero length.

The next MHD release will include clearer documentation and more options.
See https://git.gnunet.org/libmicrohttpd.git/tree/src/include/microhttpd.h?id=cdf9b66c61a444c09ba7c9f4cee385cbd2b3bcdc#n3575
https://git.gnunet.org/libmicrohttpd.git/tree/src/include/microhttpd.h?id=cdf9b66c61a444c09ba7c9f4cee385cbd2b3bcdc#n3728

But as per rfc I should be able to set content-length manually for a head 
request.

It is very easy: just provide the correct response object with correct context (response body). For HEAD requests MHD will set the "Context-Length" to the right number, but *will not* send the response body. If the same response object is used for GET requests, MHD will set the "Context-Length" *and* *will* send the response body.

The idea is that response objects are unified, can be created one time and re-used for both GET and HEAD requests freely.

The next MHD version will provide more flexibility and special responses that can be used only for HEAD requests. This functionality is already in git branches.

--
Evgeny

Attachment: OpenPGP_0x460A317C3326D2AE.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]