3 * Zend Framework (http://framework.zend.com/)
5 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
10 namespace Zend\Diactoros;
12 use InvalidArgumentException;
13 use Psr\Http\Message\StreamInterface;
14 use Psr\Http\Message\UriInterface;
17 * Trait with common request behaviors.
19 * Server and client-side requests differ slightly in how the Host header is
20 * handled; on client-side, it should be calculated on-the-fly from the
21 * composed URI (if present), while on server-side, it will be calculated from
22 * the environment. As such, this trait exists to provide the common code
23 * between both client-side and server-side requests, and each can then
24 * use the headers functionality required by their implementations.
36 * The request-target, if it has been provided or calculated.
40 private $requestTarget;
48 * Initialize request state.
50 * Used by constructors.
52 * @param null|string|UriInterface $uri URI for the request, if any.
53 * @param null|string $method HTTP method for the request, if any.
54 * @param string|resource|StreamInterface $body Message body, if any.
55 * @param array $headers Headers for the message, if any.
56 * @throws InvalidArgumentException for any invalid value.
58 private function initialize($uri = null, $method = null, $body = 'php://memory', array $headers = [])
60 $this->validateMethod($method);
62 $this->method = $method ?: '';
63 $this->uri = $this->createUri($uri);
64 $this->stream = $this->getStream($body, 'wb+');
66 $this->setHeaders($headers);
68 // per PSR-7: attempt to set the Host header from a provided URI if no
69 // Host header is provided
70 if (! $this->hasHeader('Host') && $this->uri->getHost()) {
71 $this->headerNames['host'] = 'Host';
72 $this->headers['Host'] = [$this->getHostFromUri()];
77 * Create and return a URI instance.
79 * If `$uri` is a already a `UriInterface` instance, returns it.
81 * If `$uri` is a string, passes it to the `Uri` constructor to return an
84 * If `$uri is null, creates and returns an empty `Uri` instance.
86 * Otherwise, it raises an exception.
88 * @param null|string|UriInterface $uri
89 * @return UriInterface
90 * @throws InvalidArgumentException
92 private function createUri($uri)
94 if ($uri instanceof UriInterface) {
97 if (is_string($uri)) {
103 throw new InvalidArgumentException(
104 'Invalid URI provided; must be null, a string, or a Psr\Http\Message\UriInterface instance'
109 * Retrieves the message's request target.
111 * Retrieves the message's request-target either as it will appear (for
112 * clients), as it appeared at request (for servers), or as it was
113 * specified for the instance (see withRequestTarget()).
115 * In most cases, this will be the origin-form of the composed URI,
116 * unless a value was provided to the concrete implementation (see
117 * withRequestTarget() below).
119 * If no URI is available, and no request-target has been specifically
120 * provided, this method MUST return the string "/".
124 public function getRequestTarget()
126 if (null !== $this->requestTarget) {
127 return $this->requestTarget;
130 $target = $this->uri->getPath();
131 if ($this->uri->getQuery()) {
132 $target .= '?' . $this->uri->getQuery();
135 if (empty($target)) {
143 * Create a new instance with a specific request-target.
145 * If the request needs a non-origin-form request-target — e.g., for
146 * specifying an absolute-form, authority-form, or asterisk-form —
147 * this method may be used to create an instance with the specified
148 * request-target, verbatim.
150 * This method MUST be implemented in such a way as to retain the
151 * immutability of the message, and MUST return a new instance that has the
152 * changed request target.
154 * @link http://tools.ietf.org/html/rfc7230#section-2.7 (for the various
155 * request-target forms allowed in request messages)
156 * @param mixed $requestTarget
158 * @throws InvalidArgumentException if the request target is invalid.
160 public function withRequestTarget($requestTarget)
162 if (preg_match('#\s#', $requestTarget)) {
163 throw new InvalidArgumentException(
164 'Invalid request target provided; cannot contain whitespace'
169 $new->requestTarget = $requestTarget;
174 * Retrieves the HTTP method of the request.
176 * @return string Returns the request method.
178 public function getMethod()
180 return $this->method;
184 * Return an instance with the provided HTTP method.
186 * While HTTP method names are typically all uppercase characters, HTTP
187 * method names are case-sensitive and thus implementations SHOULD NOT
188 * modify the given string.
190 * This method MUST be implemented in such a way as to retain the
191 * immutability of the message, and MUST return an instance that has the
192 * changed request method.
194 * @param string $method Case-insensitive method.
196 * @throws InvalidArgumentException for invalid HTTP methods.
198 public function withMethod($method)
200 $this->validateMethod($method);
202 $new->method = $method;
207 * Retrieves the URI instance.
209 * This method MUST return a UriInterface instance.
211 * @link http://tools.ietf.org/html/rfc3986#section-4.3
212 * @return UriInterface Returns a UriInterface instance
213 * representing the URI of the request, if any.
215 public function getUri()
221 * Returns an instance with the provided URI.
223 * This method will update the Host header of the returned request by
224 * default if the URI contains a host component. If the URI does not
225 * contain a host component, any pre-existing Host header will be carried
226 * over to the returned request.
228 * You can opt-in to preserving the original state of the Host header by
229 * setting `$preserveHost` to `true`. When `$preserveHost` is set to
230 * `true`, the returned request will not update the Host header of the
231 * returned message -- even if the message contains no Host header. This
232 * means that a call to `getHeader('Host')` on the original request MUST
233 * equal the return value of a call to `getHeader('Host')` on the returned
236 * This method MUST be implemented in such a way as to retain the
237 * immutability of the message, and MUST return an instance that has the
238 * new UriInterface instance.
240 * @link http://tools.ietf.org/html/rfc3986#section-4.3
241 * @param UriInterface $uri New request URI to use.
242 * @param bool $preserveHost Preserve the original state of the Host header.
245 public function withUri(UriInterface $uri, $preserveHost = false)
250 if ($preserveHost && $this->hasHeader('Host')) {
254 if (! $uri->getHost()) {
258 $host = $uri->getHost();
259 if ($uri->getPort()) {
260 $host .= ':' . $uri->getPort();
263 $new->headerNames['host'] = 'Host';
265 // Remove an existing host header if present, regardless of current
266 // de-normalization of the header name.
267 // @see https://github.com/zendframework/zend-diactoros/issues/91
268 foreach (array_keys($new->headers) as $header) {
269 if (strtolower($header) === 'host') {
270 unset($new->headers[$header]);
274 $new->headers['Host'] = [$host];
280 * Validate the HTTP method
282 * @param null|string $method
283 * @throws InvalidArgumentException on invalid HTTP method.
285 private function validateMethod($method)
287 if (null === $method) {
291 if (! is_string($method)) {
292 throw new InvalidArgumentException(sprintf(
293 'Unsupported HTTP method; must be a string, received %s',
294 (is_object($method) ? get_class($method) : gettype($method))
298 if (! preg_match('/^[!#$%&\'*+.^_`\|~0-9a-z-]+$/i', $method)) {
299 throw new InvalidArgumentException(sprintf(
300 'Unsupported HTTP method "%s" provided',
307 * Retrieve the host from the URI instance
311 private function getHostFromUri()
313 $host = $this->uri->getHost();
314 $host .= $this->uri->getPort() ? ':' . $this->uri->getPort() : '';