| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,261 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+declare(strict_types=1); |
|
| 4 |
+ |
|
| 5 |
+namespace GuzzleHttp\Psr7; |
|
| 6 |
+ |
|
| 7 |
+use Psr\Http\Message\MessageInterface; |
|
| 8 |
+use Psr\Http\Message\StreamInterface; |
|
| 9 |
+ |
|
| 10 |
+/** |
|
| 11 |
+ * Trait implementing functionality common to requests and responses. |
|
| 12 |
+ */ |
|
| 13 |
+trait MessageTrait |
|
| 14 |
+{
|
|
| 15 |
+ /** @var string[][] Map of all registered headers, as original name => array of values */ |
|
| 16 |
+ private $headers = []; |
|
| 17 |
+ |
|
| 18 |
+ /** @var string[] Map of lowercase header name => original name at registration */ |
|
| 19 |
+ private $headerNames = []; |
|
| 20 |
+ |
|
| 21 |
+ /** @var string */ |
|
| 22 |
+ private $protocol = '1.1'; |
|
| 23 |
+ |
|
| 24 |
+ /** @var StreamInterface|null */ |
|
| 25 |
+ private $stream; |
|
| 26 |
+ |
|
| 27 |
+ public function getProtocolVersion(): string |
|
| 28 |
+ {
|
|
| 29 |
+ return $this->protocol; |
|
| 30 |
+ } |
|
| 31 |
+ |
|
| 32 |
+ public function withProtocolVersion($version): MessageInterface |
|
| 33 |
+ {
|
|
| 34 |
+ if ($this->protocol === $version) {
|
|
| 35 |
+ return $this; |
|
| 36 |
+ } |
|
| 37 |
+ |
|
| 38 |
+ $new = clone $this; |
|
| 39 |
+ $new->protocol = $version; |
|
| 40 |
+ |
|
| 41 |
+ return $new; |
|
| 42 |
+ } |
|
| 43 |
+ |
|
| 44 |
+ public function getHeaders(): array |
|
| 45 |
+ {
|
|
| 46 |
+ return $this->headers; |
|
| 47 |
+ } |
|
| 48 |
+ |
|
| 49 |
+ public function hasHeader($header): bool |
|
| 50 |
+ {
|
|
| 51 |
+ return isset($this->headerNames[strtolower($header)]); |
|
| 52 |
+ } |
|
| 53 |
+ |
|
| 54 |
+ public function getHeader($header): array |
|
| 55 |
+ {
|
|
| 56 |
+ $header = strtolower($header); |
|
| 57 |
+ |
|
| 58 |
+ if (!isset($this->headerNames[$header])) {
|
|
| 59 |
+ return []; |
|
| 60 |
+ } |
|
| 61 |
+ |
|
| 62 |
+ $header = $this->headerNames[$header]; |
|
| 63 |
+ |
|
| 64 |
+ return $this->headers[$header]; |
|
| 65 |
+ } |
|
| 66 |
+ |
|
| 67 |
+ public function getHeaderLine($header): string |
|
| 68 |
+ {
|
|
| 69 |
+ return implode(', ', $this->getHeader($header));
|
|
| 70 |
+ } |
|
| 71 |
+ |
|
| 72 |
+ public function withHeader($header, $value): MessageInterface |
|
| 73 |
+ {
|
|
| 74 |
+ $this->assertHeader($header); |
|
| 75 |
+ $value = $this->normalizeHeaderValue($value); |
|
| 76 |
+ $normalized = strtolower($header); |
|
| 77 |
+ |
|
| 78 |
+ $new = clone $this; |
|
| 79 |
+ if (isset($new->headerNames[$normalized])) {
|
|
| 80 |
+ unset($new->headers[$new->headerNames[$normalized]]); |
|
| 81 |
+ } |
|
| 82 |
+ $new->headerNames[$normalized] = $header; |
|
| 83 |
+ $new->headers[$header] = $value; |
|
| 84 |
+ |
|
| 85 |
+ return $new; |
|
| 86 |
+ } |
|
| 87 |
+ |
|
| 88 |
+ public function withAddedHeader($header, $value): MessageInterface |
|
| 89 |
+ {
|
|
| 90 |
+ $this->assertHeader($header); |
|
| 91 |
+ $value = $this->normalizeHeaderValue($value); |
|
| 92 |
+ $normalized = strtolower($header); |
|
| 93 |
+ |
|
| 94 |
+ $new = clone $this; |
|
| 95 |
+ if (isset($new->headerNames[$normalized])) {
|
|
| 96 |
+ $header = $this->headerNames[$normalized]; |
|
| 97 |
+ $new->headers[$header] = array_merge($this->headers[$header], $value); |
|
| 98 |
+ } else {
|
|
| 99 |
+ $new->headerNames[$normalized] = $header; |
|
| 100 |
+ $new->headers[$header] = $value; |
|
| 101 |
+ } |
|
| 102 |
+ |
|
| 103 |
+ return $new; |
|
| 104 |
+ } |
|
| 105 |
+ |
|
| 106 |
+ public function withoutHeader($header): MessageInterface |
|
| 107 |
+ {
|
|
| 108 |
+ $normalized = strtolower($header); |
|
| 109 |
+ |
|
| 110 |
+ if (!isset($this->headerNames[$normalized])) {
|
|
| 111 |
+ return $this; |
|
| 112 |
+ } |
|
| 113 |
+ |
|
| 114 |
+ $header = $this->headerNames[$normalized]; |
|
| 115 |
+ |
|
| 116 |
+ $new = clone $this; |
|
| 117 |
+ unset($new->headers[$header], $new->headerNames[$normalized]); |
|
| 118 |
+ |
|
| 119 |
+ return $new; |
|
| 120 |
+ } |
|
| 121 |
+ |
|
| 122 |
+ public function getBody(): StreamInterface |
|
| 123 |
+ {
|
|
| 124 |
+ if (!$this->stream) {
|
|
| 125 |
+ $this->stream = Utils::streamFor('');
|
|
| 126 |
+ } |
|
| 127 |
+ |
|
| 128 |
+ return $this->stream; |
|
| 129 |
+ } |
|
| 130 |
+ |
|
| 131 |
+ public function withBody(StreamInterface $body): MessageInterface |
|
| 132 |
+ {
|
|
| 133 |
+ if ($body === $this->stream) {
|
|
| 134 |
+ return $this; |
|
| 135 |
+ } |
|
| 136 |
+ |
|
| 137 |
+ $new = clone $this; |
|
| 138 |
+ $new->stream = $body; |
|
| 139 |
+ |
|
| 140 |
+ return $new; |
|
| 141 |
+ } |
|
| 142 |
+ |
|
| 143 |
+ /** |
|
| 144 |
+ * @param (string|string[])[] $headers |
|
| 145 |
+ */ |
|
| 146 |
+ private function setHeaders(array $headers): void |
|
| 147 |
+ {
|
|
| 148 |
+ $this->headerNames = $this->headers = []; |
|
| 149 |
+ foreach ($headers as $header => $value) {
|
|
| 150 |
+ // Numeric array keys are converted to int by PHP. |
|
| 151 |
+ $header = (string) $header; |
|
| 152 |
+ |
|
| 153 |
+ $this->assertHeader($header); |
|
| 154 |
+ $value = $this->normalizeHeaderValue($value); |
|
| 155 |
+ $normalized = strtolower($header); |
|
| 156 |
+ if (isset($this->headerNames[$normalized])) {
|
|
| 157 |
+ $header = $this->headerNames[$normalized]; |
|
| 158 |
+ $this->headers[$header] = array_merge($this->headers[$header], $value); |
|
| 159 |
+ } else {
|
|
| 160 |
+ $this->headerNames[$normalized] = $header; |
|
| 161 |
+ $this->headers[$header] = $value; |
|
| 162 |
+ } |
|
| 163 |
+ } |
|
| 164 |
+ } |
|
| 165 |
+ |
|
| 166 |
+ /** |
|
| 167 |
+ * @param mixed $value |
|
| 168 |
+ * |
|
| 169 |
+ * @return string[] |
|
| 170 |
+ */ |
|
| 171 |
+ private function normalizeHeaderValue($value): array |
|
| 172 |
+ {
|
|
| 173 |
+ if (!is_array($value)) {
|
|
| 174 |
+ return $this->trimAndValidateHeaderValues([$value]); |
|
| 175 |
+ } |
|
| 176 |
+ |
|
| 177 |
+ return $this->trimAndValidateHeaderValues($value); |
|
| 178 |
+ } |
|
| 179 |
+ |
|
| 180 |
+ /** |
|
| 181 |
+ * Trims whitespace from the header values. |
|
| 182 |
+ * |
|
| 183 |
+ * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field. |
|
| 184 |
+ * |
|
| 185 |
+ * header-field = field-name ":" OWS field-value OWS |
|
| 186 |
+ * OWS = *( SP / HTAB ) |
|
| 187 |
+ * |
|
| 188 |
+ * @param mixed[] $values Header values |
|
| 189 |
+ * |
|
| 190 |
+ * @return string[] Trimmed header values |
|
| 191 |
+ * |
|
| 192 |
+ * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4 |
|
| 193 |
+ */ |
|
| 194 |
+ private function trimAndValidateHeaderValues(array $values): array |
|
| 195 |
+ {
|
|
| 196 |
+ return array_map(function ($value) {
|
|
| 197 |
+ if (!is_scalar($value) && null !== $value) {
|
|
| 198 |
+ throw new \InvalidArgumentException(sprintf( |
|
| 199 |
+ 'Header value must be scalar or null but %s provided.', |
|
| 200 |
+ is_object($value) ? get_class($value) : gettype($value) |
|
| 201 |
+ )); |
|
| 202 |
+ } |
|
| 203 |
+ |
|
| 204 |
+ $trimmed = trim((string) $value, " \t"); |
|
| 205 |
+ $this->assertValue($trimmed); |
|
| 206 |
+ |
|
| 207 |
+ return $trimmed; |
|
| 208 |
+ }, array_values($values)); |
|
| 209 |
+ } |
|
| 210 |
+ |
|
| 211 |
+ /** |
|
| 212 |
+ * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2 |
|
| 213 |
+ * |
|
| 214 |
+ * @param mixed $header |
|
| 215 |
+ */ |
|
| 216 |
+ private function assertHeader($header): void |
|
| 217 |
+ {
|
|
| 218 |
+ if (!is_string($header)) {
|
|
| 219 |
+ throw new \InvalidArgumentException(sprintf( |
|
| 220 |
+ 'Header name must be a string but %s provided.', |
|
| 221 |
+ is_object($header) ? get_class($header) : gettype($header) |
|
| 222 |
+ )); |
|
| 223 |
+ } |
|
| 224 |
+ |
|
| 225 |
+ if (!preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $header)) {
|
|
| 226 |
+ throw new \InvalidArgumentException( |
|
| 227 |
+ sprintf('"%s" is not valid header name.', $header)
|
|
| 228 |
+ ); |
|
| 229 |
+ } |
|
| 230 |
+ } |
|
| 231 |
+ |
|
| 232 |
+ /** |
|
| 233 |
+ * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2 |
|
| 234 |
+ * |
|
| 235 |
+ * field-value = *( field-content / obs-fold ) |
|
| 236 |
+ * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
|
| 237 |
+ * field-vchar = VCHAR / obs-text |
|
| 238 |
+ * VCHAR = %x21-7E |
|
| 239 |
+ * obs-text = %x80-FF |
|
| 240 |
+ * obs-fold = CRLF 1*( SP / HTAB ) |
|
| 241 |
+ */ |
|
| 242 |
+ private function assertValue(string $value): void |
|
| 243 |
+ {
|
|
| 244 |
+ // The regular expression intentionally does not support the obs-fold production, because as |
|
| 245 |
+ // per RFC 7230#3.2.4: |
|
| 246 |
+ // |
|
| 247 |
+ // A sender MUST NOT generate a message that includes |
|
| 248 |
+ // line folding (i.e., that has any field-value that contains a match to |
|
| 249 |
+ // the obs-fold rule) unless the message is intended for packaging |
|
| 250 |
+ // within the message/http media type. |
|
| 251 |
+ // |
|
| 252 |
+ // Clients must not send a request with line folding and a server sending folded headers is |
|
| 253 |
+ // likely very rare. Line folding is a fairly obscure feature of HTTP/1.1 and thus not accepting |
|
| 254 |
+ // folding is not likely to break any legitimate use case. |
|
| 255 |
+ if (!preg_match('/^[\x20\x09\x21-\x7E\x80-\xFF]*$/D', $value)) {
|
|
| 256 |
+ throw new \InvalidArgumentException( |
|
| 257 |
+ sprintf('"%s" is not valid header value.', $value)
|
|
| 258 |
+ ); |
|
| 259 |
+ } |
|
| 260 |
+ } |
|
| 261 |
+} |
| 1 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,214 +0,0 @@ |
| 1 |
-<?php |
|
| 2 |
- |
|
| 3 |
-namespace GuzzleHttp\Psr7; |
|
| 4 |
- |
|
| 5 |
-use Psr\Http\Message\StreamInterface; |
|
| 6 |
- |
|
| 7 |
-/** |
|
| 8 |
- * Trait implementing functionality common to requests and responses. |
|
| 9 |
- */ |
|
| 10 |
-trait MessageTrait |
|
| 11 |
-{
|
|
| 12 |
- /** @var array Map of all registered headers, as original name => array of values */ |
|
| 13 |
- private $headers = []; |
|
| 14 |
- |
|
| 15 |
- /** @var array Map of lowercase header name => original name at registration */ |
|
| 16 |
- private $headerNames = []; |
|
| 17 |
- |
|
| 18 |
- /** @var string */ |
|
| 19 |
- private $protocol = '1.1'; |
|
| 20 |
- |
|
| 21 |
- /** @var StreamInterface|null */ |
|
| 22 |
- private $stream; |
|
| 23 |
- |
|
| 24 |
- public function getProtocolVersion() |
|
| 25 |
- {
|
|
| 26 |
- return $this->protocol; |
|
| 27 |
- } |
|
| 28 |
- |
|
| 29 |
- public function withProtocolVersion($version) |
|
| 30 |
- {
|
|
| 31 |
- if ($this->protocol === $version) {
|
|
| 32 |
- return $this; |
|
| 33 |
- } |
|
| 34 |
- |
|
| 35 |
- $new = clone $this; |
|
| 36 |
- $new->protocol = $version; |
|
| 37 |
- return $new; |
|
| 38 |
- } |
|
| 39 |
- |
|
| 40 |
- public function getHeaders() |
|
| 41 |
- {
|
|
| 42 |
- return $this->headers; |
|
| 43 |
- } |
|
| 44 |
- |
|
| 45 |
- public function hasHeader($header) |
|
| 46 |
- {
|
|
| 47 |
- return isset($this->headerNames[strtolower($header)]); |
|
| 48 |
- } |
|
| 49 |
- |
|
| 50 |
- public function getHeader($header) |
|
| 51 |
- {
|
|
| 52 |
- $header = strtolower($header); |
|
| 53 |
- |
|
| 54 |
- if (!isset($this->headerNames[$header])) {
|
|
| 55 |
- return []; |
|
| 56 |
- } |
|
| 57 |
- |
|
| 58 |
- $header = $this->headerNames[$header]; |
|
| 59 |
- |
|
| 60 |
- return $this->headers[$header]; |
|
| 61 |
- } |
|
| 62 |
- |
|
| 63 |
- public function getHeaderLine($header) |
|
| 64 |
- {
|
|
| 65 |
- return implode(', ', $this->getHeader($header));
|
|
| 66 |
- } |
|
| 67 |
- |
|
| 68 |
- public function withHeader($header, $value) |
|
| 69 |
- {
|
|
| 70 |
- $this->assertHeader($header); |
|
| 71 |
- $value = $this->normalizeHeaderValue($value); |
|
| 72 |
- $normalized = strtolower($header); |
|
| 73 |
- |
|
| 74 |
- $new = clone $this; |
|
| 75 |
- if (isset($new->headerNames[$normalized])) {
|
|
| 76 |
- unset($new->headers[$new->headerNames[$normalized]]); |
|
| 77 |
- } |
|
| 78 |
- $new->headerNames[$normalized] = $header; |
|
| 79 |
- $new->headers[$header] = $value; |
|
| 80 |
- |
|
| 81 |
- return $new; |
|
| 82 |
- } |
|
| 83 |
- |
|
| 84 |
- public function withAddedHeader($header, $value) |
|
| 85 |
- {
|
|
| 86 |
- $this->assertHeader($header); |
|
| 87 |
- $value = $this->normalizeHeaderValue($value); |
|
| 88 |
- $normalized = strtolower($header); |
|
| 89 |
- |
|
| 90 |
- $new = clone $this; |
|
| 91 |
- if (isset($new->headerNames[$normalized])) {
|
|
| 92 |
- $header = $this->headerNames[$normalized]; |
|
| 93 |
- $new->headers[$header] = array_merge($this->headers[$header], $value); |
|
| 94 |
- } else {
|
|
| 95 |
- $new->headerNames[$normalized] = $header; |
|
| 96 |
- $new->headers[$header] = $value; |
|
| 97 |
- } |
|
| 98 |
- |
|
| 99 |
- return $new; |
|
| 100 |
- } |
|
| 101 |
- |
|
| 102 |
- public function withoutHeader($header) |
|
| 103 |
- {
|
|
| 104 |
- $normalized = strtolower($header); |
|
| 105 |
- |
|
| 106 |
- if (!isset($this->headerNames[$normalized])) {
|
|
| 107 |
- return $this; |
|
| 108 |
- } |
|
| 109 |
- |
|
| 110 |
- $header = $this->headerNames[$normalized]; |
|
| 111 |
- |
|
| 112 |
- $new = clone $this; |
|
| 113 |
- unset($new->headers[$header], $new->headerNames[$normalized]); |
|
| 114 |
- |
|
| 115 |
- return $new; |
|
| 116 |
- } |
|
| 117 |
- |
|
| 118 |
- public function getBody() |
|
| 119 |
- {
|
|
| 120 |
- if (!$this->stream) {
|
|
| 121 |
- $this->stream = Utils::streamFor('');
|
|
| 122 |
- } |
|
| 123 |
- |
|
| 124 |
- return $this->stream; |
|
| 125 |
- } |
|
| 126 |
- |
|
| 127 |
- public function withBody(StreamInterface $body) |
|
| 128 |
- {
|
|
| 129 |
- if ($body === $this->stream) {
|
|
| 130 |
- return $this; |
|
| 131 |
- } |
|
| 132 |
- |
|
| 133 |
- $new = clone $this; |
|
| 134 |
- $new->stream = $body; |
|
| 135 |
- return $new; |
|
| 136 |
- } |
|
| 137 |
- |
|
| 138 |
- private function setHeaders(array $headers) |
|
| 139 |
- {
|
|
| 140 |
- $this->headerNames = $this->headers = []; |
|
| 141 |
- foreach ($headers as $header => $value) {
|
|
| 142 |
- if (is_int($header)) {
|
|
| 143 |
- // Numeric array keys are converted to int by PHP but having a header name '123' is not forbidden by the spec |
|
| 144 |
- // and also allowed in withHeader(). So we need to cast it to string again for the following assertion to pass. |
|
| 145 |
- $header = (string) $header; |
|
| 146 |
- } |
|
| 147 |
- $this->assertHeader($header); |
|
| 148 |
- $value = $this->normalizeHeaderValue($value); |
|
| 149 |
- $normalized = strtolower($header); |
|
| 150 |
- if (isset($this->headerNames[$normalized])) {
|
|
| 151 |
- $header = $this->headerNames[$normalized]; |
|
| 152 |
- $this->headers[$header] = array_merge($this->headers[$header], $value); |
|
| 153 |
- } else {
|
|
| 154 |
- $this->headerNames[$normalized] = $header; |
|
| 155 |
- $this->headers[$header] = $value; |
|
| 156 |
- } |
|
| 157 |
- } |
|
| 158 |
- } |
|
| 159 |
- |
|
| 160 |
- private function normalizeHeaderValue($value) |
|
| 161 |
- {
|
|
| 162 |
- if (!is_array($value)) {
|
|
| 163 |
- return $this->trimHeaderValues([$value]); |
|
| 164 |
- } |
|
| 165 |
- |
|
| 166 |
- if (count($value) === 0) {
|
|
| 167 |
- throw new \InvalidArgumentException('Header value can not be an empty array.');
|
|
| 168 |
- } |
|
| 169 |
- |
|
| 170 |
- return $this->trimHeaderValues($value); |
|
| 171 |
- } |
|
| 172 |
- |
|
| 173 |
- /** |
|
| 174 |
- * Trims whitespace from the header values. |
|
| 175 |
- * |
|
| 176 |
- * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field. |
|
| 177 |
- * |
|
| 178 |
- * header-field = field-name ":" OWS field-value OWS |
|
| 179 |
- * OWS = *( SP / HTAB ) |
|
| 180 |
- * |
|
| 181 |
- * @param string[] $values Header values |
|
| 182 |
- * |
|
| 183 |
- * @return string[] Trimmed header values |
|
| 184 |
- * |
|
| 185 |
- * @see https://tools.ietf.org/html/rfc7230#section-3.2.4 |
|
| 186 |
- */ |
|
| 187 |
- private function trimHeaderValues(array $values) |
|
| 188 |
- {
|
|
| 189 |
- return array_map(function ($value) {
|
|
| 190 |
- if (!is_scalar($value) && null !== $value) {
|
|
| 191 |
- throw new \InvalidArgumentException(sprintf( |
|
| 192 |
- 'Header value must be scalar or null but %s provided.', |
|
| 193 |
- is_object($value) ? get_class($value) : gettype($value) |
|
| 194 |
- )); |
|
| 195 |
- } |
|
| 196 |
- |
|
| 197 |
- return trim((string) $value, " \t"); |
|
| 198 |
- }, array_values($values)); |
|
| 199 |
- } |
|
| 200 |
- |
|
| 201 |
- private function assertHeader($header) |
|
| 202 |
- {
|
|
| 203 |
- if (!is_string($header)) {
|
|
| 204 |
- throw new \InvalidArgumentException(sprintf( |
|
| 205 |
- 'Header name must be a string but %s provided.', |
|
| 206 |
- is_object($header) ? get_class($header) : gettype($header) |
|
| 207 |
- )); |
|
| 208 |
- } |
|
| 209 |
- |
|
| 210 |
- if ($header === '') {
|
|
| 211 |
- throw new \InvalidArgumentException('Header name can not be empty.');
|
|
| 212 |
- } |
|
| 213 |
- } |
|
| 214 |
-} |
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,214 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+namespace GuzzleHttp\Psr7; |
|
| 4 |
+ |
|
| 5 |
+use Psr\Http\Message\StreamInterface; |
|
| 6 |
+ |
|
| 7 |
+/** |
|
| 8 |
+ * Trait implementing functionality common to requests and responses. |
|
| 9 |
+ */ |
|
| 10 |
+trait MessageTrait |
|
| 11 |
+{
|
|
| 12 |
+ /** @var array Map of all registered headers, as original name => array of values */ |
|
| 13 |
+ private $headers = []; |
|
| 14 |
+ |
|
| 15 |
+ /** @var array Map of lowercase header name => original name at registration */ |
|
| 16 |
+ private $headerNames = []; |
|
| 17 |
+ |
|
| 18 |
+ /** @var string */ |
|
| 19 |
+ private $protocol = '1.1'; |
|
| 20 |
+ |
|
| 21 |
+ /** @var StreamInterface|null */ |
|
| 22 |
+ private $stream; |
|
| 23 |
+ |
|
| 24 |
+ public function getProtocolVersion() |
|
| 25 |
+ {
|
|
| 26 |
+ return $this->protocol; |
|
| 27 |
+ } |
|
| 28 |
+ |
|
| 29 |
+ public function withProtocolVersion($version) |
|
| 30 |
+ {
|
|
| 31 |
+ if ($this->protocol === $version) {
|
|
| 32 |
+ return $this; |
|
| 33 |
+ } |
|
| 34 |
+ |
|
| 35 |
+ $new = clone $this; |
|
| 36 |
+ $new->protocol = $version; |
|
| 37 |
+ return $new; |
|
| 38 |
+ } |
|
| 39 |
+ |
|
| 40 |
+ public function getHeaders() |
|
| 41 |
+ {
|
|
| 42 |
+ return $this->headers; |
|
| 43 |
+ } |
|
| 44 |
+ |
|
| 45 |
+ public function hasHeader($header) |
|
| 46 |
+ {
|
|
| 47 |
+ return isset($this->headerNames[strtolower($header)]); |
|
| 48 |
+ } |
|
| 49 |
+ |
|
| 50 |
+ public function getHeader($header) |
|
| 51 |
+ {
|
|
| 52 |
+ $header = strtolower($header); |
|
| 53 |
+ |
|
| 54 |
+ if (!isset($this->headerNames[$header])) {
|
|
| 55 |
+ return []; |
|
| 56 |
+ } |
|
| 57 |
+ |
|
| 58 |
+ $header = $this->headerNames[$header]; |
|
| 59 |
+ |
|
| 60 |
+ return $this->headers[$header]; |
|
| 61 |
+ } |
|
| 62 |
+ |
|
| 63 |
+ public function getHeaderLine($header) |
|
| 64 |
+ {
|
|
| 65 |
+ return implode(', ', $this->getHeader($header));
|
|
| 66 |
+ } |
|
| 67 |
+ |
|
| 68 |
+ public function withHeader($header, $value) |
|
| 69 |
+ {
|
|
| 70 |
+ $this->assertHeader($header); |
|
| 71 |
+ $value = $this->normalizeHeaderValue($value); |
|
| 72 |
+ $normalized = strtolower($header); |
|
| 73 |
+ |
|
| 74 |
+ $new = clone $this; |
|
| 75 |
+ if (isset($new->headerNames[$normalized])) {
|
|
| 76 |
+ unset($new->headers[$new->headerNames[$normalized]]); |
|
| 77 |
+ } |
|
| 78 |
+ $new->headerNames[$normalized] = $header; |
|
| 79 |
+ $new->headers[$header] = $value; |
|
| 80 |
+ |
|
| 81 |
+ return $new; |
|
| 82 |
+ } |
|
| 83 |
+ |
|
| 84 |
+ public function withAddedHeader($header, $value) |
|
| 85 |
+ {
|
|
| 86 |
+ $this->assertHeader($header); |
|
| 87 |
+ $value = $this->normalizeHeaderValue($value); |
|
| 88 |
+ $normalized = strtolower($header); |
|
| 89 |
+ |
|
| 90 |
+ $new = clone $this; |
|
| 91 |
+ if (isset($new->headerNames[$normalized])) {
|
|
| 92 |
+ $header = $this->headerNames[$normalized]; |
|
| 93 |
+ $new->headers[$header] = array_merge($this->headers[$header], $value); |
|
| 94 |
+ } else {
|
|
| 95 |
+ $new->headerNames[$normalized] = $header; |
|
| 96 |
+ $new->headers[$header] = $value; |
|
| 97 |
+ } |
|
| 98 |
+ |
|
| 99 |
+ return $new; |
|
| 100 |
+ } |
|
| 101 |
+ |
|
| 102 |
+ public function withoutHeader($header) |
|
| 103 |
+ {
|
|
| 104 |
+ $normalized = strtolower($header); |
|
| 105 |
+ |
|
| 106 |
+ if (!isset($this->headerNames[$normalized])) {
|
|
| 107 |
+ return $this; |
|
| 108 |
+ } |
|
| 109 |
+ |
|
| 110 |
+ $header = $this->headerNames[$normalized]; |
|
| 111 |
+ |
|
| 112 |
+ $new = clone $this; |
|
| 113 |
+ unset($new->headers[$header], $new->headerNames[$normalized]); |
|
| 114 |
+ |
|
| 115 |
+ return $new; |
|
| 116 |
+ } |
|
| 117 |
+ |
|
| 118 |
+ public function getBody() |
|
| 119 |
+ {
|
|
| 120 |
+ if (!$this->stream) {
|
|
| 121 |
+ $this->stream = Utils::streamFor('');
|
|
| 122 |
+ } |
|
| 123 |
+ |
|
| 124 |
+ return $this->stream; |
|
| 125 |
+ } |
|
| 126 |
+ |
|
| 127 |
+ public function withBody(StreamInterface $body) |
|
| 128 |
+ {
|
|
| 129 |
+ if ($body === $this->stream) {
|
|
| 130 |
+ return $this; |
|
| 131 |
+ } |
|
| 132 |
+ |
|
| 133 |
+ $new = clone $this; |
|
| 134 |
+ $new->stream = $body; |
|
| 135 |
+ return $new; |
|
| 136 |
+ } |
|
| 137 |
+ |
|
| 138 |
+ private function setHeaders(array $headers) |
|
| 139 |
+ {
|
|
| 140 |
+ $this->headerNames = $this->headers = []; |
|
| 141 |
+ foreach ($headers as $header => $value) {
|
|
| 142 |
+ if (is_int($header)) {
|
|
| 143 |
+ // Numeric array keys are converted to int by PHP but having a header name '123' is not forbidden by the spec |
|
| 144 |
+ // and also allowed in withHeader(). So we need to cast it to string again for the following assertion to pass. |
|
| 145 |
+ $header = (string) $header; |
|
| 146 |
+ } |
|
| 147 |
+ $this->assertHeader($header); |
|
| 148 |
+ $value = $this->normalizeHeaderValue($value); |
|
| 149 |
+ $normalized = strtolower($header); |
|
| 150 |
+ if (isset($this->headerNames[$normalized])) {
|
|
| 151 |
+ $header = $this->headerNames[$normalized]; |
|
| 152 |
+ $this->headers[$header] = array_merge($this->headers[$header], $value); |
|
| 153 |
+ } else {
|
|
| 154 |
+ $this->headerNames[$normalized] = $header; |
|
| 155 |
+ $this->headers[$header] = $value; |
|
| 156 |
+ } |
|
| 157 |
+ } |
|
| 158 |
+ } |
|
| 159 |
+ |
|
| 160 |
+ private function normalizeHeaderValue($value) |
|
| 161 |
+ {
|
|
| 162 |
+ if (!is_array($value)) {
|
|
| 163 |
+ return $this->trimHeaderValues([$value]); |
|
| 164 |
+ } |
|
| 165 |
+ |
|
| 166 |
+ if (count($value) === 0) {
|
|
| 167 |
+ throw new \InvalidArgumentException('Header value can not be an empty array.');
|
|
| 168 |
+ } |
|
| 169 |
+ |
|
| 170 |
+ return $this->trimHeaderValues($value); |
|
| 171 |
+ } |
|
| 172 |
+ |
|
| 173 |
+ /** |
|
| 174 |
+ * Trims whitespace from the header values. |
|
| 175 |
+ * |
|
| 176 |
+ * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field. |
|
| 177 |
+ * |
|
| 178 |
+ * header-field = field-name ":" OWS field-value OWS |
|
| 179 |
+ * OWS = *( SP / HTAB ) |
|
| 180 |
+ * |
|
| 181 |
+ * @param string[] $values Header values |
|
| 182 |
+ * |
|
| 183 |
+ * @return string[] Trimmed header values |
|
| 184 |
+ * |
|
| 185 |
+ * @see https://tools.ietf.org/html/rfc7230#section-3.2.4 |
|
| 186 |
+ */ |
|
| 187 |
+ private function trimHeaderValues(array $values) |
|
| 188 |
+ {
|
|
| 189 |
+ return array_map(function ($value) {
|
|
| 190 |
+ if (!is_scalar($value) && null !== $value) {
|
|
| 191 |
+ throw new \InvalidArgumentException(sprintf( |
|
| 192 |
+ 'Header value must be scalar or null but %s provided.', |
|
| 193 |
+ is_object($value) ? get_class($value) : gettype($value) |
|
| 194 |
+ )); |
|
| 195 |
+ } |
|
| 196 |
+ |
|
| 197 |
+ return trim((string) $value, " \t"); |
|
| 198 |
+ }, array_values($values)); |
|
| 199 |
+ } |
|
| 200 |
+ |
|
| 201 |
+ private function assertHeader($header) |
|
| 202 |
+ {
|
|
| 203 |
+ if (!is_string($header)) {
|
|
| 204 |
+ throw new \InvalidArgumentException(sprintf( |
|
| 205 |
+ 'Header name must be a string but %s provided.', |
|
| 206 |
+ is_object($header) ? get_class($header) : gettype($header) |
|
| 207 |
+ )); |
|
| 208 |
+ } |
|
| 209 |
+ |
|
| 210 |
+ if ($header === '') {
|
|
| 211 |
+ throw new \InvalidArgumentException('Header name can not be empty.');
|
|
| 212 |
+ } |
|
| 213 |
+ } |
|
| 214 |
+} |