PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.9
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 27.9, at vendor_prefixed/guzzlehttp/psr7/src/Message.php

190 lines 8.4 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 YoastSEO_Vendor\GuzzleHttp\Psr7;
5
6 use YoastSEO_Vendor\Psr\Http\Message\MessageInterface;
7 use YoastSEO_Vendor\Psr\Http\Message\RequestInterface;
8 use YoastSEO_Vendor\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(\YoastSEO_Vendor\Psr\Http\Message\MessageInterface $message) : string
17 {
18 if ($message instanceof \YoastSEO_Vendor\Psr\Http\Message\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 \YoastSEO_Vendor\Psr\Http\Message\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(\YoastSEO_Vendor\Psr\Http\Message\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(\YoastSEO_Vendor\Psr\Http\Message\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(\YoastSEO_Vendor\GuzzleHttp\Psr7\Rfc7230::HEADER_FOLD_REGEX, ' ', $rawHeaders);
117 }
118 /** @var array[] $headerLines */
119 $count = \preg_match_all(\YoastSEO_Vendor\GuzzleHttp\Psr7\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(\YoastSEO_Vendor\GuzzleHttp\Psr7\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) : \YoastSEO_Vendor\Psr\Http\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 \YoastSEO_Vendor\GuzzleHttp\Psr7\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) : \YoastSEO_Vendor\Psr\Http\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 \YoastSEO_Vendor\GuzzleHttp\Psr7\Response((int) $parts[1], $data['headers'], $data['body'], \explode('/', $parts[0])[1], $parts[2] ?? null);
188 }
189 }
190