Skip to content

Commit

Permalink
allow killing curl requests
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl committed Aug 12, 2024
1 parent 3b7c062 commit cd4fb6d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
28 changes: 27 additions & 1 deletion core/CliMulti.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class CliMulti
*/
private $logger;

private $wasKilled = false;

public function __construct(LoggerInterface $logger = null)
{
$this->supportsAsync = $this->supportsAsync();
Expand Down Expand Up @@ -161,6 +163,8 @@ public function runAsSuperUser($runAsSuperUser = true)
*/
public function kill(): void
{
$this->wasKilled = true;

foreach ($this->processes as $process) {
$process->killProcess();
}
Expand Down Expand Up @@ -434,7 +438,29 @@ private function executeNotAsyncHttp($url, StaticOutput $output)

try {
$this->logger->debug("Execute HTTP API request: " . $url);
$response = Http::sendHttpRequestBy('curl', $url, $timeout = 0, $userAgent = null, $destinationPath = null, $file = null, $followDepth = 0, $acceptLanguage = false, $this->acceptInvalidSSLCertificate, false, false, 'POST', null, null, $requestBody, [], $forcePost = true);
$response = Http::sendHttpRequestBy(
'curl',
$url,
$timeout = 0,
$userAgent = null,
$destinationPath = null,
$file = null,
$followDepth = 0,
$acceptLanguage = false,
$this->acceptInvalidSSLCertificate,
false,
false,
'POST',
null,
null,
$requestBody,
[],
$forcePost = true,
true,
function () {
return $this->wasKilled;
}
);
$output->write($response);
} catch (\Exception $e) {
$message = "Got invalid response from API request: $url. ";
Expand Down
15 changes: 14 additions & 1 deletion core/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,10 @@ private static function convertWildcardToPattern($wildcardHost)
* @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
* @param bool $forcePost
* @param bool $checkHostIsAllowed whether we should check if the target host is allowed or not. This should only
* be set to false when using a hardcoded URL.
* @param callback $abortFunction Callback used to check if requst should be aborted. When callback returns true, request will be aborted
*
* @return string|array true (or string/array) on success; false on HTTP response error code (1xx or 4xx)
*@throws Exception
Expand All @@ -208,7 +210,8 @@ public static function sendHttpRequestBy(
$requestBody = null,
$additionalHeaders = array(),
$forcePost = null,
$checkHostIsAllowed = true
$checkHostIsAllowed = true,
$abortFunction = null
) {
if ($followDepth > 5) {
throw new Exception('Too many redirects (' . $followDepth . ')');
Expand Down Expand Up @@ -671,6 +674,16 @@ public static function sendHttpRequestBy(
CURLOPT_TIMEOUT => $timeout,
);

if ($abortFunction) {
$curl_options[CURLOPT_PROGRESSFUNCTION] = function () use ($abortFunction) {
if ($abortFunction()) {
return 1;
}

return 0;
};
}

if ($rangeBytes) {
curl_setopt($ch, CURLOPT_RANGE, $rangeBytes);
} else {
Expand Down

0 comments on commit cd4fb6d

Please sign in to comment.