From cc91a90fade2690f63d89edf73d81854cd6fc4bb Mon Sep 17 00:00:00 2001 From: gggeek Date: Sat, 7 Sep 2024 13:26:22 +0000 Subject: [PATCH] add test for new client option --- NEWS.md | 2 ++ demo/server/methodProviders/testsuite.php | 39 +++++++++++++++++------ src/Client.php | 10 +++--- tests/09HTTPTest.php | 9 ++++++ 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/NEWS.md b/NEWS.md index c38efa19..79723929 100644 --- 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 diff --git a/demo/server/methodProviders/testsuite.php b/demo/server/methodProviders/testsuite.php index 095f610b..149e9cbd 100644 --- a/demo/server/methodProviders/testsuite.php +++ b/demo/server/methodProviders/testsuite.php @@ -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 diff --git a/src/Client.php b/src/Client.php index f427e1ea..9d4e5386 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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); } diff --git a/tests/09HTTPTest.php b/tests/09HTTPTest.php index 536e30f4..19dd85c7 100644 --- a/tests/09HTTPTest.php +++ b/tests/09HTTPTest.php @@ -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 -- 2.47.0