<?php namespace GuzzleHttp\Psr7; use InvalidArgumentException; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UriInterface; /** * PSR-7 request implementation. */ class Request implements RequestInterface { use MessageTrait; /** @var string */ private $method; /** @var null|string */ private $requestTarget; /** @var UriInterface */ private $uri; /** * @param string $method HTTP method * @param string|UriInterface $uri URI * @param array $headers Request headers * @param string|null|resource|StreamInterface $body Request body * @param string $version Protocol version */ public function __construct( $method, $uri, array $headers = [], $body = null, $version = '1.1' ) { if (!($uri instanceof UriInterface)) { $uri = new Uri($uri); } $this->method = strtoupper($method); $this->uri = $uri; $this->setHeaders($headers); $this->protocol = $version; if (!isset($this->headerNames['host'])) { $this->updateHostFromUri(); } if ($body !== '' && $body !== null) { $this->stream = stream_for($body); } } public function getRequestTarget() { if ($this->requestTarget !== null) { return $this->requestTarget; } $target = $this->uri->getPath(); if ($target == '') { $target = '/'; } if ($this->uri->getQuery() != '') { $target .= '?' . $this->uri->getQuery(); } return $target; } public function withRequestTarget($requestTarget) { if (preg_match('#\s#', $requestTarget)) { throw new InvalidArgumentException( 'Invalid request target provided; cannot contain whitespace' ); } $new = clone $this; $new->requestTarget = $requestTarget; return $new; } public function getMethod() { return $this->method; } public function withMethod($method) { $new = clone $this; $new->method = strtoupper($method); return $new; } public function getUri() { return $this->uri; } public function withUri(UriInterface $uri, $preserveHost = false) { if ($uri === $this->uri) { return $this; } $new = clone $this; $new->uri = $uri; if (!$preserveHost || !isset($this->headerNames['host'])) { $new->updateHostFromUri(); } return $new; } private function updateHostFromUri() { $host = $this->uri->getHost(); if ($host == '') { return; } if (($port = $this->uri->getPort()) !== null) { $host .= ':' . $port; } if (isset($this->headerNames['host'])) { $header = $this->headerNames['host']; } else { $header = 'Host'; $this->headerNames['host'] = 'Host'; } // Ensure Host is the first header. // See: http://tools.ietf.org/html/rfc7230#section-5.4 $this->headers = [$header => [$host]] + $this->headers; } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
AppendStream.php | File | 5.59 KB | 0644 |
|
BufferStream.php | File | 2.97 KB | 0644 |
|
CachingStream.php | File | 4.15 KB | 0644 |
|
DroppingStream.php | File | 1.05 KB | 0644 |
|
FnStream.php | File | 3.84 KB | 0644 |
|
InflateStream.php | File | 1.78 KB | 0644 |
|
LazyOpenStream.php | File | 880 B | 0644 |
|
LimitStream.php | File | 4.11 KB | 0644 |
|
MessageTrait.php | File | 4.47 KB | 0644 |
|
MultipartStream.php | File | 4.58 KB | 0644 |
|
NoSeekStream.php | File | 424 B | 0644 |
|
PumpStream.php | File | 3.94 KB | 0644 |
|
Request.php | File | 3.35 KB | 0644 |
|
Response.php | File | 4.13 KB | 0644 |
|
Rfc7230.php | File | 684 B | 0644 |
|
ServerRequest.php | File | 9.57 KB | 0644 |
|
Stream.php | File | 7.04 KB | 0644 |
|
StreamDecoratorTrait.php | File | 3.2 KB | 0644 |
|
StreamWrapper.php | File | 3.67 KB | 0644 |
|
UploadedFile.php | File | 7.37 KB | 0644 |
|
Uri.php | File | 20.3 KB | 0644 |
|
UriNormalizer.php | File | 8.12 KB | 0644 |
|
UriResolver.php | File | 8.57 KB | 0644 |
|
functions.php | File | 26.03 KB | 0644 |
|
functions_include.php | File | 156 B | 0644 |
|