PluginProbe
Gmail SMTP / trunk
Gmail SMTP vtrunk
1.2.3.21 1.2.3.20 trunk 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.9 1.2.0 1.2.3.14 1.2.3.15 1.2.3.16 1.2.3.18 1.2.3.5
gmail-smtp / google-api-php-client / vendor / guzzlehttp / guzzle / src / Handler / HeaderProcessor.php

HeaderProcessor.php in Gmail SMTP trunk, at google-api-php-client/vendor/guzzlehttp/guzzle/src/Handler/HeaderProcessor.php

78 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace GuzzleHttp\Handler;
4
5 use GuzzleHttp\Utils;
6
7 /**
8 * @internal
9 */
10 final class HeaderProcessor
11 {
12 /**
13 * Returns the HTTP version, status code, reason phrase, and headers.
14 *
15 * @param string[] $headers
16 *
17 * @return array{0:string, 1:int, 2:?string, 3:array}
18 *
19 * @throws \RuntimeException
20 */
21 public static function parseHeaders(array $headers): array
22 {
23 if ($headers === []) {
24 throw new \RuntimeException('Expected a non-empty array of header data');
25 }
26
27 $headers = self::getLastHeaderBlock(\array_values($headers));
28
29 $statusLine = \array_shift($headers);
30 if ($statusLine === null) {
31 throw new \RuntimeException('Expected a non-empty array of header data');
32 }
33
34 $parts = \explode(' ', $statusLine, 3);
35 $version = \explode('/', $parts[0])[1] ?? null;
36
37 if ($version === null) {
38 throw new \RuntimeException('HTTP version missing from header data');
39 }
40
41 $status = $parts[1] ?? null;
42
43 if ($status === null) {
44 throw new \RuntimeException('HTTP status code missing from header data');
45 }
46
47 if (!\preg_match('/^\d{3}$/', $status)) {
48 throw new \RuntimeException('HTTP status code is invalid');
49 }
50
51 foreach ($headers as $header) {
52 if (\strpos($header, ':') === false) {
53 throw new \RuntimeException('HTTP header line is invalid');
54 }
55 }
56
57 return [$version, (int) $status, $parts[2] ?? null, Utils::headersFromLines($headers)];
58 }
59
60 /**
61 * @param non-empty-list<string> $headers
62 *
63 * @return list<string>
64 */
65 private static function getLastHeaderBlock(array $headers): array
66 {
67 $lastStatusLine = 0;
68
69 foreach ($headers as $index => $line) {
70 if (\preg_match('/^HTTP\/\S+\s+/i', $line)) {
71 $lastStatusLine = $index;
72 }
73 }
74
75 return \array_slice($headers, $lastStatusLine);
76 }
77 }
78