/** * Constructor (private access) * * @param array|null $settings If present, these are used instead of global server variables */ private function __construct($settings = null) { if ($settings) { $this->properties = $settings; } else { $env = array(); //The HTTP request method $env['REQUEST_METHOD'] = $_SERVER['REQUEST_METHOD']; //The IP $env['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR']; // Server params $scriptName = $_SERVER['SCRIPT_NAME']; // <-- "/foo/index.php" $requestUri = $_SERVER['REQUEST_URI']; // <-- "/foo/bar?test=abc" or "/foo/index.php/bar?test=abc" $queryString = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''; // <-- "test=abc" or "" // Physical path if (strpos($requestUri, $scriptName) !== false) { $physicalPath = $scriptName; // <-- Without rewriting } else { $physicalPath = str_replace('\\', '', dirname($scriptName)); // <-- With rewriting } $env['SCRIPT_NAME'] = rtrim($physicalPath, '/'); // <-- Remove trailing slashes $env['PATH_INFO'] = !empty($requestUri) && !empty($physicalPath) && strpos($requestUri, $physicalPath) === 0 ? substr_replace($requestUri, '', 0, strlen($physicalPath)) : $requestUri; // <-- Remove physical path $env['PATH_INFO'] = str_replace('?' . $queryString, '', $env['PATH_INFO']); // <-- Remove query string $env['PATH_INFO'] = '/' . ltrim($env['PATH_INFO'], '/'); // <-- Ensure leading slash // Query string (without leading "?") $env['QUERY_STRING'] = $queryString; //Name of server host that is running the script $env['SERVER_NAME'] = $_SERVER['SERVER_NAME']; //Number of server port that is running the script $env['SERVER_PORT'] = $_SERVER['SERVER_PORT']; //HTTP request headers (retains HTTP_ prefix to match $_SERVER) $headers = \Slim\Http\Headers::extract($_SERVER); foreach ($headers as $key => $value) { $env[$key] = $value; } //Is the application running under HTTPS or HTTP protocol? $env['slim.url_scheme'] = empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off' ? 'http' : 'https'; //Input stream (readable one time only; not available for multipart/form-data requests) $rawInput = @file_get_contents('php://input'); if (!$rawInput) { $rawInput = ''; } $env['slim.input'] = $rawInput; //Error stream $env['slim.errors'] = @fopen('php://stderr', 'w'); // print_r($env); die; $this->properties = $env; } }
/** * Constructor * @param \Slim\Environment $env */ public function __construct(\Slim\Environment $env) { $this->env = $env; $this->headers = new \Slim\Http\Headers(\Slim\Http\Headers::extract($env)); $this->cookies = new \Slim\Helper\Set(\Slim\Http\Util::parseCookieHeader($env['HTTP_COOKIE'])); }
public function testExtractHeaders() { $test = array('HTTP_HOST' => 'foo.com', 'SERVER_NAME' => 'foo', 'CONTENT_TYPE' => 'text/html', 'X_FORWARDED_FOR' => '127.0.0.1'); $results = \Slim\Http\Headers::extract($test); $this->assertEquals(array('HTTP_HOST' => 'foo.com', 'CONTENT_TYPE' => 'text/html', 'X_FORWARDED_FOR' => '127.0.0.1'), $results); }
/** * Constructor (private access) * * @param array|null $settings If present, these are used instead of global server variables */ private function __construct($settings = null) { if ($settings) { $this->properties = $settings; } else { $env = array(); //The HTTP request method $env['REQUEST_METHOD'] = $_SERVER['REQUEST_METHOD']; //The IP $env['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR']; // Root URI (physical path) and resource URI (virtual path) $scriptName = str_replace($_SERVER['DOCUMENT_ROOT'], '', $_SERVER['SCRIPT_FILENAME']); // <-- "/physical/index.php" $requestUri = $_SERVER['REQUEST_URI']; // <-- "/physical/index.php/virtual?abc=123" or "/physical/virtual?abc=123" $queryString = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''; // <-- "abc=123" if (strpos($requestUri, $scriptName) === false) { // With rewriting $env['SCRIPT_NAME'] = str_replace('/' . basename($scriptName), '', $scriptName); } else { // Without rewriting $env['SCRIPT_NAME'] = $scriptName; } $env['PATH_INFO'] = '/' . ltrim(str_replace(array($env['SCRIPT_NAME'], '?' . $queryString), '', $requestUri), '/'); // Query string (without leading "?") $env['QUERY_STRING'] = $queryString; //Name of server host that is running the script $env['SERVER_NAME'] = $_SERVER['SERVER_NAME']; //Number of server port that is running the script $env['SERVER_PORT'] = $_SERVER['SERVER_PORT']; //HTTP request headers (retains HTTP_ prefix to match $_SERVER) $headers = \Slim\Http\Headers::extract($_SERVER); foreach ($headers as $key => $value) { $env[$key] = $value; } // $specialHeaders = array('CONTENT_TYPE', 'CONTENT_LENGTH', 'PHP_AUTH_USER', 'PHP_AUTH_PW', 'PHP_AUTH_DIGEST', 'AUTH_TYPE'); // foreach ($_SERVER as $key => $value) { // $value = is_string($value) ? trim($value) : $value; // if (strpos($key, 'HTTP_') === 0) { // $env[substr($key, 5)] = $value; // } elseif (strpos($key, 'X_') === 0 || in_array($key, $specialHeaders)) { // $env[$key] = $value; // } // } //Is the application running under HTTPS or HTTP protocol? $env['slim.url_scheme'] = empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off' ? 'http' : 'https'; //Input stream (readable one time only; not available for multipart/form-data requests) $rawInput = @file_get_contents('php://input'); if (!$rawInput) { $rawInput = ''; } $env['slim.input'] = $rawInput; //Error stream $env['slim.errors'] = @fopen('php://stderr', 'w'); $this->properties = $env; } }
/** * Constructor (private access) * * @param array|null $settings If present, these are used instead of global server variables */ private function __construct($settings = null) { if ($settings) { $this->properties = $settings; } else { $env = array(); //The HTTP request method $env['REQUEST_METHOD'] = $_SERVER['REQUEST_METHOD']; //The IP $env['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR']; /** * Application paths * * This derives two paths: SCRIPT_NAME and PATH_INFO. The SCRIPT_NAME * is the real, physical path to the application, be it in the root * directory or a subdirectory of the public document root. The PATH_INFO is the * virtual path to the requested resource within the application context. * * With htaccess, the SCRIPT_NAME will be an absolute path (without file name); * if not using htaccess, it will also include the file name. If it is "/", * it is set to an empty string (since it cannot have a trailing slash). * * The PATH_INFO will be an absolute path with a leading slash; this will be * used for application routing. */ if (strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']) === 0) { $env['SCRIPT_NAME'] = $_SERVER['SCRIPT_NAME']; //Without URL rewrite } else { $env['SCRIPT_NAME'] = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])); //With URL rewrite } $env['PATH_INFO'] = substr_replace($_SERVER['REQUEST_URI'], '', 0, strlen($env['SCRIPT_NAME'])); if (strpos($env['PATH_INFO'], '?') !== false) { $env['PATH_INFO'] = substr_replace($env['PATH_INFO'], '', strpos($env['PATH_INFO'], '?')); //query string is not removed automatically } $env['SCRIPT_NAME'] = rtrim($env['SCRIPT_NAME'], '/'); $env['PATH_INFO'] = '/' . ltrim($env['PATH_INFO'], '/'); //The portion of the request URI following the '?' $env['QUERY_STRING'] = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''; //Name of server host that is running the script $env['SERVER_NAME'] = $_SERVER['SERVER_NAME']; //Number of server port that is running the script $env['SERVER_PORT'] = $_SERVER['SERVER_PORT']; //HTTP request headers (retains HTTP_ prefix to match $_SERVER) $headers = \Slim\Http\Headers::extract($_SERVER); foreach ($headers as $key => $value) { $env[$key] = $value; } // $specialHeaders = array('CONTENT_TYPE', 'CONTENT_LENGTH', 'PHP_AUTH_USER', 'PHP_AUTH_PW', 'PHP_AUTH_DIGEST', 'AUTH_TYPE'); // foreach ($_SERVER as $key => $value) { // $value = is_string($value) ? trim($value) : $value; // if (strpos($key, 'HTTP_') === 0) { // $env[substr($key, 5)] = $value; // } elseif (strpos($key, 'X_') === 0 || in_array($key, $specialHeaders)) { // $env[$key] = $value; // } // } //Is the application running under HTTPS or HTTP protocol? $env['slim.url_scheme'] = empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off' ? 'http' : 'https'; //Input stream (readable one time only; not available for multipart/form-data requests) $rawInput = @file_get_contents('php://input'); if (!$rawInput) { $rawInput = ''; } $env['slim.input'] = $rawInput; //Error stream $env['slim.errors'] = @fopen('php://stderr', 'w'); $this->properties = $env; } }