From 2f87aa2741102641c62b62b1d7e3b184d4b8e17c Mon Sep 17 00:00:00 2001 From: bof09595 Date: Thu, 20 Dec 2018 10:49:57 +0100 Subject: #67 Unsupported HTTP protocol version "2.0" Normalizing protocol version strings to match official values 1.0, 1.1 and 2 --- src/PSR7Client.php | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/PSR7Client.php b/src/PSR7Client.php index 1ce5baed..2da53c26 100644 --- a/src/PSR7Client.php +++ b/src/PSR7Client.php @@ -31,6 +31,13 @@ class PSR7Client /*** @var UploadedFileFactoryInterface */ private $uploadsFactory; + /** @var array Valid values for HTTP protocol version */ + public static $allowedProtocolVersions = [ + '1.0', + '1.1', + '2', + ]; + /** * @param Worker $worker * @param ServerRequestFactoryInterface|null $requestFactory @@ -84,7 +91,7 @@ class PSR7Client parse_str($ctx['rawQuery'], $query); $request = $request - ->withProtocolVersion(substr($ctx['protocol'], 5)) + ->withProtocolVersion(static::normalizeHttpProtocolVersion($ctx['protocol'])) ->withCookieParams($ctx['cookies']) ->withQueryParams($query) ->withUploadedFiles($this->wrapUploads($ctx['uploads'])); @@ -181,4 +188,25 @@ class PSR7Client return $result; } + + /** + * Normalize HTTP protocol version to valid values + * @param string $version + * @return string + */ + public static function normalizeHttpProtocolVersion(string $version): string + { + $v = substr($version, 5); + + if ($v === '2.0') { + $v = '2'; + } + + // Fallback for values outside of valid protocol versions + if (!in_array($v, static::$allowedProtocolVersions, true)) { + return '1.1'; + } + + return $v; + } } -- cgit v1.2.3