Skip to content

Missing Content-Length header when serving through Apache #51127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fracz opened this issue Jul 27, 2023 · 5 comments
Closed

Missing Content-Length header when serving through Apache #51127

fracz opened this issue Jul 27, 2023 · 5 comments

Comments

@fracz
Copy link

fracz commented Jul 27, 2023

Symfony version(s) affected

4.4.44+, v5, v6

Description

In Symfony up to 4.4.43 the Apache automatically add the Content-Lenght header.

HTTP/1.1 200 OK
Date: Thu, 27 Jul 2023 08:22:47 GMT
Server: Apache/2.4.52 (Ubuntu)
Cache-Control: no-cache, private
X-Debug-Token: 3ab75d
X-Debug-Token-Link: http://xxx.local/_profiler/3ab75d
X-Robots-Tag: noindex
Content-Length: 736
Keep-Alive: timeout=5, max=98
Connection: Keep-Alive
Content-Type: application/json

After the 4.4.44 (and also in 5+ and 6+) the Apache does not set this header and also switches itself to Transfer-Encoding: chunked. The headers sent after v4.4.44 are:

HTTP/1.1 200 OK
Date: Thu, 27 Jul 2023 08:21:57 GMT
Server: Apache/2.4.52 (Ubuntu)
Cache-Control: no-cache, private
X-Debug-Token: 6505c3
X-Debug-Token-Link: http://xxx.local/_profiler/6505c3
X-Robots-Tag: noindex
Keep-Alive: timeout=5, max=98
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/json

How to reproduce

I have tracked this issue to appear after introducing the flush() call in #46931 and later moved to another place in #47434. Removing the flush() call reverts to the original behavior in v4.4.44+ and v5.

I wouldn't have noticed this change if my API wouldn't be used by some hand-crafted HTTP clients that are used inside firmwares of low-level devices. They can't parse the chunked responses so I really need to maintain the response encoding for some of my endpoints.

I understand that such change is not a breaking change for most of the users, but in my case it prevents me from upgrading to v4.4.44 and later to v5.

Possible Solution

I would like to ask if you know of some solution that would force the Apache to behave as previously. It might be an Apache configuration or some PHP code extension. Maybe the flush() call should be optional and turned on in the configuration?

I lack a knowledge of this internals and therefore could not come up with a solution myself.

Additional Context

No response

@valtzu
Copy link
Contributor

valtzu commented Jul 29, 2023

I think it's not a Symfony bug, rather a bug / missing feature in your http client (which is understandable if we're talking about embedded devices).

Anyway, as a workaround, maybe this would do the job?

namespace App\EventListener;

use Symfony\Component\HttpKernel\Event\ResponseEvent;

class KernelResponseListener
{
    public function __invoke(ResponseEvent $event)
    {
        $response = $event->getResponse();
        $response->headers->set('Content-Length', strlen($response->getContent()));
    }
}
services:
  App\EventListener\KernelResponseListener:
    tags: ['kernel.event_listener']

@aanair08
Copy link

aanair08 commented Aug 3, 2023

@valtzu It's interesting that the response got the right content length when I substituted "echo json_encode" for the JsonResponse. A minor performance issue seems to be brought on by the response's lack of a content-length header.

@xabbuh
Copy link
Member

xabbuh commented Aug 3, 2023

@aanair08 What place are talking about where you made that substitution?

@aanair08
Copy link

aanair08 commented Aug 3, 2023

@xabbuh
On the controller instead of this
return new JsonResponse($result);
I used below code

header('Content-Type: application/json; charset=utf-8'); echo json_encode($result);die;

@fracz
Copy link
Author

fracz commented Oct 5, 2023

I have solved my problem with an event listener. Thank you, @valtzu.

Closing, as my problem has been resolved.

@fracz fracz closed this as completed Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants