-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Content-Length header causing truncated content with New Relic #1690
Comments
There is no way for us to fix it... Slim renders at the end of the application life cycle... if there is another piece of software modifying the content, then its that software's responsibility to update the headers accordingly. |
Thanks for the quick reply @geggleto, and for the great work on Slim in general.
I'm not sure I agree, since PHP can't be sure it's the last link in the chain. That header doesn't have to be sent, or Slim could allow a developer to disable that header. But I understand it's your prerogative to consider it external, and I'll file an issue with New Relic as well. As this will affect any high-performance app built on Slim that wants to use New Relic or a similar server-side injected real-user monitoring script, that's limiting the framework in my opinion, but that's your call. For anyone encountering this while searching for the same problem, you can disable New Relic RUM for your Slim app, which of course negates all of the benefits of RUM, but at least allows content to be sent in full:
or you can extend the
|
Unfortunately due to HTTP protocol, we cannot delegate that responsibility at our package level. I would encourage you to look into this further. I have a very hard time believing that a product like NewRelic modifies the message output... there has to be something else going on. That's just my gut feeling. |
If Content-Length is your problem, you can solve it using a middleware. //Middleware
$mdw = function($req, $res, $next) {
$res = $next($req, $res);
return $res->withoutHeader('Content-Length');
};
//Add the middleware to app
$app->add($mdw); |
It does, see How does it work?. Its agent injects JavaScript by replacing the closing
|
Just a note that the middleware solution does not seem to work. The middleware is executing on the requests way in to the app, but not on the way out, so the App is still setting that header. |
The Middleware won't work. Analysing the code, I figured out that the Content-Length header is added after all "run" cycle. my bad =/ $app = new \Slim\App();
$app->get('/', function($request, $response){
return $response->write("Test");
});
// Take a look HERE! :D
$response = $app->run(true); //Silent mode, wont send the response
$response = $response->withoutHeader("Content-Length"); //Remove the Content-Length
$app->respond($response); //Now we send the response |
Ah, much cleaner than extending the base App class, thanks! |
We will be cleaning up some of these methods in the next few versions. I am going to re-open this as its something we should probably look into. |
I found the offending code... https://github.com/slimphp/Slim/blob/3.x/Slim/App.php#L546-L549 |
I was having a similar issue where the last character of my JSON was missing! I finally pasted the raw JSON into Notepad++ and sure enough, the character count was +1 greater than the content-length header. For some reason, I looked back at the beginning of the string and noticed there was a space before the opening character. I soon realized that someone either left an echo statement active or injected a space into PHPs output buffer unintentionally. I finally found the culprit by placing an Problem solved! (mine at least) Seems like if Slim expects to take total control of the response, it should make sure the output buffer is empty.
Otherwise, add the buffer length to the content length
|
I like the idea of add Does anyone have a preference over |
A third option would be to throw an error or emit a warning if As a note: neither options will work for content that is being added after |
Sending the content-length header without being implicitly asked to do it leading to impossibility to integrate Slim into other application. |
I actually agree with @llvdl. If the output buffer is not clean, slim should throw an error due to the difficulty of troubleshooting the side-effects. |
@grikdotnet you can easily unset the header. you can run Slim without |
@geggleto thanks for the advice. I opened the issue to support other users in case the authors will want to solve it. I will use Symfony microkernel and Lumen, they don't have integration limitations. |
@grikdotnet Yeah we are working on adding an option to omit the Content-Type header. |
You can now disable Slim's automatic setting of the content-length with: $config = [
'settings' => [
'addContentLengthHeader' => false,
]
];
$app = new Slim\App($config); |
With New Relic, or I would imagine any other daemon/agent that modifies the request the web server sends after it receives output from PHP, the response body is truncated.
Looks like this issue was raised awhile ago but the reporting user never responded with details.
You should be able to reproduce by installing the New Relic PHP agent from the link above and using any renderer. The issue appears to be having the content-size header set by the size of the stream, which is incorrect if the server is modifying any of the output after PHP hands it off.
The text was updated successfully, but these errors were encountered: