From 9561c97b4a300ceae9ae9849daefea27f8ecc3de Mon Sep 17 00:00:00 2001 From: Fabian Blechschmidt Date: Thu, 6 Aug 2020 23:27:37 +0200 Subject: [PATCH 1/4] Make Curl Client HTTP/2 compatible - I'm not sure wether HTTP/2 allows Status Codes (line 473) - this is PHP 7.1 code (not sure what openmage is expecting), list shortcut and return void --- lib/Mage/HTTP/Client/Curl.php | 56 ++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/lib/Mage/HTTP/Client/Curl.php b/lib/Mage/HTTP/Client/Curl.php index fd9e14e7b07..423b1104186 100644 --- a/lib/Mage/HTTP/Client/Curl.php +++ b/lib/Mage/HTTP/Client/Curl.php @@ -421,31 +421,29 @@ public function doError($string) * Parse headers - CURL callback functin * * @param resource $ch curl handle, not needed - * @param string $data + * @param string $data + * * @return int */ - protected function parseHeaders($ch, $data) + protected function parseHeaders($ch, $data): int { - if($this->_headerCount == 0) { + if ($this->_headerCount === 0) { + $line = explode(' ', trim($data), 3); - $line = explode(" ", trim($data), 3); - if(count($line) != 3) { - return $this->doError("Invalid response line returned from server: ".$data); - } - $this->_responseStatus = intval($line[1]); + $this->validateHttpVersion($line); + $this->_responseStatus = (int)$line[1]; } else { //var_dump($data); $name = $value = ''; - $out = explode(": ", trim($data), 2); - if(count($out) == 2) { - $name = $out[0]; - $value = $out[1]; + $out = explode(': ', trim($data), 2); + if (count($out) === 2) { + [$name, $value] = $out; } - if(strlen($name)) { - if("Set-Cookie" == $name) { - if(!isset($this->_responseHeaders[$name])) { - $this->_responseHeaders[$name] = array(); + if ('' !== $name) { + if ('Set-Cookie' === $name) { + if (!isset($this->_responseHeaders[$name])) { + $this->_responseHeaders[$name] = []; } $this->_responseHeaders[$name][] = $value; } else { @@ -456,10 +454,34 @@ protected function parseHeaders($ch, $data) } $this->_headerCount++; - return strlen($data); } + /** + * @param array $line + * + * @throws Exception + */ + protected function validateHttpVersion(array $line): void + { + if ($line[0] === 'HTTP/1.1') { + if (count($line) !== 3) { + $this->doError('Invalid response line returned from server: ' . implode(' ', $line)); + } + + return; + } + + if ($line[0] === 'HTTP/2') { + if (!in_array(count($line), [2, 3])) { + $this->doError('Invalid response line returned from server: ' . implode(' ', $line)); + } + + return; + } + $this->doError('Invalid response line returned from server: ' . $data); + } + /** * Set curl option directly * From e16c6b89599d89c997bdfef8f81234cbc1f040ae Mon Sep 17 00:00:00 2001 From: Fabian Blechschmidt Date: Thu, 6 Aug 2020 23:44:44 +0200 Subject: [PATCH 2/4] Make PHP 7.0 compatible Remove list shortcut and void return type --- lib/Mage/HTTP/Client/Curl.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Mage/HTTP/Client/Curl.php b/lib/Mage/HTTP/Client/Curl.php index 423b1104186..592700d66a5 100644 --- a/lib/Mage/HTTP/Client/Curl.php +++ b/lib/Mage/HTTP/Client/Curl.php @@ -437,7 +437,7 @@ protected function parseHeaders($ch, $data): int $name = $value = ''; $out = explode(': ', trim($data), 2); if (count($out) === 2) { - [$name, $value] = $out; + list($name, $value) = $out; } if ('' !== $name) { @@ -462,7 +462,7 @@ protected function parseHeaders($ch, $data): int * * @throws Exception */ - protected function validateHttpVersion(array $line): void + protected function validateHttpVersion(array $line) { if ($line[0] === 'HTTP/1.1') { if (count($line) !== 3) { From 6b05a34699e6c7dd62e8fcf2521a8cf9ef718d8e Mon Sep 17 00:00:00 2001 From: Tymoteusz Motylewski Date: Fri, 7 May 2021 13:42:59 +0200 Subject: [PATCH 3/4] Allow HTTP/1.0 Co-authored-by: sv3n --- lib/Mage/HTTP/Client/Curl.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Mage/HTTP/Client/Curl.php b/lib/Mage/HTTP/Client/Curl.php index 592700d66a5..31e8377d12a 100644 --- a/lib/Mage/HTTP/Client/Curl.php +++ b/lib/Mage/HTTP/Client/Curl.php @@ -464,7 +464,7 @@ protected function parseHeaders($ch, $data): int */ protected function validateHttpVersion(array $line) { - if ($line[0] === 'HTTP/1.1') { + if ($line[0] === 'HTTP/1.0' || $line[0] === 'HTTP/1.1') { if (count($line) !== 3) { $this->doError('Invalid response line returned from server: ' . implode(' ', $line)); } From 7129a8574fb4a64f498826dcafd8c3f3b09cc5cd Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Mon, 18 Jul 2022 12:28:51 +0100 Subject: [PATCH 4/4] switched the "if" notation --- lib/Mage/HTTP/Client/Curl.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Mage/HTTP/Client/Curl.php b/lib/Mage/HTTP/Client/Curl.php index 31e8377d12a..4d27ac15e03 100644 --- a/lib/Mage/HTTP/Client/Curl.php +++ b/lib/Mage/HTTP/Client/Curl.php @@ -440,8 +440,8 @@ protected function parseHeaders($ch, $data): int list($name, $value) = $out; } - if ('' !== $name) { - if ('Set-Cookie' === $name) { + if ($name !== '') { + if ($name === 'Set-Cookie') { if (!isset($this->_responseHeaders[$name])) { $this->_responseHeaders[$name] = []; }