Allow Request::readHttpRequest to work when apache_request_headers is not available.
This commit is contained in:
parent
7f4ab73048
commit
fef677168e
|
|
@ -1,3 +1,6 @@
|
||||||
|
### v1.2.1
|
||||||
|
- Allow Request to read request headers without apache_request_headers().
|
||||||
|
|
||||||
### v1.2.0
|
### v1.2.0
|
||||||
2013-05-27
|
2013-05-27
|
||||||
**Warning** Some of the changes in this update will break backward compatibility.
|
**Warning** Some of the changes in this update will break backward compatibility.
|
||||||
|
|
|
||||||
|
|
@ -74,6 +74,29 @@ class Request extends Message implements RequestInterface
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
// Accessors
|
// Accessors
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the URI for the Request. This sets the other members: hostname,
|
||||||
|
* path, port, and query.
|
||||||
|
*
|
||||||
|
* @param string $uri
|
||||||
|
*/
|
||||||
|
public function setUri($uri)
|
||||||
|
{
|
||||||
|
$parsed = parse_url($uri);
|
||||||
|
|
||||||
|
$host = isset($parsed['host']) ? $parsed['host'] : '';
|
||||||
|
$this->setHostname($host);
|
||||||
|
|
||||||
|
$path = isset($parsed['path']) ? $parsed['path'] : '';
|
||||||
|
$this->setPath($path);
|
||||||
|
|
||||||
|
$port = isset($parsed['port']) ? (int) $parsed['port'] : 80;
|
||||||
|
$this->setPort($port);
|
||||||
|
|
||||||
|
$query = isset($parsed['query']) ? $parsed['query'] : '';
|
||||||
|
$this->setQuery($query);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a reference to the singleton instance of the Request derived
|
* Return a reference to the singleton instance of the Request derived
|
||||||
* from the server's information about the request sent to the script.
|
* from the server's information about the request sent to the script.
|
||||||
|
|
@ -98,7 +121,7 @@ class Request extends Message implements RequestInterface
|
||||||
public function readHttpRequest()
|
public function readHttpRequest()
|
||||||
{
|
{
|
||||||
$this->setBody(file_get_contents("php://input"), false);
|
$this->setBody(file_get_contents("php://input"), false);
|
||||||
$this->headers = apache_request_headers();
|
$this->headers = self::getRequestHeaders();
|
||||||
|
|
||||||
// Add case insensitive headers to the lookup table.
|
// Add case insensitive headers to the lookup table.
|
||||||
foreach ($this->headers as $key => $value) {
|
foreach ($this->headers as $key => $value) {
|
||||||
|
|
@ -110,49 +133,36 @@ class Request extends Message implements RequestInterface
|
||||||
$this->hostname = $_SERVER['HTTP_HOST'];
|
$this->hostname = $_SERVER['HTTP_HOST'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** @return array all request headers from the current request. */
|
||||||
* Return the full URI includeing protocol, hostname, path, and query.
|
public static function getRequestHeaders()
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getUri()
|
|
||||||
{
|
{
|
||||||
$uri = strtolower($this->protocol) . '://' . $this->hostname;
|
if (function_exists('apache_request_headers')) {
|
||||||
|
return apache_request_headers();
|
||||||
if ($this->port !== 80) {
|
|
||||||
$uri .= ':' . $this->port;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$uri .= $this->path;
|
// If apache_request_headers is not available, use this, based on replacement code from
|
||||||
|
// the PHP manual.
|
||||||
|
|
||||||
if ($this->query) {
|
$arh = array();
|
||||||
$uri .= '?' . http_build_query($this->query);
|
$rx_http = '/\AHTTP_/';
|
||||||
|
foreach ($_SERVER as $key => $val) {
|
||||||
|
if (preg_match($rx_http, $key)) {
|
||||||
|
$arh_key = preg_replace($rx_http, '', $key);
|
||||||
|
// do some nasty string manipulations to restore the original letter case
|
||||||
|
// this should work in most cases
|
||||||
|
$rx_matches = explode('_', $arh_key);
|
||||||
|
if (count($rx_matches) > 0 and strlen($arh_key) > 2) {
|
||||||
|
foreach ($rx_matches as $ak_key => $ak_val) {
|
||||||
|
$rx_matches[$ak_key] = ucfirst(
|
||||||
|
$ak_val
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$arh_key = implode('-', $rx_matches);
|
||||||
|
}
|
||||||
|
$arh[$arh_key] = $val;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return $arh;
|
||||||
return $uri;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the URI for the Request. This sets the other members: hostname,
|
|
||||||
* path, port, and query.
|
|
||||||
*
|
|
||||||
* @param string $uri
|
|
||||||
*/
|
|
||||||
public function setUri($uri)
|
|
||||||
{
|
|
||||||
$parsed = parse_url($uri);
|
|
||||||
|
|
||||||
$host = isset($parsed['host']) ? $parsed['host'] : '';
|
|
||||||
$this->setHostname($host);
|
|
||||||
|
|
||||||
$path = isset($parsed['path']) ? $parsed['path'] : '';
|
|
||||||
$this->setPath($path);
|
|
||||||
|
|
||||||
$port = isset($parsed['port']) ? (int)$parsed['port'] : 80;
|
|
||||||
$this->setPort($port);
|
|
||||||
|
|
||||||
$query = isset($parsed['query']) ? $parsed['query'] : '';
|
|
||||||
$this->setQuery($query);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -287,8 +297,6 @@ class Request extends Message implements RequestInterface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make a cURL request out of the instance and return a Response.
|
* Make a cURL request out of the instance and return a Response.
|
||||||
*
|
*
|
||||||
|
|
@ -372,4 +380,28 @@ class Request extends Message implements RequestInterface
|
||||||
return $resp;
|
return $resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the full URI includeing protocol, hostname, path, and query.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getUri()
|
||||||
|
{
|
||||||
|
$uri = strtolower($this->protocol) . '://' . $this->hostname;
|
||||||
|
|
||||||
|
if ($this->port !== 80) {
|
||||||
|
$uri .= ':' . $this->port;
|
||||||
|
}
|
||||||
|
|
||||||
|
$uri .= $this->path;
|
||||||
|
|
||||||
|
if ($this->query) {
|
||||||
|
$uri .= '?' . http_build_query($this->query);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $uri;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue