Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[HttpClient] make pushed responses retry-able
  • Loading branch information
nicolas-grekas committed Dec 17, 2019
commit c2864f65ab6cbca6b2bed5448f8af1908f822df1
42 changes: 22 additions & 20 deletions src/Symfony/Component/HttpClient/CurlHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,6 @@ public function request(string $method, string $url, array $options = []): Respo
$options['normalized_headers']['user-agent'][] = $options['headers'][] = 'User-Agent: Symfony HttpClient/Curl';
}

if ($pushedResponse = $this->multi->pushedResponses[$url] ?? null) {
unset($this->multi->pushedResponses[$url]);

if (self::acceptPushForRequest($method, $options, $pushedResponse)) {
$this->logger && $this->logger->debug(sprintf('Accepting pushed response: "%s %s"', $method, $url));

// Reinitialize the pushed response with request's options
$pushedResponse->response->__construct($this->multi, $url, $options, $this->logger);

return $pushedResponse->response;
}

$this->logger && $this->logger->debug(sprintf('Rejecting pushed response: "%s".', $url));
}

$this->logger && $this->logger->info(sprintf('Request: "%s %s"', $method, $url));

$curlopts = [
CURLOPT_URL => $url,
CURLOPT_TCP_NODELAY => true,
Expand Down Expand Up @@ -267,7 +250,26 @@ public function request(string $method, string $url, array $options = []): Respo
$curlopts[file_exists($options['bindto']) ? CURLOPT_UNIX_SOCKET_PATH : CURLOPT_INTERFACE] = $options['bindto'];
}

$ch = curl_init();
if ($pushedResponse = $this->multi->pushedResponses[$url] ?? null) {
unset($this->multi->pushedResponses[$url]);

if (self::acceptPushForRequest($method, $options, $pushedResponse)) {
$this->logger && $this->logger->debug(sprintf('Accepting pushed response: "%s %s"', $method, $url));

// Reinitialize the pushed response with request's options
$ch = $pushedResponse->handle;
$pushedResponse = $pushedResponse->response;
$pushedResponse->__construct($this->multi, $url, $options, $this->logger);
} else {
$this->logger && $this->logger->debug(sprintf('Rejecting pushed response: "%s".', $url));
$pushedResponse = null;
}
}

if (!$pushedResponse) {
$ch = curl_init();
$this->logger && $this->logger->info(sprintf('Request: "%s %s"', $method, $url));
}

foreach ($curlopts as $opt => $value) {
if (null !== $value && !curl_setopt($ch, $opt, $value) && CURLOPT_CERTINFO !== $opt) {
Expand All @@ -279,7 +281,7 @@ public function request(string $method, string $url, array $options = []): Respo
}
}

return new CurlResponse($this->multi, $ch, $options, $this->logger, $method, self::createRedirectResolver($options, $host));
return $pushedResponse ?? new CurlResponse($this->multi, $ch, $options, $this->logger, $method, self::createRedirectResolver($options, $host));
}

/**
Expand Down Expand Up @@ -369,7 +371,7 @@ private static function handlePush($parent, $pushed, array $requestHeaders, Curl
$url .= $headers[':path'][0];
$logger && $logger->debug(sprintf('Queueing pushed response: "%s"', $url));

$multi->pushedResponses[$url] = new PushedResponse(new CurlResponse($multi, $pushed), $headers, $multi->openHandles[(int) $parent][1] ?? []);
$multi->pushedResponses[$url] = new PushedResponse(new CurlResponse($multi, $pushed), $headers, $multi->openHandles[(int) $parent][1] ?? [], $pushed);

return CURL_PUSH_OK;
}
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/HttpClient/Internal/PushedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ final class PushedResponse

public $parentOptions = [];

public function __construct(CurlResponse $response, array $requestHeaders, array $parentOptions)
public $handle;

public function __construct(CurlResponse $response, array $requestHeaders, array $parentOptions, $handle)
{
$this->response = $response;
$this->requestHeaders = $requestHeaders;
$this->parentOptions = $parentOptions;
$this->handle = $handle;
}
}