slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.94k stars 1.95k forks source link

Cannot get Authorization header in middleware #1784

Closed humpedli closed 8 years ago

humpedli commented 8 years ago

The problem is that Slim PHP 3 $request->getHeaders() function is not returning Authorization header parameter while the default getallheaders() PHP function is working correctly. When I use this Slim PHP function in a normal route, it's returning Authorization header parameter correctly, the issue exists only in the middleware.

Request headers:

Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:hu-HU,hu;q=0.8,en-US;q=0.6,en;q=0.4,it;q=0.2
Authorization:Bearer *****
Connection:keep-alive
Cookie:*****
Host:*****
Referer:*****
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36

Code snippet:

public function __invoke($request, $response, $next) {
    $slimHeaders = $request->getHeaders();
    var_dump($slimHeaders);

    $phpHeaders = getallheaders();
    var_dump($phpHeaders);
}

$slimHeaders dump:

array(8) {
  ["Host"]=>
  array(1) {
    [0]=>
    string(5) "*****"
  }
  ["HTTP_CONNECTION"]=>
  array(1) {
    [0]=>
    string(10) "keep-alive"
  }
  ["HTTP_ACCEPT"]=>
  array(1) {
    [0]=>
    string(33) "application/json, text/plain, */*"
  }
  ["HTTP_USER_AGENT"]=>
  array(1) {
    [0]=>
    string(121) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"
  }
  ["HTTP_REFERER"]=>
  array(1) {
    [0]=>
    string(5) "*****"
  }
  ["HTTP_ACCEPT_ENCODING"]=>
  array(1) {
    [0]=>
    string(19) "gzip, deflate, sdch"
  }
  ["HTTP_ACCEPT_LANGUAGE"]=>
  array(1) {
    [0]=>
    string(44) "hu-HU,hu;q=0.8,en-US;q=0.6,en;q=0.4,it;q=0.2"
  }
  ["HTTP_COOKIE"]=>
  array(1) {
    [0]=>
    string(5) "*****"
  }
}   

$phpHeaders dump:

array(9) {
  ["Host"]=>
  string(5) "*****"
  ["Connection"]=>
  string(10) "keep-alive"
  ["Accept"]=>
  string(33) "application/json, text/plain, */*"
  ["User-Agent"]=>
  string(121) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"
  ["Authorization"]=>
  string(12) "Bearer *****"
  ["Referer"]=>
  string(5) "*****"
  ["Accept-Encoding"]=>
  string(19) "gzip, deflate, sdch"
  ["Accept-Language"]=>
  string(44) "hu-HU,hu;q=0.8,en-US;q=0.6,en;q=0.4,it;q=0.2"
  ["Cookie"]=>
  string(5) "*****"
}
mathmarques commented 8 years ago

See https://github.com/slimphp/Slim/issues/1616 and https://github.com/slimphp/Slim/issues/831