Skip to content

Commit cc91a90

Browse files
committed
add test for new client option
1 parent cc36f0e commit cc91a90

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* improved: compatibility with not-yet-released PHP version 8.4
44

5+
* improved: added new option `Client::OPT_EXTRA_HEADERS`, useful to set custom HTTP headers
6+
57

68
## XML-RPC for PHP version 4.10.4 - 2024/06/27
79

demo/server/methodProviders/testsuite.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,41 @@
1717
$getallheaders_doc = 'Returns a struct containing all the HTTP headers received with the request. Provides limited functionality with IIS';
1818
function getAllHeaders_xmlrpc($req)
1919
{
20-
$encoder = new Encoder();
21-
2220
if (function_exists('getallheaders')) {
23-
return new Response($encoder->encode(getallheaders()));
21+
$headers = getallheaders();
2422
} else {
23+
// poor man's version of getallheaders. Thanks ralouphie/getallheaders
2524
$headers = array();
26-
// poor man's version of getallheaders
27-
foreach ($_SERVER as $key => $val) {
28-
if (strpos($key, 'HTTP_') === 0) {
29-
$key = ucfirst(str_replace('_', '-', strtolower(substr($key, 5))));
30-
$headers[$key] = $val;
25+
$copy_server = array(
26+
'CONTENT_TYPE' => 'Content-Type',
27+
'CONTENT_LENGTH' => 'Content-Length',
28+
'CONTENT_MD5' => 'Content-Md5',
29+
);
30+
foreach ($_SERVER as $key => $value) {
31+
if (substr($key, 0, 5) === 'HTTP_') {
32+
$key = substr($key, 5);
33+
if (!isset($copy_server[$key]) || !isset($_SERVER[$key])) {
34+
$key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', $key))));
35+
$headers[$key] = $value;
36+
}
37+
} elseif (isset($copy_server[$key])) {
38+
$headers[$copy_server[$key]] = $value;
39+
}
40+
}
41+
if (!isset($headers['Authorization'])) {
42+
if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
43+
$headers['Authorization'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
44+
} elseif (isset($_SERVER['PHP_AUTH_USER'])) {
45+
$basic_pass = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
46+
$headers['Authorization'] = 'Basic ' . base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $basic_pass);
47+
} elseif (isset($_SERVER['PHP_AUTH_DIGEST'])) {
48+
$headers['Authorization'] = $_SERVER['PHP_AUTH_DIGEST'];
3149
}
3250
}
33-
34-
return new Response($encoder->encode($headers));
3551
}
52+
53+
$encoder = new Encoder();
54+
return new Response($encoder->encode($headers));
3655
}
3756

3857
// used to test mixed-convention calling

src/Client.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class Client
261261

262262
/**
263263
* Additional headers to be included in the requests.
264-
*
264+
*
265265
* @var string[]
266266
*/
267267
protected $extra_headers = array();
@@ -1009,7 +1009,7 @@ protected function sendViaSocket($req, $method, $server, $port, $path, $opts)
10091009
}
10101010

10111011
$extraHeaders = '';
1012-
if (!empty($this->extra_headers) && is_array($this->extra_headers)) {
1012+
if (is_array($this->extra_headers) && $this->extra_headers) {
10131013
$extraHeaders = implode("\r\n", $this->extra_headers) . "\r\n";
10141014
}
10151015

@@ -1029,9 +1029,9 @@ protected function sendViaSocket($req, $method, $server, $port, $path, $opts)
10291029
$encodingHdr .
10301030
'Accept-Charset: ' . implode(',', $opts['accepted_charset_encodings']) . "\r\n" .
10311031
$cookieHeader .
1032+
'Content-Type: ' . $req->getContentType() . "\r\n" .
10321033
$extraHeaders .
1033-
'Content-Type: ' . $req->getContentType() . "\r\nContent-Length: " .
1034-
strlen($payload) . "\r\n\r\n" .
1034+
'Content-Length: ' . strlen($payload) . "\r\n\r\n" .
10351035
$payload;
10361036

10371037
if ($opts['debug'] > 1) {
@@ -1355,7 +1355,7 @@ protected function createCURLHandle($req, $method, $server, $port, $path, $opts)
13551355
$headers[] = $encodingHdr;
13561356
}
13571357

1358-
if (!empty($this->extra_headers) && is_array($this->extra_headers)) {
1358+
if (is_array($this->extra_headers) && $this->extra_headers) {
13591359
$headers = array_merge($headers, $this->extra_headers);
13601360
}
13611361

tests/09HTTPTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,15 @@ public function testDigestAuth($method)
530530
$this->$method();
531531
}
532532

533+
public function testCustomHeaders()
534+
{
535+
$this->client->setOption(\PhpXmlRpc\Client::OPT_EXTRA_HEADERS, array('X-PXR-Test: yes'));
536+
537+
$r = new \PhpXmlRpc\Request('tests.getallheaders');
538+
$v = $this->send($r);
539+
$this->assertArrayHasKey('X-Pxr-Test', $v);
540+
}
541+
533542
/**
534543
* @param \PhpXmlRpc\Response $r
535544
* @return void

0 commit comments

Comments
 (0)