/wp-content/plugins/google-calendar-events/third-party/guzzlehttp/guzzle/src/MessageFormatter.php

https://github.com/livinglab/openlab · PHP · 168 lines · 112 code · 2 blank · 54 comment · 5 complexity · 4b8d1661c457a37ed3a3e2e2454f5f45 MD5 · raw file

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