| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient\DataCollector; |
| 13 |
|
| 14 |
use Symfony\Component\HttpClient\Exception\TransportException; |
| 15 |
use Symfony\Component\HttpClient\HttpClientTrait; |
| 16 |
use Symfony\Component\HttpClient\TraceableHttpClient; |
| 17 |
use Symfony\Component\HttpFoundation\Request; |
| 18 |
use Symfony\Component\HttpFoundation\Response; |
| 19 |
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
| 20 |
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface; |
| 21 |
use Symfony\Component\Process\Process; |
| 22 |
use Symfony\Component\VarDumper\Caster\ImgStub; |
| 23 |
|
| 24 |
|
| 25 |
r> |
| 26 |
|
| 27 |
final class HttpClientDataCollector extends DataCollector implements LateDataCollectorInterface |
| 28 |
{ |
| 29 |
use HttpClientTrait; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private array $clients = []; |
| 35 |
|
| 36 |
public function registerClient(string $name, TraceableHttpClient $client): void |
| 37 |
{ |
| 38 |
$this->clients[$name] = $client; |
| 39 |
} |
| 40 |
|
| 41 |
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void |
| 42 |
{ |
| 43 |
$this->lateCollect(); |
| 44 |
} |
| 45 |
|
| 46 |
public function lateCollect(): void |
| 47 |
{ |
| 48 |
$this->data['request_count'] ??= 0; |
| 49 |
$this->data['error_count'] ??= 0; |
| 50 |
$this->data += ['clients' => []]; |
| 51 |
|
| 52 |
foreach ($this->clients as $name => $client) { |
| 53 |
[$errorCount, $traces] = $this->collectOnClient($client); |
| 54 |
|
| 55 |
$this->data['clients'] += [ |
| 56 |
$name => [ |
| 57 |
'traces' => [], |
| 58 |
'error_count' => 0, |
| 59 |
], |
| 60 |
]; |
| 61 |
|
| 62 |
$this->data['clients'][$name]['traces'] = array_merge($this->data['clients'][$name]['traces'], $traces); |
| 63 |
$this->data['request_count'] += \count($traces); |
| 64 |
$this->data['error_count'] += $errorCount; |
| 65 |
$this->data['clients'][$name]['error_count'] += $errorCount; |
| 66 |
|
| 67 |
if ($traces) { |
| 68 |
$client->reset(); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public function getClients(): array |
| 74 |
{ |
| 75 |
return $this->data['clients'] ?? []; |
| 76 |
} |
| 77 |
|
| 78 |
public function getRequestCount(): int |
| 79 |
{ |
| 80 |
return $this->data['request_count'] ?? 0; |
| 81 |
} |
| 82 |
|
| 83 |
public function getErrorCount(): int |
| 84 |
{ |
| 85 |
return $this->data['error_count'] ?? 0; |
| 86 |
} |
| 87 |
|
| 88 |
public function getName(): string |
| 89 |
{ |
| 90 |
return 'http_client'; |
| 91 |
} |
| 92 |
|
| 93 |
public function reset(): void |
| 94 |
{ |
| 95 |
$this->data = [ |
| 96 |
'clients' => [], |
| 97 |
'request_count' => 0, |
| 98 |
'error_count' => 0, |
| 99 |
]; |
| 100 |
} |
| 101 |
|
| 102 |
private function collectOnClient(TraceableHttpClient $client): array |
| 103 |
{ |
| 104 |
$traces = $client->getTracedRequests(); |
| 105 |
$errorCount = 0; |
| 106 |
$baseInfo = [ |
| 107 |
'response_headers' => 1, |
| 108 |
'retry_count' => 1, |
| 109 |
'redirect_count' => 1, |
| 110 |
'redirect_url' => 1, |
| 111 |
'user_data' => 1, |
| 112 |
'error' => 1, |
| 113 |
'url' => 1, |
| 114 |
]; |
| 115 |
|
| 116 |
foreach ($traces as $i => $trace) { |
| 117 |
if (400 <= ($trace['info']['http_code'] ?? 0)) { |
| 118 |
++$errorCount; |
| 119 |
} |
| 120 |
|
| 121 |
$info = $trace['info']; |
| 122 |
$traces[$i]['http_code'] = $info['http_code'] ?? 0; |
| 123 |
|
| 124 |
unset($info['filetime'], $info['http_code'], $info['ssl_verify_result'], $info['content_type']); |
| 125 |
|
| 126 |
if (($info['http_method'] ?? null) === $trace['method']) { |
| 127 |
unset($info['http_method']); |
| 128 |
} |
| 129 |
|
| 130 |
if (($info['url'] ?? null) === $trace['url']) { |
| 131 |
unset($info['url']); |
| 132 |
} |
| 133 |
|
| 134 |
foreach ($info as $k => $v) { |
| 135 |
if (!$v || (is_numeric($v) && 0 > $v)) { |
| 136 |
unset($info[$k]); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if (\is_string($content = $trace['content'])) { |
| 141 |
$contentType = 'application/octet-stream'; |
| 142 |
|
| 143 |
foreach ($info['response_headers'] ?? [] as $h) { |
| 144 |
if (0 === stripos($h, 'content-type: ')) { |
| 145 |
$contentType = substr($h, \strlen('content-type: ')); |
| 146 |
break; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if (str_starts_with($contentType, 'image/') && class_exists(ImgStub::class)) { |
| 151 |
$content = new ImgStub($content, $contentType, ''); |
| 152 |
} else { |
| 153 |
$content = [$content]; |
| 154 |
} |
| 155 |
|
| 156 |
$content = ['response_content' => $content]; |
| 157 |
} elseif (\is_array($content)) { |
| 158 |
$content = ['response_json' => $content]; |
| 159 |
} else { |
| 160 |
$content = []; |
| 161 |
} |
| 162 |
|
| 163 |
if (isset($info['retry_count'])) { |
| 164 |
$content['retries'] = $info['previous_info']; |
| 165 |
unset($info['previous_info']); |
| 166 |
} |
| 167 |
|
| 168 |
$debugInfo = array_diff_key($info, $baseInfo); |
| 169 |
$info = ['info' => $debugInfo] + array_diff_key($info, $debugInfo) + $content; |
| 170 |
unset($traces[$i]['info']); |
| 171 |
$traces[$i]['info'] = $this->cloneVar($info); |
| 172 |
$traces[$i]['options'] = $this->cloneVar($trace['options']); |
| 173 |
$traces[$i]['curlCommand'] = $this->getCurlCommand($trace); |
| 174 |
} |
| 175 |
|
| 176 |
return [$errorCount, $traces]; |
| 177 |
} |
| 178 |
|
| 179 |
private function getCurlCommand(array $trace): ?string |
| 180 |
{ |
| 181 |
if (!isset($trace['info']['debug'])) { |
| 182 |
return null; |
| 183 |
} |
| 184 |
|
| 185 |
$url = $trace['info']['original_url'] ?? $trace['info']['url'] ?? $trace['url']; |
| 186 |
$command = ['curl', '--compressed']; |
| 187 |
|
| 188 |
if (isset($trace['options']['resolve'])) { |
| 189 |
$port = parse_url($url, \PHP_URL_PORT) ?: (str_starts_with('http:', $url) ? 80 : 443); |
| 190 |
foreach ($trace['options']['resolve'] as $host => $ip) { |
| 191 |
if (null !== $ip) { |
| 192 |
$command[] = '--resolve '.escapeshellarg("$host:$port:$ip"); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
$dataArg = []; |
| 198 |
|
| 199 |
if ($json = $trace['options']['json'] ?? null) { |
| 200 |
$dataArg[] = '--data-raw '.$this->escapePayload(self::jsonEncode($json)); |
| 201 |
} elseif ($body = $trace['options']['body'] ?? null) { |
| 202 |
if (\is_string($body)) { |
| 203 |
$dataArg[] = '--data-raw '.$this->escapePayload($body); |
| 204 |
} elseif (\is_array($body)) { |
| 205 |
try { |
| 206 |
$body = self::normalizeBody($body); |
| 207 |
} catch (TransportException) { |
| 208 |
return null; |
| 209 |
} |
| 210 |
if (!\is_string($body)) { |
| 211 |
return null; |
| 212 |
} |
| 213 |
foreach (explode('&', $body) as $value) { |
| 214 |
$dataArg[] = '--data-raw '.$this->escapePayload(urldecode($value)); |
| 215 |
} |
| 216 |
} else { |
| 217 |
return null; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
$dataArg = $dataArg ? implode(' ', $dataArg) : null; |
| 222 |
|
| 223 |
foreach (explode("\n", $trace['info']['debug']) as $line) { |
| 224 |
$line = substr($line, 0, -1); |
| 225 |
|
| 226 |
if (str_starts_with('< ', $line)) { |
| 227 |
|
| 228 |
break; |
| 229 |
} |
| 230 |
|
| 231 |
if (str_starts_with('Due to a bug in curl ', $line)) { |
| 232 |
|
| 233 |
return null; |
| 234 |
} |
| 235 |
|
| 236 |
if ('' === $line || preg_match('/^[*<]|(Host: )/', $line)) { |
| 237 |
continue; |
| 238 |
} |
| 239 |
|
| 240 |
if (preg_match('/^> ([A-Z]+)/', $line, $match)) { |
| 241 |
$command[] = \sprintf('--request %s', $match[1]); |
| 242 |
$command[] = \sprintf('--url %s', escapeshellarg($url)); |
| 243 |
continue; |
| 244 |
} |
| 245 |
|
| 246 |
$command[] = '--header '.escapeshellarg($line); |
| 247 |
} |
| 248 |
|
| 249 |
if (null !== $dataArg) { |
| 250 |
$command[] = $dataArg; |
| 251 |
} |
| 252 |
|
| 253 |
return implode(" \\\n ", $command); |
| 254 |
} |
| 255 |
|
| 256 |
private function escapePayload(string $payload): string |
| 257 |
{ |
| 258 |
static $useProcess; |
| 259 |
|
| 260 |
if ($useProcess ??= \function_exists('proc_open') && class_exists(Process::class)) { |
| 261 |
return substr((new Process(['', $payload]))->getCommandLine(), 3); |
| 262 |
} |
| 263 |
|
| 264 |
if ('\\' === \DIRECTORY_SEPARATOR) { |
| 265 |
return '"'.str_replace('"', '""', $payload).'"'; |
| 266 |
} |
| 267 |
|
| 268 |
return "'".str_replace("'", "'\\''", $payload)."'"; |
| 269 |
} |
| 270 |
} |