libmicrohttpd
[Top][All Lists]
Advanced

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

Re: [libmicrohttpd] MHDs handling of HEADs and Content-Length


From: Christian Grothoff
Subject: Re: [libmicrohttpd] MHDs handling of HEADs and Content-Length
Date: Sat, 11 May 2019 00:44:30 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1

Dear Earp,

That explains it. You MUST NOT set "content-length" yourself. That's the
problem here, and more recent version of MHD would simply fail on the
MHD_add_response_header() call.

To do this right, you should queue a 256 byte response _even_ for the
HEAD method. MHD won't send it, so in fact *your* logic can be exactly
the same for HEAD and GET.

I hope this helps!

Happy hacking!

Christian
p.s.: don't use Gmail, it eats your e-mail!

On 5/10/19 9:50 PM, Damon Earp wrote:
> I finally got time to look into what was happening a bit closer and I
> was incorrect in my initial email. With HEAD requests my application
> doesn't retrieve the file's contents, just the metadata. Therefore I
> create an empty response object and set the headers manually. The
> following code illustrates what is going on:
> 
> #include <stddef.h>
> #include <stdio.h>
> #include <string.h>
> #include <unistd.h>
> #include <microhttpd.h>
> 
> static int handler(void *cls, struct MHD_Connection *c, const char *url,
>         const char *method, const char *version, const char *upload_data,
>         size_t *upload_data_size, void **con_cls)
> {
>     struct MHD_Response *r;
>     if (strcmp(method, "HEAD") == 0)
>     {
>         r = MHD_create_response_from_buffer(0, NULL,
> MHD_RESPMEM_PERSISTENT);
>         MHD_add_response_header(r, "Content-Length", "256");
>         MHD_queue_response(c, 200, r);
>     }
>     else
>     {
>         r = MHD_create_response_from_buffer(0, NULL,
> MHD_RESPMEM_PERSISTENT);
>         MHD_queue_response(c, MHD_HTTP_METHOD_NOT_ALLOWED, r);
>     }
>     MHD_destroy_response(r);
>     return MHD_YES;
> }
> 
> int main(int argc, const char *argv[])
> {
>   struct MHD_Daemon *d;
>   d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, 8080, NULL, NULL,
> &handler,
>           NULL, MHD_OPTION_END);
>   pause();
>   MHD_stop_daemon(d);
>   return 0;
> }
> 
> I ran the above with both v0.9.51 and v0.9.63 being linked at runtime
> and saw the behavior I described, 0.9.51 produces "Content-Length: 256"
> and 0.9.63 gives "Content-Length: 0" via `curl -I http://localhost:8080`.
> 
> Thank you so much for your help,
> Damon
> 
> PS. I didn't receive your reply in gmail. The only reason I'm responding
> right now is I went to lists.gnu.org <http://lists.gnu.org> and saw that
> there was a response! Not sure what I'm doing wrong.

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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