annotate vendor/zendframework/zend-diactoros/src/RequestTrait.php @ 0:c75dbcec494b

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