annotate vendor/zendframework/zend-diactoros/src/RequestTrait.php @ 19:fa3358dc1485 tip

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