Jelajahi Sumber

[HttpFoundation] Send proper charset along with the default text/html header

Jordi Boggiano 14 tahun lalu
induk
melakukan
4f46235ab0

+ 5 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml

@@ -35,6 +35,10 @@
         -->
         <service id="request" factory-service="http_kernel" factory-method="getRequest" shared="false" />
 
-        <service id="response" class="%response.class%" shared="false" />
+        <service id="response" class="%response.class%" shared="false">
+            <call method="setCharset">
+                <argument>%kernel.charset%</argument>
+            </call>
+        </service>
     </services>
 </container>

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

@@ -27,6 +27,7 @@ class Response
     protected $version;
     protected $statusCode;
     protected $statusText;
+    protected $charset = 'UTF-8';
 
     static public $statusTexts = array(
         100 => 'Continue',
@@ -96,7 +97,7 @@ class Response
         $content = '';
 
         if (!$this->headers->has('Content-Type')) {
-            $this->headers->set('Content-Type', 'text/html');
+            $this->headers->set('Content-Type', 'text/html; charset='.$this->charset);
         }
 
         // status
@@ -128,7 +129,7 @@ class Response
     public function sendHeaders()
     {
         if (!$this->headers->has('Content-Type')) {
-            $this->headers->set('Content-Type', 'text/html');
+            $this->headers->set('Content-Type', 'text/html; charset='.$this->charset);
         }
 
         // status
@@ -227,6 +228,26 @@ class Response
         return $this->statusCode;
     }
 
+    /**
+     * Sets response charset.
+     *
+     * @param string $charset Character set
+     */
+    public function setCharset($charset)
+    {
+        $this->charset = $charset;
+    }
+
+    /**
+     * Retrieves the response charset.
+     *
+     * @return string Character set
+     */
+    public function getCharset()
+    {
+        return $this->charset;
+    }
+
     /**
      * Returns true if the response is worth caching under any circumstance.
      *

+ 22 - 0
tests/Symfony/Tests/Component/HttpFoundation/ResponseTest.php

@@ -136,6 +136,28 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->getVary() parses multiple header name values separated by commas');
     }
 
+    public function testDefaultContentType()
+    {
+        $headerMock = $this->getMock('Symfony\Component\HttpFoundation\ResponseHeaderBag', array('set'));
+        $headerMock->expects($this->at(0))
+            ->method('set')
+            ->with('Content-Type', 'text/html; charset=UTF-8');
+        $headerMock->expects($this->at(1))
+            ->method('set')
+            ->with('Content-Type', 'text/html; charset=Foo');
+
+        $response = new Response();
+        $response->headers = $headerMock;
+
+        // verify first set()
+        $response->sendHeaders();
+
+        $response->headers->remove('Content-Type');
+        $response->setCharset('Foo');
+        // verify second set()
+        $response->__toString();
+    }
+
     protected function createDateTimeOneHourAgo()
     {
         $date = new \DateTime();