MessageTrait.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. declare(strict_types=1);
  3. namespace GuzzleHttp\Psr7;
  4. use Psr\Http\Message\MessageInterface;
  5. use Psr\Http\Message\StreamInterface;
  6. /**
  7. * Trait implementing functionality common to requests and responses.
  8. */
  9. trait MessageTrait
  10. {
  11. /** @var array<string, string[]> Map of all registered headers, as original name => array of values */
  12. private $headers = [];
  13. /** @var array<string, string> Map of lowercase header name => original name at registration */
  14. private $headerNames = [];
  15. /** @var string */
  16. private $protocol = '1.1';
  17. /** @var StreamInterface|null */
  18. private $stream;
  19. public function getProtocolVersion(): string
  20. {
  21. return $this->protocol;
  22. }
  23. public function withProtocolVersion($version): MessageInterface
  24. {
  25. if ($this->protocol === $version) {
  26. return $this;
  27. }
  28. $new = clone $this;
  29. $new->protocol = $version;
  30. return $new;
  31. }
  32. public function getHeaders(): array
  33. {
  34. return $this->headers;
  35. }
  36. public function hasHeader($header): bool
  37. {
  38. return isset($this->headerNames[strtolower($header)]);
  39. }
  40. public function getHeader($header): array
  41. {
  42. $header = strtolower($header);
  43. if (!isset($this->headerNames[$header])) {
  44. return [];
  45. }
  46. $header = $this->headerNames[$header];
  47. return $this->headers[$header];
  48. }
  49. public function getHeaderLine($header): string
  50. {
  51. return implode(', ', $this->getHeader($header));
  52. }
  53. public function withHeader($header, $value): MessageInterface
  54. {
  55. $this->assertHeader($header);
  56. $value = $this->normalizeHeaderValue($value);
  57. $normalized = strtolower($header);
  58. $new = clone $this;
  59. if (isset($new->headerNames[$normalized])) {
  60. unset($new->headers[$new->headerNames[$normalized]]);
  61. }
  62. $new->headerNames[$normalized] = $header;
  63. $new->headers[$header] = $value;
  64. return $new;
  65. }
  66. public function withAddedHeader($header, $value): MessageInterface
  67. {
  68. $this->assertHeader($header);
  69. $value = $this->normalizeHeaderValue($value);
  70. $normalized = strtolower($header);
  71. $new = clone $this;
  72. if (isset($new->headerNames[$normalized])) {
  73. $header = $this->headerNames[$normalized];
  74. $new->headers[$header] = array_merge($this->headers[$header], $value);
  75. } else {
  76. $new->headerNames[$normalized] = $header;
  77. $new->headers[$header] = $value;
  78. }
  79. return $new;
  80. }
  81. public function withoutHeader($header): MessageInterface
  82. {
  83. $normalized = strtolower($header);
  84. if (!isset($this->headerNames[$normalized])) {
  85. return $this;
  86. }
  87. $header = $this->headerNames[$normalized];
  88. $new = clone $this;
  89. unset($new->headers[$header], $new->headerNames[$normalized]);
  90. return $new;
  91. }
  92. public function getBody(): StreamInterface
  93. {
  94. if (!$this->stream) {
  95. $this->stream = Utils::streamFor('');
  96. }
  97. return $this->stream;
  98. }
  99. public function withBody(StreamInterface $body): MessageInterface
  100. {
  101. if ($body === $this->stream) {
  102. return $this;
  103. }
  104. $new = clone $this;
  105. $new->stream = $body;
  106. return $new;
  107. }
  108. /**
  109. * @param array<string|int, string|string[]> $headers
  110. */
  111. private function setHeaders(array $headers): void
  112. {
  113. $this->headerNames = $this->headers = [];
  114. foreach ($headers as $header => $value) {
  115. if (is_int($header)) {
  116. // Numeric array keys are converted to int by PHP but having a header name '123' is not forbidden by the spec
  117. // and also allowed in withHeader(). So we need to cast it to string again for the following assertion to pass.
  118. $header = (string) $header;
  119. }
  120. $this->assertHeader($header);
  121. $value = $this->normalizeHeaderValue($value);
  122. $normalized = strtolower($header);
  123. if (isset($this->headerNames[$normalized])) {
  124. $header = $this->headerNames[$normalized];
  125. $this->headers[$header] = array_merge($this->headers[$header], $value);
  126. } else {
  127. $this->headerNames[$normalized] = $header;
  128. $this->headers[$header] = $value;
  129. }
  130. }
  131. }
  132. /**
  133. * @param mixed $value
  134. *
  135. * @return string[]
  136. */
  137. private function normalizeHeaderValue($value): array
  138. {
  139. if (!is_array($value)) {
  140. return $this->trimHeaderValues([$value]);
  141. }
  142. if (count($value) === 0) {
  143. throw new \InvalidArgumentException('Header value can not be an empty array.');
  144. }
  145. return $this->trimHeaderValues($value);
  146. }
  147. /**
  148. * Trims whitespace from the header values.
  149. *
  150. * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field.
  151. *
  152. * header-field = field-name ":" OWS field-value OWS
  153. * OWS = *( SP / HTAB )
  154. *
  155. * @param mixed[] $values Header values
  156. *
  157. * @return string[] Trimmed header values
  158. *
  159. * @see https://tools.ietf.org/html/rfc7230#section-3.2.4
  160. */
  161. private function trimHeaderValues(array $values): array
  162. {
  163. return array_map(function ($value) {
  164. if (!is_scalar($value) && null !== $value) {
  165. throw new \InvalidArgumentException(sprintf(
  166. 'Header value must be scalar or null but %s provided.',
  167. is_object($value) ? get_class($value) : gettype($value)
  168. ));
  169. }
  170. return trim((string) $value, " \t");
  171. }, array_values($values));
  172. }
  173. /**
  174. * @see https://tools.ietf.org/html/rfc7230#section-3.2
  175. *
  176. * @param mixed $header
  177. */
  178. private function assertHeader($header): void
  179. {
  180. if (!is_string($header)) {
  181. throw new \InvalidArgumentException(sprintf(
  182. 'Header name must be a string but %s provided.',
  183. is_object($header) ? get_class($header) : gettype($header)
  184. ));
  185. }
  186. if (! preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/', $header)) {
  187. throw new \InvalidArgumentException(
  188. sprintf(
  189. '"%s" is not valid header name',
  190. $header
  191. )
  192. );
  193. }
  194. }
  195. }