diff options
-rw-r--r-- | core/Http.php | 14 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/Http/AdditionalHeaders.php | 7 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/HttpTest.php | 27 |
3 files changed, 44 insertions, 4 deletions
diff --git a/core/Http.php b/core/Http.php index 48e1fa14fa..3ad1b2ce39 100644 --- a/core/Http.php +++ b/core/Http.php @@ -133,6 +133,7 @@ class Http * @param string $httpUsername HTTP Auth username * @param string $httpPassword HTTP Auth password * @param array|string $requestBody If $httpMethod is 'POST' this may accept an array of variables or a string that needs to be posted + * @param array $additionalHeaders List of additional headers to set for the request * * @throws Exception * @return bool true (or string/array) on success; false on HTTP response error code (1xx or 4xx) @@ -152,7 +153,8 @@ class Http $httpMethod = 'GET', $httpUsername = null, $httpPassword = null, - $requestBody = null + $requestBody = null, + $additionalHeaders = array() ) { if ($followDepth > 5) { throw new Exception('Too many redirects (' . $followDepth . ')'); @@ -264,6 +266,7 @@ class Http . $xff . "\r\n" . $via . "\r\n" . $rangeHeader + . (!empty($additionalHeaders) ? implode("\r\n", $additionalHeaders) . "\r\n" : '') . "Connection: close\r\n"; fwrite($fsock, $requestHeader); @@ -358,7 +361,9 @@ class Http $getExtendedInfo, $httpMethod, $httpUsername, - $httpPassword + $httpPassword, + $requestBody, + $additionalHeaders ); } @@ -427,6 +432,7 @@ class Http . ($acceptLanguage ? $acceptLanguage . "\r\n" : '') . $xff . "\r\n" . $via . "\r\n" + . (!empty($additionalHeaders) ? implode("\r\n", $additionalHeaders) . "\r\n" : '') . $rangeHeader, 'max_redirects' => 5, // PHP 5.1.0 'timeout' => $timeout, // PHP 5.2.1 @@ -504,12 +510,12 @@ class Http // curl options (sorted oldest to newest) CURLOPT_URL => $aUrl, CURLOPT_USERAGENT => $userAgent, - CURLOPT_HTTPHEADER => array( + CURLOPT_HTTPHEADER => array_merge(array( $xff, $via, $rangeHeader, $acceptLanguage - ), + ), $additionalHeaders), // only get header info if not saving directly to file CURLOPT_HEADER => is_resource($file) ? false : true, CURLOPT_CONNECTTIMEOUT => $timeout, diff --git a/tests/PHPUnit/Integration/Http/AdditionalHeaders.php b/tests/PHPUnit/Integration/Http/AdditionalHeaders.php new file mode 100644 index 0000000000..88dd746048 --- /dev/null +++ b/tests/PHPUnit/Integration/Http/AdditionalHeaders.php @@ -0,0 +1,7 @@ +<?php + +// used in integration tests to see if additional header works. + +echo $_SERVER['HTTP_CUSTOMHEADER']; + +exit; diff --git a/tests/PHPUnit/Integration/HttpTest.php b/tests/PHPUnit/Integration/HttpTest.php index 1fdec7b6ed..371ad82459 100644 --- a/tests/PHPUnit/Integration/HttpTest.php +++ b/tests/PHPUnit/Integration/HttpTest.php @@ -229,6 +229,33 @@ class HttpTest extends \PHPUnit_Framework_TestCase /** * @dataProvider getMethodsToTest */ + public function testHttpCustomHeaders($method) + { + $result = Http::sendHttpRequestBy( + $method, + Fixture::getRootUrl() . 'tests/PHPUnit/Integration/Http/AdditionalHeaders.php', + 30, + $userAgent = null, + $destinationPath = null, + $file = null, + $followDepth = 0, + $acceptLanguage = false, + $acceptInvalidSslCertificate = false, + $byteRange = false, + $getExtendedInfo = false, + $httpMethod = 'POST', + $httpUsername = '', + $httpPassword = '', + array(), + array('CustomHeader: customdata') + ); + + $this->assertEquals('customdata', $result); + } + + /** + * @dataProvider getMethodsToTest + */ public function testHttpsWorksWithValidCertificate($method) { $result = Http::sendHttpRequestBy($method, 'https://builds.piwik.org/LATEST', 10); |