Skip to content

Commit 03436ed

Browse files
authored
Merge pull request #94 from weierophinney/hotfix/93
Do not compare + segments unless wildcards in either accept or priority
2 parents 2ba90aa + 516caf0 commit 03436ed

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Negotiation/Negotiator.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,18 @@ protected function match(AcceptHeader $accept, AcceptHeader $priority, $index)
4949
list($acceptSub, $acceptPlus) = $this->splitSubPart($acceptSub);
5050
list($prioritySub, $priorityPlus) = $this->splitSubPart($prioritySub);
5151

52+
// If no wildcards in either the subtype or + segment, do nothing.
53+
if (!($acceptBase === '*' || $baseEqual)
54+
|| !($acceptSub === '*' || $prioritySub === '*' || $acceptPlus === '*' || $priorityPlus === '*')
55+
) {
56+
return null;
57+
}
58+
5259
$subEqual = !strcasecmp($acceptSub, $prioritySub);
5360
$plusEqual = !strcasecmp($acceptPlus, $priorityPlus);
5461

55-
if (($acceptBase === '*' || $baseEqual)
56-
&& ($acceptSub === '*' || $subEqual || $acceptPlus === '*' || $plusEqual)
62+
if (($acceptSub === '*' || $prioritySub === '*' || $subEqual)
63+
&& ($acceptPlus === '*' || $priorityPlus === '*' || $plusEqual)
5764
&& count($intersection) === count($accept->getParameters())
5865
) {
5966
$score = 100 * $baseEqual + 10 * $subEqual + $plusEqual + count($intersection);

tests/Negotiation/Tests/NegotiatorTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ public static function dataProviderForTestGetBest()
9696
array($rfcHeader, array('text/html;q=0.4', 'text/plain'), array('text/plain', array())),
9797
# Wildcard "plus" parts (e.g., application/vnd.api+json)
9898
array('application/vnd.api+json', array('application/json', 'application/*+json'), array('application/*+json', array())),
99+
array('application/json;q=0.7, application/*+json;q=0.7', array('application/hal+json', 'application/problem+json'), array('application/hal+json', array())),
100+
array('application/json;q=0.7, application/problem+*;q=0.7', array('application/hal+xml', 'application/problem+xml'), array('application/problem+xml', array())),
99101
array($pearAcceptHeader, array('application/*+xml'), array('application/*+xml', array())),
102+
# @see https://github.com/willdurand/Negotiation/issues/93
103+
array('application/hal+json', array('application/ld+json', 'application/hal+json', 'application/xml', 'text/xml', 'application/json', 'text/html'), array('application/hal+json', array())),
100104
);
101105
}
102106

0 commit comments

Comments
 (0)