| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\GuzzleHttp\Psr7; |
| 5 |
|
| 6 |
use WCPOS\Vendor\Psr\Http\Message\MessageInterface; |
| 7 |
use WCPOS\Vendor\Psr\Http\Message\RequestInterface; |
| 8 |
use WCPOS\Vendor\Psr\Http\Message\ResponseInterface; |
| 9 |
use WCPOS\Vendor\Psr\Http\Message\UriInterface; |
| 10 |
final class Message |
| 11 |
{ |
| 12 |
private const DEFAULT_BODY_SUMMARY_TRUNCATE_AT = 120; |
| 13 |
private function __construct() |
| 14 |
{ |
| 15 |
} |
| 16 |
/** |
| 17 |
* Returns the string representation of an HTTP message. |
| 18 |
* |
| 19 |
* @param MessageInterface $message Message to convert to a string. |
| 20 |
*/ |
| 21 |
public static function toString(MessageInterface $message) : string |
| 22 |
{ |
| 23 |
if ($message instanceof RequestInterface) { |
| 24 |
$msg = \trim($message->getMethod() . ' ' . $message->getRequestTarget(), " \n\r\t\x00\v") . ' HTTP/' . $message->getProtocolVersion(); |
| 25 |
if (!$message->hasHeader('host')) { |
| 26 |
$msg .= "\r\nHost: " . self::hostHeaderFromUri($message->getUri()); |
| 27 |
} |
| 28 |
} elseif ($message instanceof ResponseInterface) { |
| 29 |
$msg = 'HTTP/' . $message->getProtocolVersion() . ' ' . $message->getStatusCode() . ' ' . $message->getReasonPhrase(); |
| 30 |
} else { |
| 31 |
throw new \InvalidArgumentException('Unknown message type'); |
| 32 |
} |
| 33 |
foreach ($message->getHeaders() as $name => $values) { |
| 34 |
if (\is_string($name) && Utils::asciiToLower($name) === 'set-cookie') { |
| 35 |
foreach ($values as $value) { |
| 36 |
$msg .= "\r\n{$name}: " . $value; |
| 37 |
} |
| 38 |
} else { |
| 39 |
$msg .= "\r\n{$name}: " . \implode(', ', $values); |
| 40 |
} |
| 41 |
} |
| 42 |
return "{$msg}\r\n\r\n" . $message->getBody(); |
| 43 |
} |
| 44 |
private static function hostHeaderFromUri(UriInterface $uri) : string |
| 45 |
{ |
| 46 |
$host = $uri->getHost(); |
| 47 |
if ($host === '') { |
| 48 |
return ''; |
| 49 |
} |
| 50 |
Uri::assertValidHost($host); |
| 51 |
if (($port = $uri->getPort()) !== null) { |
| 52 |
$host .= ':' . $port; |
| 53 |
} |
| 54 |
return $host; |
| 55 |
} |
| 56 |
/** |
| 57 |
* Get a short summary of the message body. |
| 58 |
* |
| 59 |
* Will return `null` if the response is not printable. |
| 60 |
* |
| 61 |
* Reads seekable bodies from the beginning and restores the original cursor |
| 62 |
* position before returning. Pass `null` for `$truncateAt` to use the |
| 63 |
* default summary length. |
| 64 |
* |
| 65 |
* @param MessageInterface $message The message to get the body summary |
| 66 |
* @param int|null $truncateAt Maximum allowed size of the summary |
| 67 |
*/ |
| 68 |
public static function bodySummary(MessageInterface $message, ?int $truncateAt = null) : ?string |
| 69 |
{ |
| 70 |
$truncateAt ??= self::DEFAULT_BODY_SUMMARY_TRUNCATE_AT; |
| 71 |
$body = $message->getBody(); |
| 72 |
if (!$body->isSeekable() || !$body->isReadable()) { |
| 73 |
return null; |
| 74 |
} |
| 75 |
$size = $body->getSize(); |
| 76 |
if ($size === 0) { |
| 77 |
return null; |
| 78 |
} |
| 79 |
$position = $body->tell(); |
| 80 |
try { |
| 81 |
$body->rewind(); |
| 82 |
$summary = $body->read($truncateAt); |
| 83 |
if ($size > $truncateAt) { |
| 84 |
if (\preg_match('//u', $summary) !== 1) { |
| 85 |
$summary = self::trimTrailingIncompleteUtf8Character($summary, $body->read(3)); |
| 86 |
} |
| 87 |
$summary .= ' (truncated...)'; |
| 88 |
} |
| 89 |
} finally { |
| 90 |
$body->seek($position); |
| 91 |
} |
| 92 |
// Matches any printable character, including unicode characters: |
| 93 |
// letters, marks, numbers, punctuation, spacing, and separators. |
| 94 |
if (\preg_match('/[^\\pL\\pM\\pN\\pP\\pS\\pZ\\n\\r\\t]/u', $summary) !== 0) { |
| 95 |
return null; |
| 96 |
} |
| 97 |
return $summary; |
| 98 |
} |
| 99 |
/** |
| 100 |
* Trims a partial UTF-8 character from the end of a truncated string. |
| 101 |
*/ |
| 102 |
private static function trimTrailingIncompleteUtf8Character(string $summary, string $lookahead) : string |
| 103 |
{ |
| 104 |
$length = \strlen($summary); |
| 105 |
if ($length === 0) { |
| 106 |
return $summary; |
| 107 |
} |
| 108 |
$start = $length - 1; |
| 109 |
while ($start >= 0) { |
| 110 |
$byte = \ord($summary[$start]); |
| 111 |
if ($byte < 0x80 || $byte > 0xbf) { |
| 112 |
break; |
| 113 |
} |
| 114 |
--$start; |
| 115 |
} |
| 116 |
if ($start < 0) { |
| 117 |
return $summary; |
| 118 |
} |
| 119 |
$lead = \ord($summary[$start]); |
| 120 |
if ($lead >= 0xc2 && $lead <= 0xdf) { |
| 121 |
$expectedLength = 2; |
| 122 |
} elseif ($lead >= 0xe0 && $lead <= 0xef) { |
| 123 |
$expectedLength = 3; |
| 124 |
} elseif ($lead >= 0xf0 && $lead <= 0xf4) { |
| 125 |
$expectedLength = 4; |
| 126 |
} else { |
| 127 |
return $summary; |
| 128 |
} |
| 129 |
$availableLength = $length - $start; |
| 130 |
if ($availableLength >= $expectedLength) { |
| 131 |
return $summary; |
| 132 |
} |
| 133 |
$sequence = \substr($summary, $start) . \substr($lookahead, 0, $expectedLength - $availableLength); |
| 134 |
if (\strlen($sequence) !== $expectedLength || \preg_match('//u', $sequence) !== 1) { |
| 135 |
return $summary; |
| 136 |
} |
| 137 |
return \substr($summary, 0, $start); |
| 138 |
} |
| 139 |
/** |
| 140 |
* Attempts to rewind a message body and throws an exception on failure. |
| 141 |
* |
| 142 |
* The body of the message will only be rewound if a call to `tell()` |
| 143 |
* returns a value other than `0`. |
| 144 |
* |
| 145 |
* @param MessageInterface $message Message to rewind |
| 146 |
* |
| 147 |
* @throws \RuntimeException |
| 148 |
*/ |
| 149 |
public static function rewindBody(MessageInterface $message) : void |
| 150 |
{ |
| 151 |
$body = $message->getBody(); |
| 152 |
if ($body->tell()) { |
| 153 |
$body->rewind(); |
| 154 |
} |
| 155 |
} |
| 156 |
/** |
| 157 |
* Parses an HTTP message into an associative array. |
| 158 |
* |
| 159 |
* The array contains the `start-line` key containing the start line of the |
| 160 |
* message, `headers` key containing an associative array of header array |
| 161 |
* values, and a `body` key containing the body of the message. |
| 162 |
* |
| 163 |
* @param string $message HTTP request or response to parse. |
| 164 |
*/ |
| 165 |
public static function parseMessage(string $message) : array |
| 166 |
{ |
| 167 |
return MessageParser::parseMessage($message); |
| 168 |
} |
| 169 |
/** |
| 170 |
* Constructs a URI for an HTTP request message. |
| 171 |
* |
| 172 |
* The URI is composed from the start-line path and the `Host` header, using |
| 173 |
* `https` when the host's port is `443` and `http` otherwise. Without a |
| 174 |
* `Host` header, only the path is returned, with extra leading slashes |
| 175 |
* collapsed so an origin-form target cannot be parsed as a network-path |
| 176 |
* reference with its own authority. An `InvalidArgumentException` is thrown |
| 177 |
* when the `Host` header is invalid. |
| 178 |
* |
| 179 |
* @param string $path Path from the start-line |
| 180 |
* @param array $headers Array of headers (each value an array). |
| 181 |
*/ |
| 182 |
public static function parseRequestUri(string $path, array $headers) : string |
| 183 |
{ |
| 184 |
return MessageParser::parseRequestUri($path, $headers); |
| 185 |
} |
| 186 |
/** |
| 187 |
* Parses a request message string into a request object. |
| 188 |
* |
| 189 |
* The request-target must be in origin form, absolute form (without a |
| 190 |
* userinfo component), authority form (`CONNECT`), or asterisk form |
| 191 |
* (`OPTIONS`), and any `Host` header must be a single valid value; |
| 192 |
* otherwise an `InvalidArgumentException` is thrown. Non-origin-form |
| 193 |
* targets are preserved on the returned request via `withRequestTarget()`. |
| 194 |
* |
| 195 |
* @param string $message Request message string. |
| 196 |
*/ |
| 197 |
public static function parseRequest(string $message) : RequestInterface |
| 198 |
{ |
| 199 |
return MessageParser::parseRequest($message); |
| 200 |
} |
| 201 |
/** |
| 202 |
* Parses a response message string into a response object. |
| 203 |
* |
| 204 |
* @param string $message Response message string. |
| 205 |
*/ |
| 206 |
public static function parseResponse(string $message) : ResponseInterface |
| 207 |
{ |
| 208 |
return MessageParser::parseResponse($message); |
| 209 |
} |
| 210 |
} |
| 211 |
|