-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpClient] Fix streaming and redirecting with NoPrivateNetworkHttpClient #59023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,6 @@ | |
use Symfony\Contracts\HttpClient\ChunkInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
use Symfony\Contracts\HttpClient\ResponseStreamInterface; | ||
use Symfony\Contracts\Service\ResetInterface; | ||
|
||
/** | ||
|
@@ -103,31 +102,33 @@ public function request(string $method, string $url, array $options = []): Respo | |
$ip = self::dnsResolve($dnsCache, $host, $this->ipFlags, $options); | ||
self::ipCheck($ip, $this->subnets, $this->ipFlags, $host, $url); | ||
|
||
if (0 < $maxRedirects = $options['max_redirects']) { | ||
$options['max_redirects'] = 0; | ||
$redirectHeaders['with_auth'] = $redirectHeaders['no_auth'] = $options['headers']; | ||
|
||
if (isset($options['normalized_headers']['host']) || isset($options['normalized_headers']['authorization']) || isset($options['normalized_headers']['cookie'])) { | ||
$redirectHeaders['no_auth'] = array_filter($redirectHeaders['no_auth'], static function ($h) { | ||
return 0 !== stripos($h, 'Host:') && 0 !== stripos($h, 'Authorization:') && 0 !== stripos($h, 'Cookie:'); | ||
}); | ||
} | ||
} | ||
|
||
$onProgress = $options['on_progress'] ?? null; | ||
$subnets = $this->subnets; | ||
$ipFlags = $this->ipFlags; | ||
$lastPrimaryIp = ''; | ||
|
||
$options['on_progress'] = static function (int $dlNow, int $dlSize, array $info) use ($onProgress, $subnets, $ipFlags, &$lastPrimaryIp): void { | ||
if (($info['primary_ip'] ?? '') !== $lastPrimaryIp) { | ||
if (!\in_array($info['primary_ip'] ?? '', ['', $lastPrimaryIp], true)) { | ||
self::ipCheck($info['primary_ip'], $subnets, $ipFlags, null, $info['url']); | ||
$lastPrimaryIp = $info['primary_ip']; | ||
} | ||
|
||
null !== $onProgress && $onProgress($dlNow, $dlSize, $info); | ||
}; | ||
|
||
if (0 >= $maxRedirects = $options['max_redirects']) { | ||
return new AsyncResponse($this->client, $method, $url, $options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix when |
||
} | ||
|
||
$options['max_redirects'] = 0; | ||
$redirectHeaders['with_auth'] = $redirectHeaders['no_auth'] = $options['headers']; | ||
|
||
if (isset($options['normalized_headers']['host']) || isset($options['normalized_headers']['authorization']) || isset($options['normalized_headers']['cookie'])) { | ||
$redirectHeaders['no_auth'] = array_filter($redirectHeaders['no_auth'], static function ($h) { | ||
return 0 !== stripos($h, 'Host:') && 0 !== stripos($h, 'Authorization:') && 0 !== stripos($h, 'Cookie:'); | ||
}); | ||
} | ||
|
||
return new AsyncResponse($this->client, $method, $url, $options, static function (ChunkInterface $chunk, AsyncContext $context) use (&$method, &$options, $maxRedirects, &$redirectHeaders, $subnets, $ipFlags, $dnsCache): \Generator { | ||
if (null !== $chunk->getError() || $chunk->isTimeout() || !$chunk->isFirst()) { | ||
yield $chunk; | ||
|
@@ -178,14 +179,6 @@ public function request(string $method, string $url, array $options = []): Respo | |
}); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function stream($responses, ?float $timeout = null): ResponseStreamInterface | ||
{ | ||
return $this->client->stream($responses, $timeout); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix for the |
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,11 +24,6 @@ public static function setUpBeforeClass(): void | |
TestHttpServer::start(); | ||
} | ||
|
||
public static function tearDownAfterClass(): void | ||
{ | ||
TestHttpServer::stop(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one test server instance can handle all test cases, no need to start/stop it all the time |
||
} | ||
|
||
public function testItCollectsRequestCount() | ||
{ | ||
$httpClient1 = $this->httpClientThatHasTracedRequests([ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,6 @@ public static function tearDownAfterClass(): void | |
{ | ||
TestHttpServer::stop(8067); | ||
TestHttpServer::stop(8077); | ||
TestHttpServer::stop(8087); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to stop this port, it can handle many test cases |
||
} | ||
|
||
abstract protected function getHttpClient(string $testCase): HttpClientInterface; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix for the empty ip address error (it didn't happen before, but now that we redirect on our own, it can)