|
| 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 | +abstract class AbstractMessage implements MessageInterface |
| 15 | +{ |
| 16 | + /** |
| 17 | + * [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 |
| 18 | + * |
| 19 | + * @internal |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + const REGEX_HEADERS = '/^([^()<>@,;:\\\"\/\[\]?={}\x00-\x20\x7F]++):[\x20\x09]*+((?:[\x20\x09]*+[\x21-\x7E\x80-\xFF]++)*+)[\x20\x09]*+[\r]?+\n/m'; |
| 23 | + |
| 24 | + /** @var array<string,string[]> */ |
| 25 | + private $headers = array(); |
| 26 | + |
| 27 | + /** @var array<string,string> */ |
| 28 | + private $headerNamesLowerCase = array(); |
| 29 | + |
| 30 | + /** @var string */ |
| 31 | + private $protocolVersion; |
| 32 | + |
| 33 | + /** @var StreamInterface */ |
| 34 | + private $body; |
| 35 | + |
| 36 | + /** |
| 37 | + * @param string $protocolVersion |
| 38 | + * @param array<string,string|string[]> $headers |
| 39 | + * @param StreamInterface $body |
| 40 | + */ |
| 41 | + protected function __construct($protocolVersion, array $headers, StreamInterface $body) |
| 42 | + { |
| 43 | + foreach ($headers as $name => $value) { |
| 44 | + if ($value !== array()) { |
| 45 | + if (\is_array($value)) { |
| 46 | + foreach ($value as &$one) { |
| 47 | + $one = (string) $one; |
| 48 | + } |
| 49 | + } else { |
| 50 | + $value = array((string) $value); |
| 51 | + } |
| 52 | + |
| 53 | + $lower = \strtolower($name); |
| 54 | + if (isset($this->headerNamesLowerCase[$lower])) { |
| 55 | + $value = \array_merge($this->headers[$this->headerNamesLowerCase[$lower]], $value); |
| 56 | + unset($this->headers[$this->headerNamesLowerCase[$lower]]); |
| 57 | + } |
| 58 | + |
| 59 | + $this->headers[$name] = $value; |
| 60 | + $this->headerNamesLowerCase[$lower] = $name; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + $this->protocolVersion = (string) $protocolVersion; |
| 65 | + $this->body = $body; |
| 66 | + } |
| 67 | + |
| 68 | + public function getProtocolVersion(): string |
| 69 | + { |
| 70 | + return $this->protocolVersion; |
| 71 | + } |
| 72 | + |
| 73 | + public function withProtocolVersion(string $version): MessageInterface |
| 74 | + { |
| 75 | + if ((string) $version === $this->protocolVersion) { |
| 76 | + return $this; |
| 77 | + } |
| 78 | + |
| 79 | + $message = clone $this; |
| 80 | + $message->protocolVersion = (string) $version; |
| 81 | + |
| 82 | + return $message; |
| 83 | + } |
| 84 | + |
| 85 | + public function getHeaders(): array |
| 86 | + { |
| 87 | + return $this->headers; |
| 88 | + } |
| 89 | + |
| 90 | + public function hasHeader(string $name): bool |
| 91 | + { |
| 92 | + return isset($this->headerNamesLowerCase[\strtolower($name)]); |
| 93 | + } |
| 94 | + |
| 95 | + public function getHeader(string $name): array |
| 96 | + { |
| 97 | + $lower = \strtolower($name); |
| 98 | + return isset($this->headerNamesLowerCase[$lower]) ? $this->headers[$this->headerNamesLowerCase[$lower]] : array(); |
| 99 | + } |
| 100 | + |
| 101 | + public function getHeaderLine(string $name): string |
| 102 | + { |
| 103 | + return \implode(', ', $this->getHeader($name)); |
| 104 | + } |
| 105 | + |
| 106 | + public function withHeader(string $name, $value): MessageInterface |
| 107 | + { |
| 108 | + if ($value === array()) { |
| 109 | + return $this->withoutHeader($name); |
| 110 | + } elseif (\is_array($value)) { |
| 111 | + foreach ($value as &$one) { |
| 112 | + $one = (string) $one; |
| 113 | + } |
| 114 | + } else { |
| 115 | + $value = array((string) $value); |
| 116 | + } |
| 117 | + |
| 118 | + $lower = \strtolower($name); |
| 119 | + if (isset($this->headerNamesLowerCase[$lower]) && $this->headerNamesLowerCase[$lower] === (string) $name && $this->headers[$this->headerNamesLowerCase[$lower]] === $value) { |
| 120 | + return $this; |
| 121 | + } |
| 122 | + |
| 123 | + $message = clone $this; |
| 124 | + if (isset($message->headerNamesLowerCase[$lower])) { |
| 125 | + unset($message->headers[$message->headerNamesLowerCase[$lower]]); |
| 126 | + } |
| 127 | + |
| 128 | + $message->headers[$name] = $value; |
| 129 | + $message->headerNamesLowerCase[$lower] = $name; |
| 130 | + |
| 131 | + return $message; |
| 132 | + } |
| 133 | + |
| 134 | + public function withAddedHeader(string $name, $value): MessageInterface |
| 135 | + { |
| 136 | + if ($value === array()) { |
| 137 | + return $this; |
| 138 | + } |
| 139 | + |
| 140 | + return $this->withHeader($name, \array_merge($this->getHeader($name), \is_array($value) ? $value : array($value))); |
| 141 | + } |
| 142 | + |
| 143 | + public function withoutHeader(string $name): MessageInterface |
| 144 | + { |
| 145 | + $lower = \strtolower($name); |
| 146 | + if (!isset($this->headerNamesLowerCase[$lower])) { |
| 147 | + return $this; |
| 148 | + } |
| 149 | + |
| 150 | + $message = clone $this; |
| 151 | + unset($message->headers[$message->headerNamesLowerCase[$lower]], $message->headerNamesLowerCase[$lower]); |
| 152 | + |
| 153 | + return $message; |
| 154 | + } |
| 155 | + |
| 156 | + public function getBody(): StreamInterface |
| 157 | + { |
| 158 | + return $this->body; |
| 159 | + } |
| 160 | + |
| 161 | + public function withBody(StreamInterface $body): MessageInterface |
| 162 | + { |
| 163 | + if ($body === $this->body) { |
| 164 | + return $this; |
| 165 | + } |
| 166 | + |
| 167 | + $message = clone $this; |
| 168 | + $message->body = $body; |
| 169 | + |
| 170 | + return $message; |
| 171 | + } |
| 172 | +} |
0 commit comments