Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[#3739 & #3740] Using version-compare in accept header handler params. #3743

Merged
merged 1 commit into from

2 participants

Dolf Schimmel Matthew Weier O'Phinney
Dolf Schimmel
Collaborator

This commit provides functionality to define a set of version number
ranges to use in your accept header handling. Support for mere
integers was already present, but now support for ranges like
3.1.1-4.0.0 has been added.

Also, support for development stages like DEV or BETA was added. This
allows one to define ranges like 4.0.0-"5.0.0-Beta".

Beware, when using inside an accept header, the parameter value must
be quoted as well, and the quotes inside the parameter should be
escaped: application/vnd.com.example+xml; version="3.1-\"3.1.1-DEV\""

Dolf Schimmel Freeaqingme commented on the diff
tests/ZendTest/Http/Header/AcceptTest.php
@@ -301,14 +353,16 @@ public function testVersioningAndPriorization()
$this->assertEquals($value, $res->$key);
}
- $expected = (object) array('typeString' => 'text/html',
- 'type' => 'text',
- 'subtype' => 'html',
- 'subtypeRaw' => 'html',
- 'format' => 'html',
- 'priority' => 0.4,
- 'params' => array('q' => 0.4, 'level' => 2),
- 'raw' => 'text/html;level=2;q=0.4');
+ $expected = (object) array(
Dolf Schimmel Collaborator

This block was only about white space. No contents were changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Dolf Schimmel
Collaborator

Github didn't pick up the references. This PR should fix both #3739 and #3740.

Dolf Schimmel Freeaqingme [#3739 & #3740] Using version-compare in accept header handler params.
This commit provides functionality to define a set of version number
ranges to use in your accept header handling. Support for mere
integers was already present, but now support for ranges like
3.1.1-4.0.0 has been added.

Also, support for development stages like DEV or BETA was added. This
allows one to define ranges like 4.0.0-"5.0.0-Beta".

Beware, when using inside an accept header, the parameter value must
be quoted as well, and the quotes inside the parameter should be
escaped: application/vnd.com.example+xml; version="\3.1\"-\"3.1.1-DEV\""
21400b5
Matthew Weier O'Phinney weierophinney merged commit 21400b5 into from
Matthew Weier O'Phinney weierophinney referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/3743'
Close #3743
Fixes #3739
Fixes #3740
68b031b
Dolf Schimmel Freeaqingme deleted the branch
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/3743'
Close #3743
Fixes #3739
Fixes #3740
5a91134
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/3743' into develop
Close #3743
46b67ce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 8, 2013
  1. Dolf Schimmel

    [#3739 & #3740] Using version-compare in accept header handler params.

    Freeaqingme authored
    This commit provides functionality to define a set of version number
    ranges to use in your accept header handling. Support for mere
    integers was already present, but now support for ranges like
    3.1.1-4.0.0 has been added.
    
    Also, support for development stages like DEV or BETA was added. This
    allows one to define ranges like 4.0.0-"5.0.0-Beta".
    
    Beware, when using inside an accept header, the parameter value must
    be quoted as well, and the quotes inside the parameter should be
    escaped: application/vnd.com.example+xml; version="\3.1\"-\"3.1.1-DEV\""
This page is out of date. Refresh to see the latest.
17 library/Zend/Http/Header/AbstractAccept.php
View
@@ -168,7 +168,7 @@ protected function getParametersFromFieldValuePart($fieldValuePart)
$explode = explode('=', $param, 2);
$value = trim($explode[1]);
- if ($value[0] == '"' && substr($value, -1) == '"') {
+ if (isset($value[0]) && $value[0] == '"' && substr($value, -1) == '"') {
$value = substr(substr($value, 1), 0, -1);
}
@@ -341,10 +341,17 @@ protected function matchAcceptParams($match1, $match2)
foreach ($match2->params as $key => $value) {
if (isset($match1->params[$key])) {
if (strpos($value, '-')) {
- $values = explode('-', $value, 2);
- if ($values[0] > $match1->params[$key] ||
- $values[1] < $match1->params[$key])
- {
+ preg_match(
+ '/^(?|([^"-]*)|"([^"]*)")-(?|([^"-]*)|"([^"]*)")\z/',
+ $value,
+ $pieces
+ );
+
+ if (count($pieces) == 3 &&
+ (version_compare($pieces[1], $match1->params[$key], '<=') xor
+ version_compare($pieces[2], $match1->params[$key], '>=')
+ )
+ ) {
return false;
}
} elseif (strpos($value, '|')) {
71 tests/ZendTest/Http/Header/AcceptTest.php
View
@@ -280,6 +280,59 @@ public function testWildcardWithDifferentParamsAndRanges()
$this->assertEquals('21', $res->getParams()->version);
}
+ /**
+ * @group 3739
+ * @covers Accept::matchAcceptParams()
+ */
+ public function testParamRangesWithDecimals()
+ {
+ $acceptHeader = Accept::fromString('Accept: application/vnd.com.example+xml; version=10');
+ $this->assertFalse($acceptHeader->match('application/vnd.com.example+xml; version="\"3.1\"-\"3.1.1-DEV\""'));
+ }
+
+ /**
+ * @group 3740
+ * @dataProvider provideParamRanges
+ * @covers Accept::matchAcceptParams()
+ * @covers Accept::getParametersFromFieldValuePart()
+ */
+ public function testParamRangesSupportDevStage($range, $input, $success)
+ {
+ $acceptHeader = Accept::fromString(
+ 'Accept: application/vnd.com.example+xml; version="' . addslashes($input) . '"'
+ );
+
+ $res = $acceptHeader->match(
+ 'application/vnd.com.example+xml; version="' . addslashes($range) . '"'
+ );
+
+ if ($success) {
+ $this->assertInstanceOf('Zend\Http\Header\Accept\FieldValuePart\AcceptFieldValuePart', $res);
+ } else {
+ $this->assertFalse($res);
+ }
+ }
+
+ /**
+ * @group 3740
+ * @return array
+ */
+ public function provideParamRanges()
+ {
+ return array(
+ array('"3.1.1-DEV"-3.1.1', '3.1.1', true),
+ array('3.1.0-"3.1.1-DEV"', '3.1.1', false),
+ array('3.1.0-"3.1.1-DEV"', '3.1.1-DEV', true),
+ array('3.1.0-"3.1.1-DEV"', '3.1.2-DEV', false),
+ array('3.1.0-"3.1.1"', '3.1.2-DEV', false),
+ array('3.1.0-"3.1.1"', '3.1.0-DEV', false),
+ array('"3.1.0-DEV"-"3.1.1-BETA"', '3.1.0', true),
+ array('"3.1.0-DEV"-"3.1.1-BETA"', '3.1.1', false),
+ array('"3.1.0-DEV"-"3.1.1-BETA"', '3.1.1-BETA', true),
+ array('"3.1.0-DEV"-"3.1.1-BETA"', '3.1.0-DEV', true),
+ );
+ }
+
public function testVersioningAndPriorization()
{
$acceptStr = 'Accept: text/html; version=23, text/json; version=15.3; q=0.9,' .
@@ -301,14 +354,16 @@ public function testVersioningAndPriorization()
$this->assertEquals($value, $res->$key);
}
- $expected = (object) array('typeString' => 'text/html',
- 'type' => 'text',
- 'subtype' => 'html',
- 'subtypeRaw' => 'html',
- 'format' => 'html',
- 'priority' => 0.4,
- 'params' => array('q' => 0.4, 'level' => 2),
- 'raw' => 'text/html;level=2;q=0.4');
+ $expected = (object) array(
Dolf Schimmel Collaborator

This block was only about white space. No contents were changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 'typeString' => 'text/html',
+ 'type' => 'text',
+ 'subtype' => 'html',
+ 'subtypeRaw' => 'html',
+ 'format' => 'html',
+ 'priority' => 0.4,
+ 'params' => array('q' => 0.4, 'level' => 2),
+ 'raw' => 'text/html;level=2;q=0.4'
+ );
$str = 'text/html; version=17,text/json; version=15-16; q=0.5';
$res = $acceptHeader->match($str);
Something went wrong with that request. Please try again.