|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace React\Http\Io\V2; |
| 4 | + |
| 5 | +use Psr\Http\Message\MessageInterface; |
| 6 | +use Psr\Http\Message\StreamInterface; |
| 7 | + |
| 8 | +/** |
| 9 | + * [Internal] Abstract HTTP message base class (PSR-7) |
| 10 | + * |
| 11 | + * @internal |
| 12 | + * @see MessageInterface |
| 13 | + * |
| 14 | + * @deprecated Do not reference this FQCN directly. Not a runtime deprecation: |
| 15 | + * when psr/http-message v2 is installed this class is the |
| 16 | + * implementation behind the public name. Marked deprecated so |
| 17 | + * tooling suggests the aliased class instead (see sibling file). |
| 18 | + * @see \React\Http\Io\AbstractMessage |
| 19 | + * @see ../AbstractMessage.php |
| 20 | + */ |
| 21 | +abstract class AbstractMessage implements MessageInterface |
| 22 | +{ |
| 23 | + /** |
| 24 | + * [Internal] Regex used to match all request header fields into an array, thanks to @kelunik for checking the HTTP specs and coming up with this regex |
| 25 | + * |
| 26 | + * @internal |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + const REGEX_HEADERS = '/^([^()<>@,;:\\\"\/\[\]?={}\x00-\x20\x7F]++):[\x20\x09]*+((?:[\x20\x09]*+[\x21-\x7E\x80-\xFF]++)*+)[\x20\x09]*+[\r]?+\n/m'; |
| 30 | + |
| 31 | + /** @var array<string,string[]> */ |
| 32 | + private $headers = array(); |
| 33 | + |
| 34 | + /** @var array<string,string> */ |
| 35 | + private $headerNamesLowerCase = array(); |
| 36 | + |
| 37 | + /** @var string */ |
| 38 | + private $protocolVersion; |
| 39 | + |
| 40 | + /** @var StreamInterface */ |
| 41 | + private $body; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param string $protocolVersion |
| 45 | + * @param array<string,string|string[]> $headers |
| 46 | + * @param StreamInterface $body |
| 47 | + */ |
| 48 | + protected function __construct($protocolVersion, array $headers, StreamInterface $body) |
| 49 | + { |
| 50 | + foreach ($headers as $name => $value) { |
| 51 | + if ($value !== array()) { |
| 52 | + if (\is_array($value)) { |
| 53 | + foreach ($value as &$one) { |
| 54 | + $one = (string) $one; |
| 55 | + } |
| 56 | + } else { |
| 57 | + $value = array((string) $value); |
| 58 | + } |
| 59 | + |
| 60 | + $lower = \strtolower($name); |
| 61 | + if (isset($this->headerNamesLowerCase[$lower])) { |
| 62 | + $value = \array_merge($this->headers[$this->headerNamesLowerCase[$lower]], $value); |
| 63 | + unset($this->headers[$this->headerNamesLowerCase[$lower]]); |
| 64 | + } |
| 65 | + |
| 66 | + $this->headers[$name] = $value; |
| 67 | + $this->headerNamesLowerCase[$lower] = $name; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + $this->protocolVersion = (string) $protocolVersion; |
| 72 | + $this->body = $body; |
| 73 | + } |
| 74 | + |
| 75 | + public function getProtocolVersion(): string |
| 76 | + { |
| 77 | + return $this->protocolVersion; |
| 78 | + } |
| 79 | + |
| 80 | + public function withProtocolVersion(string $version): MessageInterface |
| 81 | + { |
| 82 | + if ((string) $version === $this->protocolVersion) { |
| 83 | + return $this; |
| 84 | + } |
| 85 | + |
| 86 | + $message = clone $this; |
| 87 | + $message->protocolVersion = (string) $version; |
| 88 | + |
| 89 | + return $message; |
| 90 | + } |
| 91 | + |
| 92 | + public function getHeaders(): array |
| 93 | + { |
| 94 | + return $this->headers; |
| 95 | + } |
| 96 | + |
| 97 | + public function hasHeader(string $name): bool |
| 98 | + { |
| 99 | + return isset($this->headerNamesLowerCase[\strtolower($name)]); |
| 100 | + } |
| 101 | + |
| 102 | + public function getHeader(string $name): array |
| 103 | + { |
| 104 | + $lower = \strtolower($name); |
| 105 | + return isset($this->headerNamesLowerCase[$lower]) ? $this->headers[$this->headerNamesLowerCase[$lower]] : array(); |
| 106 | + } |
| 107 | + |
| 108 | + public function getHeaderLine(string $name): string |
| 109 | + { |
| 110 | + return \implode(', ', $this->getHeader($name)); |
| 111 | + } |
| 112 | + |
| 113 | + public function withHeader(string $name, $value): MessageInterface |
| 114 | + { |
| 115 | + if ($value === array()) { |
| 116 | + return $this->withoutHeader($name); |
| 117 | + } elseif (\is_array($value)) { |
| 118 | + foreach ($value as &$one) { |
| 119 | + $one = (string) $one; |
| 120 | + } |
| 121 | + } else { |
| 122 | + $value = array((string) $value); |
| 123 | + } |
| 124 | + |
| 125 | + $lower = \strtolower($name); |
| 126 | + if (isset($this->headerNamesLowerCase[$lower]) && $this->headerNamesLowerCase[$lower] === (string) $name && $this->headers[$this->headerNamesLowerCase[$lower]] === $value) { |
| 127 | + return $this; |
| 128 | + } |
| 129 | + |
| 130 | + $message = clone $this; |
| 131 | + if (isset($message->headerNamesLowerCase[$lower])) { |
| 132 | + unset($message->headers[$message->headerNamesLowerCase[$lower]]); |
| 133 | + } |
| 134 | + |
| 135 | + $message->headers[$name] = $value; |
| 136 | + $message->headerNamesLowerCase[$lower] = $name; |
| 137 | + |
| 138 | + return $message; |
| 139 | + } |
| 140 | + |
| 141 | + public function withAddedHeader(string $name, $value): MessageInterface |
| 142 | + { |
| 143 | + if ($value === array()) { |
| 144 | + return $this; |
| 145 | + } |
| 146 | + |
| 147 | + return $this->withHeader($name, \array_merge($this->getHeader($name), \is_array($value) ? $value : array($value))); |
| 148 | + } |
| 149 | + |
| 150 | + public function withoutHeader(string $name): MessageInterface |
| 151 | + { |
| 152 | + $lower = \strtolower($name); |
| 153 | + if (!isset($this->headerNamesLowerCase[$lower])) { |
| 154 | + return $this; |
| 155 | + } |
| 156 | + |
| 157 | + $message = clone $this; |
| 158 | + unset($message->headers[$message->headerNamesLowerCase[$lower]], $message->headerNamesLowerCase[$lower]); |
| 159 | + |
| 160 | + return $message; |
| 161 | + } |
| 162 | + |
| 163 | + public function getBody(): StreamInterface |
| 164 | + { |
| 165 | + return $this->body; |
| 166 | + } |
| 167 | + |
| 168 | + public function withBody(StreamInterface $body): MessageInterface |
| 169 | + { |
| 170 | + if ($body === $this->body) { |
| 171 | + return $this; |
| 172 | + } |
| 173 | + |
| 174 | + $message = clone $this; |
| 175 | + $message->body = $body; |
| 176 | + |
| 177 | + return $message; |
| 178 | + } |
| 179 | +} |
0 commit comments