Skip to content

[HttpClient] Fix Copy as curl with base uri #46665

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

Merged
merged 1 commit into from
Jun 19, 2022
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpClient/CurlHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public function request(string $method, string $url, array $options = []): Respo
}
}

return $pushedResponse ?? new CurlResponse($this->multi, $ch, $options, $this->logger, $method, self::createRedirectResolver($options, $host, $port), CurlClientState::$curlVersion['version_number']);
return $pushedResponse ?? new CurlResponse($this->multi, $ch, $options, $this->logger, $method, self::createRedirectResolver($options, $host, $port), CurlClientState::$curlVersion['version_number'], $url);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ private function getCurlCommand(array $trace): ?string
return null;
}

$debug = explode("\n", $trace['info']['debug']);
$url = self::mergeQueryString($trace['url'], $trace['options']['query'] ?? [], true);
$url = $trace['info']['original_url'] ?? $trace['info']['url'] ?? $trace['url'];
$command = ['curl', '--compressed'];

if (isset($trace['options']['resolve'])) {
Expand All @@ -199,7 +198,7 @@ private function getCurlCommand(array $trace): ?string
if (\is_string($body)) {
try {
$dataArg[] = '--data '.escapeshellarg($body);
} catch (\ValueError $e) {
} catch (\ValueError) {
return null;
}
} elseif (\is_array($body)) {
Expand All @@ -214,7 +213,7 @@ private function getCurlCommand(array $trace): ?string

$dataArg = empty($dataArg) ? null : implode(' ', $dataArg);

foreach ($debug as $line) {
foreach (explode("\n", $trace['info']['debug']) as $line) {
$line = substr($line, 0, -1);

if (str_starts_with('< ', $line)) {
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/Response/AmpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function __construct(AmpClientState $multi, Request $request, array $opti
$info['http_method'] = $request->getMethod();
$info['start_time'] = null;
$info['redirect_url'] = null;
$info['original_url'] = $info['url'];
$info['redirect_time'] = 0.0;
$info['redirect_count'] = 0;
$info['size_upload'] = 0.0;
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpClient/Response/CurlResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class CurlResponse implements ResponseInterface, StreamableInterface
/**
* @internal
*/
public function __construct(CurlClientState $multi, \CurlHandle|string $ch, array $options = null, LoggerInterface $logger = null, string $method = 'GET', callable $resolveRedirect = null, int $curlVersion = null)
public function __construct(CurlClientState $multi, \CurlHandle|string $ch, array $options = null, LoggerInterface $logger = null, string $method = 'GET', callable $resolveRedirect = null, int $curlVersion = null, string $originalUrl = null)
{
$this->multi = $multi;

Expand All @@ -69,6 +69,7 @@ public function __construct(CurlClientState $multi, \CurlHandle|string $ch, arra
$this->info['user_data'] = $options['user_data'] ?? null;
$this->info['max_duration'] = $options['max_duration'] ?? null;
$this->info['start_time'] = $this->info['start_time'] ?? microtime(true);
$this->info['original_url'] = $originalUrl ?? $this->info['url'] ?? curl_getinfo($ch, \CURLINFO_EFFECTIVE_URL);
$info = &$this->info;
$headers = &$this->headers;
$debugBuffer = $this->debugBuffer;
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/Response/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public static function fromRequest(string $method, string $url, array $options,
$response->info['user_data'] = $options['user_data'] ?? null;
$response->info['max_duration'] = $options['max_duration'] ?? null;
$response->info['url'] = $url;
$response->info['original_url'] = $url;

if ($mock instanceof self) {
$mock->requestOptions = $response->requestOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function __construct(NativeClientState $multi, $context, string $url, arr
// Temporary resource to dechunk the response stream
$this->buffer = fopen('php://temp', 'w+');

$info['original_url'] = implode('', $info['url']);
$info['user_data'] = $options['user_data'];
$info['max_duration'] = $options['max_duration'];
++$multi->responseCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ public function provideCurlRequests(): iterable
--url %1$shttp://localhost:8057/json%1$s \\
--header %1$sAccept: */*%1$s \\
--header %1$sAccept-Encoding: gzip%1$s \\
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s',
];
yield 'GET with base uri' => [
[
'method' => 'GET',
'url' => '1',
'options' => [
'base_uri' => 'http://localhost:8057/json/',
],
],
'curl \\
--compressed \\
--request GET \\
--url %1$shttp://localhost:8057/json/1%1$s \\
--header %1$sAccept: */*%1$s \\
--header %1$sAccept-Encoding: gzip%1$s \\
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s',
];
yield 'GET with resolve' => [
Expand Down