10000 [HttpClient] Fix Copy as curl with base uri · symfony/symfony@84fc01e · GitHub
[go: up one dir, main page]

Skip to content

Commit 84fc01e

Browse files
committed
[HttpClient] Fix Copy as curl with base uri
1 parent f0247e0 commit 84fc01e

File tree

2 files changed

+55
-23
lines changed

2 files changed

+55
-23
lines changed

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

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

181+
$scheme = parse_url($trace['url'], \PHP_URL_SCHEME) ?? parse_url($trace['options']['base_uri'], \PHP_URL_SCHEME);
182+
$host = $request = $path = null;
183+
$headers = [];
184+
181185
$debug = explode("\n", $trace['info']['debug']);
182-
$url = self::mergeQueryString($trace['url'], $trace['options']['query'] ?? [], true);
186+
foreach ($debug as $line) {
187+
$line = substr($line, 0, -1);
188+
189+
if (str_starts_with('< ', $line)) {
190+
// End of the request, beginning of the response. Stop parsing.
191+
break;
192+
}
193+
194+
if ('' === $line || str_starts_with($line, '*')) {
195+
continue;
196+
}
197+
198+
if (preg_match('/Host: (.+)/', $line, $match)) {
199+
$host = $match[1];
200+
continue;
201+
}
202+
203+
if (preg_match('/^> ([A-Z]+) ([^ ]+)/', $line, $match)) {
204+
[, $request, $path] = $match;
205+
continue;
206+
}
207+
208+
$headers[] = $line;
209+
}
210+
211+
$url = $scheme.'://'.$host.$path;
212+
183213
$command = ['curl', '--compressed'];
184214

185215
if (isset($trace['options']['resolve'])) {
@@ -191,6 +221,13 @@ private function getCurlCommand(array $trace): ?string
191221
}
192222
}
193223

224+
$command[] = sprintf('--request %s', $request);
225+
$command[] = sprintf('--url %s', escapeshellarg($url));
226+
227+
foreach ($headers as $header) {
228+
$command[] = '--header '.escapeshellarg($header);
229+
}
230+
194231
$dataArg = [];
195232

196233
if ($json = $trace['options']['json'] ?? null) {
@@ -199,7 +236,7 @@ private function getCurlCommand(array $trace): ?string
199236
if (\is_string($body)) {
200237
try {
201238
$dataArg[] = '--data '.escapeshellarg($body);
202-
} catch (\ValueError $e) {
239+
} catch (\ValueError) {
203240
return null;
204241
}
205242
} elseif (\is_array($body)) {
@@ -214,27 +251,6 @@ private function getCurlCommand(array $trace): ?string
214251

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

217-
foreach ($debug as $line) {
218-
$line = substr($line, 0, -1);
219-
220-
if (str_starts_with('< ', $line)) {
221-
// End of the request, beginning of the response. Stop parsing.
222-
break;
223-
}
224-
225-
if ('' === $line || preg_match('/^[*<]|(Host: )/', $line)) {
226-
continue;
227-
}
228-
229-
if (preg_match('/^> ([A-Z]+)/', $line, $match)) {
230-
$command[] = sprintf('--request %s', $match[1]);
231-
$command[] = sprintf('--url %s', escapeshellarg($url));
232-
continue;
233-
}
234-
235-
$command[] = '--header '.escapeshellarg($line);
236-
}
237-
238254
if (null !== $dataArg) {
239255
$command[] = $dataArg;
240256
}

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)
0