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 / guzzle / src / MessageFormatter.php

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

169 lines 7.1 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 /** @var string */
71 return \preg_replace_callback('/{\\s*([A-Za-z_\\-\\.0-9]+)\\s*}/', function (array $matches) use($request, $response, $error, &$cache) {
72 if (isset($cache[$matches[1]])) {
73 return $cache[$matches[1]];
74 }
75 $result = '';
76 switch ($matches[1]) {
77 case 'request':
78 $result = \YoastSEO_Vendor\GuzzleHttp\Psr7\Message::toString($request);
79 break;
80 case 'response':
81 $result = $response ? \YoastSEO_Vendor\GuzzleHttp\Psr7\Message::toString($response) : '';
82 break;
83 case 'req_headers':
84 $result = \trim($request->getMethod() . ' ' . $request->getRequestTarget()) . ' HTTP/' . $request->getProtocolVersion() . "\r\n" . $this->headers($request);
85 break;
86 case 'res_headers':
87 $result = $response ? \sprintf('HTTP/%s %d %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase()) . "\r\n" . $this->headers($response) : 'NULL';
88 break;
89 case 'req_body':
90 $result = $request->getBody()->__toString();
91 break;
92 case 'res_body':
93 if (!$response instanceof \YoastSEO_Vendor\Psr\Http\Message\ResponseInterface) {
94 $result = 'NULL';
95 break;
96 }
97 $body = $response->getBody();
98 if (!$body->isSeekable()) {
99 $result = 'RESPONSE_NOT_LOGGEABLE';
100 break;
101 }
102 $result = $response->getBody()->__toString();
103 break;
104 case 'ts':
105 case 'date_iso_8601':
106 $result = \gmdate('c');
107 break;
108 case 'date_common_log':
109 $result = \date('d/M/Y:H:i:s O');
110 break;
111 case 'method':
112 $result = $request->getMethod();
113 break;
114 case 'version':
115 $result = $request->getProtocolVersion();
116 break;
117 case 'uri':
118 case 'url':
119 $result = $request->getUri()->__toString();
120 break;
121 case 'target':
122 $result = $request->getRequestTarget();
123 break;
124 case 'req_version':
125 $result = $request->getProtocolVersion();
126 break;
127 case 'res_version':
128 $result = $response ? $response->getProtocolVersion() : 'NULL';
129 break;
130 case 'host':
131 $result = $request->getHeaderLine('Host');
132 break;
133 case 'hostname':
134 $result = \gethostname();
135 break;
136 case 'code':
137 $result = $response ? $response->getStatusCode() : 'NULL';
138 break;
139 case 'phrase':
140 $result = $response ? $response->getReasonPhrase() : 'NULL';
141 break;
142 case 'error':
143 $result = $error ? $error->getMessage() : 'NULL';
144 break;
145 default:
146 // handle prefixed dynamic headers
147 if (\strpos($matches[1], 'req_header_') === 0) {
148 $result = $request->getHeaderLine(\substr($matches[1], 11));
149 } elseif (\strpos($matches[1], 'res_header_') === 0) {
150 $result = $response ? $response->getHeaderLine(\substr($matches[1], 11)) : 'NULL';
151 }
152 }
153 $cache[$matches[1]] = $result;
154 return $result;
155 }, $this->template);
156 }
157 /**
158 * Get headers from message as string
159 */
160 private function headers(\YoastSEO_Vendor\Psr\Http\Message\MessageInterface $message) : string
161 {
162 $result = '';
163 foreach ($message->getHeaders() as $name => $values) {
164 $result .= $name . ': ' . \implode(', ', $values) . "\r\n";
165 }
166 return \trim($result);
167 }
168 }
169