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
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@
Profile
</th>
{% endif %}
{% if trace.curlCommand is not null %}
<th>
<button class="btn btn-sm hidden label" title="Copy as cURL command" data-clipboard-text="{{ trace.curlCommand }}">cURL</button>
</th>
{% endif %}
</tr>
</thead>
<tbody>
Expand All @@ -110,7 +115,7 @@
{{ trace.http_code }}
</span>
</th>
<td>
<td{% if trace.curlCommand %} colspan="2"{% endif %}>
{{ profiler_dump(trace.info, maxDepth=1) }}
</td>
{% if profiler_token and profiler_link %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,29 @@ if (typeof Sfjs === 'undefined' || typeof Sfjs.loadToolbar === 'undefined') {
}

if (navigator.clipboard) {
document.querySelectorAll('[data-clipboard-text]').forEach(function(element) {
removeClass(element, 'hidden');
element.addEventListener('click', function() {
navigator.clipboard.writeText(element.getAttribute('data-clipboard-text'));
})
document.addEventListener('readystatechange', () => {
if (document.readyState !== 'complete') {
return;
}

document.querySelectorAll('[data-clipboard-text]').forEach(function (element) {
removeClass(element, 'hidden');
element.addEventListener('click', function () {
navigator.clipboard.writeText(element.getAttribute('data-clipboard-text'));

if (element.classList.contains("label")) {
let oldContent = element.textContent;

element.textContent = "✅ Copied!";
element.classList.add("status-success");

setTimeout(() => {
element.textContent = oldContent;
element.classList.remove("status-success");
}, 7000);
}
});
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,61 @@ private function collectOnClient(TraceableHttpClient $client): array
unset($traces[$i]['info']); // break PHP reference used by TraceableHttpClient
$traces[$i]['info'] = $this->cloneVar($info);
$traces[$i]['options'] = $this->cloneVar($trace['options']);
$traces[$i]['curlCommand'] = $this->getCurlCommand($trace);
}

return [$errorCount, $traces];
}

private function getCurlCommand(array $trace): ?string
{
$debug = explode("\n", $trace['info']['debug']);
$url = $trace['url'];
$command = ['curl', '--compressed'];

$dataArg = [];

if ($json = $trace['options']['json'] ?? null) {
$dataArg[] = '--data '.escapeshellarg(json_encode($json, \JSON_PRETTY_PRINT));
} elseif ($body = $trace['options']['body'] ?? null) {
if (\is_string($body)) {
$dataArg[] = '--data '.escapeshellarg($body);
} elseif (\is_array($body)) {
foreach ($body as $key => $value) {
$dataArg[] = '--data '.escapeshellarg("$key=$value");
}
} else {
return null;
}
}

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

foreach ($debug as $line) {
$line = substr($line, 0, -1);

if (str_starts_with('< ', $line)) {
// End of the request, beginning of the response. Stop parsing.
break;
}

if ('' === $line || preg_match('/^[*<]|(Host: )/', $line)) {
continue;
}

if (preg_match('/^> ([A-Z]+)/', $line, $match)) {
$command[] = sprintf('--request %s', $match[1]);
$command[] = sprintf('--url %s', escapeshellarg($url));
continue;
}

$command[] = '--header '.escapeshellarg($line);
}

if (null !== $dataArg) {
$command[] = $dataArg;
}

return implode(" \\\n ", $command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,81 @@ public function testItIsEmptyAfterReset()
$this->assertEquals(0, $sut->getRequestCount());
}

/**
* @requires extension openssl
*/
public function testItGeneratesCurlCommandsAsExpected()
{
$sut = new HttpClientDataCollector();
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([
[
'method' => 'GET',
'url' => 'https://symfony.com/releases.json',
],
]));
$sut->collect(new Request(), new Response());
$collectedData = $sut->getClients();
self::assertCount(1, $collectedData['http_client']['traces']);
$curlCommand = $collectedData['http_client']['traces'][0]['curlCommand'];
self::assertEquals("curl \\
--compressed \\
--request GET \\
--url 'https://symfony.com/releases.json' \\
--header 'Accept: */*' \\
--header 'Accept-Encoding: gzip' \\
--header 'User-Agent: Symfony HttpClient/Native'", $curlCommand
);
}

/**
* @requires extension openssl
*/
public function testItDoesNotFollowRedirectionsWhenGeneratingCurlCommands()
{
$sut = new HttpClientDataCollector();
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([
[
'method' => 'GET',
'url' => 'http://symfony.com/releases.json',
],
]));
$sut->collect(new Request(), new Response());
$collectedData = $sut->getClients();
self::assertCount(1, $collectedData['http_client']['traces']);
$curlCommand = $collectedData['http_client']['traces'][0]['curlCommand'];
self::assertEquals("curl \\
--compressed \\
--request GET \\
--url 'http://symfony.com/releases.json' \\
--header 'Accept: */*' \\
--header 'Accept-Encoding: gzip' \\
--header 'User-Agent: Symfony HttpClient/Native'", $curlCommand
);
}

/**
* @requires extension openssl
*/
public function testItDoesNotGeneratesCurlCommandsForUnsupportedBodyType()
{
$sut = new HttpClientDataCollector();
$sut->registerClient('http_client', $this->httpClientThatHasTracedRequests([
[
'method' => 'GET',
'url' => 'https://symfony.com/releases.json',
'options' => [
'body' => static fn (int $size): string => '',
],
],
]));
$sut->collect(new Request(), new Response());
$collectedData = $sut->getClients();
self::assertCount(1, $collectedData['http_client']['traces']);
$curlCommand = $collectedData['http_client']['traces'][0]['curlCommand'];
self::assertNull($curlCommand
);
}

private function httpClientThatHasTracedRequests($tracedRequests): TraceableHttpClient
{
$httpClient = new TraceableHttpClient(new NativeHttpClient());
Expand Down