PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/server/wordpress/wp-content/plugins/wordpress-seo/vendor_prefixed/guzzlehttp/guzzle/src/MessageFormatter.php

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