laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.46k stars 11k forks source link

Content-Length header lost in HEAD request #29169

Closed yueyuzhao closed 5 years ago

yueyuzhao commented 5 years ago

Hi there, I want to return Content-Length field in the response headers in HEAD requests, but I always get Content-Length: 0. I read the codes and found that Symfony\Component\HttpFoundation\Response@perpare handles HEAD request and cleans body and keeps Content-Length, but the framework doesn't set that.

if ($request->isMethod('HEAD')) {
    // cf. RFC2616 14.13
    $length = $headers->get('Content-Length');
    $this->setContent(null);

    if ($length) {
        $headers->set('Content-Length', $length);
    }
}
driesvints commented 5 years ago

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.

yueyuzhao commented 5 years ago

Hi @driesvints , thanks for your advice, but I do think this is a bug. For example, this is what I had in my routes/web.php:

Route::get('test', function () {
    return 'hello';
});

when I made a HEAD request to this, I should have had Content-Length: 5 field in the headers.

yueyuzhao commented 5 years ago

From this post from stackoverflow and HTTP 1.1 RFC

The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET.

In my opinion there should be a Content-Length field with content length in the response headers.

yueyuzhao commented 5 years ago

And HTTP RFC sec9.4

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request.

driesvints commented 5 years ago

@yueyuzhao if this is a bug then please fill out the issue template. Please submit again. Thanks.