demo nitpicks
authorgggeek <giunta.gaetano@gmail.com>
Tue, 17 Jan 2023 12:07:38 +0000 (12:07 +0000)
committergggeek <giunta.gaetano@gmail.com>
Tue, 17 Jan 2023 12:07:38 +0000 (12:07 +0000)
demo/client/windowscharset.php [new file with mode: 0644]
demo/client/wrap.php
demo/server/discuss.php
demo/server/methodProviders/functions.php
demo/server/proxy.php
demo/vardemo.php

diff --git a/demo/client/windowscharset.php b/demo/client/windowscharset.php
new file mode 100644 (file)
index 0000000..c785cd7
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+require_once __DIR__ . "/_prepend.php";
+
+/**
+ * Demoing the charset conversion of the library: create a client class which uses data in the CP-1252 character set,
+ * regardless of the character set used by the server
+ */
+
+use PhpXmlRpc\Client;
+use PhpXmlRpc\PhpXmlRpc;
+use PhpXmlRpc\Request;
+use PhpXmlRpc\Value;
+
+PhpXmlRpc::$xmlrpc_internalencoding = 'Windows-1252';
+
+// this is a very contrived way of creating a CP-1252 string...
+$input = 'Euro sign is €, per mille is ‰, trademark is ™, copyright is ©, smart quotes are “these”';
+//var_dump(mb_list_encodings());
+echo "This is the value we start with (in UTF-8): ";
+var_dump($input);
+
+$input = mb_convert_encoding($input, 'Windows-1252', 'UTF-8');
+
+echo "In CP-1252, it looks like this: ";
+var_dump($input);
+
+$c = new Client(XMLRPCSERVER);
+
+// allow the full request and response to be seen on screen
+$c->setDebug(2);
+// tell the server not to compress the response
+$c->accepted_compression = array();
+// tell the server not to encode everything as ASCII - this is not necessary btw. It is only done to make the demo nicer
+$c->accepted_charset_encodings = array('UTF-8');
+// force the client not to encode everything as ASCII - this is not necessary btw. It is only done to make the demo nicer
+$c->request_charset_encoding = 'UTF-8';
+
+$r = $c->send(new Request('examples.stringecho', array(new Value($input))));
+$output = $r->value()->scalarval();
+
+echo "This is the value we got back from the server (in CP-1252): ";
+var_dump($output);
+
+echo "In UTF-8, it looks like this: ";
+var_dump(mb_convert_encoding($output, 'UTF-8', 'Windows-1252'));
index aaf7b70..beb271d 100644 (file)
@@ -11,7 +11,7 @@ output('<html lang="en">
 <h3>The code demonstrates usage of some of the most automagic client usage possible:<br/>
     1) client that returns php values instead of xml-rpc Value objects<br/>
     2) wrapping of remote methods into php functions<br/>
-    See also proxy.php for an alternative take
+    See also proxy.php and codegen.php for alternative takes
 </h3>
 <p>You can see the source to this page here: <a href="wrap.php?showSource=1">wrap.php</a></p>
 ');
index b31dc3c..3288c09 100644 (file)
@@ -27,7 +27,9 @@ $getComments_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcString));
 $getComments_doc = 'Returns an array of comments for a given ID, which is the sole argument. Each array item is a struct ' .
     'containing name and comment text.';
 
