From 6021e6a9e14d6ab62dcd4f83364058d4978d6890 Mon Sep 17 00:00:00 2001 From: William DURAND Date: Thu, 1 Aug 2013 16:37:18 +0200 Subject: [PATCH] Fix LWS issue Refs #6 --- src/Negotiation/FormatNegotiator.php | 3 ++- src/Negotiation/Negotiator.php | 20 +++++++++++++++--- .../Tests/FormatNegotiatorTest.php | 21 +++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/Negotiation/FormatNegotiator.php b/src/Negotiation/FormatNegotiator.php index cd99d7d..e5e1e26 100644 --- a/src/Negotiation/FormatNegotiator.php +++ b/src/Negotiation/FormatNegotiator.php @@ -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) { @@ -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(); diff --git a/src/Negotiation/Negotiator.php b/src/Negotiation/Negotiator.php index c9afc40..df8c73e 100644 --- a/src/Negotiation/Negotiator.php +++ b/src/Negotiation/Negotiator.php @@ -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) { @@ -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; @@ -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); + } } diff --git a/tests/Negotiation/Tests/FormatNegotiatorTest.php b/tests/Negotiation/Tests/FormatNegotiatorTest.php index 3453993..2bfdd7e 100644 --- a/tests/Negotiation/Tests/FormatNegotiatorTest.php +++ b/tests/Negotiation/Tests/FormatNegotiatorTest.php @@ -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, + ) + ), ); }