PluginProbe
Media Cloud Sync / 1.2.10
Media Cloud Sync v1.2.10
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.2.10, at includes/sdk/s3/GuzzleHttp/Psr7/Message.php

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