-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.php
More file actions
361 lines (337 loc) · 10.6 KB
/
Copy pathMessage.php
File metadata and controls
361 lines (337 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/**
* This file is part of HttpMessage
*
* @package bdk/http-message
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2024 Brad Kent
* @version v1.0
*/
namespace bdk\HttpMessage;
use bdk\HttpMessage\AssertionTrait;
use bdk\HttpMessage\Stream;
use InvalidArgumentException;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamInterface;
/**
* Http Message
*
* @psalm-consistent-constructor
*/
class Message implements MessageInterface
{
use AssertionTrait;
/**
* @var StreamInterface|null
*/
private $body;
/**
* @var array<string,string[]> Map of all registered headers, as name => array of values
*/
private $headers = array();
/**
* @var array<string,string> Map of lowercase header name => original name at registration
*/
private $headerNames = array();
/** @var string */
protected $protocolVersion = '1.1';
/**
* Retrieves the HTTP protocol version as a string.
*
* @return string HTTP protocol version (e.g., "1.1", "1.0").
*/
public function getProtocolVersion(): string
{
return $this->protocolVersion;
}
/**
* Return an instance with the specified HTTP protocol version.
*
* The version string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
*
* @param string $version HTTP protocol version
*
* @return static
*/
public function withProtocolVersion(string $version): static
{
$this->assertProtocolVersion($version);
if ($version === $this->protocolVersion) {
return $this;
}
$new = clone $this;
$new->protocolVersion = $version;
return $new;
}
/**
* {@inheritDoc}
*
* @return string[][] Returns an associative array of the message's headers. Each
* key is a header name, and each value is an array of strings for that header.
*/
public function getHeaders(): array
{
return $this->headers;
}
/**
* Checks if a header exists by the given case-insensitive name.
*
* @param string $name Case-insensitive header field name.
*
* @return bool Returns true if any header names match the given header
* name using a case-insensitive string comparison. Returns false if
* no matching header name is found in the message.
*/
public function hasHeader(string $name): bool
{
$nameLower = \strtolower($name);
return isset($this->headerNames[$nameLower]);
}
/**
* Retrieves a message header value by the given case-insensitive name.
*
* This method returns an array of all the header values of the given
* case-insensitive header name.
*
* @param string $name header name
*
* @return string[] An array of string values as provided for the given
* header. If the header does not appear in the message, an empty array is returned.
*/
public function getHeader(string $name): array
{
$nameLower = \strtolower($name);
if (!isset($this->headerNames[$nameLower])) {
return array();
}
$name = $this->headerNames[$nameLower];
return $this->headers[$name];
}
/**
* Retrieves a comma-separated string of the values for a single header.
*
* This method returns all of the header values of the given
* case-insensitive header name as a string concatenated together using
* a comma.
*
* NOTE: Not all header values may be appropriately represented using
* comma concatenation. For such headers, use getHeader() instead
* and supply your own delimiter when concatenating.
*
* If the header does not appear in the message, this method will return
* an empty string.
*
* @param string $name Case-insensitive header field name.
*
* @return string A string of values as provided for the given header
* concatenated together using a comma. If the header does not appear in
* the message, this method will return an empty string.
*/
public function getHeaderLine(string $name): string
{
return \implode(', ', $this->getHeader($name));
}
/**
* Return an instance with the provided value replacing the specified header.
*
* @param string $name Case-insensitive header field name.
* @param string|string[] $value Header value(s).
*
* @return static
* @throws InvalidArgumentException for invalid header names or values.
*/
public function withHeader(string $name, $value): static
{
$this->assertHeaderName($name);
$name = $this->normalizeHeaderName($name);
$this->assertHeaderValue($value);
$values = $this->normalizeHeaderValue($value);
$nameLower = \strtolower($name);
$new = clone $this;
if (isset($new->headerNames[$nameLower])) {
// remove previous header-name
$namePrev = $new->headerNames[$nameLower];
unset($new->headers[$namePrev]);
}
$new->headerNames[$nameLower] = $name;
$new->headers[$name] = $values;
if ($nameLower === 'host') {
$new->afterUpdateHost();
}
return $new;
}
/**
* Return an instance with the specified header values appended to the current value
*
* Existing values for the specified header will be maintained.
* The new value(s) will be appended to the existing list.
* If the header did not exist previously, it will be added.
*
* @param string $name Case-insensitive header field name to add.
* @param string|string[] $value Header value(s).
*
* @return static
* @throws InvalidArgumentException for invalid header names.
* @throws InvalidArgumentException for invalid header values.
*/
public function withAddedHeader(string $name, $value): static
{
// assert before using as array key (which will typecast)
$this->assertHeaderName($name);
$new = clone $this;
$new->setHeaders(array(
$name => $value,
));
return $new;
}
/**
* Return an instance without the specified header.
*
* @param string $name Case-insensitive header field name to remove.
*
* @return static
*/
public function withoutHeader(string $name): static
{
$nameLower = \strtolower($name);
if (!isset($this->headerNames[$nameLower])) {
return $this;
}
$new = clone $this;
unset($new->headers[$name], $new->headerNames[$nameLower]);
return $new;
}
/**
* Gets the body of the message.
*
* @return StreamInterface The body as a stream.
*/
public function getBody(): StreamInterface
{
if (!$this->body) {
$this->body = new Stream();
}
return $this->body;
}
/**
* Return an instance with the specified message body.
*
* @param StreamInterface $body Body
*
* @return static
* @throws \InvalidArgumentException
*/
public function withBody(StreamInterface $body): static
{
if ($body === $this->body) {
return $this;
}
$new = clone $this;
$new->body = $body;
return $new;
}
/**
* Assert only one Host value / sort Host header to beginning
*
* @return void
*
* @throws InvalidArgumentException
*/
private function afterUpdateHost()
{
$this->headers['Host'] = \array_unique($this->headers['Host']);
if (\count($this->headers['Host']) > 1) {
throw new InvalidArgumentException(
'Only one Host header is allowed.'
);
}
// Ensure Host is the first header.
// See: https://datatracker.ietf.org/doc/html/rfc7230#section-5.4
if (isset($this->headers['Host'])) {
$this->headers = \array_replace(
array('Host' => $this->headers['Host']),
$this->headers
);
}
}
/**
* Normalize header name
*
* @param string $name header name
*
* @return string
*/
private function normalizeHeaderName($name)
{
$nameLower = \strtolower($name);
return $nameLower === 'host'
? 'Host'
: $name;
}
/**
* Trims whitespace from the header value(s).
*
* Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field.
*
* header-field = field-name ":" OWS field-value OWS
* OWS = *( SP / HTAB )
*
* @param non-empty-string|int|float|string[]|int[]|float[] $value header value
*
* @return string[] Trimmed header values
*
* @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4
*/
private function normalizeHeaderValue($value)
{
$values = (array) $value;
$values = \array_map(static function ($value) {
return \trim((string) $value, " \t");
}, $values);
return \array_values($values);
}
/**
* Set header values
*
* @param array $headers header name/value pairs
*
* @return void
*/
protected function setHeaders(array $headers)
{
\array_walk($headers, function ($value, $name) {
if (\is_int($name)) {
// Numeric array keys are converted to int by PHP but having a header name '123' is not forbidden by the spec
// and also allowed in withHeader(). So we need to cast it to string again for the following assertion to pass.
$name = (string) $name;
}
$this->assertHeaderName($name);
$this->assertHeaderValue($value);
$name = $this->normalizeHeaderName($name);
$values = $this->normalizeHeaderValue($value);
$this->setHeaderValues($name, $values);
});
}
/**
* Append the given values
*
* @param string $name header name
* @param string[] $values header values
*
* @return void
*/
private function setHeaderValues($name, array $values)
{
$nameLower = \strtolower($name);
if (isset($this->headerNames[$nameLower])) {
$name = $this->headerNames[$nameLower];
$values = \array_merge($this->headers[$name], $values);
}
$this->headerNames[$nameLower] = $name;
$this->headers[$name] = $values;
if ($nameLower === 'host') {
$this->afterUpdateHost();
}
}
}