PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.1
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 18.1, at vendor_prefixed/guzzlehttp/guzzle/src/MessageFormatter.php

157 lines 6.5 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 class MessageFormatter
36 {
37 /**
38 * Apache Common Log Format.
39 * @link http://httpd.apache.org/docs/2.4/logs.html#common
40 * @var string
41 */
42 const CLF = "{hostname} {req_header_User-Agent} - [{date_common_log}] \"{method} {target} HTTP/{version}\" {code} {res_header_Content-Length}";
43 const DEBUG = ">>>>>>>>\n{request}\n<<<<<<<<\n{response}\n--------\n{error}";
44 const SHORT = '[{ts}] "{method} {target} HTTP/{version}" {code}';
45 /** @var string Template used to format log messages */
46 private $template;
47 /**
48 * @param string $template Log message template
49 */
50 public function __construct($template = self::CLF)
51 {
52 $this->template = $template ?: self::CLF;
53 }
54 /**
55 * Returns a formatted message string.
56 *
57 * @param RequestInterface $request Request that was sent
58 * @param ResponseInterface $response Response that was received
59 * @param \Exception $error Exception that was received
60 *
61 * @return string
62 */
63 public function format(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, \YoastSEO_Vendor\Psr\Http\Message\ResponseInterface $response = null, \Exception $error = null)
64 {
65 $cache = [];
66 return \preg_replace_callback('/{\\s*([A-Za-z_\\-\\.0-9]+)\\s*}/', function (array $matches) use($request, $response, $error, &$cache) {
67 if (isset($cache[$matches[1]])) {
68 return $cache[$matches[1]];
69 }
70 $result = '';
71 switch ($matches[1]) {
72 case 'request':
73 $result = \YoastSEO_Vendor\GuzzleHttp\Psr7\str($request);
74 break;
75 case 'response':
76 $result = $response ? \YoastSEO_Vendor\GuzzleHttp\Psr7\str($response) : '';
77 break;
78 case 'req_headers':
79 $result = \trim($request->getMethod() . ' ' . $request->getRequestTarget()) . ' HTTP/' . $request->getProtocolVersion() . "\r\n" . $this->headers($request);
80 break;
81 case 'res_headers':
82 $result = $response ? \sprintf('HTTP/%s %d %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase()) . "\r\n" . $this->headers($response) : 'NULL';
83 break;
84 case 'req_body':
85 $result = $request->getBody();
86 break;
87 case 'res_body':
88 $result = $response ? $response->getBody() : 'NULL';
89 break;
90 case 'ts':
91 case 'date_iso_8601':
92 $result = \gmdate('c');
93 break;
94 case 'date_common_log':
95 $result = \date('d/M/Y:H:i:s O');
96 break;
97 case 'method':
98 $result = $request->getMethod();
99 break;
100 case 'version':
101 $result = $request->getProtocolVersion();
102 break;
103 case 'uri':
104 case 'url':
105 $result = $request->getUri();
106 break;
107 case 'target':
108 $result = $request->getRequestTarget();
109 break;
110 case 'req_version':
111 $result = $request->getProtocolVersion();
112 break;
113 case 'res_version':
114 $result = $response ? $response->getProtocolVersion() : 'NULL';
115 break;
116 case 'host':
117 $result = $request->getHeaderLine('Host');
118 break;
119 case 'hostname':
120 $result = \gethostname();
121 break;
122 case 'code':
123 $result = $response ? $response->getStatusCode() : 'NULL';
124 break;
125 case 'phrase':
126 $result = $response ? $response->getReasonPhrase() : 'NULL';
127 break;
128 case 'error':
129 $result = $error ? $error->getMessage() : 'NULL';
130 break;
131 default:
132 // handle prefixed dynamic headers
133 if (\strpos($matches[1], 'req_header_') === 0) {
134 $result = $request->getHeaderLine(\substr($matches[1], 11));
135 } elseif (\strpos($matches[1], 'res_header_') === 0) {
136 $result = $response ? $response->getHeaderLine(\substr($matches[1], 11)) : 'NULL';
137 }
138 }
139 $cache[$matches[1]] = $result;
140 return $result;
141 }, $this->template);
142 }
143 /**
144 * Get headers from message as string
145 *
146 * @return string
147 */
148 private function headers(\YoastSEO_Vendor\Psr\Http\Message\MessageInterface $message)
149 {
150 $result = '';
151 foreach ($message->getHeaders() as $name => $values) {
152 $result .= $name . ': ' . \implode(', ', $values) . "\r\n";
153 }
154 return \trim($result);
155 }
156 }
157