Skip to content

Commit

Permalink
[HttpFoundation] Fixed #5611 - Request::splitHttpAcceptHeader incorre…
Browse files Browse the repository at this point in the history
…ct result order.

* Makes items with equal q-values return in the original provided order.
* Fixes tests to reflect this behavior
  • Loading branch information
kerihenare committed Oct 2, 2012
1 parent 302c0e4 commit 6c59fbd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -1051,23 +1051,31 @@ public function splitHttpAcceptHeader($header)
}

$values = array();
$groups = array();
foreach (array_filter(explode(',', $header)) as $value) {
// Cut off any q-value that might come after a semi-colon
if (preg_match('/;\s*(q=.*$)/', $value, $match)) {
$q = (float) substr(trim($match[1]), 2);
$q = substr(trim($match[1]), 2);
$value = trim(substr($value, 0, -strlen($match[0])));
} else {
$q = 1;
}

$groups[$q][] = $value;
}

krsort($groups);

foreach ($groups as $q => $items) {
$q = (float) $q;

if (0 < $q) {
$values[trim($value)] = $q;
foreach ($items as $value) {
$values[trim($value)] = $q;
}
}
}

arsort($values);
reset($values);

return $values;
}

Expand Down
16 changes: 14 additions & 2 deletions tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php
Expand Up @@ -758,7 +758,7 @@ public function testGetCharsets()

$request = new Request();
$request->headers->set('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7');
$this->assertEquals(array('ISO-8859-1', '*', 'utf-8'), $request->getCharsets());
$this->assertEquals(array('ISO-8859-1', 'utf-8', '*'), $request->getCharsets());
}

public function testGetAcceptableContentTypes()
Expand All @@ -770,7 +770,7 @@ public function testGetAcceptableContentTypes()

$request = new Request();
$request->headers->set('Accept', 'application/vnd.wap.wmlscriptc, text/vnd.wap.wml, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, multipart/mixed, */*');
$this->assertEquals(array('multipart/mixed', '*/*', 'text/html', 'application/xhtml+xml', 'text/vnd.wap.wml', 'application/vnd.wap.xhtml+xml', 'application/vnd.wap.wmlscriptc'), $request->getAcceptableContentTypes());
$this->assertEquals(array('application/vnd.wap.wmlscriptc', 'text/vnd.wap.wml', 'application/vnd.wap.xhtml+xml', 'application/xhtml+xml', 'text/html', 'multipart/mixed', '*/*'), $request->getAcceptableContentTypes());
}

public function testGetLanguages()
Expand All @@ -783,6 +783,18 @@ public function testGetLanguages()
$this->assertEquals(array('zh', 'en_US', 'en'), $request->getLanguages());
$this->assertEquals(array('zh', 'en_US', 'en'), $request->getLanguages());

$request = new Request();
$request->headers->set('Accept-language', 'zh, en-us; q=0.6, en; q=0.8');
$this->assertEquals(array('zh', 'en', 'en_US'), $request->getLanguages()); // Test out of order qvalues

$request = new Request();
$request->headers->set('Accept-language', 'zh, en, en-us');
$this->assertEquals(array('zh', 'en', 'en_US'), $request->getLanguages()); // Test equal weighting without qvalues

$request = new Request();
$request->headers->set('Accept-language', 'zh; q=0.6, en, en-us; q=0.6');
$this->assertEquals(array('en', 'zh', 'en_US'), $request->getLanguages()); // Test equal weighting with qvalues

$request = new Request();
$request->headers->set('Accept-language', 'zh, i-cherokee; q=0.6');
$this->assertEquals(array('zh', 'cherokee'), $request->getLanguages());
Expand Down

0 comments on commit 6c59fbd

Please sign in to comment.