[Patch] XML_RPC Client : Ability to set "Cookie" HTTP header
| From: | Luis Cruz | Date: | Thu, 22 Jan 2004 20:38:53 +0000 |
| Subject: | [Patch] XML_RPC Client : Ability to set "Cookie" HTTP header | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-25229@lists.php.net to get a copy of this message | ||
Greetings,
The enclosed patch allows the client to send an optional "Cookie" header to
an XML-RPC server. To add cookies to the header, a new function was added
to the XML_RPC_Client class along with a corresponding member variable in the
class.
$client = new XML_RPC_Client(...);
$client->setCookies($cookies);
The parameter should be an associative array of name/value pairs:
$client->setCookies(array("Customer" => "WILE_E_COYOTE"));
While one can specify all the cookie portions outlined in RFC2965
(http://www.faqs.org/rfcs/rfc2965; e.g., $Path, $Version, etc.), the
patch does not check for validity of the entries in the array. Thus, one
could create a mangled cookie header. I'll leave that for a future patch
enhancement or another project (existing or otherwise).
Comments, suggestions, etc. are welcome; but it's my first patch submission,
so please be gentle.
Regards.
---
Luis A. Cruz (lcruz@astaro.com)
Web Project Manager
Astaro (http://www.astaro.com)
? XML_RPC.diff.txt Index: RPC.php =================================================================== RCS file: /repository/pear/XML_RPC/RPC.php,v retrieving revision 1.19 diff -u -r1.19 RPC.php --- RPC.php 17 Dec 2003 21:25:28 -0000 1.19 +++ RPC.php 22 Jan 2004 20:03:38 -0000 @@ -404,6 +404,11 @@ var $username = ""; var $password = ""; + // Associative array that holds cookieName => cookieValue. + // e.g., array("Customer" => "WILE_E_COYOTE") + // [Luis A. Cruz <lcruz@astaro.com>] + var $cookies = array(); + function XML_RPC_Client($path, $server, $port = 80, $proxy = '', $proxy_port = 8080, $proxy_user = '', $proxy_pass = '') @@ -432,6 +437,12 @@ $this->password = $p; } + // [Luis A. Cruz <lcruz@astaro.com>] + function setCookies($cookies) + { + $this->cookies = $cookies; + } + function send($msg, $timeout = 0) { // where msg is an xmlrpcmsg @@ -481,6 +492,17 @@ base64_encode($username . ":" . $password) . "\r\n"; } + // Create the cookie string to send to server + // [Luis A. Cruz <lcruz@astaro.com>] + $cookieString = ""; + if ((is_array($this->cookies)) && (sizeof($this->cookies) > 0)) { + foreach ($this->cookies as $cookieName => $cookieValue) { + $cookieString .= $cookieName . "=\"" . $cookieValue . "\"; "; + } + + // Remove trailing semi-colon and whitespace. + $cookieString = rtrim($cookieString, " ;"); + } if ($this->proxy) { $op = "POST http://" . $this->server; @@ -500,6 +522,13 @@ base64_encode($this->proxy_user . ':' . $this->proxy_pass) . "\r\n"; } + + // Include cookie string if available. + // [Luis A. Cruz <lcruz@astaro.com>] + if ($cookieString != "") { + $op .= "Cookie: " . $cookieString . "\r\n"; + } + $op .= $credentials . "Content-Type: text/xml\r\n" . "Content-Length: " . strlen($msg->payload) . "\r\n\r\n" .
? XML_RPC.diff.txt Index: RPC.php =================================================================== RCS file: /repository/pear/XML_RPC/RPC.php,v retrieving revision 1.19 diff -u -r1.19 RPC.php --- RPC.php 17 Dec 2003 21:25:28 -0000 1.19 +++ RPC.php 22 Jan 2004 20:03:38 -0000 @@ -404,6 +404,11 @@ var $username = ""; var $password = ""; + // Associative array that holds cookieName => cookieValue. + // e.g., array("Customer" => "WILE_E_COYOTE") + // [Luis A. Cruz <lcruz@astaro.com>] + var $cookies = array(); + function XML_RPC_Client($path, $server, $port = 80, $proxy = '', $proxy_port = 8080, $proxy_user = '', $proxy_pass = '') @@ -432,6 +437,12 @@ $this->password = $p; } + // [Luis A. Cruz <lcruz@astaro.com>] + function setCookies($cookies) + { + $this->cookies = $cookies; + } + function send($msg, $timeout = 0) { // where msg is an xmlrpcmsg @@ -481,6 +492,17 @@ base64_encode($username . ":" . $password) . "\r\n"; } + // Create the cookie string to send to server + // [Luis A. Cruz <lcruz@astaro.com>] + $cookieString = ""; + if ((is_array($this->cookies)) && (sizeof($this->cookies) > 0)) { + foreach ($this->cookies as $cookieName => $cookieValue) { + $cookieString .= $cookieName . "=\"" . $cookieValue . "\"; "; + } + + // Remove trailing semi-colon and whitespace. + $cookieString = rtrim($cookieString, " ;"); + } if ($this->proxy) { $op = "POST http://" . $this->server; @@ -500,6 +522,13 @@ base64_encode($this->proxy_user . ':' . $this->proxy_pass) . "\r\n"; } + + // Include cookie string if available. + // [Luis A. Cruz <lcruz@astaro.com>] + if ($cookieString != "") { + $op .= "Cookie: " . $cookieString . "\r\n"; + } + $op .= $credentials . "Content-Type: text/xml\r\n" . "Content-Length: " . strlen($msg->payload) . "\r\n\r\n" .