Request.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. if (!($uri instanceof UriInterface)) {
  34. $uri = new Uri($uri);
  35. }
  36. $this->method = strtoupper($method);
  37. $this->uri = $uri;
  38. $this->setHeaders($headers);
  39. $this->protocol = $version;
  40. if (!$this->hasHeader('Host')) {
  41. $this->updateHostFromUri();
  42. }
  43. if ($body !== '' && $body !== null) {
  44. $this->stream = stream_for($body);
  45. }
  46. }
  47. public function getRequestTarget()
  48. {
  49. if ($this->requestTarget !== null) {
  50. return $this->requestTarget;
  51. }
  52. $target = $this->uri->getPath();
  53. if ($target == '') {
  54. $target = '/';
  55. }
  56. if ($this->uri->getQuery() != '') {
  57. $target .= '?' . $this->uri->getQuery();
  58. }
  59. return $target;
  60. }
  61. public function withRequestTarget($requestTarget)
  62. {
  63. if (preg_match('#\s#', $requestTarget)) {
  64. throw new InvalidArgumentException(
  65. 'Invalid request target provided; cannot contain whitespace'
  66. );
  67. }
  68. $new = clone $this;
  69. $new->requestTarget = $requestTarget;
  70. return $new;
  71. }
  72. public function getMethod()
  73. {
  74. return $this->method;
  75. }
  76. public function withMethod($method)
  77. {
  78. $new = clone $this;
  79. $new->method = strtoupper($method);
  80. return $new;
  81. }
  82. public function getUri()
  83. {
  84. return $this->uri;
  85. }
  86. public function withUri(UriInterface $uri, $preserveHost = false)
  87. {
  88. if ($uri === $this->uri) {
  89. return $this;
  90. }
  91. $new = clone $this;
  92. $new->uri = $uri;
  93. if (!$preserveHost) {
  94. $new->updateHostFromUri();
  95. }
  96. return $new;
  97. }
  98. private function updateHostFromUri()
  99. {
  100. $host = $this->uri->getHost();
  101. if ($host == '') {
  102. return;
  103. }
  104. if (($port = $this->uri->getPort()) !== null) {
  105. $host .= ':' . $port;
  106. }
  107. if (isset($this->headerNames['host'])) {
  108. $header = $this->headerNames['host'];
  109. } else {
  110. $header = 'Host';
  111. $this->headerNames['host'] = 'Host';
  112. }
  113. // Ensure Host is the first header.
  114. // See: http://tools.ietf.org/html/rfc7230#section-5.4
  115. $this->headers = [$header => [$host]] + $this->headers;
  116. }
  117. }