From 43ab67cced4dc6f56801695679511bd8eeab82b9 Mon Sep 17 00:00:00 2001 From: Mathieu Santostefano Date: Tue, 14 Jun 2022 17:30:05 +0200 Subject: [PATCH 1/2] Fix HttpClientTrait::jsonEncode flags usage --- DataCollector/HttpClientDataCollector.php | 2 +- Tests/DataCollector/HttpClientDataCollectorTest.php | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/DataCollector/HttpClientDataCollector.php b/DataCollector/HttpClientDataCollector.php index dc4b676..e026b42 100644 --- a/DataCollector/HttpClientDataCollector.php +++ b/DataCollector/HttpClientDataCollector.php @@ -194,7 +194,7 @@ private function getCurlCommand(array $trace): ?string $dataArg = []; if ($json = $trace['options']['json'] ?? null) { - $dataArg[] = '--data '.escapeshellarg(json_encode($json, \JSON_PRETTY_PRINT)); + $dataArg[] = '--data '.escapeshellarg(self::jsonEncode($json)); } elseif ($body = $trace['options']['body'] ?? null) { if (\is_string($body)) { try { diff --git a/Tests/DataCollector/HttpClientDataCollectorTest.php b/Tests/DataCollector/HttpClientDataCollectorTest.php index 863c91d..1eee2fd 100755 --- a/Tests/DataCollector/HttpClientDataCollectorTest.php +++ b/Tests/DataCollector/HttpClientDataCollectorTest.php @@ -307,6 +307,8 @@ public function __toString(): string 'json' => [ 'foo' => [ 'bar' => 'baz', + 'qux' => [1.10, 1.0], + 'fred' => ['',"'bar'",'"baz"','&blong&'], ], ], ], @@ -317,14 +319,10 @@ public function __toString(): string --url %1$shttp://localhost:8057/json%1$s \\ --header %1$sContent-Type: application/json%1$s \\ --header %1$sAccept: */*%1$s \\ - --header %1$sContent-Length: 21%1$s \\ + --header %1$sContent-Length: 120%1$s \\ --header %1$sAccept-Encoding: gzip%1$s \\ --header %1$sUser-Agent: Symfony HttpClient/Native%1$s \\ - --data %1$s{ - "foo": { - "bar": "baz" - } -}%1$s', + --data %1$s{"foo":{"bar":"baz","qux":[1.1,1.0],"fred":["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026"]}}%1$s', ]; } } From cb21bb66c77a9da38c0142a6477d52a4aadde06b Mon Sep 17 00:00:00 2001 From: HypeMC Date: Sun, 19 Jun 2022 00:46:53 +0200 Subject: [PATCH 2/2] [HttpClient] Fix Copy as curl with base uri --- CurlHttpClient.php | 2 +- DataCollector/HttpClientDataCollector.php | 7 +++---- Response/AmpResponse.php | 1 + Response/CurlResponse.php | 3 ++- Response/MockResponse.php | 1 + Response/NativeResponse.php | 1 + .../HttpClientDataCollectorTest.php | 16 ++++++++++++++++ 7 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CurlHttpClient.php b/CurlHttpClient.php index 9aff84d..96c37a5 100644 --- a/CurlHttpClient.php +++ b/CurlHttpClient.php @@ -301,7 +301,7 @@ public function request(string $method, string $url, array $options = []): Respo } } - return $pushedResponse ?? new CurlResponse($this->multi, $ch, $options, $this->logger, $method, self::createRedirectResolver($options, $host, $port), CurlClientState::$curlVersion['version_number']); + return $pushedResponse ?? new CurlResponse($this->multi, $ch, $options, $this->logger, $method, self::createRedirectResolver($options, $host, $port), CurlClientState::$curlVersion['version_number'], $url); } /** diff --git a/DataCollector/HttpClientDataCollector.php b/DataCollector/HttpClientDataCollector.php index dc4b676..3c8443e 100644 --- a/DataCollector/HttpClientDataCollector.php +++ b/DataCollector/HttpClientDataCollector.php @@ -178,8 +178,7 @@ private function getCurlCommand(array $trace): ?string return null; } - $debug = explode("\n", $trace['info']['debug']); - $url = self::mergeQueryString($trace['url'], $trace['options']['query'] ?? [], true); + $url = $trace['info']['original_url'] ?? $trace['info']['url'] ?? $trace['url']; $command = ['curl', '--compressed']; if (isset($trace['options']['resolve'])) { @@ -199,7 +198,7 @@ private function getCurlCommand(array $trace): ?string if (\is_string($body)) { try { $dataArg[] = '--data '.escapeshellarg($body); - } catch (\ValueError $e) { + } catch (\ValueError) { return null; } } elseif (\is_array($body)) { @@ -214,7 +213,7 @@ private function getCurlCommand(array $trace): ?string $dataArg = empty($dataArg) ? null : implode(' ', $dataArg); - foreach ($debug as $line) { + foreach (explode("\n", $trace['info']['debug']) as $line) { $line = substr($line, 0, -1); if (str_starts_with('< ', $line)) { diff --git a/Response/AmpResponse.php b/Response/AmpResponse.php index f5d0095..f107ccf 100644 --- a/Response/AmpResponse.php +++ b/Response/AmpResponse.php @@ -80,6 +80,7 @@ public function __construct(AmpClientState $multi, Request $request, array $opti $info['http_method'] = $request->getMethod(); $info['start_time'] = null; $info['redirect_url'] = null; + $info['original_url'] = $info['url']; $info['redirect_time'] = 0.0; $info['redirect_count'] = 0; $info['size_upload'] = 0.0; diff --git a/Response/CurlResponse.php b/Response/CurlResponse.php index 9a8abca..233198f 100644 --- a/Response/CurlResponse.php +++ b/Response/CurlResponse.php @@ -43,7 +43,7 @@ final class CurlResponse implements ResponseInterface, StreamableInterface /** * @internal */ - public function __construct(CurlClientState $multi, \CurlHandle|string $ch, array $options = null, LoggerInterface $logger = null, string $method = 'GET', callable $resolveRedirect = null, int $curlVersion = null) + 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) { $this->multi = $multi; @@ -69,6 +69,7 @@ public function __construct(CurlClientState $multi, \CurlHandle|string $ch, arra $this->info['user_data'] = $options['user_data'] ?? null; $this->info['max_duration'] = $options['max_duration'] ?? null; $this->info['start_time'] = $this->info['start_time'] ?? microtime(true); + $this->info['original_url'] = $originalUrl ?? $this->info['url'] ?? curl_getinfo($ch, \CURLINFO_EFFECTIVE_URL); $info = &$this->info; $headers = &$this->headers; $debugBuffer = $this->debugBuffer; diff --git a/Response/MockResponse.php b/Response/MockResponse.php index fe89cd1..044734a 100644 --- a/Response/MockResponse.php +++ b/Response/MockResponse.php @@ -142,6 +142,7 @@ public static function fromRequest(string $method, string $url, array $options, $response->info['user_data'] = $options['user_data'] ?? null; $response->info['max_duration'] = $options['max_duration'] ?? null; $response->info['url'] = $url; + $response->info['original_url'] = $url; if ($mock instanceof self) { $mock->requestOptions = $response->requestOptions; diff --git a/Response/NativeResponse.php b/Response/NativeResponse.php index 077d8f9..ab0cf09 100644 --- a/Response/NativeResponse.php +++ b/Response/NativeResponse.php @@ -66,6 +66,7 @@ public function __construct(NativeClientState $multi, $context, string $url, arr // Temporary resource to dechunk the response stream $this->buffer = fopen('php://temp', 'w+'); + $info['original_url'] = implode('', $info['url']); $info['user_data'] = $options['user_data']; $info['max_duration'] = $options['max_duration']; ++$multi->responseCount; diff --git a/Tests/DataCollector/HttpClientDataCollectorTest.php b/Tests/DataCollector/HttpClientDataCollectorTest.php index 863c91d..3a0dc64 100755 --- a/Tests/DataCollector/HttpClientDataCollectorTest.php +++ b/Tests/DataCollector/HttpClientDataCollectorTest.php @@ -194,6 +194,22 @@ public function provideCurlRequests(): iterable --url %1$shttp://localhost:8057/json%1$s \\ --header %1$sAccept: */*%1$s \\ --header %1$sAccept-Encoding: gzip%1$s \\ + --header %1$sUser-Agent: Symfony HttpClient/Native%1$s', + ]; + yield 'GET with base uri' => [ + [ + 'method' => 'GET', + 'url' => '1', + 'options' => [ + 'base_uri' => 'http://localhost:8057/json/', + ], + ], + 'curl \\ + --compressed \\ + --request GET \\ + --url %1$shttp://localhost:8057/json/1%1$s \\ + --header %1$sAccept: */*%1$s \\ + --header %1$sAccept-Encoding: gzip%1$s \\ --header %1$sUser-Agent: Symfony HttpClient/Native%1$s', ]; yield 'GET with resolve' => [ 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