MessageTrait.php 5.8 KB

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