Skip to content

Commit ce49922

Browse files
improve content type negotiation and HTML request detection
1 parent 5a25f3f commit ce49922

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

framework/core/src/Http/RequestUtil.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,23 @@ public static function isApiRequest(Request $request): bool
3434

3535
public static function isHtmlRequest(Request $request): bool
3636
{
37-
return Str::contains(
38-
$request->getHeaderLine('Accept'),
39-
'text/html'
40-
);
37+
return ! self::isApiRequest($request);
4138
}
4239

40+
/**
41+
* @param Request $request
42+
* @param array $types The content types to check against, in order of priority. The best match will be returned.
43+
* @return string
44+
*/
4345
public static function getPreferredContentType(Request $request, array $types): string
4446
{
4547
$accept = $request->getHeaderLine('Accept');
4648

49+
// We get some errors if $accept is empty, so we need to check for that and set it to */* if it is.
50+
if (! filled($accept)) {
51+
$accept = '*/*';
52+
}
53+
4754
return Mimeparse::bestMatch($types, $accept);
4855
}
4956

0 commit comments

Comments
 (0)