From 8af9f32b068e3b8a94a1027f37f9b2f31d6ae9db Mon Sep 17 00:00:00 2001 From: bch36 <31776675+bch36@users.noreply.github.com> Date: Thu, 10 Oct 2024 12:57:46 -0500 Subject: [PATCH 1/2] Fix bug parsing response into header and body The response variable may contain binary data, making mb_substr the incorrect function to use. Pre PHP 8.3.2, mb_substr would encounter invalid UTF characters in the binary data and fall back to using substr, but from 8.3.2 on, the behavior of mb_substr has changed to replacing the invalid UTF characters with "?". This results in corrupted binary data. --- src/Core/HttpClients/CurlHttpClient.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Core/HttpClients/CurlHttpClient.php b/src/Core/HttpClients/CurlHttpClient.php index f5c66634..a4e0dbde 100644 --- a/src/Core/HttpClients/CurlHttpClient.php +++ b/src/Core/HttpClients/CurlHttpClient.php @@ -118,8 +118,8 @@ private function handleErrors(){ */ public function setIntuitResponse($response){ $headerSize = $this->basecURL->getInfo(CURLINFO_HEADER_SIZE); - $rawHeaders = mb_substr($response, 0, $headerSize); - $rawBody = mb_substr($response, $headerSize); + $rawHeaders = substr($response, 0, $headerSize); + $rawBody = substr($response, $headerSize); $httpStatusCode = $this->basecURL->getInfo(CURLINFO_HTTP_CODE); $theIntuitResponse = new IntuitResponse($rawHeaders, $rawBody, $httpStatusCode, true); $this->intuitResponse = $theIntuitResponse; From f80bf2cef27878df08867b9f6efcecbca3ceeea2 Mon Sep 17 00:00:00 2001 From: Ben Hardin Date: Wed, 11 Dec 2024 11:44:13 -0600 Subject: [PATCH 2/2] Request/response log message callback Add a callback function to custom-handler request/response log messages. --- src/DataService/DataService.php | 17 +++++++++++++- src/Diagnostics/LogRequestsToDisk.php | 34 ++++++++++++++++++++------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/DataService/DataService.php b/src/DataService/DataService.php index 688ab41b..de31cf2b 100644 --- a/src/DataService/DataService.php +++ b/src/DataService/DataService.php @@ -270,7 +270,22 @@ public function setLogLocation($new_log_location) return $this; } - /** + /** (bch36) + * Set a callback function for request and response logs + * + * @param callable $callback The callback function for receiving request and response logs + * + * @return $this + */ + public function setLogCallback($callback) + { + $restHandler = $this->restHandler; + $loggerUsedByRestHandler = $restHandler->getRequestLogger(); + $loggerUsedByRestHandler->setLogCallback($callback); + return $this; + } + + /** * Set logging for OAuth calls * * @param Boolean $enableLogs Turns on logging for OAuthCalls diff --git a/src/Diagnostics/LogRequestsToDisk.php b/src/Diagnostics/LogRequestsToDisk.php index 4100ecd8..25cd4f0c 100644 --- a/src/Diagnostics/LogRequestsToDisk.php +++ b/src/Diagnostics/LogRequestsToDisk.php @@ -23,6 +23,12 @@ class LogRequestsToDisk */ public $ServiceRequestLoggingLocation; + /** + * The Service Request Logging Callback. (bch36) + * @var callable + */ + public $ServiceRequestLoggingCallback; + /** * Initializes a new instance of the LogRequestsToDisk class. * @param bool enableServiceRequestLogging Value indicating whether to log request response messages @@ -50,6 +56,15 @@ public function setLogDirectory($logDirectory){ $this->ServiceRequestLoggingLocation = $logDirectory; } + /** (bch36) + * Set a callback function for request and response logs + * @param callable $callback The callback function for receiving request and response logs + */ + public function setLogCallback($callback) + { + $this->ServiceRequestLoggingCallback = $callback; + } + /** * Gets the log destination folder * @return string log destination folder @@ -104,15 +119,16 @@ public function LogPlatformRequests($xml, $url, $headers, $isRequest) $collapsedHeaders[] = "{$key}: {$val}"; } - file_put_contents($filePath, - ($isRequest?"REQUEST":"RESPONSE")." URI FOR SEQUENCE ID {$sequenceNumber}\n==================================\n{$url}\n\n", - FILE_APPEND); - file_put_contents($filePath, - ($isRequest?"REQUEST":"RESPONSE")." HEADERS\n================\n".implode("\n", $collapsedHeaders)."\n\n", - FILE_APPEND); - file_put_contents($filePath, - ($isRequest?"REQUEST":"RESPONSE")." BODY\n=============\n".$xml."\n\n", - FILE_APPEND); + $message = + ($isRequest?"REQUEST":"RESPONSE")." URI FOR SEQUENCE ID {$sequenceNumber}\n==================================\n{$url}\n\n" . + ($isRequest?"REQUEST":"RESPONSE")." HEADERS\n================\n".implode("\n", $collapsedHeaders)."\n\n" . + ($isRequest?"REQUEST":"RESPONSE")." BODY\n=============\n".$xml."\n\n"; + + file_put_contents($filePath, $message); + + if (is_callable($this->ServiceRequestLoggingCallback)) + call_user_func($this->ServiceRequestLoggingCallback, $isRequest, $message, $url, $xml, $headers); + } catch (\Exception $e) { throw new IdsException("Exception during LogPlatformRequests."); }