Skip to content

Commit

Permalink
Fix LWS issue
Browse files Browse the repository at this point in the history
Refs #6
  • Loading branch information
willdurand committed Aug 1, 2013
1 parent d5e80a0 commit 6021e6a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Negotiation/FormatNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function registerFormat($format, array $mimeTypes, $override = false)
public function getBest($acceptHeader, array $priorities = array())
{
$acceptHeaders = $this->parseAcceptHeader($acceptHeader);
$priorities = array_map('strtolower', $priorities);
$priorities = $this->sanitizePriorities($priorities);
$catchAllEnabled = $this->isCatchAllEnabled($priorities);

foreach ($acceptHeaders as $accept) {
Expand Down Expand Up @@ -101,6 +101,7 @@ public function getBest($acceptHeader, array $priorities = array())
*/
public function getBestFormat($acceptHeader, array $priorities = array())
{
$priorities = $this->sanitizePriorities($priorities);
$catchAllEnabled = $this->isCatchAllEnabled($priorities);

$mimeTypes = array();
Expand Down
20 changes: 17 additions & 3 deletions src/Negotiation/Negotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getBest($acceptHeader, array $priorities = array())
}

if (0 !== count($priorities)) {
$priorities = array_map('strtolower', $priorities);
$priorities = $this->sanitizePriorities($priorities);

$wildcardAccept = null;
foreach ($accepts as $accept) {
Expand Down Expand Up @@ -59,9 +59,11 @@ protected function parseAcceptHeader($acceptHeader)
$catchAll = null;
foreach ($acceptParts as $accept) {
$quality = 1;
$parts = preg_split('/;\s*q=/i', $accept, 0, PREG_SPLIT_NO_EMPTY);

if (false !== strpos($accept, ';q=')) {
list($accept, $quality) = explode(';q=', $accept);
if (2 === count($parts)) {
$accept = $parts[0];
$quality = $parts[1];
} else {
if (self::CATCH_ALL_VALUE === $accept) {
$quality = 0.01;
Expand Down Expand Up @@ -111,4 +113,16 @@ protected function parseAcceptHeader($acceptHeader)
return $accept['item'];
}, array_values($accepts));
}

/**
* @param array $priorities
*
* @return array
*/
protected function sanitizePriorities(array $priorities)
{
return array_map(function ($priority) {
return preg_replace('/\s+/', '', strtolower($priority));
}, $priorities);
}
}
21 changes: 21 additions & 0 deletions tests/Negotiation/Tests/FormatNegotiatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,27 @@ public static function dataProviderForGetBest()
'quality' => 0.7,
)
),
// LWS / case sensitivity
array(
'text/* ; q=0.3, text/html ;Q=0.7, text/html ; level=1, text/html ;level = 2 ;q=0.4, */* ; q=0.5',
array(
'text/html; level=2'
),
array(
'value' => 'text/html;level=2',
'quality' => 0.4,
)
),
array(
'text/* ; q=0.3, text/html;Q=0.7, text/html ;level=1, text/html; level=2;q=0.4, */*;q=0.5',
array(
'text/html; level=3'
),
array(
'value' => 'text/html;level=3',
'quality' => 0.7,
)
),
);
}

Expand Down

0 comments on commit 6021e6a

Please sign in to comment.