Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@12
|
3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
|
Chris@12
|
4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
|
Chris@0
|
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 namespace Zend\Diactoros;
|
Chris@0
|
9
|
Chris@0
|
10 use InvalidArgumentException;
|
Chris@0
|
11 use Psr\Http\Message\StreamInterface;
|
Chris@0
|
12
|
Chris@16
|
13 use function array_map;
|
Chris@16
|
14 use function array_merge;
|
Chris@16
|
15 use function get_class;
|
Chris@16
|
16 use function gettype;
|
Chris@16
|
17 use function implode;
|
Chris@16
|
18 use function is_array;
|
Chris@16
|
19 use function is_object;
|
Chris@16
|
20 use function is_resource;
|
Chris@16
|
21 use function is_string;
|
Chris@16
|
22 use function preg_match;
|
Chris@16
|
23 use function sprintf;
|
Chris@16
|
24 use function strtolower;
|
Chris@16
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * Trait implementing the various methods defined in MessageInterface.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @see https://github.com/php-fig/http-message/tree/master/src/MessageInterface.php
|
Chris@0
|
30 */
|
Chris@0
|
31 trait MessageTrait
|
Chris@0
|
32 {
|
Chris@0
|
33 /**
|
Chris@0
|
34 * List of all registered headers, as key => array of values.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @var array
|
Chris@0
|
37 */
|
Chris@0
|
38 protected $headers = [];
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * Map of normalized header name to original name used to register header.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @var array
|
Chris@0
|
44 */
|
Chris@0
|
45 protected $headerNames = [];
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * @var string
|
Chris@0
|
49 */
|
Chris@0
|
50 private $protocol = '1.1';
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * @var StreamInterface
|
Chris@0
|
54 */
|
Chris@0
|
55 private $stream;
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * Retrieves the HTTP protocol version as a string.
|
Chris@0
|
59 *
|
Chris@0
|
60 * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
|
Chris@0
|
61 *
|
Chris@0
|
62 * @return string HTTP protocol version.
|
Chris@0
|
63 */
|
Chris@0
|
64 public function getProtocolVersion()
|
Chris@0
|
65 {
|
Chris@0
|
66 return $this->protocol;
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * Return an instance with the specified HTTP protocol version.
|
Chris@0
|
71 *
|
Chris@0
|
72 * The version string MUST contain only the HTTP version number (e.g.,
|
Chris@0
|
73 * "1.1", "1.0").
|
Chris@0
|
74 *
|
Chris@0
|
75 * This method MUST be implemented in such a way as to retain the
|
Chris@0
|
76 * immutability of the message, and MUST return an instance that has the
|
Chris@0
|
77 * new protocol version.
|
Chris@0
|
78 *
|
Chris@0
|
79 * @param string $version HTTP protocol version
|
Chris@0
|
80 * @return static
|
Chris@0
|
81 */
|
Chris@0
|
82 public function withProtocolVersion($version)
|
Chris@0
|
83 {
|
Chris@0
|
84 $this->validateProtocolVersion($version);
|
Chris@0
|
85 $new = clone $this;
|
Chris@0
|
86 $new->protocol = $version;
|
Chris@0
|
87 return $new;
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@0
|
91 * Retrieves all message headers.
|
Chris@0
|
92 *
|
Chris@0
|
93 * The keys represent the header name as it will be sent over the wire, and
|
Chris@0
|
94 * each value is an array of strings associated with the header.
|
Chris@0
|
95 *
|
Chris@0
|
96 * // Represent the headers as a string
|
Chris@0
|
97 * foreach ($message->getHeaders() as $name => $values) {
|
Chris@0
|
98 * echo $name . ": " . implode(", ", $values);
|
Chris@0
|
99 * }
|
Chris@0
|
100 *
|
Chris@0
|
101 * // Emit headers iteratively:
|
Chris@0
|
102 * foreach ($message->getHeaders() as $name => $values) {
|
Chris@0
|
103 * foreach ($values as $value) {
|
Chris@0
|
104 * header(sprintf('%s: %s', $name, $value), false);
|
Chris@0
|
105 * }
|
Chris@0
|
106 * }
|
Chris@0
|
107 *
|
Chris@0
|
108 * @return array Returns an associative array of the message's headers. Each
|
Chris@0
|
109 * key MUST be a header name, and each value MUST be an array of strings.
|
Chris@0
|
110 */
|
Chris@0
|
111 public function getHeaders()
|
Chris@0
|
112 {
|
Chris@0
|
113 return $this->headers;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * Checks if a header exists by the given case-insensitive name.
|
Chris@0
|
118 *
|
Chris@0
|
119 * @param string $header Case-insensitive header name.
|
Chris@0
|
120 * @return bool Returns true if any header names match the given header
|
Chris@0
|
121 * name using a case-insensitive string comparison. Returns false if
|
Chris@0
|
122 * no matching header name is found in the message.
|
Chris@0
|
123 */
|
Chris@0
|
124 public function hasHeader($header)
|
Chris@0
|
125 {
|
Chris@0
|
126 return isset($this->headerNames[strtolower($header)]);
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 /**
|
Chris@0
|
130 * Retrieves a message header value by the given case-insensitive name.
|
Chris@0
|
131 *
|
Chris@0
|
132 * This method returns an array of all the header values of the given
|
Chris@0
|
133 * case-insensitive header name.
|
Chris@0
|
134 *
|
Chris@0
|
135 * If the header does not appear in the message, this method MUST return an
|
Chris@0
|
136 * empty array.
|
Chris@0
|
137 *
|
Chris@0
|
138 * @param string $header Case-insensitive header field name.
|
Chris@0
|
139 * @return string[] An array of string values as provided for the given
|
Chris@0
|
140 * header. If the header does not appear in the message, this method MUST
|
Chris@0
|
141 * return an empty array.
|
Chris@0
|
142 */
|
Chris@0
|
143 public function getHeader($header)
|
Chris@0
|
144 {
|
Chris@0
|
145 if (! $this->hasHeader($header)) {
|
Chris@0
|
146 return [];
|
Chris@0
|
147 }
|
Chris@0
|
148
|
Chris@0
|
149 $header = $this->headerNames[strtolower($header)];
|
Chris@0
|
150
|
Chris@0
|
151 return $this->headers[$header];
|
Chris@0
|
152 }
|
Chris@0
|
153
|
Chris@0
|
154 /**
|
Chris@0
|
155 * Retrieves a comma-separated string of the values for a single header.
|
Chris@0
|
156 *
|
Chris@0
|
157 * This method returns all of the header values of the given
|
Chris@0
|
158 * case-insensitive header name as a string concatenated together using
|
Chris@0
|
159 * a comma.
|
Chris@0
|
160 *
|
Chris@0
|
161 * NOTE: Not all header values may be appropriately represented using
|
Chris@0
|
162 * comma concatenation. For such headers, use getHeader() instead
|
Chris@0
|
163 * and supply your own delimiter when concatenating.
|
Chris@0
|
164 *
|
Chris@0
|
165 * If the header does not appear in the message, this method MUST return
|
Chris@0
|
166 * an empty string.
|
Chris@0
|
167 *
|
Chris@0
|
168 * @param string $name Case-insensitive header field name.
|
Chris@0
|
169 * @return string A string of values as provided for the given header
|
Chris@0
|
170 * concatenated together using a comma. If the header does not appear in
|
Chris@0
|
171 * the message, this method MUST return an empty string.
|
Chris@0
|
172 */
|
Chris@0
|
173 public function getHeaderLine($name)
|
Chris@0
|
174 {
|
Chris@0
|
175 $value = $this->getHeader($name);
|
Chris@0
|
176 if (empty($value)) {
|
Chris@0
|
177 return '';
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 return implode(',', $value);
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 /**
|
Chris@0
|
184 * Return an instance with the provided header, replacing any existing
|
Chris@0
|
185 * values of any headers with the same case-insensitive name.
|
Chris@0
|
186 *
|
Chris@0
|
187 * While header names are case-insensitive, the casing of the header will
|
Chris@0
|
188 * be preserved by this function, and returned from getHeaders().
|
Chris@0
|
189 *
|
Chris@0
|
190 * This method MUST be implemented in such a way as to retain the
|
Chris@0
|
191 * immutability of the message, and MUST return an instance that has the
|
Chris@0
|
192 * new and/or updated header and value.
|
Chris@0
|
193 *
|
Chris@0
|
194 * @param string $header Case-insensitive header field name.
|
Chris@0
|
195 * @param string|string[] $value Header value(s).
|
Chris@0
|
196 * @return static
|
Chris@0
|
197 * @throws \InvalidArgumentException for invalid header names or values.
|
Chris@0
|
198 */
|
Chris@0
|
199 public function withHeader($header, $value)
|
Chris@0
|
200 {
|
Chris@0
|
201 $this->assertHeader($header);
|
Chris@0
|
202
|
Chris@0
|
203 $normalized = strtolower($header);
|
Chris@0
|
204
|
Chris@0
|
205 $new = clone $this;
|
Chris@0
|
206 if ($new->hasHeader($header)) {
|
Chris@0
|
207 unset($new->headers[$new->headerNames[$normalized]]);
|
Chris@0
|
208 }
|
Chris@0
|
209
|
Chris@0
|
210 $value = $this->filterHeaderValue($value);
|
Chris@0
|
211
|
Chris@0
|
212 $new->headerNames[$normalized] = $header;
|
Chris@0
|
213 $new->headers[$header] = $value;
|
Chris@0
|
214
|
Chris@0
|
215 return $new;
|
Chris@0
|
216 }
|
Chris@0
|
217
|
Chris@0
|
218 /**
|
Chris@0
|
219 * Return an instance with the specified header appended with the
|
Chris@0
|
220 * given value.
|
Chris@0
|
221 *
|
Chris@0
|
222 * Existing values for the specified header will be maintained. The new
|
Chris@0
|
223 * value(s) will be appended to the existing list. If the header did not
|
Chris@0
|
224 * exist previously, it will be added.
|
Chris@0
|
225 *
|
Chris@0
|
226 * This method MUST be implemented in such a way as to retain the
|
Chris@0
|
227 * immutability of the message, and MUST return an instance that has the
|
Chris@0
|
228 * new header and/or value.
|
Chris@0
|
229 *
|
Chris@0
|
230 * @param string $header Case-insensitive header field name to add.
|
Chris@0
|
231 * @param string|string[] $value Header value(s).
|
Chris@0
|
232 * @return static
|
Chris@0
|
233 * @throws \InvalidArgumentException for invalid header names or values.
|
Chris@0
|
234 */
|
Chris@0
|
235 public function withAddedHeader($header, $value)
|
Chris@0
|
236 {
|
Chris@0
|
237 $this->assertHeader($header);
|
Chris@0
|
238
|
Chris@0
|
239 if (! $this->hasHeader($header)) {
|
Chris@0
|
240 return $this->withHeader($header, $value);
|
Chris@0
|
241 }
|
Chris@0
|
242
|
Chris@0
|
243 $header = $this->headerNames[strtolower($header)];
|
Chris@0
|
244
|
Chris@0
|
245 $new = clone $this;
|
Chris@0
|
246 $value = $this->filterHeaderValue($value);
|
Chris@0
|
247 $new->headers[$header] = array_merge($this->headers[$header], $value);
|
Chris@0
|
248 return $new;
|
Chris@0
|
249 }
|
Chris@0
|
250
|
Chris@0
|
251 /**
|
Chris@0
|
252 * Return an instance without the specified header.
|
Chris@0
|
253 *
|
Chris@0
|
254 * Header resolution MUST be done without case-sensitivity.
|
Chris@0
|
255 *
|
Chris@0
|
256 * This method MUST be implemented in such a way as to retain the
|
Chris@0
|
257 * immutability of the message, and MUST return an instance that removes
|
Chris@0
|
258 * the named header.
|
Chris@0
|
259 *
|
Chris@0
|
260 * @param string $header Case-insensitive header field name to remove.
|
Chris@0
|
261 * @return static
|
Chris@0
|
262 */
|
Chris@0
|
263 public function withoutHeader($header)
|
Chris@0
|
264 {
|
Chris@0
|
265 if (! $this->hasHeader($header)) {
|
Chris@0
|
266 return clone $this;
|
Chris@0
|
267 }
|
Chris@0
|
268
|
Chris@0
|
269 $normalized = strtolower($header);
|
Chris@0
|
270 $original = $this->headerNames[$normalized];
|
Chris@0
|
271
|
Chris@0
|
272 $new = clone $this;
|
Chris@0
|
273 unset($new->headers[$original], $new->headerNames[$normalized]);
|
Chris@0
|
274 return $new;
|
Chris@0
|
275 }
|
Chris@0
|
276
|
Chris@0
|
277 /**
|
Chris@0
|
278 * Gets the body of the message.
|
Chris@0
|
279 *
|
Chris@0
|
280 * @return StreamInterface Returns the body as a stream.
|
Chris@0
|
281 */
|
Chris@0
|
282 public function getBody()
|
Chris@0
|
283 {
|
Chris@0
|
284 return $this->stream;
|
Chris@0
|
285 }
|
Chris@0
|
286
|
Chris@0
|
287 /**
|
Chris@0
|
288 * Return an instance with the specified message body.
|
Chris@0
|
289 *
|
Chris@0
|
290 * The body MUST be a StreamInterface object.
|
Chris@0
|
291 *
|
Chris@0
|
292 * This method MUST be implemented in such a way as to retain the
|
Chris@0
|
293 * immutability of the message, and MUST return a new instance that has the
|
Chris@0
|
294 * new body stream.
|
Chris@0
|
295 *
|
Chris@0
|
296 * @param StreamInterface $body Body.
|
Chris@0
|
297 * @return static
|
Chris@0
|
298 * @throws \InvalidArgumentException When the body is not valid.
|
Chris@0
|
299 */
|
Chris@0
|
300 public function withBody(StreamInterface $body)
|
Chris@0
|
301 {
|
Chris@0
|
302 $new = clone $this;
|
Chris@0
|
303 $new->stream = $body;
|
Chris@0
|
304 return $new;
|
Chris@0
|
305 }
|
Chris@0
|
306
|
Chris@0
|
307 private function getStream($stream, $modeIfNotInstance)
|
Chris@0
|
308 {
|
Chris@0
|
309 if ($stream instanceof StreamInterface) {
|
Chris@0
|
310 return $stream;
|
Chris@0
|
311 }
|
Chris@0
|
312
|
Chris@0
|
313 if (! is_string($stream) && ! is_resource($stream)) {
|
Chris@0
|
314 throw new InvalidArgumentException(
|
Chris@0
|
315 'Stream must be a string stream resource identifier, '
|
Chris@0
|
316 . 'an actual stream resource, '
|
Chris@0
|
317 . 'or a Psr\Http\Message\StreamInterface implementation'
|
Chris@0
|
318 );
|
Chris@0
|
319 }
|
Chris@0
|
320
|
Chris@0
|
321 return new Stream($stream, $modeIfNotInstance);
|
Chris@0
|
322 }
|
Chris@0
|
323
|
Chris@0
|
324 /**
|
Chris@0
|
325 * Filter a set of headers to ensure they are in the correct internal format.
|
Chris@0
|
326 *
|
Chris@0
|
327 * Used by message constructors to allow setting all initial headers at once.
|
Chris@0
|
328 *
|
Chris@0
|
329 * @param array $originalHeaders Headers to filter.
|
Chris@0
|
330 */
|
Chris@0
|
331 private function setHeaders(array $originalHeaders)
|
Chris@0
|
332 {
|
Chris@0
|
333 $headerNames = $headers = [];
|
Chris@0
|
334
|
Chris@0
|
335 foreach ($originalHeaders as $header => $value) {
|
Chris@0
|
336 $value = $this->filterHeaderValue($value);
|
Chris@0
|
337
|
Chris@0
|
338 $this->assertHeader($header);
|
Chris@0
|
339
|
Chris@0
|
340 $headerNames[strtolower($header)] = $header;
|
Chris@0
|
341 $headers[$header] = $value;
|
Chris@0
|
342 }
|
Chris@0
|
343
|
Chris@0
|
344 $this->headerNames = $headerNames;
|
Chris@0
|
345 $this->headers = $headers;
|
Chris@0
|
346 }
|
Chris@0
|
347
|
Chris@0
|
348 /**
|
Chris@0
|
349 * Validate the HTTP protocol version
|
Chris@0
|
350 *
|
Chris@0
|
351 * @param string $version
|
Chris@0
|
352 * @throws InvalidArgumentException on invalid HTTP protocol version
|
Chris@0
|
353 */
|
Chris@0
|
354 private function validateProtocolVersion($version)
|
Chris@0
|
355 {
|
Chris@0
|
356 if (empty($version)) {
|
Chris@12
|
357 throw new InvalidArgumentException(
|
Chris@0
|
358 'HTTP protocol version can not be empty'
|
Chris@12
|
359 );
|
Chris@0
|
360 }
|
Chris@0
|
361 if (! is_string($version)) {
|
Chris@0
|
362 throw new InvalidArgumentException(sprintf(
|
Chris@0
|
363 'Unsupported HTTP protocol version; must be a string, received %s',
|
Chris@0
|
364 (is_object($version) ? get_class($version) : gettype($version))
|
Chris@0
|
365 ));
|
Chris@0
|
366 }
|
Chris@0
|
367
|
Chris@0
|
368 // HTTP/1 uses a "<major>.<minor>" numbering scheme to indicate
|
Chris@0
|
369 // versions of the protocol, while HTTP/2 does not.
|
Chris@0
|
370 if (! preg_match('#^(1\.[01]|2)$#', $version)) {
|
Chris@0
|
371 throw new InvalidArgumentException(sprintf(
|
Chris@0
|
372 'Unsupported HTTP protocol version "%s" provided',
|
Chris@0
|
373 $version
|
Chris@0
|
374 ));
|
Chris@0
|
375 }
|
Chris@0
|
376 }
|
Chris@0
|
377
|
Chris@0
|
378 /**
|
Chris@0
|
379 * @param mixed $values
|
Chris@0
|
380 * @return string[]
|
Chris@0
|
381 */
|
Chris@0
|
382 private function filterHeaderValue($values)
|
Chris@0
|
383 {
|
Chris@0
|
384 if (! is_array($values)) {
|
Chris@0
|
385 $values = [$values];
|
Chris@0
|
386 }
|
Chris@0
|
387
|
Chris@17
|
388 if ([] === $values) {
|
Chris@17
|
389 throw new InvalidArgumentException(
|
Chris@17
|
390 'Invalid header value: must be a string or array of strings; '
|
Chris@17
|
391 . 'cannot be an empty array'
|
Chris@17
|
392 );
|
Chris@17
|
393 }
|
Chris@17
|
394
|
Chris@0
|
395 return array_map(function ($value) {
|
Chris@0
|
396 HeaderSecurity::assertValid($value);
|
Chris@0
|
397
|
Chris@0
|
398 return (string) $value;
|
Chris@17
|
399 }, array_values($values));
|
Chris@0
|
400 }
|
Chris@0
|
401
|
Chris@0
|
402 /**
|
Chris@0
|
403 * Ensure header name and values are valid.
|
Chris@0
|
404 *
|
Chris@0
|
405 * @param string $name
|
Chris@0
|
406 *
|
Chris@0
|
407 * @throws InvalidArgumentException
|
Chris@0
|
408 */
|
Chris@0
|
409 private function assertHeader($name)
|
Chris@0
|
410 {
|
Chris@0
|
411 HeaderSecurity::assertValidName($name);
|
Chris@0
|
412 }
|
Chris@0
|
413 }
|