Açıklama Yok

Request.php 3.6KB

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