mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-07 20:12:42 +00:00
Fixing RequesHandler::prefers(). It was previously entirely wrong.
It took the ordered list of accept types, and blindly assumed the first in the list was the most preferred. This is an incorrect assumption to make, as all types with the same q value are equal. - Using CakeRequest::parseAccept() to access only the most preferred content types. - Using in_array() to check for the desired type. - Updating tests for RequestHandler.
This commit is contained in:
parent
e2f48b4a95
commit
bb3a1d546b
2 changed files with 15 additions and 12 deletions
|
@ -468,14 +468,17 @@ class RequestHandlerComponent extends Component {
|
|||
* @see RequestHandlerComponent::setContent()
|
||||
*/
|
||||
public function prefers($type = null) {
|
||||
$accepts = $this->accepts();
|
||||
$acceptRaw = $this->request->parseAccept();
|
||||
|
||||
if (empty($acceptRaw)) {
|
||||
return $this->ext;
|
||||
}
|
||||
$accepts = array_shift($acceptRaw);
|
||||
$accepts = $this->mapType($accepts);
|
||||
|
||||
if ($type == null) {
|
||||
if (empty($this->ext)) {
|
||||
if (is_array($accepts)) {
|
||||
return $accepts[0];
|
||||
}
|
||||
return $accepts;
|
||||
if (empty($this->ext) && !empty($accepts)) {
|
||||
return $accepts[0];
|
||||
}
|
||||
return $this->ext;
|
||||
}
|
||||
|
@ -484,9 +487,9 @@ class RequestHandlerComponent extends Component {
|
|||
|
||||
if (count($types) === 1) {
|
||||
if (!empty($this->ext)) {
|
||||
return ($types[0] == $this->ext);
|
||||
return in_array($this->ext, $types);
|
||||
}
|
||||
return ($types[0] == $accepts[0]);
|
||||
return in_array($types[0], $accepts);
|
||||
}
|
||||
|
||||
$intersect = array_values(array_intersect($accepts, $types));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue