Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace GuzzleHttp\Psr7;
   6  
   7  use InvalidArgumentException;
   8  use Psr\Http\Message\RequestInterface;
   9  use Psr\Http\Message\StreamInterface;
  10  use Psr\Http\Message\UriInterface;
  11  
  12  /**
  13   * PSR-7 request implementation.
  14   */
  15  class Request implements RequestInterface
  16  {
  17      use MessageTrait;
  18  
  19      /** @var string */
  20      private $method;
  21  
  22      /** @var string|null */
  23      private $requestTarget;
  24  
  25      /** @var UriInterface */
  26      private $uri;
  27  
  28      /**
  29       * @param string                               $method  HTTP method
  30       * @param string|UriInterface                  $uri     URI
  31       * @param array<string, string|string[]>       $headers Request headers
  32       * @param string|resource|StreamInterface|null $body    Request body
  33       * @param string                               $version Protocol version
  34       */
  35      public function __construct(
  36          string $method,
  37          $uri,
  38          array $headers = [],
  39          $body = null,
  40          string $version = '1.1'
  41      ) {
  42          $this->assertMethod($method);
  43          if (!($uri instanceof UriInterface)) {
  44              $uri = new Uri($uri);
  45          }
  46  
  47          $this->method = strtoupper($method);
  48          $this->uri = $uri;
  49          $this->setHeaders($headers);
  50          $this->protocol = $version;
  51  
  52          if (!isset($this->headerNames['host'])) {
  53              $this->updateHostFromUri();
  54          }
  55  
  56          if ($body !== '' && $body !== null) {
  57              $this->stream = Utils::streamFor($body);
  58          }
  59      }
  60  
  61      public function getRequestTarget(): string
  62      {
  63          if ($this->requestTarget !== null) {
  64              return $this->requestTarget;
  65          }
  66  
  67          $target = $this->uri->getPath();
  68          if ($target === '') {
  69              $target = '/';
  70          }
  71          if ($this->uri->getQuery() != '') {
  72              $target .= '?' . $this->uri->getQuery();
  73          }
  74  
  75          return $target;
  76      }
  77  
  78      public function withRequestTarget($requestTarget): RequestInterface
  79      {
  80          if (preg_match('#\s#', $requestTarget)) {
  81              throw new InvalidArgumentException(
  82                  'Invalid request target provided; cannot contain whitespace'
  83              );
  84          }
  85  
  86          $new = clone $this;
  87          $new->requestTarget = $requestTarget;
  88          return $new;
  89      }
  90  
  91      public function getMethod(): string
  92      {
  93          return $this->method;
  94      }
  95  
  96      public function withMethod($method): RequestInterface
  97      {
  98          $this->assertMethod($method);
  99          $new = clone $this;
 100          $new->method = strtoupper($method);
 101          return $new;
 102      }
 103  
 104      public function getUri(): UriInterface
 105      {
 106          return $this->uri;
 107      }
 108  
 109      public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
 110      {
 111          if ($uri === $this->uri) {
 112              return $this;
 113          }
 114  
 115          $new = clone $this;
 116          $new->uri = $uri;
 117  
 118          if (!$preserveHost || !isset($this->headerNames['host'])) {
 119              $new->updateHostFromUri();
 120          }
 121  
 122          return $new;
 123      }
 124  
 125      private function updateHostFromUri(): void
 126      {
 127          $host = $this->uri->getHost();
 128  
 129          if ($host == '') {
 130              return;
 131          }
 132  
 133          if (($port = $this->uri->getPort()) !== null) {
 134              $host .= ':' . $port;
 135          }
 136  
 137          if (isset($this->headerNames['host'])) {
 138              $header = $this->headerNames['host'];
 139          } else {
 140              $header = 'Host';
 141              $this->headerNames['host'] = 'Host';
 142          }
 143          // Ensure Host is the first header.
 144          // See: http://tools.ietf.org/html/rfc7230#section-5.4
 145          $this->headers = [$header => [$host]] + $this->headers;
 146      }
 147  
 148      /**
 149       * @param mixed $method
 150       */
 151      private function assertMethod($method): void
 152      {
 153          if (!is_string($method) || $method === '') {
 154              throw new InvalidArgumentException('Method must be a non-empty string.');
 155          }
 156      }
 157  }