From 851b5685ae8c19fa2a2dde6cbbfb850e6bd7ea40 Mon Sep 17 00:00:00 2001 From: theboolean Date: Mon, 16 May 2016 10:51:58 +0200 Subject: [PATCH 1/4] Add a test Test correct handling of accept headers with semicolon without equal sign --- test/Header/AcceptCharsetTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/Header/AcceptCharsetTest.php b/test/Header/AcceptCharsetTest.php index 2ec6c0f630..63f2b4c6b3 100644 --- a/test/Header/AcceptCharsetTest.php +++ b/test/Header/AcceptCharsetTest.php @@ -32,6 +32,12 @@ public function testAcceptCharsetGetFieldValueReturnsProperValue() $this->assertEquals('xxx', $acceptCharsetHeader->getFieldValue()); } + public function testAcceptCharsetGetFieldValueReturnsProperValueWithSemicolonWithoutEqualSign() + { + $acceptCharsetHeader = AcceptCharset::fromString('Accept-Charset: xxx;yyy'); + $this->assertEquals('xxx;yyy', $acceptCharsetHeader->getFieldValue()); + } + public function testAcceptCharsetToStringReturnsHeaderFormattedString() { $acceptCharsetHeader = new AcceptCharset(); From 7f9863216374aa3728157fe42595b24fd1befb75 Mon Sep 17 00:00:00 2001 From: theboolean Date: Thu, 19 May 2016 18:13:20 +0200 Subject: [PATCH 2/4] Fix: avoid triggering a notice when an accept header without an equal sign is parsed --- src/Header/AbstractAccept.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Header/AbstractAccept.php b/src/Header/AbstractAccept.php index 7a8c379ee4..0304c7bced 100644 --- a/src/Header/AbstractAccept.php +++ b/src/Header/AbstractAccept.php @@ -170,7 +170,12 @@ protected function getParametersFromFieldValuePart($fieldValuePart) foreach ($paramsStrings as $param) { $explode = explode('=', $param, 2); - $value = trim($explode[1]); + if (count($explode) === 2) { + $value = trim($explode[1]); + } else { + $value = null; + } + if (isset($value[0]) && $value[0] == '"' && substr($value, -1) == '"') { $value = substr(substr($value, 1), 0, -1); } @@ -226,9 +231,9 @@ function ($v) { ); if ($escaped == $value && !array_intersect(str_split($value), $separators)) { - $value = $key . '=' . $value; + $value = $key . ($value ? '=' . $value : ''); } else { - $value = $key . '="' . $escaped . '"'; + $value = $key . ($value ? '="' . $escaped . '"' : ''); } return $value; From b3b51cb2723534ac02919499e8bfd0f270923421 Mon Sep 17 00:00:00 2001 From: theboolean Date: Thu, 19 May 2016 18:14:25 +0200 Subject: [PATCH 3/4] Add a test Test correct handling of accept headers with trailing semicolon --- test/Header/AcceptCharsetTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/Header/AcceptCharsetTest.php b/test/Header/AcceptCharsetTest.php index 63f2b4c6b3..115472c01b 100644 --- a/test/Header/AcceptCharsetTest.php +++ b/test/Header/AcceptCharsetTest.php @@ -32,6 +32,12 @@ public function testAcceptCharsetGetFieldValueReturnsProperValue() $this->assertEquals('xxx', $acceptCharsetHeader->getFieldValue()); } + public function testAcceptCharsetGetFieldValueReturnsProperValueWithTrailingSemicolon() + { + $acceptCharsetHeader = AcceptCharset::fromString('Accept-Charset: xxx;'); + $this->assertEquals('xxx', $acceptCharsetHeader->getFieldValue()); + } + public function testAcceptCharsetGetFieldValueReturnsProperValueWithSemicolonWithoutEqualSign() { $acceptCharsetHeader = AcceptCharset::fromString('Accept-Charset: xxx;yyy'); From 56a297fa63e7133161b5abecd5f4c764bc9a56f9 Mon Sep 17 00:00:00 2001 From: theboolean Date: Thu, 19 May 2016 18:23:51 +0200 Subject: [PATCH 4/4] Add new tests Test correct handling of accept headers with trailing semicolon and with semicolon without equal sign --- test/Header/AcceptEncodingTest.php | 12 ++++++++++++ test/Header/AcceptLanguageTest.php | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/test/Header/AcceptEncodingTest.php b/test/Header/AcceptEncodingTest.php index e85fc0e593..9707dbc8d4 100644 --- a/test/Header/AcceptEncodingTest.php +++ b/test/Header/AcceptEncodingTest.php @@ -32,6 +32,18 @@ public function testAcceptEncodingGetFieldValueReturnsProperValue() $this->assertEquals('xxx', $acceptEncodingHeader->getFieldValue()); } + public function testAcceptEncodingGetFieldValueReturnsProperValueWithTrailingSemicolon() + { + $acceptEncodingHeader = AcceptEncoding::fromString('Accept-Encoding: xxx;'); + $this->assertEquals('xxx', $acceptEncodingHeader->getFieldValue()); + } + + public function testAcceptEncodingGetFieldValueReturnsProperValueWithSemicolonWithoutEqualSign() + { + $acceptEncodingHeader = AcceptEncoding::fromString('Accept-Encoding: xxx;yyy'); + $this->assertEquals('xxx;yyy', $acceptEncodingHeader->getFieldValue()); + } + public function testAcceptEncodingToStringReturnsHeaderFormattedString() { $acceptEncodingHeader = new AcceptEncoding(); diff --git a/test/Header/AcceptLanguageTest.php b/test/Header/AcceptLanguageTest.php index bc7d4eb6b2..1942ffa546 100644 --- a/test/Header/AcceptLanguageTest.php +++ b/test/Header/AcceptLanguageTest.php @@ -32,6 +32,18 @@ public function testAcceptLanguageGetFieldValueReturnsProperValue() $this->assertEquals('xxx', $acceptLanguageHeader->getFieldValue()); } + public function testAcceptLanguageGetFieldValueReturnsProperValueWithTrailingSemicolon() + { + $acceptLanguageHeader = AcceptLanguage::fromString('Accept-Language: xxx;'); + $this->assertEquals('xxx', $acceptLanguageHeader->getFieldValue()); + } + + public function testAcceptLanguageGetFieldValueReturnsProperValueWithSemicolonWithoutEqualSign() + { + $acceptLanguageHeader = AcceptLanguage::fromString('Accept-Language: xxx;yyy'); + $this->assertEquals('xxx;yyy', $acceptLanguageHeader->getFieldValue()); + } + public function testAcceptLanguageToStringReturnsHeaderFormattedString() { $acceptLanguageHeader = new AcceptLanguage();