Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Request format tweaks #575

Merged
merged 3 commits into from

2 participants

Lukas Kahwe Smith Fabien Potencier
Lukas Kahwe Smith
Collaborator

The goal is to make the base Request class more flexible.

For a use case see: https://github.com/FriendsOfSymfony/RestBundle/blob/master/Request/RequestListener.php

See #565

Fabien Potencier
Owner

I'm fine with the first two commits, but not the last one as it changes the current behavior. Before the patch, the content-type would be 'text/html; charset=utf-8'. After the patch, it will only be 'text/html'.

Fabien Potencier
Owner

nevermind, I've changed the behavior of the Response class to fix this problem there.

Fabien Potencier fabpot merged commit c2ac830 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
18 src/Symfony/Component/HttpFoundation/Request.php
View
@@ -630,14 +630,16 @@ public function setFormat($format, $mimeTypes)
*
* * format defined by the user (with setRequestFormat())
* * _format request parameter
- * * null
+ * * $default
+ *
+ * @param string $default The default format
*
* @return string The request format
*/
- public function getRequestFormat()
+ public function getRequestFormat($default = 'html')
{
if (null === $this->format) {
- $this->format = $this->get('_format', 'html');
+ $this->format = $this->get('_format', $default);
}
return $this->format;
@@ -726,7 +728,7 @@ public function getLanguages()
$languages = $this->splitHttpAcceptHeader($this->headers->get('Accept-Language'));
$this->languages = array();
- foreach ($languages as $lang) {
+ foreach ($languages as $lang => $q) {
if (strstr($lang, '-')) {
$codes = explode('-', $lang);
if ($codes[0] == 'i') {
@@ -764,7 +766,7 @@ public function getCharsets()
return $this->charsets;
}
- return $this->charsets = $this->splitHttpAcceptHeader($this->headers->get('Accept-Charset'));
+ return $this->charsets = array_keys($this->splitHttpAcceptHeader($this->headers->get('Accept-Charset')));
}
/**
@@ -778,7 +780,7 @@ public function getAcceptableContentTypes()
return $this->acceptableContentTypes;
}
- return $this->acceptableContentTypes = $this->splitHttpAcceptHeader($this->headers->get('Accept'));
+ return $this->acceptableContentTypes = array_keys($this->splitHttpAcceptHeader($this->headers->get('Accept')));
}
/**
@@ -821,8 +823,9 @@ public function splitHttpAcceptHeader($header)
}
arsort($values);
+ reset($values);
- return array_keys($values);
+ return $values;
}
/*
@@ -971,6 +974,7 @@ protected function preparePathInfo()
static protected function initializeFormats()
{
static::$formats = array(
+ 'html' => array('text/html', 'application/xhtml+xml'),
'txt' => array('text/plain'),
'js' => array('application/javascript', 'application/x-javascript', 'text/javascript'),
'css' => array('text/css'),
13 tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php
View
@@ -666,4 +666,17 @@ public function testGetLanguages()
$request->headers->set('Accept-language', 'zh, i-cherokee; q=0.6');
$this->assertEquals(array('zh', 'cherokee'), $request->getLanguages());
}
+
+ public function testGetRequestFormat()
+ {
+ $request = new Request();
+ $this->assertEquals('html', $request->getRequestFormat());
+
+ $request = new Request();
+ $this->assertEquals(null, $request->getRequestFormat(null));
+
+ $request = new Request();
+ $this->assertEquals(null, $request->setRequestFormat('foo'));
+ $this->assertEquals('foo', $request->getRequestFormat(null));
+ }
}
2  tests/Symfony/Tests/Component/HttpKernel/ResponseListenerTest.php
View
@@ -62,7 +62,7 @@ public function testFilterDoesNothingIfRequestFormatIsNotDefined()
$event = new FilterResponseEvent($this->kernel, Request::create('/'), HttpKernelInterface::MASTER_REQUEST, $response);
$this->dispatcher->dispatch(Events::onCoreResponse, $event);
- $this->assertEquals('', $event->getResponse()->headers->get('content-type'));
+ $this->assertEquals('text/html', $event->getResponse()->headers->get('content-type'));
}
public function testFilterSetContentType()
Something went wrong with that request. Please try again.