From 18198b4aaa925c2cff83100bba3c072d2dd624e5 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 1 Oct 2019 16:33:11 +0000 Subject: [PATCH 01/11] Enable the possibility to fetch a specific header variable --- src/Network/CurlResult.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Network/CurlResult.php b/src/Network/CurlResult.php index 1a475c7cec..998644a876 100644 --- a/src/Network/CurlResult.php +++ b/src/Network/CurlResult.php @@ -226,11 +226,26 @@ class CurlResult /** * Returns the Curl headers * - * @return string the Curl headers + * @param string $field optional header field. Return all fields if empty + * + * @return string|bool the Curl headers, "false" when field isn't found */ - public function getHeader() + public function getHeader($field = '') { - return $this->header; + if (empty($field)) { + return $this->header; + } + + $lines = explode("\n", $this->header); + foreach ($lines as $line) { + $parts = explode(':', $line); + $headerfield = array_shift($parts); + if (strtolower(trim($field)) == strtolower(trim($headerfield))) { + return implode(':', $parts); + } + } + + return false; } /** From 2c730a5c45abeef65fcb520653fe81191281e8d7 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 1 Oct 2019 18:22:33 +0000 Subject: [PATCH 02/11] New function for fetching associated header array --- src/Network/CurlResult.php | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Network/CurlResult.php b/src/Network/CurlResult.php index 998644a876..b9ab07be9f 100644 --- a/src/Network/CurlResult.php +++ b/src/Network/CurlResult.php @@ -230,7 +230,7 @@ class CurlResult * * @return string|bool the Curl headers, "false" when field isn't found */ - public function getHeader($field = '') + public function getHeader(string $field = '') { if (empty($field)) { return $this->header; @@ -241,13 +241,36 @@ class CurlResult $parts = explode(':', $line); $headerfield = array_shift($parts); if (strtolower(trim($field)) == strtolower(trim($headerfield))) { - return implode(':', $parts); + return trim(implode(':', $parts)); } } return false; } + /** + * Returns the Curl headers as an associated array + * + * @return array associated header array + */ + public function getHeaderArray() + { + $headers = []; + + $lines = explode("\n", $this->header); + foreach ($lines as $line) { + $parts = explode(':', $line); + $headerfield = strtolower(trim(array_shift($parts))); + $headerdata = trim(implode(':', $parts)); + + if (!empty($headerdata)) { + $headers[$headerfield] = $headerdata; + } + } + + return $headers; + } + /** * @return bool */ From c37663f1c19ba725ae99586ff43852046634ff39 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 1 Oct 2019 19:02:26 +0000 Subject: [PATCH 03/11] Changed return value --- src/Network/CurlResult.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Network/CurlResult.php b/src/Network/CurlResult.php index b9ab07be9f..1c43db17d4 100644 --- a/src/Network/CurlResult.php +++ b/src/Network/CurlResult.php @@ -228,7 +228,7 @@ class CurlResult * * @param string $field optional header field. Return all fields if empty * - * @return string|bool the Curl headers, "false" when field isn't found + * @return string the Curl headers or the specified content of the header variable */ public function getHeader(string $field = '') { @@ -245,7 +245,7 @@ class CurlResult } } - return false; + return ''; } /** From 516fd02812b0ac86fd6cbc6d2f4d9adb58d959a9 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 1 Oct 2019 21:46:18 +0000 Subject: [PATCH 04/11] New function to check for key existance --- src/Network/CurlResult.php | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/Network/CurlResult.php b/src/Network/CurlResult.php index 1c43db17d4..06ca45fbce 100644 --- a/src/Network/CurlResult.php +++ b/src/Network/CurlResult.php @@ -236,16 +236,29 @@ class CurlResult return $this->header; } - $lines = explode("\n", $this->header); - foreach ($lines as $line) { - $parts = explode(':', $line); - $headerfield = array_shift($parts); - if (strtolower(trim($field)) == strtolower(trim($headerfield))) { - return trim(implode(':', $parts)); - } - } + $field = strtolower(trim($field)); - return ''; + $headers = self::getHeaderArray(); + + if (isset($headers[$field])) { + return $headers[$field]; + } + } + + /** + * Check if a specified header exists + * + * @param string $field header field + * + * @return boolean "true" if header exists + */ + public function headerExists(string $field) + { + $field = strtolower(trim($field)); + + $headers = self::getHeaderArray(); + + return array_key_exists($field, $headers); } /** From 5cdeb8615fb9233be02d3b7257eabac4f064cb99 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 2 Oct 2019 03:31:58 +0000 Subject: [PATCH 05/11] Use $this --- src/Network/CurlResult.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Network/CurlResult.php b/src/Network/CurlResult.php index 06ca45fbce..a8dd3dd631 100644 --- a/src/Network/CurlResult.php +++ b/src/Network/CurlResult.php @@ -238,7 +238,7 @@ class CurlResult $field = strtolower(trim($field)); - $headers = self::getHeaderArray(); + $headers = $this->getHeaderArray(); if (isset($headers[$field])) { return $headers[$field]; @@ -256,7 +256,7 @@ class CurlResult { $field = strtolower(trim($field)); - $headers = self::getHeaderArray(); + $headers = $this->getHeaderArray(); return array_key_exists($field, $headers); } From 489510e7a9df2441cac820dc1d8e6b8410050b72 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 2 Oct 2019 03:45:32 +0000 Subject: [PATCH 06/11] Cache the header fields --- src/Network/CurlResult.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Network/CurlResult.php b/src/Network/CurlResult.php index a8dd3dd631..2f5c94187e 100644 --- a/src/Network/CurlResult.php +++ b/src/Network/CurlResult.php @@ -26,6 +26,11 @@ class CurlResult */ private $header; + /** + * @var array the HTTP headers of the Curl call + */ + private $header_fields; + /** * @var boolean true (if HTTP 2xx result) or false */ @@ -268,20 +273,21 @@ class CurlResult */ public function getHeaderArray() { - $headers = []; + if (!empty($this->header_fields)) { + return $this->header_fields; + } - $lines = explode("\n", $this->header); + $this->header_fields = []; + + $lines = explode("\n", trim($this->header)); foreach ($lines as $line) { $parts = explode(':', $line); $headerfield = strtolower(trim(array_shift($parts))); $headerdata = trim(implode(':', $parts)); - - if (!empty($headerdata)) { - $headers[$headerfield] = $headerdata; - } + $this->header_fields[$headerfield] = $headerdata; } - return $headers; + return $this->header_fields; } /** From b051804dc3a2f972c3c4cf59bcd9fe7b7641f17b Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 2 Oct 2019 04:10:36 +0000 Subject: [PATCH 07/11] Empty the header array --- src/Network/CurlResult.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Network/CurlResult.php b/src/Network/CurlResult.php index 2f5c94187e..26fe7a4780 100644 --- a/src/Network/CurlResult.php +++ b/src/Network/CurlResult.php @@ -134,6 +134,7 @@ class CurlResult $this->body = substr($result, strlen($header)); $this->header = $header; + $this->header_fields = []; // Is filled on demand } private function checkSuccess() From 6a6a48c58d40552e77c7fdd5dfd763dec1313028 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 2 Oct 2019 06:24:29 +0000 Subject: [PATCH 08/11] Renamed function --- src/Network/CurlResult.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Network/CurlResult.php b/src/Network/CurlResult.php index 26fe7a4780..f8bdca1f88 100644 --- a/src/Network/CurlResult.php +++ b/src/Network/CurlResult.php @@ -258,7 +258,7 @@ class CurlResult * * @return boolean "true" if header exists */ - public function headerExists(string $field) + public function inHeader(string $field) { $field = strtolower(trim($field)); From 52c3f64cee977049bace13d86f860c3efa2cd45e Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 2 Oct 2019 09:26:52 +0000 Subject: [PATCH 09/11] Tests added --- tests/src/Network/CurlResultTest.php | 55 ++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/src/Network/CurlResultTest.php b/tests/src/Network/CurlResultTest.php index 72991c6d0f..62c21fa3fc 100644 --- a/tests/src/Network/CurlResultTest.php +++ b/tests/src/Network/CurlResultTest.php @@ -134,4 +134,59 @@ class CurlResultTest extends TestCase $this->assertSame('https://test.local/test/it?key=value', $curlResult->getUrl()); $this->assertSame('https://test.other/some/?key=value', $curlResult->getRedirectUrl()); } + + /** + * @small + */ + public function testInHeader() + { + $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head'); + $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body'); + + $curlResult = new CurlResult('https://test.local', $header . $body, [ + 'http_code' => 200, + 'content_type' => 'text/html; charset=utf-8', + 'url' => 'https://test.local' + ]); + $this->assertTrue($curlResult->inHeader('vary')); + $this->assertFalse($curlResult->inHeader('wrongHeader')); + } + + /** + * @small + */ + public function testGetHeaderArray() + { + $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head'); + $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body'); + + $curlResult = new CurlResult('https://test.local', $header . $body, [ + 'http_code' => 200, + 'content_type' => 'text/html; charset=utf-8', + 'url' => 'https://test.local' + ]); + + $headers = $curlResult->getHeaderArray(); + + $this->assertNotEmpty($headers); + $this->assertArrayHasKey('vary', $headers); + } + + /** + * @small + */ + public function testGetHeaderWithParam() + { + $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head'); + $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body'); + + $curlResult = new CurlResult('https://test.local', $header . $body, [ + 'http_code' => 200, + 'content_type' => 'text/html; charset=utf-8', + 'url' => 'https://test.local' + ]); + + $this->assertNotEmpty($curlResult->getHeader()); + $this->assertEmpty('vary', $curlResult->getHeader('wrongHeader')); + } } From 7991b77343dc32b4ac8834665ecf289bf5e67efe Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 2 Oct 2019 09:37:05 +0000 Subject: [PATCH 10/11] Return value added --- src/Network/CurlResult.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Network/CurlResult.php b/src/Network/CurlResult.php index f8bdca1f88..c12bb28266 100644 --- a/src/Network/CurlResult.php +++ b/src/Network/CurlResult.php @@ -249,6 +249,8 @@ class CurlResult if (isset($headers[$field])) { return $headers[$field]; } + + return ''; } /** From fbe7a78dfb68e7f6fd6f259bdcf1db9e35eb2b3f Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 2 Oct 2019 10:46:07 +0000 Subject: [PATCH 11/11] Correcting tests --- tests/src/Network/CurlResultTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/Network/CurlResultTest.php b/tests/src/Network/CurlResultTest.php index 62c21fa3fc..03e415a99b 100644 --- a/tests/src/Network/CurlResultTest.php +++ b/tests/src/Network/CurlResultTest.php @@ -187,6 +187,6 @@ class CurlResultTest extends TestCase ]); $this->assertNotEmpty($curlResult->getHeader()); - $this->assertEmpty('vary', $curlResult->getHeader('wrongHeader')); + $this->assertEmpty($curlResult->getHeader('wrongHeader')); } }