Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Better content-type negotiation #3442

Merged
merged 1 commit into from

2 participants

Matthew Weier O'Phinney Rob Allen
Matthew Weier O'Phinney
  • Don't pull the header value on each iteration of the loop
  • Get the media type of the header value before comparisons
  • Ensure there is no whitespace around the media type
Matthew Weier O'Phinney weierophinney Better content-type negotiation
- Don't pull the header value on each iteration of the loop
- Get the media type of the header value before comparisons
- Ensure there is no whitespace around the media type
b533f7a
Rob Allen akrabat merged commit b533f7a into from
Matthew Weier O'Phinney weierophinney deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 15, 2013
  1. Matthew Weier O'Phinney

    Better content-type negotiation

    weierophinney authored
    - Don't pull the header value on each iteration of the loop
    - Get the media type of the header value before comparisons
    - Ensure there is no whitespace around the media type
This page is out of date. Refresh to see the latest.
8 library/Zend/Mvc/Controller/AbstractRestfulController.php
View
@@ -347,9 +347,15 @@ public function requestHasContentType(Request $request, $contentType = '')
return false;
}
+ $requestedContentType = $headerContentType->getFieldValue();
+ if (strstr($requestedContentType, ';')) {
+ $headerData = explode(';', $requestedContentType);
+ $requestedContentType = array_shift($headerData);
+ }
+ $requestedContentType = trim($requestedContentType);
if (array_key_exists($contentType, $this->contentTypes)) {
foreach ($this->contentTypes[$contentType] as $contentTypeValue) {
- if (stripos($contentTypeValue, $headerContentType->getFieldValue()) === 0) {
+ if (stripos($contentTypeValue, $requestedContentType) === 0) {
return true;
}
}
36 tests/ZendTest/Mvc/Controller/RestfulControllerTest.php
View
@@ -305,4 +305,40 @@ public function testParsingDataAsJsonWillReturnAsArray()
$this->assertInternalType('array', $result);
$this->assertEquals(array('entity' => array('foo' => 'bar')), $result);
}
+
+ public function matchingContentTypes()
+ {
+ return array(
+ 'exact-first' => array('application/hal+json'),
+ 'exact-second' => array('application/json'),
+ 'with-charset' => array('application/json; charset=utf-8'),
+ 'with-whitespace' => array('application/json '),
+ );
+ }
+
+ /**
+ * @dataProvider matchingContentTypes
+ */
+ public function testRequestingContentTypeReturnsTrueForValidMatches($contentType)
+ {
+ $this->request->getHeaders()->addHeaderLine('Content-Type', $contentType);
+ $this->assertTrue($this->controller->requestHasContentType($this->request, TestAsset\RestfulTestController::CONTENT_TYPE_JSON));
+ }
+
+ public function nonMatchingContentTypes()
+ {
+ return array(
+ 'specific-type' => array('application/xml'),
+ 'generic-type' => array('text/json'),
+ );
+ }
+
+ /**
+ * @dataProvider nonMatchingContentTypes
+ */
+ public function testRequestingContentTypeReturnsFalseForInvalidMatches($contentType)
+ {
+ $this->request->getHeaders()->addHeaderLine('Content-Type', $contentType);
+ $this->assertFalse($this->controller->requestHasContentType($this->request, TestAsset\RestfulTestController::CONTENT_TYPE_JSON));
+ }
}
Something went wrong with that request. Please try again.