Skip to content

Commit 12e40a0

Browse files
HypeMCnicolas-grekas
authored andcommitted
[HttpClient] Fix Copy as curl with base uri
1 parent f0247e0 commit 12e40a0

File tree

7 files changed

+25
-6
lines changed

7 files changed

+25
-6
lines changed

src/Symfony/Component/HttpClient/CurlHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ public function request(string $method, string $url, array $options = []): Respo
301301
}
302302
}
303303

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

307307
/**

src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ private function getCurlCommand(array $trace): ?string
178178
return null;
179179
}
180180

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

185184
if (isset($trace['options']['resolve'])) {
@@ -199,7 +198,7 @@ private function getCurlCommand(array $trace): ?string
199198
if (\is_string($body)) {
200199
try {
201200
$dataArg[] = '--data '.escapeshellarg($body);
202-
} catch (\ValueError $e) {
201+
} catch (\ValueError) {
203202
return null;
204203
}
205204
} elseif (\is_array($body)) {
@@ -214,7 +213,7 @@ private function getCurlCommand(array $trace): ?string
214213

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

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

220219
if (str_starts_with('< ', $line)) {

src/Symfony/Component/HttpClient/Response/AmpResponse.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function __construct(AmpClientState $multi, Request $request, array $opti
8080
$info['http_method'] = $request->getMethod();
8181
$info['start_time'] = null;
8282
$info['redirect_url'] = null;
83+
$info['original_url'] = $info['url'];
8384
$info['redirect_time'] = 0.0;
8485
$info['redirect_count'] = 0;
8586
$info['size_upload'] = 0.0;

src/Symfony/Component/HttpClient/Response/CurlResponse.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ final class CurlResponse implements ResponseInterface, StreamableInterface
4343
/**
4444
* @internal
4545
*/
46-
public function __construct(CurlClientState $multi, \CurlHandle|string $ch, array $options = null, LoggerInterface $logger = null, string $method = 'GET', callable $resolveRedirect = null, int $curlVersion = null)
46+
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)
4747
{
4848
$this->multi = $multi;
4949

@@ -69,6 +69,7 @@ public function __construct(CurlClientState $multi, \CurlHandle|string $ch, arra
6969
$this->info['user_data'] = $options['user_data'] ?? null;
7070
$this->info['max_duration'] = $options['max_duration'] ?? null;
7171
$this->info['start_time'] = $this->info['start_time'] ?? microtime(true);
72+
$this->info['original_url'] = $originalUrl ?? $this->info['url'] ?? curl_getinfo($ch, \CURLINFO_EFFECTIVE_URL);
7273
$info = &$this->info;
7374
$headers = &$this->headers;
7475
$debugBuffer = $this->debugBuffer;

src/Symfony/Component/HttpClient/Response/MockResponse.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public static function fromRequest(string $method, string $url, array $options,
142142
$response->info['user_data'] = $options['user_data'] ?? null;
143143
$response->info['max_duration'] = $options['max_duration'] ?? null;
144144
$response->info['url'] = $url;
145+
$response->info['original_url'] = $url;
145146

146147
if ($mock instanceof self) {
147148
$mock->requestOptions = $response->requestOptions;

src/Symfony/Component/HttpClient/Response/NativeResponse.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function __construct(NativeClientState $multi, $context, string $url, arr
6666
// Temporary resource to dechunk the response stream
6767
$this->buffer = fopen('php://temp', 'w+');
6868

69+
$info['original_url'] = implode('', $info['url']);
6970
$info['user_data'] = $options['user_data'];
7071
$info['max_duration'] = $options['max_duration'];
7172
++$multi->responseCount;

src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,22 @@ public function provideCurlRequests(): iterable
194194
--url %1$shttp://localhost:8057/json%1$s \\
195195
--header %1$sAccept: */*%1$s \\
196196
--header %1$sAccept-Encoding: gzip%1$s \\
197+
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s',
198+
];
199+
yield 'GET with base uri' => [
200+
[
201+
'method' => 'GET',
202+
'url' => '1',
203+
'options' => [
204+
'base_uri' => 'http://localhost:8057/json/',
205+
],
206+
],
207+
'curl \\
208+
--compressed \\
209+
--request GET \\
210+
--url %1$shttp://localhost:8057/json/1%1$s \\
211+
--header %1$sAccept: */*%1$s \\
212+
--header %1$sAccept-Encoding: gzip%1$s \\
197213
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s',
198214
];
199215
yield 'GET with resolve' => [

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy