namespace GuzzleHttp\Psr7;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UriInterface;
* Server-side HTTP request
* Extends the Request definition to add methods for accessing incoming data,
* specifically server parameters, cookies, matched path parameters, query
* string arguments, body parameters, and upload file information.
* "Attributes" are discovered via decomposing the request (and usually
* specifically the URI path), and typically will be injected by the application.
* Requests are considered immutable; all methods that might change state are
* implemented such that they retain the internal state of the current
* message and return a new instance that contains the changed state.
class ServerRequest extends Request implements ServerRequestInterface
private $attributes = [];
private $cookieParams = [];
private $queryParams = [];
private $uploadedFiles = [];
* @param string $method HTTP method
* @param string|UriInterface $uri URI
* @param array $headers Request headers
* @param string|resource|StreamInterface|null $body Request body
* @param string $version Protocol version
* @param array $serverParams Typically the $_SERVER superglobal
public function __construct(
$this->serverParams = $serverParams;
parent::__construct($method, $uri, $headers, $body, $version);
* Return an UploadedFile instance array.
* @param array $files A array which respect $_FILES structure
* @throws InvalidArgumentException for unrecognized values
public static function normalizeFiles(array $files)
foreach ($files as $key => $value) {
if ($value instanceof UploadedFileInterface) {
$normalized[$key] = $value;
} elseif (is_array($value) && isset($value['tmp_name'])) {
$normalized[$key] = self::createUploadedFileFromSpec($value);
} elseif (is_array($value)) {
$normalized[$key] = self::normalizeFiles($value);
throw new InvalidArgumentException('Invalid value in files specification');
* Create and return an UploadedFile instance from a $_FILES specification.
* If the specification represents an array of values, this method will
* delegate to normalizeNestedFileSpec() and return that return value.
* @param array $value $_FILES struct
* @return array|UploadedFileInterface
private static function createUploadedFileFromSpec(array $value)
if (is_array($value['tmp_name'])) {
return self::normalizeNestedFileSpec($value);
* Normalize an array of file specifications.
* Loops through all nested files and returns a normalized array of
* UploadedFileInterface instances.
* @return UploadedFileInterface[]
private static function normalizeNestedFileSpec(array $files = [])
foreach (array_keys($files['tmp_name']) as $key) {
'tmp_name' => $files['tmp_name'][$key],
'size' => $files['size'][$key],
'error' => $files['error'][$key],
'name' => $files['name'][$key],
'type' => $files['type'][$key],
$normalizedFiles[$key] = self::createUploadedFileFromSpec($spec);
* Return a ServerRequest populated with superglobals:
* @return ServerRequestInterface
public static function fromGlobals()
$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
$headers = getallheaders();
$uri = self::getUriFromGlobals();
$body = new CachingStream(new LazyOpenStream('php://input', 'r+'));
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']) : '1.1';
$serverRequest = new ServerRequest($method, $uri, $headers, $body, $protocol, $_SERVER);
->withCookieParams($_COOKIE)
->withUploadedFiles(self::normalizeFiles($_FILES));
private static function extractHostAndPortFromAuthority($authority)
$uri = 'http://' . $authority;
$parts = parse_url($uri);
$host = isset($parts['host']) ? $parts['host'] : null;
$port = isset($parts['port']) ? $parts['port'] : null;
* Get a Uri populated with values from $_SERVER.
public static function getUriFromGlobals()
$uri = $uri->withScheme(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http');
if (isset($_SERVER['HTTP_HOST'])) {
list($host, $port) = self::extractHostAndPortFromAuthority($_SERVER['HTTP_HOST']);
$uri = $uri->withHost($host);
$uri = $uri->withPort($port);
} elseif (isset($_SERVER['SERVER_NAME'])) {
$uri = $uri->withHost($_SERVER['SERVER_NAME']);
} elseif (isset($_SERVER['SERVER_ADDR'])) {
$uri = $uri->withHost($_SERVER['SERVER_ADDR']);
if (!$hasPort && isset($_SERVER['SERVER_PORT'])) {
$uri = $uri->withPort($_SERVER['SERVER_PORT']);
if (isset($_SERVER['REQUEST_URI'])) {
$requestUriParts = explode('?', $_SERVER['REQUEST_URI'], 2);
$uri = $uri->withPath($requestUriParts[0]);
if (isset($requestUriParts[1])) {
$uri = $uri->withQuery($requestUriParts[1]);
if (!$hasQuery && isset($_SERVER['QUERY_STRING'])) {
$uri = $uri->withQuery($_SERVER['QUERY_STRING']);
public function getServerParams()
return $this->serverParams;
public function getUploadedFiles()
return $this->uploadedFiles;
public function withUploadedFiles(array $uploadedFiles)
$new->uploadedFiles = $uploadedFiles;
public function getCookieParams()
return $this->cookieParams;
public function withCookieParams(array $cookies)
$new->cookieParams = $cookies;
public function getQueryParams()
return $this->queryParams;
public function withQueryParams(array $query)
$new->queryParams = $query;
public function getParsedBody()
return $this->parsedBody;
public function withParsedBody($data)
$new->parsedBody = $data;
public function getAttributes()
return $this->attributes;
public function getAttribute($attribute, $default = null)
if (false === array_key_exists($attribute, $this->attributes)) {
return $this->attributes[$attribute];
public function withAttribute($attribute, $value)
$new->attributes[$attribute] = $value;
public function withoutAttribute($attribute)
if (false === array_key_exists($attribute, $this->attributes)) {
unset($new->attributes[$attribute]);