Repository: symfony/symfony
File: src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php (PHP)

Code before:

        if (\is_object($controller)) {
            $r = new \ReflectionClass($controller);

            return [
                'class' => $r->getName(),
                'method' => null,
                'file' => $r->getFileName(),
                'line' => $r->getStartLine(),
            ];
        }

        return \is_string($controller) ? $controller : 'n/a';
    }

    public function getCurlCommand(): ?string
    {
        if (!isset($this->data['method']) || !isset($this->data['path_info'])) {
            return null;
        }

        $command = ['curl', '--compressed'];

        // Build the full URL
        $scheme = $this->data['request_server']['HTTPS'] ?? false ? 'https' : 'http';
        $host = $this->data['request_server']['HTTP_HOST'] ?? $this->data['request_server']['SERVER_NAME'] ?? 'localhost';
        $url = $scheme.'://'.$host.$this->data['path_info'];

        if (!empty($this->data['request_query'])) {
            $url .= '?'.http_build_query($this->data['request_query']->getValue());
        }

        // Add HTTP method
        $method = $this->data['method'];
        if (Request::METHOD_GET !== $method) {
            $command[] = \sprintf('--request %s', $method);
        }

        $command[] = \sprintf('--url %s', escapeshellarg($url));

        // Add headers
        foreach ((\is_array($this->data['request_headers']) ? $this->data['request_headers'] : $this->data['request_headers']->getValue()) as $name => $value) {
            if (\is_array($value)) {
                $value = implode(', ', $value);
            }

            // Skip certain headers
            if (\in_array(strtolower($name), ['host', 'cookie'])) {
                continue;
            }


Review comment (bug):
this is the wrong way to implement this, as it won't take trusted proxies into account.
The appropriate getters of `Request` must be used (which might imply collecting them in the data if not done already)

Code after:
        }

        if (\is_object($controller)) {
            $r = new \ReflectionClass($controller);

            return [
                'class' => $r->getName(),
                'method' => null,
                'file' => $r->getFileName(),
                'line' => $r->getStartLine(),
            ];
        }

        return \is_string($controller) ? $controller : 'n/a';
    }

    private function computeCurlCommand(Request $request, ?string $content): string
    {
        $command = ['curl', '--compressed'];

        $method = $request->getMethod();

        if (Request::METHOD_HEAD === $method) {
            $command[] = '--head';
        } elseif (Request::METHOD_GET !== $method) {
            $command[] = \sprintf('--request %s', $method);
        }

        $command[] = \sprintf('--url %s', escapeshellarg($request->getUri()));

        foreach ($request->headers->all() as $name => $values) {
            if (\in_array(strtolower($name), ['host', 'cookie'], true)) {
                continue;
            }

            $command[] = '--header '.escapeshellarg(ucwords($name, '-').': '.implode(', ', $values));
        }

        if ($request->cookies->all()) {
            $cookies = [];
            foreach ($request->cookies->all() as $name => $value) {
                $cookies[] = urlencode($name).'='.urlencode($value);
            }
            $command[] = '--cookie '.escapeshellarg(implode('; ', $cookies));
        }

        if ($content && \in_array($method, [Request::METHOD_POST, Request::METHOD_PUT, Request::METHOD_PATCH, Request::METHOD_DELETE], true)) {
            $command[] = '--data-raw '.$this->escapePayload($content);
        }

        return implode(" \\\n  ", $command);