Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -180,34 +180,56 @@ class Controller extends AbstractController
*/
protected function getOutputFormat(string $accept, array $produced): ?string
{
// Figure out what the client accepts
$accept = preg_split("/[\s,]+/", $accept);

// Remove q-factor weighting. E.g. "application/json;q=0.8" becomes "application/json"
$accept = array_map(function ($type) {return explode(';', $type)[0];}, $accept);

if (in_array('*/*', $accept) || in_array('application/*', $accept)) {
// Prefer JSON if the client has no preference
if (in_array('application/json', $produced)) {
return 'application/json';
}
if (in_array('application/xml', $produced)) {
return 'application/xml';
// First get the list of formats accepted by the client by weight.
// eg: text/html,*/*; q=0.7, text/*;q=0.8,text/plain;format=fixed;q=0.6 is turned to
// [
// text/html => 1, // because when no weight is present then the default value is 1. See https://httpwg.org/specs/rfc9110.html#quality.values .
// */* => 0.7,
// text/* => 0.8,
// text/plain => 0.6,
// ]
// So we can subsequently order that list by descending weight (because 1 is the most prefered value, 0.001 is the least preferred)
$weightedFormats = array();
foreach (explode(",", str_replace(' ', '', $accept)) as $accept) {
$exploded = explode(';', $accept);

// If no weight is present then the default value is 1 (see https://httpwg.org/specs/rfc9110.html#quality.values )
if (count($exploded) === 1) {
$weight = 1.0;
} else {
$lastItem = end($exploded);
if (str_starts_with($lastItem, "q=")) {
$weight = (float) str_replace("q=", "", $lastItem);
} else {
$weight = 1.0;
}
}
$weightedFormats[$exploded[0]] = $weight;
}
arsort($weightedFormats);

// Now return the first produced format that matches
foreach (array_keys($weightedFormats) as $acceptedFormat) {
$acceptedFormatParts = explode('/', $acceptedFormat);
if (count($acceptedFormatParts) != 2) {
// badly formatted header sent by the client. Let's continue (instead of crashing)
continue;
}
$acceptedFormatType = $acceptedFormatParts[0];
$acceptedFormatSubtype = $acceptedFormatParts[1];

if (in_array('application/json', $accept) && in_array('application/json', $produced)) {
return 'application/json';
}

if (in_array('application/xml', $accept) && in_array('application/xml', $produced)) {
return 'application/xml';
}

if (in_array('*/*', $accept)) {
return $produced[0];
foreach ($produced as $producedFormat) {
if ($acceptedFormat === $producedFormat) {
return $producedFormat;
}
if ($acceptedFormatSubtype === '*' && $acceptedFormatType === explode("/", $producedFormat)[0]) {
return $producedFormat;
}
if ($acceptedFormat === "*/*") {
return $producedFormat;
}
}
}

// If we reach this point, we don't have a common ground between server and client
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,34 +190,56 @@ private function exceptionToArray(?\Throwable $exception = null): ?array
*/
protected function getOutputFormat(string $accept, array $produced): ?string
{
// Figure out what the client accepts
$accept = preg_split("/[\s,]+/", $accept);

// Remove q-factor weighting. E.g. "application/json;q=0.8" becomes "application/json"
$accept = array_map(function ($type) {return explode(';', $type)[0];}, $accept);

if (in_array('*/*', $accept) || in_array('application/*', $accept)) {
// Prefer JSON if the client has no preference
if (in_array('application/json', $produced)) {
return 'application/json';
}
if (in_array('application/xml', $produced)) {
return 'application/xml';
// First get the list of formats accepted by the client by weight.
// eg: text/html,*/*; q=0.7, text/*;q=0.8,text/plain;format=fixed;q=0.6 is turned to
// [
// text/html => 1, // because when no weight is present then the default value is 1. See https://httpwg.org/specs/rfc9110.html#quality.values .
// */* => 0.7,
// text/* => 0.8,
// text/plain => 0.6,
// ]
// So we can subsequently order that list by descending weight (because 1 is the most prefered value, 0.001 is the least preferred)
$weightedFormats = array();
foreach (explode(",", str_replace(' ', '', $accept)) as $accept) {
$exploded = explode(';', $accept);

// If no weight is present then the default value is 1 (see https://httpwg.org/specs/rfc9110.html#quality.values )
if (count($exploded) === 1) {
$weight = 1.0;
} else {
$lastItem = end($exploded);
if (str_starts_with($lastItem, "q=")) {
$weight = (float) str_replace("q=", "", $lastItem);
} else {
$weight = 1.0;
}
}
$weightedFormats[$exploded[0]] = $weight;
}
arsort($weightedFormats);

// Now return the first produced format that matches
foreach (array_keys($weightedFormats) as $acceptedFormat) {
$acceptedFormatParts = explode('/', $acceptedFormat);
if (count($acceptedFormatParts) != 2) {
// badly formatted header sent by the client. Let's continue (instead of crashing)
continue;
}
$acceptedFormatType = $acceptedFormatParts[0];
$acceptedFormatSubtype = $acceptedFormatParts[1];

if (in_array('application/json', $accept) && in_array('application/json', $produced)) {
return 'application/json';
}

if (in_array('application/xml', $accept) && in_array('application/xml', $produced)) {
return 'application/xml';
}

if (in_array('*/*', $accept)) {
return $produced[0];
foreach ($produced as $producedFormat) {
if ($acceptedFormat === $producedFormat) {
return $producedFormat;
}
if ($acceptedFormatSubtype === '*' && $acceptedFormatType === explode("/", $producedFormat)[0]) {
return $producedFormat;
}
if ($acceptedFormat === "*/*") {
return $producedFormat;
}
}
}

// If we reach this point, we don't have a common ground between server and client
return null;
}
Expand Down
Loading