Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactoring how q values are parsed, as content types were

reversed for similar q value types.  This causes issues in
RequestHandlerComponent.
  • Loading branch information...
commit e2f48b4a954424b491ad52aa9d8105a610eac8ee 1 parent ef4bb22
@markstory markstory authored
View
40 lib/Cake/Network/CakeRequest.php
@@ -613,27 +613,47 @@ public function subdomains($tldLength = 1) {
* provided type.
*/
public function accepts($type = null) {
+ $raw = $this->parseAccept();
+ $accept = array();
+ foreach ($raw as $value => $types) {
+ $accept = array_merge($accept, $types);
+ }
+ if ($type === null) {
+ return $accept;
+ }
+ return in_array($type, $accept);
+ }
+
+/**
+ * Parse the HTTP_ACCEPT header and return a sorted array with content types
+ * as the keys, and pref values as the values.
+ *
+ * Generally you want to use CakeRequest::accept() to get a simple list
+ * of the accepted content types.
+ *
+ * @return array An array of prefValue => array(content/types)
+ */
+ public function parseAccept() {
$accept = array();
$header = explode(',', $this->header('accept'));
- foreach (array_filter(array_reverse($header)) as $value) {
+ foreach (array_filter($header) as $value) {
$prefPos = strpos($value, ';');
if ($prefPos !== false) {
- $prefValue = (float) substr($value, strpos($value, '=') + 1);
+ $prefValue = substr($value, strpos($value, '=') + 1);
$value = trim(substr($value, 0, $prefPos));
} else {
- $prefValue = 1.0;
+ $prefValue = '1.0';
$value = trim($value);
}
+ if (!isset($accept[$prefValue])) {
+ $accept[$prefValue] = array();
+ }
if ($prefValue) {
- $accept[$value] = $prefValue;
+ $accept[$prefValue][] = $value;
}
}
- arsort($accept);
- $accept = array_keys($accept);
- if ($type === null) {
- return $accept;
- }
- return in_array($type, $accept);
+ krsort($accept);
+ return $accept;
}
/**
View
19 lib/Cake/Test/Case/Network/CakeRequestTest.php
@@ -785,7 +785,7 @@ public function testAccepts() {
$result = $request->accepts();
$expected = array(
- 'text/xml', 'application/xhtml+xml', 'text/html', 'image/png', 'text/plain', 'application/xml'
+ 'text/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png', 'application/xml'
);
$this->assertEquals($expected, $result, 'Content types differ.');
@@ -827,6 +827,23 @@ public function testAcceptWithQvalueSorting() {
}
/**
+ * Test the raw parsing of accept headers into the q value formatting.
+ *
+ * @return void
+ */
+ public function testParseAcceptWithQValue() {
+ $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0,image/png';
+ $request = new CakeRequest('/', false);
+ $result = $request->parseAccept();
+ $expected = array(
+ '1.0' => array('application/xml', 'image/png'),
+ '0.8' => array('text/html'),
+ '0.7' => array('application/json'),
+ );
+ $this->assertEquals($expected, $result);
+ }
+
+/**
* testBaseUrlAndWebrootWithModRewrite method
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.