Skip to content

Commit

Permalink
Backport fix incorrect behavior with priorities
Browse files Browse the repository at this point in the history
See #24
  • Loading branch information
willdurand committed Feb 28, 2014
1 parent 84c855a commit d1347ba
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
21 changes: 20 additions & 1 deletion src/Negotiation/FormatNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ public function getBest($header, array $priorities = array())
$acceptHeaders = $this->parseHeader($header);
$priorities = $this->sanitize($priorities);
$catchAllEnabled = $this->isCatchAllEnabled($priorities);
$catchAllHeader = null;

foreach ($acceptHeaders as $accept) {
$mimeType = $accept->getValue();

if (self::CATCH_ALL_VALUE === $mimeType) {
$catchAllHeader = $accept;
}

if ('/*' !== substr($mimeType, -2)) {
if (in_array($mimeType, $priorities)) {
return $accept;
Expand Down Expand Up @@ -68,7 +73,21 @@ public function getBest($header, array $priorities = array())
}
}

// If $priorities is empty or contains a catch-all mime type
// if client sends `*/*` in Accept header, and nothing has been negotiated before
// then, return the first priority value if available
if (null !== $catchAllHeader) {
$value = array_shift($priorities);

if (null !== $value && self::CATCH_ALL_VALUE !== $value) {
return new AcceptHeader(
$value,
$catchAllHeader->getQuality(),
$this->parseParameters($catchAllHeader->getValue())
);
}
}

// if `$priorities` is empty or contains a catch-all mime type
if ($catchAllEnabled) {
return array_shift($acceptHeaders) ?: null;
}
Expand Down
39 changes: 37 additions & 2 deletions tests/Negotiation/Tests/FormatNegotiatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static function dataProviderForGetBest()
'application/rss+xml',
'*/*',
),
'text/html'
'application/rss+xml'
),
// See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
array(
Expand Down Expand Up @@ -252,6 +252,41 @@ public static function dataProviderForGetBest()
),
null
),
array(
'text/rdf+n3; q=0.8, application/rdf+json; q=0.8, text/turtle; q=1.0, text/n3; q=0.8, application/ld+json; q=0.5, application/rdf+xml; q=0.8',
array(),
'text/turtle'
),
array(
'application/rdf+xml;q=0.5,text/html;q=.3',
array(),
'application/rdf+xml'
),
array(
'application/xhtml+xml;q=0.5',
array(),
'application/xhtml+xml'
),
array(
'application/rdf+xml;q=0.5,text/html;q=.5',
array(),
'application/rdf+xml'
),
array(
'text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c',
array(),
'text/html',
),
// IE8 Accept header
array(
'image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*',
array(
'text/html',
'application/xhtml+xml',
'*/*'
),
'text/html',
),
);
}

Expand All @@ -262,7 +297,7 @@ public static function dataProviderForGetBestFormat()
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', array(), 'html'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', array('html', 'json', '*/*'), 'html'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', array('html', 'json', '*/*'), 'html'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', array('rss', '*/*'), 'html'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', array('rss', '*/*'), 'rss'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', array('xml'), 'xml'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', array('json', 'xml'), 'xml'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', array('json'), 'json'),
Expand Down

0 comments on commit d1347ba

Please sign in to comment.