Request.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace GuzzleHttp\Psr7;
  3. use InvalidArgumentException;
  4. use Psr\Http\Message\RequestInterface;
  5. use Psr\Http\Message\StreamInterface;
  6. use Psr\Http\Message\UriInterface;
  7. /**
  8. * PSR-7 request implementation.
  9. */
  10. class Request implements RequestInterface
  11. {
  12. use MessageTrait;
  13. /** @var string */
  14. private $method;
  15. /** @var null|string */
  16. private $requestTarget;
  17. /** @var UriInterface */
  18. private $uri;
  19. /**
  20. * @param string $method HTTP method
  21. * @param string|UriInterface $uri URI
  22. * @param array $headers Request headers
  23. * @param string|null|resource|StreamInterface $body Request body
  24. * @param string $version Protocol version
  25. */
  26. public function __construct(
  27. $method,
  28. $uri,
  29. array $headers = [],
  30. $body = null,
  31. $version = '1.1'
  32. ) {
  33. $this->assertMethod($method);
  34. if (!($uri instanceof UriInterface)) {
  35. $uri = new Uri($uri);
  36. }
  37. $this->method = strtoupper($method);
  38. $this->uri = $uri;
  39. $this->setHeaders($headers);
  40. $this->protocol = $version;
  41. if (!isset($this->headerNames['host'])) {
  42. $this->updateHostFromUri();
  43. }
  44. if ($body !== '' && $body !== null) {
  45. $this->stream = stream_for($body);
  46. }
  47. }
  48. public function getRequestTarget()
  49. {
  50. if ($this->requestTarget !== null) {
  51. return $this->requestTarget;
  52. }
  53. $target = $this->uri->getPath();
  54. if ($target == '') {
  55. $target = '/';
  56. }
  57. if ($this->uri->getQuery() != '') {
  58. $target .= '?' . $this->uri->getQuery();
  59. }
  60. return $target;
  61. }
  62. public function withRequestTarget($requestTarget)
  63. {
  64. if (preg_match('#\s#', $requestTarget)) {
  65. throw new InvalidArgumentException(
  66. 'Invalid request target provided; cannot contain whitespace'
  67. );
  68. }
  69. $new = clone $this;
  70. $new->requestTarget = $requestTarget;
  71. return $new;
  72. }
  73. public function getMethod()
  74. {
  75. return $this->method;
  76. }
  77. public function withMethod($method)
  78. {
  79. $this->assertMethod($method);
  80. $new = clone $this;
  81. $new->method = strtoupper($method);
  82. return $new;
  83. }
  84. public function getUri()
  85. {
  86. return $this->uri;
  87. }
  88. public function withUri(UriInterface $uri, $preserveHost = false)
  89. {
  90. if ($uri === $this->uri) {
  91. return $this;
  92. }
  93. $new = clone $this;
  94. $new->uri = $uri;
  95. if (!$preserveHost || !isset($this->headerNames['host'])) {
  96. $new->updateHostFromUri();
  97. }
  98. return $new;
  99. }
  100. private function updateHostFromUri()
  101. {
  102. $host = $this->uri->getHost();
  103. if ($host == '') {
  104. return;
  105. }
  106. if (($port = $this->uri->getPort()) !== null) {
  107. $host .= ':' . $port;
  108. }
  109. if (isset($this->headerNames['host'])) {
  110. $header = $this->headerNames['host'];
  111. } else {
  112. $header = 'Host';
  113. $this->headerNames['host'] = 'Host';
  114. }
  115. // Ensure Host is the first header.
  116. // See: http://tools.ietf.org/html/rfc7230#section-5.4
  117. $this->headers = [$header => [$host]] + $this->headers;
  118. }
  119. private function assertMethod($method)
  120. {
  121. if (!is_string($method) || $method === '') {
  122. throw new \InvalidArgumentException('Method must be a non-empty string.');
  123. }
  124. }
  125. }