Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does XSendfile emit intermittent garbled responses when used with Symfony Components BinaryFileResponse class?
    primarykey
    data
    text
    <p><strong>Background</strong></p> <p>Part of my application's responsibility is handling requests for static resources (CSS, JavaScript, images) in a controlled manner. Based on some application logic, it will return one from a selection of different files that might be served on that URL at different times and to different users. These are therefore static files, but delivered in a dynamic way.</p> <p>The application is based on Symfony Components and the serving of these static-ish files is handled by the <code>BinaryFileResponse</code> class.</p> <p>The bootstrap code calls the <code>trustXSendfileTypeHeader</code> method:</p> <pre><code>\Symfony\Component\HttpFoundation\BinaryFileResponse::trustXSendfileTypeHeader(); </code></pre> <p>The application uses some internal logic based on configuration and the detection and use of <code>apache_get_modules()</code> to determine availability. If <a href="https://tn123.org/mod_xsendfile/" rel="nofollow">XSendfile</a> is available and the configuration says to use it, it sets the <code>X-Sendfile-Type</code> header:</p> <pre><code>if ($useHeader === true) { $request-&gt;headers-&gt;set('X-Sendfile-Type', $header); } $response = new BinaryFileResponse($filename); </code></pre> <p><strong>Problem</strong></p> <p>When I run this with the configuration set to never use XSendfile, or through the PHP built-in web server, which obviously does not support XSendfile, everything is perfect.</p> <p>When I utilise XSendfile, it also works -- most of the time.</p> <p>Every so often, typically if I press the f5 key 3-4 times in quick succession, "something" wigs out and I get a garbled response. For example, this is supposed to be a JavaScript file (copied from "Response" tab under "Net" in Firebug):</p> <blockquote> <p>hxYîãx��HTTP/1.1 200 OK Date: Tue, 05 Feb 2013 14:49:10 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.4.6-1ubuntu1.1 Cache-Control: public Last-Modified: Tue, 29 Jan 2013 13:33:23 GMT Accept-Ranges: bytes Content-Transfer-Encoding: binary ETag: "10426f-9f6-0" Vary: Accept-Encoding Content-Encoding: gzip Content-Length: 1011 Keep-Alive: timeout=5, max=98 Connection: Keep-Alive Content-Type: application/javascript</p> <p>������­VmoÛ6þ,ÿkÀ²ãIý°~q [Üt] XÑt¶H¤@Rv¼Àÿ}w(YS<code>ÀØ2yïå¹*¾Á&gt;¯¥¥,è) Æ^Ât¸BaÆ\éjgäjí Î&amp;ð*¸Åí¸tY!³Ç$Óe"jÞ![#,n®®oï®A¨þ¸þù××Þ©¼¼ôÇêÚd¹49mv°ÔrtBÖ^;WÍÓÔg´Y¥´FéôÁR9o°35Îà^º­´N=UÐè­Eµ¢XE¸íÒ%ª°¨Úò7¬KñT¾{;£ÈrTnß³étUè{QÀçÍn·:'üJëQÍÄËZeNjOàyÕÁ:#3wö~4Òét1ù$µ</code>eN)RD| ¶FTØJ·ß½¥¨¸õGç >9TyÜxzgl-J:) b«9ûAQ½KX<code>É!yÐÓ] óÆÎ@W¡?¢vún­·7j©ÿ¢ðõÖGEÁy\ºp¤÷cKxf?ï*¼Éç0^ïîÌǰñDQ¸mYJ|4t¾ñæËۯЍ6:çøp(}þÑò|LÂ;Õ(#v¹* /[¨U|xª æ]ÍyìjµòÛ¯p?4sI¥"v÷ôp|uQ4ò4&amp;Ï·$eÒc¸ xo%7Ôi´2ñx;</code>TuÙj23 áÊ%ħ¿¹lÌwÀS.&amp;ÏØß7¸}ó ZXzå k2'Zdùè �¦ºû-Ù[Ó²ÿU(¯¤¥=pÃjô¾ç]]Øhhô²×ÙãÚÍ4¨[!Õ}'Òþ^Ð�ûxÿ@+ÚVÞ~áÌáy?d aíD¹·U×ÃÚ]­ õ5íÃø¨o÷ÂAvUÆmÍaày`¦ä©A?mL[-}®(ÿË d°öò¬}Ç¢³Çp1À^6%0 hTô^ts´ÞíWô fO¶ö¢ÎNÜæ·HîUôÔ¶±ÌCµsxh.9åçi Û·_ÈÞØ_ÄãY_Ö}G&lt;ì°ý2wÔ¿aw8/þù\ã±þ"0C oÂh'tE¶À¤¥7I½éßRt.s?á^d|k/Æ)wRw÷cG¿&lt;Þ ¼´°/^ø*ʤAVZ×y¿zÅΪ¥[²Õ1ò_Vµæï_YXÁÕö ��YXÁÕö ��</p> </blockquote> <p>Note the presence of the headers in the response body, and the rest of it which is clearly not JavaScript. There are also some spurious characters at the start, which possibly is what leads to the headers being pushed to the body. I have tried to determine if this content is the result of gzipping, but I can't confirm that yet. (See also update below)</p> <p><strong>Question</strong></p> <p>Firstly, is <code>BinaryFileResponse</code> even the correct class to use for serving text (non-binary) files? The documentation for the class only says "BinaryFileResponse represents an HTTP response delivering a file." This isn't very detailed but it doesn't say anything about it being exclusively for "binary" files. However the name has its own implications, why didn't Fabien just call this class <code>FileResponse</code>?</p> <p>Secondly, and more importantly, what could be causing this? I don't believe it is a browser issue because it is repeatable in both Firefox and Chrome. Is this a bug in the XSendfile module or in the <code>BinaryFileResponse</code> class perhaps? (I am likely to think it is not the former because I have used it before in a more "raw" way not via Symfony Components, with no such issues).</p> <p>Has anyone else experienced this? Any idea where I should even start looking to track this down? I've looked at the <code>BinaryResponseFile</code> source code but it doesn't really do much with XSendfile, just sets the relevant header and prevents content in the response body, from what I can see.</p> <p><strong>Update</strong></p> <p>I've just noticed a couple of things about these garbled responses:</p> <ol> <li>There are no actual headers being sent at all, i.e. on the "Headers" tab in Firebug, for the garbled responses, it only lists Request headers and doesn't even show the heading for Response headers.</li> <li>Even if I set some custom header on the Response in PHP, that header does not appear at all in the garbled responses (as a header or in the response body), but the custom headers appear correctly for the responses that aren't broken.</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload