merged branch lsmith77/fix_q_handling (PR #2365)

Commits
-------

d3f137b cosmetic tweak
2877883 anything in front of ;q= is part of the mime type, anything after may be ignored

Discussion
----------

[HttpFoundation] fix splitHttpAcceptHeader() parsing of parameters

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -

anything in front of ;q= is part of the mime type, anything after may be ignored

see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1

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

by lsmith77 at 2011/10/09 04:00:12 -0700

i must admit .. i am not 100% that my implemention is correct either .. but i am sure the current one isn't.

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

by lsmith77 at 2011/10/09 07:57:33 -0700

@fabpot: I am also not sure if getFormat() should optionally not support matching parameters, aka anything before ``;q=..``
This commit is contained in:
Fabien Potencier 2011-10-25 17:26:31 +02:00
commit bc330d4487
2 changed files with 27 additions and 3 deletions

View File

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

View File

@ -838,6 +838,30 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$this->assertContains('Accept-Language: zh, en-us; q=0.8, en; q=0.6', $request->__toString());
}
/**
* @dataProvider splitHttpAcceptHeaderData
*/
public function testSplitHttpAcceptHeader($acceptHeader, $expected)
{
$request = new Request();
$this->assertEquals($expected, $request->splitHttpAcceptHeader($acceptHeader));
}
public function splitHttpAcceptHeaderData()
{
return array(
array(null, array()),
array('text/html;q=0.8', array('text/html' => 0.8)),
array('text/html;foo=bar;q=0.8 ', array('text/html;foo=bar' => 0.8)),
array('text/html;charset=utf-8; q=0.8', array('text/html;charset=utf-8' => 0.8)),
array('text/html,application/xml;q=0.9,*/*;charset=utf-8; q=0.8', array('text/html' => 1, 'application/xml' => 0.9, '*/*;charset=utf-8' => 0.8)),
array('text/html,application/xhtml+xml;q=0.9,*/*;q=0.8; foo=bar', array('text/html' => 1, 'application/xhtml+xml' => 0.9, '*/*' => 0.8)),
array('text/html,application/xhtml+xml;charset=utf-8;q=0.9; foo=bar,*/*', array('text/html' => 1, '*/*' => 1, 'application/xhtml+xml;charset=utf-8' => 0.9)),
array('text/html,application/xhtml+xml', array('application/xhtml+xml' => 1, 'text/html' => 1)),
);
}
}
class RequestContentProxy extends Request