Skip to content

Commit 85a8549

Browse files
[8.x] Make accept header comparison case-insensitive (#39413)
* Make accept header comparison case-insensitive * Update comment and ref link * Update InteractsWithContentTypes.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 13247e1 commit 85a8549

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/Illuminate/Http/Concerns/InteractsWithContentTypes.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function wantsJson()
3535
{
3636
$acceptable = $this->getAcceptableContentTypes();
3737

38-
return isset($acceptable[0]) && Str::contains($acceptable[0], ['/json', '+json']);
38+
return isset($acceptable[0]) && Str::contains(strtolower($acceptable[0]), ['/json', '+json']);
3939
}
4040

4141
/**
@@ -60,6 +60,10 @@ public function accepts($contentTypes)
6060
}
6161

6262
foreach ($types as $type) {
63+
$accept = strtolower($accept);
64+
65+
$type = strtolower($type);
66+
6367
if ($this->matchesType($accept, $type) || $accept === strtok($type, '/').'/*') {
6468
return true;
6569
}
@@ -93,6 +97,10 @@ public function prefers($contentTypes)
9397
$type = $mimeType;
9498
}
9599

100+
$accept = strtolower($accept);
101+
102+
$type = strtolower($type);
103+
96104
if ($this->matchesType($type, $accept) || $accept === strtok($type, '/').'/*') {
97105
return $contentType;
98106
}

tests/Http/HttpRequestTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,21 @@ public function testBadAcceptHeader()
10061006
$this->assertFalse($request->accepts('text/html'));
10071007
}
10081008

1009+
public function testCaseInsensitiveAcceptHeader()
1010+
{
1011+
$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'APPLICATION/JSON']);
1012+
$this->assertTrue($request->accepts(['text/html', 'application/json']));
1013+
1014+
$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'AppLiCaTion/JsOn']);
1015+
$this->assertTrue($request->accepts(['text/html', 'application/json']));
1016+
1017+
$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'APPLICATION/*']);
1018+
$this->assertTrue($request->accepts(['text/html', 'application/json']));
1019+
1020+
$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'APPLICATION/JSON']);
1021+
$this->assertTrue($request->expectsJson());
1022+
}
1023+
10091024
public function testSessionMethod()
10101025
{
10111026
$this->expectException(RuntimeException::class);

0 commit comments

Comments
 (0)