浏览代码

merged branch lenar/patch-3 (PR #1551)

Commits
-------

f7d0f65 RFC2616 changes
b9a218a [HttpFoundation] set Content-Length header to the length of content

Discussion
----------

[HttpFoundation] set Content-Length header to the length of content

I can't think of why this could be bad but if somebody knows please chime in.

The good thing is that with this change keepalive will work out of the box.

---------------------------------------------------------------------------

by Seldaek at 2011/07/06 05:34:51 -0700

That sounds like a great change. I think it might explain/fix the issues I've encountered with AppCache on my production box. Never had time to look into it, but IIRC I noticed the missing Content-Length, and it seemed to load forever.

---------------------------------------------------------------------------

by fabpot at 2011/07/06 06:46:50 -0700

The `Content-Length` is automatically added by servers like Apache. Moreover, sometimes, you should not add it: http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4

---------------------------------------------------------------------------

by lenar at 2011/07/06 07:54:45 -0700

It is not added automatically by default. Yes, in case of Apache it is actually added if deflate module is enabled and if that module decides to compress the content (decision based on content-type).

About RFC2616: I will read it and add changes to this PR if applicable.

---------------------------------------------------------------------------

by fabpot at 2011/07/06 08:38:14 -0700

https://github.com/symfony/symfony/commit/e943fde2ef58b77ac9f3120e54ae8215f5992df9

---------------------------------------------------------------------------

by Seldaek at 2011/07/06 08:45:22 -0700

@lenar all you have to do is skip setting the Content-Length for `1xx`, `204`, and `304` responses I believe.

---------------------------------------------------------------------------

by Seldaek at 2011/07/06 08:46:54 -0700

But this should maybe be done in sendHeaders() à la `fixContentType`, because you can't be sure about the statusCode before that.

---------------------------------------------------------------------------

by lenar at 2011/07/06 13:55:33 -0700

I propose this based on what I read and understood from RFC2616.

---------------------------------------------------------------------------

by mheleniak at 2011/07/10 03:57:26 -0700

+1
Fabien Potencier 14 年之前
父节点
当前提交
de2ab0b506

+ 21 - 2
src/Symfony/Component/HttpFoundation/Response.php

@@ -98,7 +98,7 @@ class Response
      */
     public function __toString()
     {
-        $this->fixContentType();
+        $this->fixContent();
 
         return
             sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n".
@@ -124,7 +124,7 @@ class Response
             return;
         }
 
-        $this->fixContentType();
+        $this->fixContent();
 
         // status
         header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText));
@@ -753,6 +753,15 @@ class Response
         return in_array($this->statusCode, array(201, 204, 304));
     }
 
+    protected function fixContent()
+    {
+        if ($this->isInformational() || in_array($this->statusCode, array(204, 304))) {
+            $this->setContent('');
+        }
+        $this->fixContentType();
+        $this->fixContentLength();
+    }
+
     protected function fixContentType()
     {
         $charset = $this->charset ?: 'UTF-8';
@@ -763,4 +772,14 @@ class Response
             $this->headers->set('Content-Type', $this->headers->get('Content-Type').'; charset='.$charset);
         }
     }
+    
+    protected function fixContentLength()
+    {
+        if (!$this->headers->has('Content-Length')) {
+            $this->headers->set('Content-Length', strlen($this->content));
+        }
+        if ($this->headers->has('Transfer-Encoding')) {
+            $this->headers->remove('Content-Length');
+        }
+    }
 }

+ 1 - 1
src/Symfony/Component/HttpKernel/EventListener/ResponseListener.php

@@ -40,7 +40,7 @@ class ResponseListener
         $response = $event->getResponse();
 
         if ('HEAD' === $request->getMethod()) {
-            // cf. RFC2611 14.13
+            // cf. RFC2616 14.13
             $length = $response->headers->get('Content-Length');
             $response->setContent('');
             if ($length) {