public static createFromServer ( array $server ) : |
||
$server | array | An array similar or equal to $_SERVER, containing headers in the form of "HTTP_FOO_BAR" |
리턴 |
public static function createFromServer(array $server)
{
$headerFields = [];
if (isset($server['PHP_AUTH_USER']) && isset($server['PHP_AUTH_PW'])) {
$headerFields['Authorization'] = 'Basic ' . base64_encode($server['PHP_AUTH_USER'] . ':' . $server['PHP_AUTH_PW']);
}
foreach ($server as $name => $value) {
if (strpos($name, 'HTTP_') === 0) {
$name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
$headerFields[$name] = $value;
} elseif ($name == 'REDIRECT_REMOTE_AUTHORIZATION' && !isset($headerFields['Authorization'])) {
$headerFields['Authorization'] = $value;
} elseif (in_array($name, ['CONTENT_TYPE', 'CONTENT_LENGTH'])) {
$name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', $name))));
$headerFields[$name] = $value;
}
}
return new self($headerFields);
}
/** * Constructs a new Request object based on the given environment data. * * @param array $get Data similar to that which is typically provided by $_GET * @param array $post Data similar to that which is typically provided by $_POST * @param array $files Data similar to that which is typically provided by $_FILES * @param array $server Data similar to that which is typically provided by $_SERVER * @see create() * @see createFromEnvironment() * @api */ public function __construct(array $get, array $post, array $files, array $server) { $this->headers = Headers::createFromServer($server); $this->setAttribute(self::ATTRIBUTE_CLIENT_IP, isset($server['REMOTE_ADDR']) ? $server['REMOTE_ADDR'] : null); $method = isset($server['REQUEST_METHOD']) ? $server['REQUEST_METHOD'] : 'GET'; if ($method === 'POST') { if (isset($post['__method'])) { $method = $post['__method']; } elseif (isset($server['HTTP_X_HTTP_METHOD_OVERRIDE'])) { $method = $server['HTTP_X_HTTP_METHOD_OVERRIDE']; } elseif (isset($server['HTTP_X_HTTP_METHOD'])) { $method = $server['HTTP_X_HTTP_METHOD']; } } $this->setMethod($method); $protocol = isset($server['SSL_SESSION_ID']) || isset($server['HTTPS']) && ($server['HTTPS'] === 'on' || strcmp($server['HTTPS'], '1') === 0) ? 'https' : 'http'; $host = isset($server['HTTP_HOST']) ? $server['HTTP_HOST'] : 'localhost'; $requestUri = isset($server['REQUEST_URI']) ? $server['REQUEST_URI'] : '/'; if (substr($requestUri, 0, 10) === '/index.php') { $requestUri = '/' . ltrim(substr($requestUri, 10), '/'); } $this->uri = new Uri($protocol . '://' . $host . $requestUri); if (isset($server['SERVER_PORT'])) { $this->uri->setPort($server['SERVER_PORT']); } $this->server = $server; $this->arguments = $this->buildUnifiedArguments($get, $post, $files); }