From ea7eb7bf5b55e7d54f88294ce60d342d7f72aa57 Mon Sep 17 00:00:00 2001 From: gggeek Date: Tue, 24 Jan 2023 10:30:15 +0000 Subject: [PATCH] add a demo for logger injection --- NEWS.md | 10 ++-- demo/client/_prepend.php | 3 +- demo/client/loggerinjection.php | 83 +++++++++++++++++++++++++++++++++ tests/04LoggerTest.php | 1 - tests/10DemofilesTest.php | 5 ++ 5 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 demo/client/loggerinjection.php diff --git a/NEWS.md b/NEWS.md index ba4c1291..44ab8d3d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -45,13 +45,13 @@ * improved: limit the size of incoming data which will be used in error responses and logged error messages, making it slightly harder to carry out DOS attacks against the library -* fixed: when calling `Client::multicall()` with `$client->return_type = 'xml'`, we would be always falling back to - non-multicall requests - * fixed: when a server is configured with its default value of 'xmlrpcvals' for `$functions_parameters_type`, and a method handler in the dispatch was defined with `'parameters_type' = 'phpvals'`, the handler would be passed a Request object instead of plain php values. +* fixed: when calling `Client::multicall()` with `$client->return_type = 'xml'`, we would be always falling back to + non-multicall requests + * fixed: receiving integers which use the '' xml tag * new: added the `Client::setTimeout` method, meant to replace usage of the `$timeout` argument in calls to `send` @@ -76,6 +76,8 @@ * new: methods `Wrapper::holdObject()` and `Wrapper::getheldObject()`, allowing flexibility in storing object instances for code-generation scenarios involving `Wrapper::wrapPhpClass` and `Wrapper::wrapPhpFunction` +* improved: made sure all debug output goes through the logger at response parsing time (there was one printf call left) + * improved: all the Client's `setSomething()` methods now return the client object, allowing for usage of fluent style calling. The same applies to `Request::setDebug` @@ -99,7 +101,7 @@ * improved: the debugger will now sport the "load method synopsis" button when interacting with json-rpc servers -* improved: made sure the test container has at least one locale with comma as decimal separator +* improved: made sure the test container and gha test runners have at least one locale with comma as decimal separator * BC notes: diff --git a/demo/client/_prepend.php b/demo/client/_prepend.php index 269b0ddf..5f50d81b 100644 --- a/demo/client/_prepend.php +++ b/demo/client/_prepend.php @@ -21,5 +21,6 @@ if (isset($_SERVER['HTTPSERVER'])) { // A helper for cli vs web output: function output($text) { - echo PHP_SAPI == 'cli' ? strip_tags(str_replace('
', "\n", $text)) : $text; + /// @todo we should only strip html tags, and let through all xml tags + echo PHP_SAPI == 'cli' ? strip_tags(str_replace(array('
','
'), "\n", $text)) : $text; } diff --git a/demo/client/loggerinjection.php b/demo/client/loggerinjection.php new file mode 100644 index 00000000..9d2133c7 --- /dev/null +++ b/demo/client/loggerinjection.php @@ -0,0 +1,83 @@ +debugBuffer .= $message . "\n"; + } + + // logger API + public function errorLog($message) + { + $this->errorBuffer .= $message . "\n"; + } + + public function getDebug() + { + return $this->debugBuffer; + } + + public function getError() + { + return $this->errorBuffer; + } +} + +// create the custom logger instance + +$logger = new MyLogger(); + +// inject it into all the classes (possibly) involved + +Charset::setLogger($logger); +Client::setLogger($logger); +Encoder::setLogger($logger); +Http::setLogger($logger); +Request::setLogger($logger); +XMLParser::setLogger($logger); + +// then send a request + +$input = array( + array('name' => 'Dave', 'age' => 24), + array('name' => 'Edd', 'age' => 45), + array('name' => 'Joe', 'age' => 37), + array('name' => 'Fred', 'age' => 27), +); + +$encoder = new Encoder(); +$client = new Client(XMLRPCSERVER); + +// set maximum debug level, to have all the communication details logged +$client->setDebug(2); + +// avid compressed responses, as they mess up the output if echoed on the command-line +$client->setAcceptedCompression(''); + +// send request +output("Sending the request. No output debug should appear below...
"); +$request = new Request('examples.sortByAge', array($encoder->encode($input))); +$response = $client->send($request); +output("Response received.
"); + +output("The client error info is:
\n" . $logger->getError() . "\n
"); +output("The client debug info is:
\n" . $logger->getDebug() . "\n
"); diff --git a/tests/04LoggerTest.php b/tests/04LoggerTest.php index 479948fd..d1c8cbe8 100644 --- a/tests/04LoggerTest.php +++ b/tests/04LoggerTest.php @@ -23,7 +23,6 @@ class LoggerTest extends PhpXmlRpc_PolyfillTestCase $l = $ch->getLogger(); Charset::setLogger($this); - // silence the mbstring warning $ch->encodeEntities('hello world', 'UTF-8', 'NOT-A-CHARSET'); $this->assertStringContainsString("via mbstring: failed", $this->errorBuffer); diff --git a/tests/10DemofilesTest.php b/tests/10DemofilesTest.php index 1dfef489..eb4474b0 100644 --- a/tests/10DemofilesTest.php +++ b/tests/10DemofilesTest.php @@ -41,6 +41,11 @@ class DemoFilesTest extends PhpXmlRpc_WebTestCase $page = $this->request('?demo=client/getstatename.php', 'POST', array('stateno' => '1')); } + public function testLoggerInjection() + { + $page = $this->request('?demo=client/loggerinjection.php'); + } + public function testIntrospect() { $page = $this->request('?demo=client/introspect.php'); -- 2.47.0