add test for new client option
authorgggeek <giunta.gaetano@gmail.com>
Sat, 7 Sep 2024 13:26:22 +0000 (13:26 +0000)
committergggeek <giunta.gaetano@gmail.com>
Sat, 7 Sep 2024 13:26:22 +0000 (13:26 +0000)
NEWS.md
demo/server/methodProviders/testsuite.php
src/Client.php
tests/09HTTPTest.php

diff --git a/NEWS.md b/NEWS.md
index c38efa1..7972392 100644 (file)
--- a/NEWS.md
+++ b/NEWS.md
@@ -2,6 +2,8 @@
 
 * improved: compatibility with not-yet-released PHP version 8.4
 
+* improved: added new option `Client::OPT_EXTRA_HEADERS`, useful to set custom HTTP headers
+
 
 ## XML-RPC for PHP version 4.10.4 - 2024/06/27
 
index 095f610..149e9cb 100644 (file)
@@ -17,22 +17,41 @@ $getallheaders_sig = array(array(Value::$xmlrpcStruct));
 $getallheaders_doc = 'Returns a struct containing all the HTTP headers received with the request. Provides limited functionality with IIS';
 function getAllHeaders_xmlrpc($req)
 {
-    $encoder = new Encoder();
-
     if (function_exists('getallheaders')) {
-        return new Response($encoder->encode(getallheaders()));
+        $headers = getallheaders();
     } else {
+        // poor man's version of getallheaders. Thanks ralouphie/getallheaders
         $headers = array();
-        // poor man's version of getallheaders
-        foreach ($_SERVER as $key => $val) {
-            if (strpos($key, 'HTTP_') === 0) {
-                $key = ucfirst(str_replace('_', '-', strtolower(substr($key, 5))));
-                $headers[$key] = $val;
+        $copy_server = array(
+            'CONTENT_TYPE'   => 'Content-Type',
+            'CONTENT_LENGTH' => 'Content-Length',
+            'CONTENT_MD5'    => 'Content-Md5',
+        );
+        foreach ($_SERVER as $key => $value) {
+            if (substr($key, 0, 5) === 'HTTP_') {
+                $key = substr($key, 5);
+                if (!isset($copy_server[$key]) || !isset($_SERVER[$key])) {
+                    $key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', $key))));
+                    $headers[$key] = $value;
+                }
+            } elseif (isset($copy_server[$key])) {
+                $headers[$copy_server[$key]] = $value;
+            }
+        }
+        if (!isset($headers['Authorization'])) {
+            if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
+                $headers['Authorization'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
+            } elseif (isset($_SERVER['PHP_AUTH_USER'])) {
+                $basic_pass = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
+                $headers['Authorization'] = 'Basic ' . base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $basic_pass);
+            } elseif (isset($_SERVER['PHP_AUTH_DIGEST'])) {
+                $headers['Authorization'] = $_SERVER['PHP_AUTH_DIGEST'];
             }
         }
-
-        return new Response($encoder->encode($headers));
     }
+
+    $encoder = new Encoder();
+    return new Response($encoder->encode($headers));
 }
 
 // used to test mixed-convention calling
index f427e1e..9d4e538 100644 (file)
@@ -261,7 +261,7 @@ class Client
 
     /**
      * Additional headers to be included in the requests.
-     * 
+     *
      * @var string[]
      */
     protected $extra_headers = array();
@@ -1009,7 +1009,7 @@ class Client
         }
 
         $extraHeaders = '';
-        if (!empty($this->extra_headers) && is_array($this->extra_headers)) {
+        if (is_array($this->extra_headers) && $this->extra_headers) {
             $extraHeaders = implode("\r\n", $this->extra_headers) . "\r\n";
         }
 
@@ -1029,9 +1029,9 @@ class Client
             $encodingHdr .
             'Accept-Charset: ' . implode(',', $opts['accepted_charset_encodings']) . "\r\n" .
             $cookieHeader .
+            'Content-Type: ' . $req->getContentType() . "\r\n" .
             $extraHeaders .
-            'Content-Type: ' . $req->getContentType() . "\r\nContent-Length: " .
-            strlen($payload) . "\r\n\r\n" .
+            'Content-Length: ' . strlen($payload) . "\r\n\r\n" .
             $payload;
 
         if ($opts['debug'] > 1) {
@@ -1355,7 +1355,7 @@ class Client
             $headers[] = $encodingHdr;
         }
 
-        if (!empty($this->extra_headers) && is_array($this->extra_headers)) {
+        if (is_array($this->extra_headers) && $this->extra_headers) {
             $headers = array_merge($headers, $this->extra_headers);
         }
 
index 536e30f..19dd85c 100644 (file)
@@ -530,6 +530,15 @@ class HTTPTest extends ServerTest
         $this->$method();
     }
 
+    public function testCustomHeaders()
+    {
+        $this->client->setOption(\PhpXmlRpc\Client::OPT_EXTRA_HEADERS, array('X-PXR-Test: yes'));
+
+        $r = new \PhpXmlRpc\Request('tests.getallheaders');
+        $v = $this->send($r);
+        $this->assertArrayHasKey('X-Pxr-Test', $v);
+    }
+
     /**
      * @param \PhpXmlRpc\Response $r
      * @return void