Debian Codegraph @

lib/3rdParty/composer/guzzlehttp/psr7/src/MessageTrait.php

raw symbols SCIP
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 /**
33 * @return static
34 */
35 public function withProtocolVersion($version): MessageInterface
36 {
37 if (!\is_string($version)) {
38 \trigger_deprecation(
39 'guzzlehttp/psr7',
40 '2.11',
41 'Passing %s to MessageInterface::withProtocolVersion() is deprecated; guzzlehttp/psr7 3.0 requires string.',
42 \get_debug_type($version)
43 );
44 }
45
46 if ($this->protocol === $version) {
47 return $this;
48 }
49
50 $new = clone $this;
51 $new->protocol = $version;
52
53 return $new;
54 }
55
56 public function getHeaders(): array
57 {
58 return $this->headers;
59 }
60
61 public function hasHeader($header): bool
62 {
63 return isset($this->headerNames[strtolower($header)]);
64 }
65
66 public function getHeader($header): array
67 {
68 $header = strtolower($header);
69
70 if (!isset($this->headerNames[$header])) {
71 return [];
72 }
73
74 $header = $this->headerNames[$header];
75
76 return $this->headers[$header];
77 }
78
79 public function getHeaderLine($header): string
80 {
81 return implode(', ', $this->getHeader($header));
82 }
83
84 /**
85 * @return static
86 */
87 public function withHeader($header, $value): MessageInterface
88 {
89 $this->assertHeader($header);
90 $values = \is_array($value) ? $value : [$value];
91 foreach ($values as $item) {
92 if (!\is_string($item) && (\is_scalar($item) || $item === null)) {
93 \trigger_deprecation(
94 'guzzlehttp/psr7',
95 '2.11',
96 'Passing %s to MessageInterface::withHeader() is deprecated; guzzlehttp/psr7 3.0 requires string|string[].',
97 \get_debug_type($item)
98 );
99
100 break;
101 }
102 }
103 $value = $this->normalizeHeaderValue($value);
104 $normalized = strtolower($header);
105
106 $new = clone $this;
107 if (isset($new->headerNames[$normalized])) {
108 unset($new->headers[$new->headerNames[$normalized]]);
109 }
110 $new->headerNames[$normalized] = $header;
111 $new->headers[$header] = $value;
112
113 return $new;
114 }
115
116 /**
117 * @return static
118 */
119 public function withAddedHeader($header, $value): MessageInterface
120 {
121 $this->assertHeader($header);
122 $values = \is_array($value) ? $value : [$value];
123 foreach ($values as $item) {
124 if (!\is_string($item) && (\is_scalar($item) || $item === null)) {
125 \trigger_deprecation(
126 'guzzlehttp/psr7',
127 '2.11',
128 'Passing %s to MessageInterface::withAddedHeader() is deprecated; guzzlehttp/psr7 3.0 requires string|string[].',
129 \get_debug_type($item)
130 );
131
132 break;
133 }
134 }
135 $value = $this->normalizeHeaderValue($value);
136 $normalized = strtolower($header);
137
138 $new = clone $this;
139 if (isset($new->headerNames[$normalized])) {
140 $header = $this->headerNames[$normalized];
141 $new->headers[$header] = array_merge($this->headers[$header], $value);
142 } else {
143 $new->headerNames[$normalized] = $header;
144 $new->headers[$header] = $value;
145 }
146
147 return $new;
148 }
149
150 /**
151 * @return static
152 */
153 public function withoutHeader($header): MessageInterface
154 {
155 $normalized = strtolower($header);
156
157 if (!isset($this->headerNames[$normalized])) {
158 return $this;
159 }
160
161 $header = $this->headerNames[$normalized];
162
163 $new = clone $this;
164 unset($new->headers[$header], $new->headerNames[$normalized]);
165
166 return $new;
167 }
168
169 public function getBody(): StreamInterface
170 {
171 if (!$this->stream) {
172 $this->stream = Utils::streamFor('');
173 }
174
175 return $this->stream;
176 }
177
178 /**
179 * @return static
180 */
181 public function withBody(StreamInterface $body): MessageInterface
182 {
183 if ($body === $this->stream) {
184 return $this;
185 }
186
187 $new = clone $this;
188 $new->stream = $body;
189
190 return $new;
191 }
192
193 /**
194 * @param (string|string[])[] $headers
195 */
196 private function setHeaders(array $headers): void
197 {
198 $this->headerNames = $this->headers = [];
199 foreach ($headers as $header => $value) {
200 // Numeric array keys are converted to int by PHP.
201 $header = (string) $header;
202
203 $this->assertHeader($header);
204 $values = \is_array($value) ? $value : [$value];
205 foreach ($values as $item) {
206 if (!\is_string($item) && (\is_scalar($item) || $item === null)) {
207 \trigger_deprecation(
208 'guzzlehttp/psr7',
209 '2.11',
210 'Passing %s to %s::__construct() is deprecated; guzzlehttp/psr7 3.0 requires string|string[].',
211 \get_debug_type($item),
212 static::class
213 );
214
215 break;
216 }
217 }
218 $value = $this->normalizeHeaderValue($value);
219 $normalized = strtolower($header);
220 if (isset($this->headerNames[$normalized])) {
221 $header = $this->headerNames[$normalized];
222 $this->headers[$header] = array_merge($this->headers[$header], $value);
223 } else {
224 $this->headerNames[$normalized] = $header;
225 $this->headers[$header] = $value;
226 }
227 }
228 }
229
230 /**
231 * @param mixed $value
232 *
233 * @return string[]
234 */
235 private function normalizeHeaderValue($value): array
236 {
237 if (is_array($value) && $value === []) {
238 \trigger_deprecation(
239 'guzzlehttp/psr7',
240 '2.11',
241 'Passing an empty array as a header value is deprecated; guzzlehttp/psr7 3.0 rejects empty header value arrays.'
242 );
243 }
244
245 if (!is_array($value)) {
246 return $this->trimAndValidateHeaderValues([$value]);
247 }
248
249 return $this->trimAndValidateHeaderValues($value);
250 }
251
252 /**
253 * Trims whitespace from the header values.
254 *
255 * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field.
256 *
257 * header-field = field-name ":" OWS field-value OWS
258 * OWS = *( SP / HTAB )
259 *
260 * @param mixed[] $values Header values
261 *
262 * @return string[] Trimmed header values
263 *
264 * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4
265 */
266 private function trimAndValidateHeaderValues(array $values): array
267 {
268 return array_map(function ($value) {
269 if (!is_scalar($value) && null !== $value) {
270 throw new \InvalidArgumentException(sprintf(
271 'Header value must be scalar or null but %s provided.',
272 is_object($value) ? get_class($value) : gettype($value)
273 ));
274 }
275
276 $trimmed = trim((string) $value, " \t");
277 $this->assertValue($trimmed);
278
279 return $trimmed;
280 }, array_values($values));
281 }
282
283 /**
284 * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2
285 *
286 * @param mixed $header
287 */
288 private function assertHeader($header): void
289 {
290 if (!is_string($header)) {
291 throw new \InvalidArgumentException(sprintf(
292 'Header name must be a string but %s provided.',
293 is_object($header) ? get_class($header) : gettype($header)
294 ));
295 }
296
297 if (!preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $header)) {
298 throw new \InvalidArgumentException(
299 sprintf('"%s" is not valid header name.', $header)
300 );
301 }
302 }
303
304 /**
305 * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2
306 *
307 * field-value = *( field-content / obs-fold )
308 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
309 * field-vchar = VCHAR / obs-text
310 * VCHAR = %x21-7E
311 * obs-text = %x80-FF
312 * obs-fold = CRLF 1*( SP / HTAB )
313 */
314 private function assertValue(string $value): void
315 {
316 // The regular expression intentionally does not support the obs-fold production, because as
317 // per RFC 7230#3.2.4:
318 //
319 // A sender MUST NOT generate a message that includes
320 // line folding (i.e., that has any field-value that contains a match to
321 // the obs-fold rule) unless the message is intended for packaging
322 // within the message/http media type.
323 //
324 // Clients must not send a request with line folding and a server sending folded headers is
325 // likely very rare. Line folding is a fairly obscure feature of HTTP/1.1 and thus not accepting
326 // folding is not likely to break any legitimate use case.
327 if (!preg_match('/^[\x20\x09\x21-\x7E\x80-\xFF]*$/D', $value)) {
328 throw new \InvalidArgumentException(
329 sprintf('"%s" is not valid header value.', $value)
330 );
331 }
332 }
333 }