Merge pull request #7687 from annando/get-header-field

Enable the possibility to fetch a specific header variable
pull/7693/head
Philipp 2019-10-02 13:22:30 +02:00 zatwierdzone przez GitHub
commit a855c6888b
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 118 dodań i 3 usunięć

Wyświetl plik

@ -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
*/
@ -129,6 +134,7 @@ class CurlResult
$this->body = substr($result, strlen($header));
$this->header = $header;
$this->header_fields = []; // Is filled on demand
}
private function checkSuccess()
@ -226,11 +232,65 @@ class CurlResult
/**
* Returns the Curl headers
*
* @return string the Curl headers
* @param string $field optional header field. Return all fields if empty
*
* @return string the Curl headers or the specified content of the header variable
*/
public function getHeader()
public function getHeader(string $field = '')
{
return $this->header;
if (empty($field)) {
return $this->header;
}
$field = strtolower(trim($field));
$headers = $this->getHeaderArray();
if (isset($headers[$field])) {
return $headers[$field];
}
return '';
}
/**
* Check if a specified header exists
*
* @param string $field header field
*
* @return boolean "true" if header exists
*/
public function inHeader(string $field)
{
$field = strtolower(trim($field));
$headers = $this->getHeaderArray();
return array_key_exists($field, $headers);
}
/**
* Returns the Curl headers as an associated array
*
* @return array associated header array
*/
public function getHeaderArray()
{
if (!empty($this->header_fields)) {
return $this->header_fields;
}
$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));
$this->header_fields[$headerfield] = $headerdata;
}
return $this->header_fields;
}
/**

Wyświetl plik

@ -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($curlResult->getHeader('wrongHeader'));
}
}