PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Psr7 / Message.php

Message.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/GuzzleHttp/Psr7/Message.php

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