HeaderProcessor.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace GuzzleHttp\Handler;
  3. use GuzzleHttp\Utils;
  4. /**
  5. * @internal
  6. */
  7. final class HeaderProcessor
  8. {
  9. /**
  10. * Returns the HTTP version, status code, reason phrase, and headers.
  11. *
  12. * @param string[] $headers
  13. *
  14. * @return array{0:string, 1:int, 2:?string, 3:array}
  15. *
  16. * @throws \RuntimeException
  17. */
  18. public static function parseHeaders(array $headers): array
  19. {
  20. if ($headers === []) {
  21. throw new \RuntimeException('Expected a non-empty array of header data');
  22. }
  23. $parts = \explode(' ', \array_shift($headers), 3);
  24. $version = \explode('/', $parts[0])[1] ?? null;
  25. if ($version === null) {
  26. throw new \RuntimeException('HTTP version missing from header data');
  27. }
  28. $status = $parts[1] ?? null;
  29. if ($status === null) {
  30. throw new \RuntimeException('HTTP status code missing from header data');
  31. }
  32. return [$version, (int) $status, $parts[2] ?? null, Utils::headersFromLines($headers)];
  33. }
  34. }