From 516b8249d4179c8755f5221a4afad56456f0986b Mon Sep 17 00:00:00 2001 From: Kunal Ray Date: Fri, 29 Oct 2021 18:55:02 +0530 Subject: [PATCH] [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 --- .../Http/Concerns/InteractsWithContentTypes.php | 10 +++++++++- tests/Http/HttpRequestTest.php | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php b/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php index 0d6ae9e605d1..d11db73ba3af 100644 --- a/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php +++ b/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php @@ -37,7 +37,7 @@ public function wantsJson() { $acceptable = $this->getAcceptableContentTypes(); - return isset($acceptable[0]) && Str::contains($acceptable[0], ['/json', '+json']); + return isset($acceptable[0]) && Str::contains(strtolower($acceptable[0]), ['/json', '+json']); } /** @@ -62,6 +62,10 @@ public function accepts($contentTypes) } foreach ($types as $type) { + $accept = strtolower($accept); + + $type = strtolower($type); + if ($this->matchesType($accept, $type) || $accept === strtok($type, '/').'/*') { return true; } @@ -95,6 +99,10 @@ public function prefers($contentTypes) $type = $mimeType; } + $accept = strtolower($accept); + + $type = strtolower($type); + if ($this->matchesType($type, $accept) || $accept === strtok($type, '/').'/*') { return $contentType; } diff --git a/tests/Http/HttpRequestTest.php b/tests/Http/HttpRequestTest.php index 346305a39185..3d422c2a3672 100644 --- a/tests/Http/HttpRequestTest.php +++ b/tests/Http/HttpRequestTest.php @@ -1006,6 +1006,21 @@ public function testBadAcceptHeader() $this->assertFalse($request->accepts('text/html')); } + public function testCaseInsensitiveAcceptHeader() + { + $request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'APPLICATION/JSON']); + $this->assertTrue($request->accepts(['text/html', 'application/json'])); + + $request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'AppLiCaTion/JsOn']); + $this->assertTrue($request->accepts(['text/html', 'application/json'])); + + $request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'APPLICATION/*']); + $this->assertTrue($request->accepts(['text/html', 'application/json'])); + + $request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'APPLICATION/JSON']); + $this->assertTrue($request->expectsJson()); + } + public function testSessionMethod() { $this->expectException(RuntimeException::class);