Skip to content
Extraits de code Groupes Projets
Valider f68a5411 rédigé par Thomas Steur's avatar Thomas Steur
Parcourir les fichiers

fixes #3325 add possibility to post content via Http class, needed for premium plugins

parent 7cdd2ae4
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -125,6 +125,7 @@ class Http ...@@ -125,6 +125,7 @@ class Http
* @param string $httpMethod The HTTP method to use. Defaults to `'GET'`. * @param string $httpMethod The HTTP method to use. Defaults to `'GET'`.
* @param string $httpUsername HTTP Auth username * @param string $httpUsername HTTP Auth username
* @param string $httpPassword HTTP Auth password * @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
* *
* @throws Exception * @throws Exception
* @return bool true (or string/array) on success; false on HTTP response error code (1xx or 4xx) * @return bool true (or string/array) on success; false on HTTP response error code (1xx or 4xx)
...@@ -143,7 +144,8 @@ class Http ...@@ -143,7 +144,8 @@ class Http
$getExtendedInfo = false, $getExtendedInfo = false,
$httpMethod = 'GET', $httpMethod = 'GET',
$httpUsername = null, $httpUsername = null,
$httpPassword = null $httpPassword = null,
$requestBody = null
) { ) {
if ($followDepth > 5) { if ($followDepth > 5) {
throw new Exception('Too many redirects (' . $followDepth . ')'); throw new Exception('Too many redirects (' . $followDepth . ')');
...@@ -152,6 +154,10 @@ class Http ...@@ -152,6 +154,10 @@ class Http
$contentLength = 0; $contentLength = 0;
$fileLength = 0; $fileLength = 0;
if (!empty($requestBody) && is_array($requestBody)) {
$requestBody = http_build_query($requestBody);
}
// Piwik services behave like a proxy, so we should act like one. // Piwik services behave like a proxy, so we should act like one.
$xff = 'X-Forwarded-For: ' $xff = 'X-Forwarded-For: '
. (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] . ',' : '') . (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] . ',' : '')
...@@ -251,10 +257,17 @@ class Http ...@@ -251,10 +257,17 @@ class Http
. $xff . "\r\n" . $xff . "\r\n"
. $via . "\r\n" . $via . "\r\n"
. $rangeHeader . $rangeHeader
. "Connection: close\r\n" . "Connection: close\r\n";
. "\r\n";
fwrite($fsock, $requestHeader); fwrite($fsock, $requestHeader);
if (strtolower($httpMethod) === 'post' && !empty($requestBody)) {
fwrite($fsock, self::buildHeadersForPost($requestBody));
fwrite($fsock, "\r\n");
fwrite($fsock, $requestBody);
} else {
fwrite($fsock, "\r\n");
}
$streamMetaData = array('timed_out' => false); $streamMetaData = array('timed_out' => false);
@stream_set_blocking($fsock, true); @stream_set_blocking($fsock, true);
...@@ -421,6 +434,14 @@ class Http ...@@ -421,6 +434,14 @@ class Http
} }
} }
if (strtolower($httpMethod) === 'post' && !empty($requestBody)) {
$postHeader = self::buildHeadersForPost($requestBody);
$postHeader .= "\r\n";
$stream_options['http']['method'] = 'POST';
$stream_options['http']['header'] .= $postHeader;
$stream_options['http']['content'] = $requestBody;
}
$ctx = stream_context_create($stream_options); $ctx = stream_context_create($stream_options);
} }
...@@ -499,6 +520,11 @@ class Http ...@@ -499,6 +520,11 @@ class Http
@curl_setopt($ch, CURLOPT_NOBODY, true); @curl_setopt($ch, CURLOPT_NOBODY, true);
} }
if (strtolower($httpMethod) === 'post' && !empty($requestBody)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
}
if (!empty($httpUsername) && !empty($httpPassword)) { if (!empty($httpUsername) && !empty($httpPassword)) {
$curl_options += array( $curl_options += array(
CURLOPT_USERPWD => $httpUsername . ':' . $httpPassword, CURLOPT_USERPWD => $httpUsername . ':' . $httpPassword,
...@@ -596,6 +622,14 @@ class Http ...@@ -596,6 +622,14 @@ class Http
} }
} }
private static function buildHeadersForPost($requestBody)
{
$postHeader = "Content-Type: application/x-www-form-urlencoded\r\n";
$postHeader .= "Content-Length: " . strlen($requestBody) . "\r\n";
return $postHeader;
}
/** /**
* Downloads the next chunk of a specific file. The next chunk's byte range * Downloads the next chunk of a specific file. The next chunk's byte range
* is determined by the existing file's size and the expected file size, which * is determined by the existing file's size and the expected file size, which
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter