annotate vendor/zendframework/zend-diactoros/src/RequestTrait.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children c2387f117808
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@12 3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
Chris@12 4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
Chris@0 5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
Chris@0 6 */
Chris@0 7
Chris@0 8 namespace Zend\Diactoros;
Chris@0 9
Chris@0 10 use InvalidArgumentException;
Chris@0 11 use Psr\Http\Message\StreamInterface;
Chris@0 12 use Psr\Http\Message\UriInterface;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Trait with common request behaviors.
Chris@0 16 *
Chris@0 17 * Server and client-side requests differ slightly in how the Host header is
Chris@0 18 * handled; on client-side, it should be calculated on-the-fly from the
Chris@0 19 * composed URI (if present), while on server-side, it will be calculated from
Chris@0 20 * the environment. As such, this trait exists to provide the common code
Chris@0 21 * between both client-side and server-side requests, and each can then
Chris@0 22 * use the headers functionality required by their implementations.
Chris@0 23 */
Chris@0 24 trait RequestTrait
Chris@0 25 {
Chris@0 26 use MessageTrait;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * @var string
Chris@0 30 */
Chris@0 31 private $method = '';
Chris@0 32
Chris@0 33 /**
Chris@0 34 * The request-target, if it has been provided or calculated.
Chris@0 35 *
Chris@0 36 * @var null|string
Chris@0 37 */
Chris@0 38 private $requestTarget;
Chris@0 39
Chris@0 40 /**
Chris@0 41 * @var UriInterface
Chris@0 42 */
Chris@0 43 private $uri;
Chris@0 44
Chris@0 45 /**
Chris@0 46 * Initialize request state.
Chris@0 47 *
Chris@0 48 * Used by constructors.
Chris@0 49 *
Chris@0 50 * @param null|string|UriInterface $uri URI for the request, if any.
Chris@0 51 * @param null|string $method HTTP method for the request, if any.
Chris@0 52 * @param string|resource|StreamInterface $body Message body, if any.
Chris@0 53 * @param array $headers Headers for the message, if any.
Chris@0 54 * @throws InvalidArgumentException for any invalid value.
Chris@0 55 */
Chris@0 56 private function initialize($uri = null, $method = null, $body = 'php://memory', array $headers = [])
Chris@0 57 {
Chris@0 58 $this->validateMethod($method);
Chris@0 59
Chris@0 60 $this->method = $method ?: '';
Chris@0 61 $this->uri = $this->createUri($uri);
Chris@0 62 $this->stream = $this->getStream($body, 'wb+');
Chris@0 63
Chris@0 64 $this->setHeaders($headers);
Chris@0 65
Chris@0 66 // per PSR-7: attempt to set the Host header from a provided URI if no
Chris@0 67 // Host header is provided
Chris@0 68 if (! $this->hasHeader('Host') && $this->uri->getHost()) {
Chris@0 69 $this->headerNames['host'] = 'Host';
Chris@0 70 $this->headers['Host'] = [$this->getHostFromUri()];
Chris@0 71 }
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * Create and return a URI instance.
Chris@0 76 *
Chris@0 77 * If `$uri` is a already a `UriInterface` instance, returns it.
Chris@0 78 *
Chris@0 79 * If `$uri` is a string, passes it to the `Uri` constructor to return an
Chris@0 80 * instance.
Chris@0 81 *
Chris@0 82 * If `$uri is null, creates and returns an empty `Uri` instance.
Chris@0 83 *
Chris@0 84 * Otherwise, it raises an exception.
Chris@0 85 *
Chris@0 86 * @param null|string|UriInterface $uri
Chris@0 87 * @return UriInterface
Chris@0 88 * @throws InvalidArgumentException
Chris@0 89 */
Chris@0 90 private function createUri($uri)
Chris@0 91 {
Chris@0 92 if ($uri instanceof UriInterface) {
Chris@0 93 return $uri;
Chris@0 94 }
Chris@0 95 if (is_string($uri)) {
Chris@0 96 return new Uri($uri);
Chris@0 97 }
Chris@0 98 if ($uri === null) {
Chris@0 99 return new Uri();
Chris@0 100 }
Chris@0 101 throw new InvalidArgumentException(
Chris@0 102 'Invalid URI provided; must be null, a string, or a Psr\Http\Message\UriInterface instance'
Chris@0 103 );
Chris@0 104 }
Chris@0 105
Chris@0 106 /**
Chris@0 107 * Retrieves the message's request target.
Chris@0 108 *
Chris@0 109 * Retrieves the message's request-target either as it will appear (for
Chris@0 110 * clients), as it appeared at request (for servers), or as it was
Chris@0 111 * specified for the instance (see withRequestTarget()).
Chris@0 112 *
Chris@0 113 * In most cases, this will be the origin-form of the composed URI,
Chris@0 114 * unless a value was provided to the concrete implementation (see
Chris@0 115 * withRequestTarget() below).
Chris@0 116 *
Chris@0 117 * If no URI is available, and no request-target has been specifically
Chris@0 118 * provided, this method MUST return the string "/".
Chris@0 119 *
Chris@0 120 * @return string
Chris@0 121 */
Chris@0 122 public function getRequestTarget()
Chris@0 123 {
Chris@0 124 if (null !== $this->requestTarget) {
Chris@0 125 return $this->requestTarget;
Chris@0 126 }
Chris@0 127
Chris@0 128 $target = $this->uri->getPath();
Chris@0 129 if ($this->uri->getQuery()) {
Chris@0 130 $target .= '?' . $this->uri->getQuery();
Chris@0 131 }
Chris@0 132
Chris@0 133 if (empty($target)) {
Chris@0 134 $target = '/';
Chris@0 135 }
Chris@0 136
Chris@0 137 return $target;
Chris@0 138 }
Chris@0 139
Chris@0 140 /**
Chris@0 141 * Create a new instance with a specific request-target.
Chris@0 142 *
Chris@0 143 * If the request needs a non-origin-form request-target — e.g., for
Chris@0 144 * specifying an absolute-form, authority-form, or asterisk-form —
Chris@0 145 * this method may be used to create an instance with the specified
Chris@0 146 * request-target, verbatim.
Chris@0 147 *
Chris@0 148 * This method MUST be implemented in such a way as to retain the
Chris@0 149 * immutability of the message, and MUST return a new instance that has the
Chris@0 150 * changed request target.
Chris@0 151 *
Chris@0 152 * @link http://tools.ietf.org/html/rfc7230#section-2.7 (for the various
Chris@0 153 * request-target forms allowed in request messages)
Chris@0 154 * @param mixed $requestTarget
Chris@0 155 * @return static
Chris@0 156 * @throws InvalidArgumentException if the request target is invalid.
Chris@0 157 */
Chris@0 158 public function withRequestTarget($requestTarget)
Chris@0 159 {
Chris@0 160 if (preg_match('#\s#', $requestTarget)) {
Chris@0 161 throw new InvalidArgumentException(
Chris@0 162 'Invalid request target provided; cannot contain whitespace'
Chris@0 163 );
Chris@0 164 }
Chris@0 165
Chris@0 166 $new = clone $this;
Chris@0 167 $new->requestTarget = $requestTarget;
Chris@0 168 return $new;
Chris@0 169 }
Chris@0 170
Chris@0 171 /**
Chris@0 172 * Retrieves the HTTP method of the request.
Chris@0 173 *
Chris@0 174 * @return string Returns the request method.
Chris@0 175 */
Chris@0 176 public function getMethod()
Chris@0 177 {
Chris@0 178 return $this->method;
Chris@0 179 }
Chris@0 180
Chris@0 181 /**
Chris@0 182 * Return an instance with the provided HTTP method.
Chris@0 183 *
Chris@0 184 * While HTTP method names are typically all uppercase characters, HTTP
Chris@0 185 * method names are case-sensitive and thus implementations SHOULD NOT
Chris@0 186 * modify the given string.
Chris@0 187 *
Chris@0 188 * This method MUST be implemented in such a way as to retain the
Chris@0 189 * immutability of the message, and MUST return an instance that has the
Chris@0 190 * changed request method.
Chris@0 191 *
Chris@0 192 * @param string $method Case-insensitive method.
Chris@0 193 * @return static
Chris@0 194 * @throws InvalidArgumentException for invalid HTTP methods.
Chris@0 195 */
Chris@0 196 public function withMethod($method)
Chris@0 197 {
Chris@0 198 $this->validateMethod($method);
Chris@0 199 $new = clone $this;
Chris@0 200 $new->method = $method;
Chris@0 201 return $new;
Chris@0 202 }
Chris@0 203
Chris@0 204 /**
Chris@0 205 * Retrieves the URI instance.
Chris@0 206 *
Chris@0 207 * This method MUST return a UriInterface instance.
Chris@0 208 *
Chris@0 209 * @link http://tools.ietf.org/html/rfc3986#section-4.3
Chris@0 210 * @return UriInterface Returns a UriInterface instance
Chris@0 211 * representing the URI of the request, if any.
Chris@0 212 */
Chris@0 213 public function getUri()
Chris@0 214 {
Chris@0 215 return $this->uri;
Chris@0 216 }
Chris@0 217
Chris@0 218 /**
Chris@0 219 * Returns an instance with the provided URI.
Chris@0 220 *
Chris@0 221 * This method will update the Host header of the returned request by
Chris@0 222 * default if the URI contains a host component. If the URI does not
Chris@0 223 * contain a host component, any pre-existing Host header will be carried
Chris@0 224 * over to the returned request.
Chris@0 225 *
Chris@0 226 * You can opt-in to preserving the original state of the Host header by
Chris@0 227 * setting `$preserveHost` to `true`. When `$preserveHost` is set to
Chris@0 228 * `true`, the returned request will not update the Host header of the
Chris@0 229 * returned message -- even if the message contains no Host header. This
Chris@0 230 * means that a call to `getHeader('Host')` on the original request MUST
Chris@0 231 * equal the return value of a call to `getHeader('Host')` on the returned
Chris@0 232 * request.
Chris@0 233 *
Chris@0 234 * This method MUST be implemented in such a way as to retain the
Chris@0 235 * immutability of the message, and MUST return an instance that has the
Chris@0 236 * new UriInterface instance.
Chris@0 237 *
Chris@0 238 * @link http://tools.ietf.org/html/rfc3986#section-4.3
Chris@0 239 * @param UriInterface $uri New request URI to use.
Chris@0 240 * @param bool $preserveHost Preserve the original state of the Host header.
Chris@0 241 * @return static
Chris@0 242 */
Chris@0 243 public function withUri(UriInterface $uri, $preserveHost = false)
Chris@0 244 {
Chris@0 245 $new = clone $this;
Chris@0 246 $new->uri = $uri;
Chris@0 247
Chris@0 248 if ($preserveHost && $this->hasHeader('Host')) {
Chris@0 249 return $new;
Chris@0 250 }
Chris@0 251
Chris@0 252 if (! $uri->getHost()) {
Chris@0 253 return $new;
Chris@0 254 }
Chris@0 255
Chris@0 256 $host = $uri->getHost();
Chris@0 257 if ($uri->getPort()) {
Chris@0 258 $host .= ':' . $uri->getPort();
Chris@0 259 }
Chris@0 260
Chris@0 261 $new->headerNames['host'] = 'Host';
Chris@0 262
Chris@0 263 // Remove an existing host header if present, regardless of current
Chris@0 264 // de-normalization of the header name.
Chris@0 265 // @see https://github.com/zendframework/zend-diactoros/issues/91
Chris@0 266 foreach (array_keys($new->headers) as $header) {
Chris@0 267 if (strtolower($header) === 'host') {
Chris@0 268 unset($new->headers[$header]);
Chris@0 269 }
Chris@0 270 }
Chris@0 271
Chris@0 272 $new->headers['Host'] = [$host];
Chris@0 273
Chris@0 274 return $new;
Chris@0 275 }
Chris@0 276
Chris@0 277 /**
Chris@0 278 * Validate the HTTP method
Chris@0 279 *
Chris@0 280 * @param null|string $method
Chris@0 281 * @throws InvalidArgumentException on invalid HTTP method.
Chris@0 282 */
Chris@0 283 private function validateMethod($method)
Chris@0 284 {
Chris@0 285 if (null === $method) {
Chris@0 286 return;
Chris@0 287 }
Chris@0 288
Chris@0 289 if (! is_string($method)) {
Chris@0 290 throw new InvalidArgumentException(sprintf(
Chris@0 291 'Unsupported HTTP method; must be a string, received %s',
Chris@0 292 (is_object($method) ? get_class($method) : gettype($method))
Chris@0 293 ));
Chris@0 294 }
Chris@0 295
Chris@0 296 if (! preg_match('/^[!#$%&\'*+.^_`\|~0-9a-z-]+$/i', $method)) {
Chris@0 297 throw new InvalidArgumentException(sprintf(
Chris@0 298 'Unsupported HTTP method "%s" provided',
Chris@0 299 $method
Chris@0 300 ));
Chris@0 301 }
Chris@0 302 }
Chris@0 303
Chris@0 304 /**
Chris@0 305 * Retrieve the host from the URI instance
Chris@0 306 *
Chris@0 307 * @return string
Chris@0 308 */
Chris@0 309 private function getHostFromUri()
Chris@0 310 {
Chris@0 311 $host = $this->uri->getHost();
Chris@0 312 $host .= $this->uri->getPort() ? ':' . $this->uri->getPort() : '';
Chris@0 313 return $host;
Chris@0 314 }
Chris@0 315 }