PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/server/wordpress/wp-content/plugins/wordpress-seo/vendor_prefixed/guzzlehttp/psr7/src/Message.php

https://gitlab.com/suporte.spturis/carnaval2015.spturis.com.br
PHP | 197 lines | 124 code | 2 blank | 71 comment | 23 complexity | 5c9e6fcc8e72a7feafa67c413529fe9d MD5 | raw file
  1. <?php
  2. namespace YoastSEO_Vendor\GuzzleHttp\Psr7;
  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. final class Message
  7. {
  8. /**
  9. * Returns the string representation of an HTTP message.
  10. *
  11. * @param MessageInterface $message Message to convert to a string.
  12. *
  13. * @return string
  14. */
  15. public static function toString(\YoastSEO_Vendor\Psr\Http\Message\MessageInterface $message)
  16. {
  17. if ($message instanceof \YoastSEO_Vendor\Psr\Http\Message\RequestInterface) {
  18. $msg = \trim($message->getMethod() . ' ' . $message->getRequestTarget()) . ' HTTP/' . $message->getProtocolVersion();
  19. if (!$message->hasHeader('host')) {
  20. $msg .= "\r\nHost: " . $message->getUri()->getHost();
  21. }
  22. } elseif ($message instanceof \YoastSEO_Vendor\Psr\Http\Message\ResponseInterface) {
  23. $msg = 'HTTP/' . $message->getProtocolVersion() . ' ' . $message->getStatusCode() . ' ' . $message->getReasonPhrase();
  24. } else {
  25. throw new \InvalidArgumentException('Unknown message type');
  26. }
  27. foreach ($message->getHeaders() as $name => $values) {
  28. if (\strtolower($name) === 'set-cookie') {
  29. foreach ($values as $value) {
  30. $msg .= "\r\n{$name}: " . $value;
  31. }
  32. } else {
  33. $msg .= "\r\n{$name}: " . \implode(', ', $values);
  34. }
  35. }
  36. return "{$msg}\r\n\r\n" . $message->getBody();
  37. }
  38. /**
  39. * Get a short summary of the message body.
  40. *
  41. * Will return `null` if the response is not printable.
  42. *
  43. * @param MessageInterface $message The message to get the body summary
  44. * @param int $truncateAt The maximum allowed size of the summary
  45. *
  46. * @return string|null
  47. */
  48. public static function bodySummary(\YoastSEO_Vendor\Psr\Http\Message\MessageInterface $message, $truncateAt = 120)
  49. {
  50. $body = $message->getBody();
  51. if (!$body->isSeekable() || !$body->isReadable()) {
  52. return null;
  53. }
  54. $size = $body->getSize();
  55. if ($size === 0) {
  56. return null;
  57. }
  58. $summary = $body->read($truncateAt);
  59. $body->rewind();
  60. if ($size > $truncateAt) {
  61. $summary .= ' (truncated...)';
  62. }
  63. // Matches any printable character, including unicode characters:
  64. // letters, marks, numbers, punctuation, spacing, and separators.
  65. if (\preg_match('/[^\\pL\\pM\\pN\\pP\\pS\\pZ\\n\\r\\t]/u', $summary)) {
  66. return null;
  67. }
  68. return $summary;
  69. }
  70. /**
  71. * Attempts to rewind a message body and throws an exception on failure.
  72. *
  73. * The body of the message will only be rewound if a call to `tell()`
  74. * returns a value other than `0`.
  75. *
  76. * @param MessageInterface $message Message to rewind
  77. *
  78. * @throws \RuntimeException
  79. */
  80. public static function rewindBody(\YoastSEO_Vendor\Psr\Http\Message\MessageInterface $message)
  81. {
  82. $body = $message->getBody();
  83. if ($body->tell()) {
  84. $body->rewind();
  85. }
  86. }
  87. /**
  88. * Parses an HTTP message into an associative array.
  89. *
  90. * The array contains the "start-line" key containing the start line of
  91. * the message, "headers" key containing an associative array of header
  92. * array values, and a "body" key containing the body of the message.
  93. *
  94. * @param string $message HTTP request or response to parse.
  95. *
  96. * @return array
  97. */
  98. public static function parseMessage($message)
  99. {
  100. if (!$message) {
  101. throw new \InvalidArgumentException('Invalid message');
  102. }
  103. $message = \ltrim($message, "\r\n");
  104. $messageParts = \preg_split("/\r?\n\r?\n/", $message, 2);
  105. if ($messageParts === \false || \count($messageParts) !== 2) {
  106. throw new \InvalidArgumentException('Invalid message: Missing header delimiter');
  107. }
  108. list($rawHeaders, $body) = $messageParts;
  109. $rawHeaders .= "\r\n";
  110. // Put back the delimiter we split previously
  111. $headerParts = \preg_split("/\r?\n/", $rawHeaders, 2);
  112. if ($headerParts === \false || \count($headerParts) !== 2) {
  113. throw new \InvalidArgumentException('Invalid message: Missing status line');
  114. }
  115. list($startLine, $rawHeaders) = $headerParts;
  116. if (\preg_match("/(?:^HTTP\\/|^[A-Z]+ \\S+ HTTP\\/)(\\d+(?:\\.\\d+)?)/i", $startLine, $matches) && $matches[1] === '1.0') {
  117. // Header folding is deprecated for HTTP/1.1, but allowed in HTTP/1.0
  118. $rawHeaders = \preg_replace(\YoastSEO_Vendor\GuzzleHttp\Psr7\Rfc7230::HEADER_FOLD_REGEX, ' ', $rawHeaders);
  119. }
  120. /** @var array[] $headerLines */
  121. $count = \preg_match_all(\YoastSEO_Vendor\GuzzleHttp\Psr7\Rfc7230::HEADER_REGEX, $rawHeaders, $headerLines, \PREG_SET_ORDER);
  122. // If these aren't the same, then one line didn't match and there's an invalid header.
  123. if ($count !== \substr_count($rawHeaders, "\n")) {
  124. // Folding is deprecated, see https://tools.ietf.org/html/rfc7230#section-3.2.4
  125. if (\preg_match(\YoastSEO_Vendor\GuzzleHttp\Psr7\Rfc7230::HEADER_FOLD_REGEX, $rawHeaders)) {
  126. throw new \InvalidArgumentException('Invalid header syntax: Obsolete line folding');
  127. }
  128. throw new \InvalidArgumentException('Invalid header syntax');
  129. }
  130. $headers = [];
  131. foreach ($headerLines as $headerLine) {
  132. $headers[$headerLine[1]][] = $headerLine[2];
  133. }
  134. return ['start-line' => $startLine, 'headers' => $headers, 'body' => $body];
  135. }
  136. /**
  137. * Constructs a URI for an HTTP request message.
  138. *
  139. * @param string $path Path from the start-line
  140. * @param array $headers Array of headers (each value an array).
  141. *
  142. * @return string
  143. */
  144. public static function parseRequestUri($path, array $headers)
  145. {
  146. $hostKey = \array_filter(\array_keys($headers), function ($k) {
  147. return \strtolower($k) === 'host';
  148. });
  149. // If no host is found, then a full URI cannot be constructed.
  150. if (!$hostKey) {
  151. return $path;
  152. }
  153. $host = $headers[\reset($hostKey)][0];
  154. $scheme = \substr($host, -4) === ':443' ? 'https' : 'http';
  155. return $scheme . '://' . $host . '/' . \ltrim($path, '/');
  156. }
  157. /**
  158. * Parses a request message string into a request object.
  159. *
  160. * @param string $message Request message string.
  161. *
  162. * @return Request
  163. */
  164. public static function parseRequest($message)
  165. {
  166. $data = self::parseMessage($message);
  167. $matches = [];
  168. if (!\preg_match('/^[\\S]+\\s+([a-zA-Z]+:\\/\\/|\\/).*/', $data['start-line'], $matches)) {
  169. throw new \InvalidArgumentException('Invalid request string');
  170. }
  171. $parts = \explode(' ', $data['start-line'], 3);
  172. $version = isset($parts[2]) ? \explode('/', $parts[2])[1] : '1.1';
  173. $request = new \YoastSEO_Vendor\GuzzleHttp\Psr7\Request($parts[0], $matches[1] === '/' ? self::parseRequestUri($parts[1], $data['headers']) : $parts[1], $data['headers'], $data['body'], $version);
  174. return $matches[1] === '/' ? $request : $request->withRequestTarget($parts[1]);
  175. }
  176. /**
  177. * Parses a response message string into a response object.
  178. *
  179. * @param string $message Response message string.
  180. *
  181. * @return Response
  182. */
  183. public static function parseResponse($message)
  184. {
  185. $data = self::parseMessage($message);
  186. // According to https://tools.ietf.org/html/rfc7230#section-3.1.2 the space
  187. // between status-code and reason-phrase is required. But browsers accept
  188. // responses without space and reason as well.
  189. if (!\preg_match('/^HTTP\\/.* [0-9]{3}( .*|$)/', $data['start-line'])) {
  190. throw new \InvalidArgumentException('Invalid response string: ' . $data['start-line']);
  191. }
  192. $parts = \explode(' ', $data['start-line'], 3);
  193. return new \YoastSEO_Vendor\GuzzleHttp\Psr7\Response((int) $parts[1], $data['headers'], $data['body'], \explode('/', $parts[0])[1], isset($parts[2]) ? $parts[2] : null);
  194. }
  195. }