Skip to content

Commit

Permalink
improved header handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tflori committed Nov 21, 2016
1 parent d22740b commit 26714cb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 38 deletions.
41 changes: 34 additions & 7 deletions src/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class HttpRequest implements Request
protected $server;
protected $files;
protected $cookies;
protected $inputStream;

public function __construct(
array $get,
Expand Down Expand Up @@ -213,38 +214,55 @@ public function getMethod()
return $this->getServerVariable('REQUEST_METHOD');
}

/**
* Get the variable from $header.
*
* @param string $header
* @return string|null
*/
public function getHeader($header)
{
try {
$header = str_replace('-', '_', strtoupper($header));
return $this->getServerVariable('HTTP_' . $header);
} catch (MissingRequestMetaVariableException $e) {
return null;
}
}

/**
* Contents of the Accept: header from the current request, if there is one.
*
* @return string
* @throws MissingRequestMetaVariableException
* @deprecated use getHeader instead
*/
public function getHttpAccept()
{
return $this->getServerVariable('HTTP_ACCEPT');
return $this->getHeader('Accept');
}

/**
* The address of the page (if any) which referred the user agent to the
* current page.
*
* @return string
* @throws MissingRequestMetaVariableException
* @deprecated use getHeader instead
*/
public function getReferer()
{
return $this->getServerVariable('HTTP_REFERER');
// may the future fix the typo
return $this->getHeader('Referer') ?: $this->getHeader('Referrer');
}

/**
* Content of the User-Agent header from the request, if there is one.
*
* @return string
* @throws MissingRequestMetaVariableException
* @deprecated use getHeader instead
*/
public function getUserAgent()
{
return $this->getServerVariable('HTTP_USER_AGENT');
return $this->getHeader('User-Agent');
}

/**
Expand All @@ -255,7 +273,16 @@ public function getUserAgent()
*/
public function getIpAddress()
{
return $this->getServerVariable('REMOTE_ADDR');
$client = $this->getHeader('Client-Ip');
$forward = $this->getHeader('X-Forwarded-For');

if (filter_var($client, FILTER_VALIDATE_IP)) {
return $client;
} elseif (filter_var($forward, FILTER_VALIDATE_IP)) {
return $forward;
} else {
return $this->getServerVariable('REMOTE_ADDR');
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function getUri();
public function getPath();
public function getRelativePath();
public function getMethod();
public function getHeader($header);
public function getHttpAccept();
public function getReferer();
public function getUserAgent();
Expand Down
75 changes: 44 additions & 31 deletions test/Unit/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,22 @@ public function testGetMethodException()
$request->getMethod();
}

public function testGetHeader()
{
$request = new HttpRequest([], [], [], [], [
'HTTP_HOST' => 'example.com'
]);

self::assertSame('example.com', $request->getheader('Host'));
}

public function testGetHeaderReturnsNull()
{
$request = new HttpRequest([], [], [], [], []);

self::assertNull($request->getHeader('Unknown'));
}

public function testGetUri()
{
$server = ['REQUEST_URI' => '/test?abc=def'];
Expand Down Expand Up @@ -324,36 +340,18 @@ public function testGetHttpAccept()
);
}

/**
* @expectedException Http\MissingRequestMetaVariableException
*/
public function testGetHttpAcceptException()
{
$request = new HttpRequest([], [], [], [], []);
$request->getHttpAccept();
}

public function testGetReferer()
{
$server = ['HTTP_REFERER' => 'http://www.example.com/abc?s=a&b=c'];

$request = new HttpRequest([], [], [], [], $server);

$this->assertEquals(
$request->getReferer(),
$server['HTTP_REFERER']
self::assertEquals(
$server['HTTP_REFERER'],
$request->getReferer()
);
}

/**
* @expectedException Http\MissingRequestMetaVariableException
*/
public function testGetRefererException()
{
$request = new HttpRequest([], [], [], [], []);
$request->getReferer();
}

public function testGetUserAgent()
{
$server = ['HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0'];
Expand All @@ -366,24 +364,39 @@ public function testGetUserAgent()
);
}

/**
* @expectedException Http\MissingRequestMetaVariableException
*/
public function testGetUserAgentException()
public function testGetIpAddress()
{
$request = new HttpRequest([], [], [], [], []);
$request->getUserAgent();
$server = ['REMOTE_ADDR' => '127.0.0.1'];

$request = new HttpRequest([], [], [], [], $server);

$this->assertEquals(
$server['REMOTE_ADDR'],
$request->getIpAddress()
);
}

public function testGetIpAddress()
public function testGetIpAddressFromXForwardedFor()
{
$server = ['REMOTE_ADDR' => '127.0.0.1'];
$server = ['REMOTE_ADDR' => '127.0.0.1', 'HTTP_X_FORWARDED_FOR' => '8.8.8.8'];

$request = new HttpRequest([], [], [], [], $server);

$this->assertEquals(
$server['HTTP_X_FORWARDED_FOR'],
$request->getIpAddress()
);
}

public function testGetIpAddressFromClientIp()
{
$server = ['REMOTE_ADDR' => '127.0.0.1', 'HTTP_CLIENT_IP' => '8.8.8.8'];

$request = new HttpRequest([], [], [], [], $server);

$this->assertEquals(
$request->getIpAddress(),
$server['REMOTE_ADDR']
$server['HTTP_CLIENT_IP'],
$request->getIpAddress()
);
}

Expand Down

0 comments on commit 26714cb

Please sign in to comment.