Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Giehl <stefan@piwik.org>2017-05-24 22:52:10 +0300
committerGitHub <noreply@github.com>2017-05-24 22:52:10 +0300
commit85e5cfb2fc5dabec712954566bdb63d8f1e30671 (patch)
tree22db598e5f11094fb8a7b1086b3987fd52974a67
parentf7569268f2a7fecd4fd1a95c430d3a1005471e08 (diff)
Allow setting custom headers for requests done by Http::sendHttpRequestBy (#11738)
-rw-r--r--core/Http.php14
-rw-r--r--tests/PHPUnit/Integration/Http/AdditionalHeaders.php7
-rw-r--r--tests/PHPUnit/Integration/HttpTest.php27
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);