| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7; |
| 4 | 5 | |
| 5 | 6 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\MessageInterface; |
| 6 | 7 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; |
| @@ -10,12 +11,10 @@ | ||
| 10 | 11 | /** |
| 11 | 12 | * Returns the string representation of an HTTP message. |
| 12 | 13 | * |
| 13 | 14 | * @param MessageInterface $message Message to convert to a string. |
| 14 | - * | |
| 15 | - * @return string | |
| 16 | 15 | */ |
| 17 | - public static function toString(MessageInterface $message) | |
| 16 | + public static function toString(MessageInterface $message) : string | |
| 18 | 17 | { |
| 19 | 18 | if ($message instanceof RequestInterface) { |
| 20 | 19 | $msg = \trim($message->getMethod() . ' ' . $message->getRequestTarget()) . ' HTTP/' . $message->getProtocolVersion(); |
| 21 | 20 | if (!$message->hasHeader('host')) { |
| @@ -26,9 +25,9 @@ | ||
| 26 | 25 | } else { |
| 27 | 26 | throw new \InvalidArgumentException('Unknown message type'); |
| 28 | 27 | } |
| 29 | 28 | foreach ($message->getHeaders() as $name => $values) { |
| 30 | - if (\strtolower($name) === 'set-cookie') { | |
| 29 | + if (\is_string($name) && \strtolower($name) === 'set-cookie') { | |
| 31 | 30 | foreach ($values as $value) { |
| 32 | 31 | $msg .= "\r\n{$name}: " . $value; |
| 33 | 32 | } |
| 34 | 33 | } else { |
| @@ -43,12 +42,10 @@ | ||
| 43 | 42 | * Will return `null` if the response is not printable. |
| 44 | 43 | * |
| 45 | 44 | * @param MessageInterface $message The message to get the body summary |
| 46 | 45 | * @param int $truncateAt The maximum allowed size of the summary |
| 47 | - * | |
| 48 | - * @return string|null | |
| 49 | 46 | */ |
| 50 | - public static function bodySummary(MessageInterface $message, $truncateAt = 120) | |
| 47 | + public static function bodySummary(MessageInterface $message, int $truncateAt = 120) : ?string | |
| 51 | 48 | { |
| 52 | 49 | $body = $message->getBody(); |
| 53 | 50 | if (!$body->isSeekable() || !$body->isReadable()) { |
| 54 | 51 | return null; |
| @@ -56,8 +53,9 @@ | ||
| 56 | 53 | $size = $body->getSize(); |
| 57 | 54 | if ($size === 0) { |
| 58 | 55 | return null; |
| 59 | 56 | } |
| 57 | + $body->rewind(); | |
| 60 | 58 | $summary = $body->read($truncateAt); |
| 61 | 59 | $body->rewind(); |
| 62 | 60 | if ($size > $truncateAt) { |
| 63 | 61 | $summary .= ' (truncated...)'; |
| @@ -63,9 +61,9 @@ | ||
| 63 | 61 | $summary .= ' (truncated...)'; |
| 64 | 62 | } |
| 65 | 63 | // Matches any printable character, including unicode characters: |
| 66 | 64 | // letters, marks, numbers, punctuation, spacing, and separators. |
| 67 | - if (\preg_match('/[^\\pL\\pM\\pN\\pP\\pS\\pZ\\n\\r\\t]/u', $summary)) { | |
| 65 | + if (\preg_match('/[^\\pL\\pM\\pN\\pP\\pS\\pZ\\n\\r\\t]/u', $summary) !== 0) { | |
| 68 | 66 | return null; |
| 69 | 67 | } |
| 70 | 68 | return $summary; |
| 71 | 69 | } |
| @@ -78,9 +76,9 @@ | ||
| 78 | 76 | * @param MessageInterface $message Message to rewind |
| 79 | 77 | * |
| 80 | 78 | * @throws \RuntimeException |
| 81 | 79 | */ |
| 82 | - public static function rewindBody(MessageInterface $message) | |
| 80 | + public static function rewindBody(MessageInterface $message) : void | |
| 83 | 81 | { |
| 84 | 82 | $body = $message->getBody(); |
| 85 | 83 | if ($body->tell()) { |
| 86 | 84 | $body->rewind(); |
| @@ -93,12 +91,10 @@ | ||
| 93 | 91 | * the message, "headers" key containing an associative array of header |
| 94 | 92 | * array values, and a "body" key containing the body of the message. |
| 95 | 93 | * |
| 96 | 94 | * @param string $message HTTP request or response to parse. |
| 97 | - * | |
| 98 | - * @return array | |
| 99 | 95 | */ |
| 100 | - public static function parseMessage($message) | |
| 96 | + public static function parseMessage(string $message) : array | |
| 101 | 97 | { |
| 102 | 98 | if (!$message) { |
| 103 | 99 | throw new \InvalidArgumentException('Invalid message'); |
| 104 | 100 | } |
| @@ -106,9 +102,9 @@ | ||
| 106 | 102 | $messageParts = \preg_split("/\r?\n\r?\n/", $message, 2); |
| 107 | 103 | if ($messageParts === \false || \count($messageParts) !== 2) { |
| 108 | 104 | throw new \InvalidArgumentException('Invalid message: Missing header delimiter'); |
| 109 | 105 | } |
| 110 | - list($rawHeaders, $body) = $messageParts; | |
| 106 | + [$rawHeaders, $body] = $messageParts; | |
| 111 | 107 | $rawHeaders .= "\r\n"; |
| 112 | 108 | // Put back the delimiter we split previously |
| 113 | 109 | $headerParts = \preg_split("/\r?\n/", $rawHeaders, 2); |
| 114 | 110 | if ($headerParts === \false || \count($headerParts) !== 2) { |
| @@ -113,9 +109,9 @@ | ||
| 113 | 109 | $headerParts = \preg_split("/\r?\n/", $rawHeaders, 2); |
| 114 | 110 | if ($headerParts === \false || \count($headerParts) !== 2) { |
| 115 | 111 | throw new \InvalidArgumentException('Invalid message: Missing status line'); |
| 116 | 112 | } |
| 117 | - list($startLine, $rawHeaders) = $headerParts; | |
| 113 | + [$startLine, $rawHeaders] = $headerParts; | |
| 118 | 114 | if (\preg_match("/(?:^HTTP\\/|^[A-Z]+ \\S+ HTTP\\/)(\\d+(?:\\.\\d+)?)/i", $startLine, $matches) && $matches[1] === '1.0') { |
| 119 | 115 | // Header folding is deprecated for HTTP/1.1, but allowed in HTTP/1.0 |
| 120 | 116 | $rawHeaders = \preg_replace(Rfc7230::HEADER_FOLD_REGEX, ' ', $rawHeaders); |
| 121 | 117 | } |
| @@ -122,9 +118,9 @@ | ||
| 122 | 118 | /** @var array[] $headerLines */ |
| 123 | 119 | $count = \preg_match_all(Rfc7230::HEADER_REGEX, $rawHeaders, $headerLines, \PREG_SET_ORDER); |
| 124 | 120 | // If these aren't the same, then one line didn't match and there's an invalid header. |
| 125 | 121 | if ($count !== \substr_count($rawHeaders, "\n")) { |
| 126 | - // Folding is deprecated, see https://tools.ietf.org/html/rfc7230#section-3.2.4 | |
| 122 | + // Folding is deprecated, see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4 | |
| 127 | 123 | if (\preg_match(Rfc7230::HEADER_FOLD_REGEX, $rawHeaders)) { |
| 128 | 124 | throw new \InvalidArgumentException('Invalid header syntax: Obsolete line folding'); |
| 129 | 125 | } |
| 130 | 126 | throw new \InvalidArgumentException('Invalid header syntax'); |
| @@ -139,14 +135,14 @@ | ||
| 139 | 135 | * Constructs a URI for an HTTP request message. |
| 140 | 136 | * |
| 141 | 137 | * @param string $path Path from the start-line |
| 142 | 138 | * @param array $headers Array of headers (each value an array). |
| 143 | - * | |
| 144 | - * @return string | |
| 145 | 139 | */ |
| 146 | - public static function parseRequestUri($path, array $headers) | |
| 140 | + public static function parseRequestUri(string $path, array $headers) : string | |
| 147 | 141 | { |
| 148 | 142 | $hostKey = \array_filter(\array_keys($headers), function ($k) { |
| 143 | + // Numeric array keys are converted to int by PHP. | |
| 144 | + $k = (string) $k; | |
| 149 | 145 | return \strtolower($k) === 'host'; |
| 150 | 146 | }); |
| 151 | 147 | // If no host is found, then a full URI cannot be constructed. |
| 152 | 148 | if (!$hostKey) { |
| @@ -159,12 +155,10 @@ | ||
| 159 | 155 | /** |
| 160 | 156 | * Parses a request message string into a request object. |
| 161 | 157 | * |
| 162 | 158 | * @param string $message Request message string. |
| 163 | - * | |
| 164 | - * @return Request | |
| 165 | 159 | */ |
| 166 | - public static function parseRequest($message) | |
| 160 | + public static function parseRequest(string $message) : RequestInterface | |
| 167 | 161 | { |
| 168 | 162 | $data = self::parseMessage($message); |
| 169 | 163 | $matches = []; |
| 170 | 164 | if (!\preg_match('/^[\\S]+\\s+([a-zA-Z]+:\\/\\/|\\/).*/', $data['start-line'], $matches)) { |
| @@ -178,20 +172,18 @@ | ||
| 178 | 172 | /** |
| 179 | 173 | * Parses a response message string into a response object. |
| 180 | 174 | * |
| 181 | 175 | * @param string $message Response message string. |
| 182 | - * | |
| 183 | - * @return Response | |
| 184 | 176 | */ |
| 185 | - public static function parseResponse($message) | |
| 177 | + public static function parseResponse(string $message) : ResponseInterface | |
| 186 | 178 | { |
| 187 | 179 | $data = self::parseMessage($message); |
| 188 | - // According to https://tools.ietf.org/html/rfc7230#section-3.1.2 the space | |
| 189 | - // between status-code and reason-phrase is required. But browsers accept | |
| 190 | - // responses without space and reason as well. | |
| 180 | + // According to https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.2 | |
| 181 | + // the space between status-code and reason-phrase is required. But | |
| 182 | + // browsers accept responses without space and reason as well. | |
| 191 | 183 | if (!\preg_match('/^HTTP\\/.* [0-9]{3}( .*|$)/', $data['start-line'])) { |
| 192 | 184 | throw new \InvalidArgumentException('Invalid response string: ' . $data['start-line']); |
| 193 | 185 | } |
| 194 | 186 | $parts = \explode(' ', $data['start-line'], 3); |
| 195 | - return new Response((int) $parts[1], $data['headers'], $data['body'], \explode('/', $parts[0])[1], isset($parts[2]) ? $parts[2] : null); | |
| 187 | + return new Response((int) $parts[1], $data['headers'], $data['body'], \explode('/', $parts[0])[1], $parts[2] ?? null); | |
| 196 | 188 | } |
| 197 | 189 | } |