-$srv = new Server(array(
+$srv = new Server();
+
+$srv->setDispatchMap(array(
     "discuss.addComment" => array(
         "function" => array($manager, "addComment"),
         "signature" => $addComment_sig,
@@ -38,9 +40,9 @@ $srv = new Server(array(
         "signature" => $getComments_sig,
         "docstring" => $getComments_doc,
     ),
-), false);
+));
 
-// let the xml=rpc server know that the method-handler functions expect plain php values
+// let the xml-rpc server know that the method-handler functions expect plain php values
 $srv->functions_parameters_type = 'phpvals';
 
 // let code exceptions float all the way to the remote caller as xml-rpc faults - it helps debugging
index 58444e0..2949034 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 use PhpXmlRpc\Encoder;
+use PhpXmlRpc\PhpXmlRpc;
 use PhpXmlRpc\Response;
 use PhpXmlRpc\Server;
 use PhpXmlRpc\Value;
@@ -62,7 +63,7 @@ class exampleMethods
 
         if ($err != '') {
             // if we generated an error, create an error return response
-            return new Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
+            return new Response(0, PhpXmlRpc::$xmlrpcerruser, $err);
         } else {
             // otherwise, we create the right response with the state name
             return new Response(new Value($stateName));
@@ -108,7 +109,7 @@ And the array will be returned with the entries sorted by their numbers.';
 
         if ($err != '') {
             Server::xmlrpc_debugmsg("Aborting 'agesorter'");
-            return new Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
+            return new Response(0, PhpXmlRpc::$xmlrpcerruser, $err);
         }
 
         asort($agar);
@@ -156,8 +157,15 @@ And the array will be returned with the entries sorted by their numbers.';
     public static $echoback_doc = 'Accepts a string parameter, returns the entire incoming payload';
     public static function echoBack($req)
     {
-        // just sends back a string with what I got sent to me, that's all (escaping for xml is automatic)
-        $s = "I got the following message:\n" . $req->serialize();
+        // just sends back a string with what I got sent to me, that's all
+
+        /// @todo file_get_contents does not take into account either receiving compressed requests, or requests with
+        ///       data which is not in UTF-8. Otoh using req->serialize means that what we are sending back is not
+        ///       byte-for-byte identical to what we received, and that <, >, ', " and & will be double-encoded.
+        ///       In fact, we miss some API (or extra data) in the Request...
+        //$payload = file_get_contents('php://input');
+        $payload = $req->serialize(PhpXmlRpc::$xmlrpc_internalencoding);
+        $s = "I got the following message:\n" . $payload;
 
         return new Response(new Value($s));
     }
@@ -251,7 +259,7 @@ mimetype, a string, is a standard MIME type, for example, text/plain.';
         }
 
         if ($err) {
-            return new Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
+            return new Response(0, PhpXmlRpc::$xmlrpcerruser, $err);
         } else {
             return new Response(new Value(true, Value::$xmlrpcBoolean));
         }
index 95b4390..30cc317 100644 (file)
@@ -47,17 +47,35 @@ function forward_request($req)
     $client = new Client($url);
 
     if ($req->getNumParams() > 3) {
-        // we have to set some options onto the client.
+        // We have to set some options onto the client.
         // Note that if we do not untaint the received values, warnings might be generated...
         $options = $encoder->decode($req->getParam(3));
         foreach ($options as $key => $val) {
             switch ($key) {
-                case 'Cookie':
+                case 'authType':
+                    /// @todo add support for this if needed
+                    break;
+                case 'followRedirects':
+                    // requires cURL to be enabled
+                    if ($val) {
+                        $client->use_curl = Client::USE_CURL_ALWAYS;
+                        $client->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => true, CURLOPT_POSTREDIR => 3));
+                    }
+                case 'Cookies':
                     /// @todo add support for this if needed
                     break;
                 case 'Credentials':
                     /// @todo add support for this as well if needed
                     break;
+                case 'HTTPProxy':
+                case 'HTTPProxyCredentials':
+                    /// @todo add support for this as well if needed
+                    break;
+                case 'RequestCharsetEncoding':
+                    // allow the server to work as charset transcoder.
+                    // NB: works best with mbstring enabled
+                    $client->request_charset_encoding = $val;
+                    break;
                 case 'RequestCompression':
                     $client->setRequestCompression($val);
                     break;
index 22c5429..d693d76 100644 (file)
@@ -89,6 +89,9 @@ output("<h3>Testing value serialization - character encoding</h3>\n");
 $v = new PhpXmlRpc\Value('κόσμε');
 output("Greek (default encoding): <PRE>" . htmlentities($v->serialize()) . "</PRE>");
 output("Greek (utf8 encoding): <PRE>" . htmlentities($v->serialize('UTF-8')) . "</PRE>");
+if (function_exists('mb_convert_encoding')) {
+    output("Greek (ISO-8859-7 encoding): <PRE>" . htmlentities($v->serialize('ISO-8859-7')) . "</PRE>");
+}
 
 output("<h3>Testing request serialization</h3>\n");
 $req = new PhpXmlRpc\Request('examples.getStateName');