@@ -14,131 +14,131 @@ |
||
| 14 | 14 | class Message implements MessageInterface |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * RFC-2616, RFC-7230 - case-insensitive; HTTP2 pack convert all header to lowercase |
|
| 19 | - * |
|
| 20 | - * @var array - all header name will be convert to lowercase |
|
| 21 | - */ |
|
| 22 | - protected array $headers = []; |
|
| 23 | - protected string $protocol = '1.1'; |
|
| 24 | - protected ?StreamInterface $stream = null; |
|
| 25 | - |
|
| 26 | - public function getProtocolVersion(): string |
|
| 27 | - { |
|
| 28 | - return $this->protocol; |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - public function withProtocolVersion($version): self |
|
| 32 | - { |
|
| 33 | - if ($this->protocol !== $version) { |
|
| 34 | - $this->protocol = $version; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - return $this; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - public function getHeaders(): array |
|
| 41 | - { |
|
| 42 | - return $this->headers; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public function hasHeader($name): bool |
|
| 46 | - { |
|
| 47 | - $name = strtolower($name); |
|
| 48 | - $hasHeader = $this->headers[$name] ?? null; |
|
| 49 | - return $hasHeader !== null; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - public function getHeader($name): array |
|
| 53 | - { |
|
| 54 | - $name = strtolower($name); |
|
| 55 | - return $this->headers[$name] ?? []; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - public function getHeaderLine($name): string |
|
| 59 | - { |
|
| 60 | - return implode(', ', $this->getHeader($name)); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - public function withHeader($name, $value): self |
|
| 64 | - { |
|
| 65 | - $name = strtolower($name); |
|
| 66 | - $this->headers[$name] = $this->validateAndTrimHeader($name, (array)$value); |
|
| 67 | - return $this; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - public function withoutHeader($name): self |
|
| 71 | - { |
|
| 72 | - $name = strtolower($name); |
|
| 73 | - unset($this->headers[$name]); |
|
| 74 | - return $this; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - public function getBody(): StreamInterface |
|
| 78 | - { |
|
| 79 | - if (!$this->stream) { |
|
| 80 | - $this->stream = Stream::create(''); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - return $this->stream; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - public function withBody(StreamInterface $body): self |
|
| 87 | - { |
|
| 88 | - $this->stream = $body; |
|
| 89 | - return $this; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - public function reset(): self |
|
| 93 | - { |
|
| 94 | - $this->headers = []; |
|
| 95 | - if ($this->stream) { |
|
| 96 | - $this->stream->close(); |
|
| 97 | - $this->stream = null; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - return $this; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - protected function validateAndTrimHeader(string $name, array $values): array |
|
| 104 | - { |
|
| 105 | - if (preg_match("@^[!#$%&'*+.^_`|~0-9A-Za-z-]+$@", $name) !== 1) { |
|
| 106 | - throw new InvalidArgumentException('Header name must be an RFC 7230 compatible string.'); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - $returnValues = []; |
|
| 110 | - foreach ($values as $v) { |
|
| 111 | - if ((!is_numeric($v) && !is_string($v)) || 1 !== preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string)$v)) { |
|
| 112 | - throw new InvalidArgumentException('Header values must be RFC 7230 compatible strings.'); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $returnValues[] = trim((string)$v); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - return $returnValues; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - public function withAddedHeader($name, $value): self |
|
| 122 | - { |
|
| 123 | - $this->setHeaders([$name => $value]); |
|
| 124 | - return $this; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - protected function setHeaders(array $headers): void |
|
| 128 | - { |
|
| 129 | - foreach ($headers as $name => $values) { |
|
| 130 | - $values = (array)$values; |
|
| 131 | - $name = strtolower((string)$name); |
|
| 132 | - |
|
| 133 | - if (!$this->hasHeader($name)) { |
|
| 134 | - $this->headers[$name] = []; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - $values = $this->validateAndTrimHeader($name, $values); |
|
| 138 | - |
|
| 139 | - foreach ($values as $value) { |
|
| 140 | - $this->headers[$name][] = $value; |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - } |
|
| 17 | + /** |
|
| 18 | + * RFC-2616, RFC-7230 - case-insensitive; HTTP2 pack convert all header to lowercase |
|
| 19 | + * |
|
| 20 | + * @var array - all header name will be convert to lowercase |
|
| 21 | + */ |
|
| 22 | + protected array $headers = []; |
|
| 23 | + protected string $protocol = '1.1'; |
|
| 24 | + protected ?StreamInterface $stream = null; |
|
| 25 | + |
|
| 26 | + public function getProtocolVersion(): string |
|
| 27 | + { |
|
| 28 | + return $this->protocol; |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + public function withProtocolVersion($version): self |
|
| 32 | + { |
|
| 33 | + if ($this->protocol !== $version) { |
|
| 34 | + $this->protocol = $version; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + return $this; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + public function getHeaders(): array |
|
| 41 | + { |
|
| 42 | + return $this->headers; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public function hasHeader($name): bool |
|
| 46 | + { |
|
| 47 | + $name = strtolower($name); |
|
| 48 | + $hasHeader = $this->headers[$name] ?? null; |
|
| 49 | + return $hasHeader !== null; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + public function getHeader($name): array |
|
| 53 | + { |
|
| 54 | + $name = strtolower($name); |
|
| 55 | + return $this->headers[$name] ?? []; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + public function getHeaderLine($name): string |
|
| 59 | + { |
|
| 60 | + return implode(', ', $this->getHeader($name)); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + public function withHeader($name, $value): self |
|
| 64 | + { |
|
| 65 | + $name = strtolower($name); |
|
| 66 | + $this->headers[$name] = $this->validateAndTrimHeader($name, (array)$value); |
|
| 67 | + return $this; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + public function withoutHeader($name): self |
|
| 71 | + { |
|
| 72 | + $name = strtolower($name); |
|
| 73 | + unset($this->headers[$name]); |
|
| 74 | + return $this; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + public function getBody(): StreamInterface |
|
| 78 | + { |
|
| 79 | + if (!$this->stream) { |
|
| 80 | + $this->stream = Stream::create(''); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + return $this->stream; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + public function withBody(StreamInterface $body): self |
|
| 87 | + { |
|
| 88 | + $this->stream = $body; |
|
| 89 | + return $this; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + public function reset(): self |
|
| 93 | + { |
|
| 94 | + $this->headers = []; |
|
| 95 | + if ($this->stream) { |
|
| 96 | + $this->stream->close(); |
|
| 97 | + $this->stream = null; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + return $this; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + protected function validateAndTrimHeader(string $name, array $values): array |
|
| 104 | + { |
|
| 105 | + if (preg_match("@^[!#$%&'*+.^_`|~0-9A-Za-z-]+$@", $name) !== 1) { |
|
| 106 | + throw new InvalidArgumentException('Header name must be an RFC 7230 compatible string.'); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + $returnValues = []; |
|
| 110 | + foreach ($values as $v) { |
|
| 111 | + if ((!is_numeric($v) && !is_string($v)) || 1 !== preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string)$v)) { |
|
| 112 | + throw new InvalidArgumentException('Header values must be RFC 7230 compatible strings.'); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $returnValues[] = trim((string)$v); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + return $returnValues; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + public function withAddedHeader($name, $value): self |
|
| 122 | + { |
|
| 123 | + $this->setHeaders([$name => $value]); |
|
| 124 | + return $this; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + protected function setHeaders(array $headers): void |
|
| 128 | + { |
|
| 129 | + foreach ($headers as $name => $values) { |
|
| 130 | + $values = (array)$values; |
|
| 131 | + $name = strtolower((string)$name); |
|
| 132 | + |
|
| 133 | + if (!$this->hasHeader($name)) { |
|
| 134 | + $this->headers[$name] = []; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + $values = $this->validateAndTrimHeader($name, $values); |
|
| 138 | + |
|
| 139 | + foreach ($values as $value) { |
|
| 140 | + $this->headers[$name][] = $value; |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | 144 | } |