PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.4
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.4
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / psr7 / src / Message.php

Message.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.4, at vendor_prefixed/guzzlehttp/psr7/src/Message.php

198 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YoastSEO_Vendor\GuzzleHttp\Psr7;
4
5 use YoastSEO_Vendor\Psr\Http\Message\MessageInterface;
6 use YoastSEO_Vendor\Psr\Http\Message\RequestInterface;
7 use YoastSEO_Vendor\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(\YoastSEO_Vendor\Psr\Http\Message\MessageInterface $message)
18 {
19 if ($message instanceof \YoastSEO_Vendor\Psr\Http\Message\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 \YoastSEO_Vendor\Psr\Http\Message\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(\YoastSEO_Vendor\Psr\Http\Message\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(\YoastSEO_Vendor\Psr\Http\Message\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(\YoastSEO_Vendor\GuzzleHttp\Psr7\Rfc7230::HEADER_FOLD_REGEX, ' ', $rawHeaders);
121 }
122 /** @var array[] $headerLines */
123 $count = \preg_match_all(\YoastSEO_Vendor\GuzzleHttp\Psr7\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(\YoastSEO_Vendor\GuzzleHttp\Psr7\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 \YoastSEO_Vendor\GuzzleHttp\Psr7\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 \YoastSEO_Vendor\GuzzleHttp\Psr7\Response((int) $parts[1], $data['headers'], $data['body'], \explode('/', $parts[0])[1], isset($parts[2]) ? $parts[2] : null);
196 }
197 }
198