Skip to content

Commit

Permalink
Merge pull request #121 from mlocati/extra_headers-v2
Browse files Browse the repository at this point in the history
Allow specifying custom headers to be included in requests
  • Loading branch information
gggeek authored Sep 7, 2024
2 parents 18715eb + 568ae5b commit cc36f0e
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Client
const OPT_USE_CURL = 'use_curl';
const OPT_VERIFY_HOST = 'verifyhost';
const OPT_VERIFY_PEER = 'verifypeer';
const OPT_EXTRA_HEADERS = 'extra_headers';

/** @var string */
protected static $requestClass = '\\PhpXmlRpc\\Request';
Expand Down Expand Up @@ -258,6 +259,13 @@ class Client
*/
protected $user_agent;

/**
* Additional headers to be included in the requests.
*
* @var string[]
*/
protected $extra_headers = array();

/**
* CURL handle: used for keep-alive
* @internal
Expand Down Expand Up @@ -299,6 +307,7 @@ class Client
self::OPT_USERNAME,
self::OPT_VERIFY_HOST,
self::OPT_VERIFY_PEER,
self::OPT_EXTRA_HEADERS,
);

/**
Expand Down Expand Up @@ -999,6 +1008,11 @@ protected function sendViaSocket($req, $method, $server, $port, $path, $opts)
$cookieHeader = 'Cookie:' . $version . substr($cookieHeader, 0, -1) . "\r\n";
}

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

// omit port if default
if (($port == 80 && in_array($method, array('http', 'http10'))) || ($port == 443 && $method == 'https')) {
$port = '';
Expand All @@ -1015,6 +1029,7 @@ protected function sendViaSocket($req, $method, $server, $port, $path, $opts)
$encodingHdr .
'Accept-Charset: ' . implode(',', $opts['accepted_charset_encodings']) . "\r\n" .
$cookieHeader .
$extraHeaders .
'Content-Type: ' . $req->getContentType() . "\r\nContent-Length: " .
strlen($payload) . "\r\n\r\n" .
$payload;
Expand Down Expand Up @@ -1340,6 +1355,10 @@ protected function createCURLHandle($req, $method, $server, $port, $path, $opts)
$headers[] = $encodingHdr;
}

if (!empty($this->extra_headers) && is_array($this->extra_headers)) {
$headers = array_merge($headers, $this->extra_headers);
}

// Fix the HTTP/1.1 417 Expectation Failed Bug (curl by default adds a 'Expect: 100-continue' header when POST
// size exceeds 1025 bytes, apparently)
$headers[] = 'Expect:';
Expand Down

0 comments on commit cc36f0e

Please sign in to comment.