Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for PHP FPM on PATCH/DELETE/PUT requests. #195

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 58 additions & 56 deletions src/Bramus/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,62 +185,64 @@ public function mount($baseRoute, $fn)
$this->baseRoute = $curBaseRoute;
}

/**
* Get all request headers.
*
* @return array The request headers
*/
public function getRequestHeaders()
{
$headers = array();

// If getallheaders() is available, use that
if (function_exists('getallheaders')) {
$headers = getallheaders();

// getallheaders() can return false if something went wrong
if ($headers !== false) {
return $headers;
}
}

// Method getallheaders() not available or went wrong: manually extract 'm
foreach ($_SERVER as $name => $value) {
if ((substr($name, 0, 5) == 'HTTP_') || ($name == 'CONTENT_TYPE') || ($name == 'CONTENT_LENGTH')) {
$headers[str_replace(array(' ', 'Http'), array('-', 'HTTP'), ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}

return $headers;
}

/**
* Get the request method used, taking overrides into account.
*
* @return string The Request method to handle
*/
public function getRequestMethod()
{
// Take the method as found in $_SERVER
$method = $_SERVER['REQUEST_METHOD'];

// If it's a HEAD request override it to being GET and prevent any output, as per HTTP Specification
// @url http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4
if ($_SERVER['REQUEST_METHOD'] == 'HEAD') {
ob_start();
$method = 'GET';
}

// If it's a POST request, check for a method override header
elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
$headers = $this->getRequestHeaders();
if (isset($headers['X-HTTP-Method-Override']) && in_array($headers['X-HTTP-Method-Override'], array('PUT', 'DELETE', 'PATCH'))) {
$method = $headers['X-HTTP-Method-Override'];
}
}

return $method;
}
/**
* Get all request headers.
*
* @return array The request headers
*/
public function getRequestHeaders()
{
function formatHeadersName($headers, $removeFirstChars = false)
{

foreach ($headers as $name => $value) {

if ((substr($name, 0, 5) == 'HTTP_') || (substr($name, 0, 5) == 'X-Htt') || ($name == 'CONTENT_TYPE') || ($name == 'CONTENT_LENGTH')) {
$headers[str_replace([' ', 'Http'], ['-', 'HTTP'], ucwords(strtolower(str_replace(['_', '-'], ' ', ($removeFirstChars ? substr($name, 5) : $name) ))))] = $value;
}
}
return $headers;
}

// If getallheaders() is available, use that
if (function_exists('getallheaders')) {
$headers = formatHeadersName(getallheaders(), false);
// getallheaders() can return false if something went wrong
if ($headers !== FALSE) {
return $headers;
}
} else {
// Method getallheaders() not available or went wrong: manually extract 'm
$headers = formatHeadersName($_SERVER, true);
}
return $headers;
}

/**
* Get the request method used, taking overrides into account.
*
* @return string The Request method to handle
*/
public function getRequestMethod()
{
// Take the method as found in $_SERVER
$method = $_SERVER['REQUEST_METHOD'];

// If it's a HEAD request override it to being GET and prevent any output, as per HTTP Specification
// @url http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4
if ($_SERVER['REQUEST_METHOD'] === 'HEAD') {
ob_start();
$method = 'GET';
} // If it's a POST request, check for a method override header
elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
$headers = $this->getRequestHeaders();
if (isset($headers['X-HTTP-Method-Override']) && in_array($headers['X-HTTP-Method-Override'], ['PUT', 'DELETE', 'PATCH'])) {
$method = $headers['X-HTTP-Method-Override'];
}
}

return $method;
}

/**
* Set a Default Lookup Namespace for Callable methods.
Expand Down