PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
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 / guzzle / src / MessageFormatter.php

MessageFormatter.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at vendor_prefixed/guzzlehttp/guzzle/src/MessageFormatter.php

172 lines 7.3 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;
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 /**
9 * Formats log messages using variable substitutions for requests, responses,
10 * and other transactional data.
11 *
12 * The following variable substitutions are supported:
13 *
14 * - {request}: Full HTTP request message
15 * - {response}: Full HTTP response message
16 * - {ts}: ISO 8601 date in GMT
17 * - {date_iso_8601} ISO 8601 date in GMT
18 * - {date_common_log} Apache common log date using the configured timezone.
19 * - {host}: Host of the request
20 * - {method}: Method of the request
21 * - {uri}: URI of the request
22 * - {version}: Protocol version
23 * - {target}: Request target of the request (path + query + fragment)
24 * - {hostname}: Hostname of the machine that sent the request
25 * - {code}: Status code of the response (if available)
26 * - {phrase}: Reason phrase of the response (if available)
27 * - {error}: Any error messages (if available)
28 * - {req_header_*}: Replace `*` with the lowercased name of a request header to add to the message
29 * - {res_header_*}: Replace `*` with the lowercased name of a response header to add to the message
30 * - {req_headers}: Request headers
31 * - {res_headers}: Response headers
32 * - {req_body}: Request body
33 * - {res_body}: Response body
34 *
35 * @final
36 */
37 class MessageFormatter implements \YoastSEO_Vendor\GuzzleHttp\MessageFormatterInterface
38 {
39 /**
40 * Apache Common Log Format.
41 *
42 * @see https://httpd.apache.org/docs/2.4/logs.html#common
43 *
44 * @var string
45 */
46 public const CLF = '{hostname} {req_header_User-Agent} - [{date_common_log}] "{method} {target} HTTP/{version}" {code} {res_header_Content-Length}';
47 public const DEBUG = ">>>>>>>>\n{request}\n<<<<<<<<\n{response}\n--------\n{error}";
48 public const SHORT = '[{ts}] "{method} {target} HTTP/{version}" {code}';
49 /**
50 * @var string Template used to format log messages
51 */
52 private $template;
53 /**
54 * @param string $template Log message template
55 */
56 public function __construct(?string $template = self::CLF)
57 {
58 $this->template = $template ?: self::CLF;
59 }
60 /**
61 * Returns a formatted message string.
62 *
63 * @param RequestInterface $request Request that was sent
64 * @param ResponseInterface|null $response Response that was received
65 * @param \Throwable|null $error Exception that was received
66 */
67 public function format(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, ?\YoastSEO_Vendor\Psr\Http\Message\ResponseInterface $response = null, ?\Throwable $error = null) : string
68 {
69 $cache = [];
70 $result = \preg_replace_callback('/{\\s*([A-Za-z_\\-\\.0-9]+)\\s*}/', function (array $matches) use($request, $response, $error, &$cache) {
71 if (isset($cache[$matches[1]])) {
72 return $cache[$matches[1]];
73 }
74 $result = '';
75 switch ($matches[1]) {
76 case 'request':
77 $result = \YoastSEO_Vendor\GuzzleHttp\Psr7\Message::toString($request);
78 break;
79 case 'response':
80 $result = $response ? \YoastSEO_Vendor\GuzzleHttp\Psr7\Message::toString($response) : '';
81 break;
82 case 'req_headers':
83 $result = \trim($request->getMethod() . ' ' . $request->getRequestTarget(), " \n\r\t\x00\v") . ' HTTP/' . $request->getProtocolVersion() . "\r\n" . $this->headers($request);
84 break;
85 case 'res_headers':
86 $result = $response ? \sprintf('HTTP/%s %d %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase()) . "\r\n" . $this->headers($response) : 'NULL';
87 break;
88 case 'req_body':
89 $result = $request->getBody()->__toString();
90 break;
91 case 'res_body':
92 if (!$response instanceof \YoastSEO_Vendor\Psr\Http\Message\ResponseInterface) {
93 $result = 'NULL';
94 break;
95 }
96 $body = $response->getBody();
97 if (!$body->isSeekable()) {
98 $result = 'RESPONSE_NOT_LOGGEABLE';
99 break;
100 }
101 $result = $response->getBody()->__toString();
102 break;
103 case 'ts':
104 case 'date_iso_8601':
105 $result = \gmdate('c');
106 break;
107 case 'date_common_log':
108 $result = \date('d/M/Y:H:i:s O');
109 break;
110 case 'method':
111 $result = $request->getMethod();
112 break;
113 case 'version':
114 $result = $request->getProtocolVersion();
115 break;
116 case 'uri':
117 case 'url':
118 $result = $request->getUri()->__toString();
119 break;
120 case 'target':
121 $result = $request->getRequestTarget();
122 break;
123 case 'req_version':
124 $result = $request->getProtocolVersion();
125 break;
126 case 'res_version':
127 $result = $response ? $response->getProtocolVersion() : 'NULL';
128 break;
129 case 'host':
130 $result = $request->getHeaderLine('Host');
131 break;
132 case 'hostname':
133 $result = \gethostname();
134 break;
135 case 'code':
136 $result = $response ? $response->getStatusCode() : 'NULL';
137 break;
138 case 'phrase':
139 $result = $response ? $response->getReasonPhrase() : 'NULL';
140 break;
141 case 'error':
142 $result = $error ? $error->getMessage() : 'NULL';
143 break;
144 default:
145 // handle prefixed dynamic headers
146 if (\strpos($matches[1], 'req_header_') === 0) {
147 $result = $request->getHeaderLine(\substr($matches[1], 11));
148 } elseif (\strpos($matches[1], 'res_header_') === 0) {
149 $result = $response ? $response->getHeaderLine(\substr($matches[1], 11)) : 'NULL';
150 }
151 }
152 $cache[$matches[1]] = $result;
153 return $result;
154 }, $this->template);
155 if ($result === null) {
156 throw new \RuntimeException('Unable to format message: ' . \preg_last_error_msg());
157 }
158 return $result;
159 }
160 /**
161 * Get headers from message as string
162 */
163 private function headers(\YoastSEO_Vendor\Psr\Http\Message\MessageInterface $message) : string
164 {
165 $result = '';
166 foreach ($message->getHeaders() as $name => $values) {
167 $result .= $name . ': ' . \implode(', ', $values) . "\r\n";
168 }
169 return \trim($result, " \n\r\t\x00\v");
170 }
171 }
172