From 059c03b9d791c26ea3cff32f6aed5c046d2640cc Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Wed, 27 Feb 2008 22:54:08 +0100
Subject: [PATCH] use DokuHTTPClient in XMLRPC library

The code should work but is completely untested because it is currently not used.

darcs-hash:20080227215408-7ad00-952ebba433991bde0c4282beeb4887c637eb25aa.gz
---
 inc/HTTPClient.php  | 23 ++++++++---
 inc/IXR_Library.php | 94 +++++++++++++--------------------------------
 2 files changed, 45 insertions(+), 72 deletions(-)

diff --git a/inc/HTTPClient.php b/inc/HTTPClient.php
index e34027d02..d40a7b4db 100644
--- a/inc/HTTPClient.php
+++ b/inc/HTTPClient.php
@@ -139,8 +139,18 @@ class HTTPClient {
     }
 
     /**
-     * Do an HTTP request
+     * Send an HTTP request
      *
+     * This method handles the whole HTTP communication. It respects set proxy settings,
+     * builds the request headers, follows redirects and parses the response.
+     *
+     * Post data should be passed as associative array. When passed as string it will be
+     * sent as is. You will need to setup your own Content-Type header then.
+     *
+     * @param  string $url    - the complete URL
+     * @param  mixed  $data   - the post data
+     * @param  string $method - HTTP Method usually GET or POST.
+     * @return bool - true on success
      * @author Andreas Goetz <cpuidle@gmx.de>
      * @author Andreas Gohr <andi@splitbrain.org>
      */
@@ -181,9 +191,12 @@ class HTTPClient {
         $headers['Referer']    = $this->referer;
         $headers['Connection'] = 'Close';
         if($method == 'POST'){
-            $post = $this->_postEncode($data);
-            $headers['Content-Type']   = 'application/x-www-form-urlencoded';
-            $headers['Content-Length'] = strlen($post);
+            if(is_array($data)){
+                $headers['Content-Type']   = 'application/x-www-form-urlencoded';
+                $data = $this->_postEncode($data);
+            }
+            $headers['Content-Length'] = strlen($data);
+            $rmethod = 'POST';
         }
         if($this->user) {
             $headers['Authorization'] = 'Basic '.base64_encode($this->user.':'.$this->pass);
@@ -210,7 +223,7 @@ class HTTPClient {
         $request .= $this->_buildHeaders($headers);
         $request .= $this->_getCookies();
         $request .= HTTP_NL;
-        $request .= $post;
+        $request .= $data;
 
         $this->_debug('request',$request);
 
diff --git a/inc/IXR_Library.php b/inc/IXR_Library.php
index 5a4859813..e46ebb20a 100644
--- a/inc/IXR_Library.php
+++ b/inc/IXR_Library.php
@@ -470,83 +470,43 @@ EOD;
     }
 }
 
-# FIXME use DokuHTTPClient here
-class IXR_Client {
-    var $server;
-    var $port;
-    var $path;
-    var $useragent;
-    var $response;
+/**
+ * Changed for DokuWiki to use DokuHTTPClient
+ *
+ * This should be compatible to the original class, but uses DokuWiki's
+ * HTTP client library which will respect proxy settings
+ *
+ * Because the XMLRPC client is not used in DokuWiki currently this is completely
+ * untested
+ */
+class IXR_Client extends DokuHTTPClient {
+    var $posturl = '';
     var $message = false;
-    var $debug = false;
-    // Storage place for an error message
-    var $error = false;
+    var $xmlerror = false;
+
     function IXR_Client($server, $path = false, $port = 80) {
         if (!$path) {
             // Assume we have been given a URL instead
-            $bits = parse_url($server);
-            $this->server = $bits['host'];
-            $this->port = isset($bits['port']) ? $bits['port'] : 80;
-            $this->path = isset($bits['path']) ? $bits['path'] : '/';
-            // Make absolutely sure we have a path
-            if (!$this->path) {
-                $this->path = '/';
-            }
-        } else {
-            $this->server = $server;
-            $this->path = $path;
-            $this->port = $port;
+            $this->posturl = $server;
+        }else{
+            $this->posturl = 'http://'.$server.':'.$port.$path;
         }
-        $this->useragent = 'The Incutio XML-RPC PHP Library';
     }
+
     function query() {
         $args = func_get_args();
         $method = array_shift($args);
         $request = new IXR_Request($method, $args);
-        $length = $request->getLength();
         $xml = $request->getXml();
-        $r = "\r\n";
-        $request  = "POST {$this->path} HTTP/1.0$r";
-        $request .= "Host: {$this->server}$r";
-        $request .= "Content-Type: text/xml$r";
-        $request .= "User-Agent: {$this->useragent}$r";
-        $request .= "Content-length: {$length}$r$r";
-        $request .= $xml;
-        // Now send the request
-        if ($this->debug) {
-            echo '<pre>'.htmlspecialchars($request)."\n</pre>\n\n";
-        }
-        $fp = @fsockopen($this->server, $this->port);
-        if (!$fp) {
-            $this->error = new IXR_Error(-32300, 'transport error - could not open socket');
+
+        $this->$headers['Content-Type'] = 'text/xml';
+        if(!$this->sendRequest($this->posturl,$xml,'RAW')){
+            $this->xmlerror = new IXR_Error(-32300, 'transport error - '.$this->error);
             return false;
         }
-        fputs($fp, $request);
-        $contents = '';
-        $gotFirstLine = false;
-        $gettingHeaders = true;
-        while (!feof($fp)) {
-            $line = fgets($fp, 4096);
-            if (!$gotFirstLine) {
-                // Check line for '200'
-                if (strstr($line, '200') === false) {
-                    $this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200');
-                    return false;
-                }
-                $gotFirstLine = true;
-            }
-            if (trim($line) == '') {
-                $gettingHeaders = false;
-            }
-            if (!$gettingHeaders) {
-                $contents .= trim($line)."\n";
-            }
-        }
-        if ($this->debug) {
-            echo '<pre>'.htmlspecialchars($contents)."\n</pre>\n\n";
-        }
+
         // Now parse what we've got back
-        $this->message = new IXR_Message($contents);
+        $this->message = new IXR_Message($this->resp_body);
         if (!$this->message->parse()) {
             // XML error
             $this->error = new IXR_Error(-32700, 'parse error. not well formed');
@@ -565,13 +525,13 @@ class IXR_Client {
         return $this->message->params[0];
     }
     function isError() {
-        return (is_object($this->error));
+        return (is_object($this->xmlerror));
     }
     function getErrorCode() {
-        return $this->error->code;
+        return $this->xmlerror->code;
     }
     function getErrorMessage() {
-        return $this->error->message;
+        return $this->xmlerror->message;
     }
 }
 
@@ -812,7 +772,7 @@ class IXR_ClientMulticall extends IXR_Client {
     var $calls = array();
     function IXR_ClientMulticall($server, $path = false, $port = 80) {
         parent::IXR_Client($server, $path, $port);
-        $this->useragent = 'The Incutio XML-RPC PHP Library (multicall client)';
+        //$this->useragent = 'The Incutio XML-RPC PHP Library (multicall client)';
     }
     function addCall() {
         $args = func_get_args();
-- 
GitLab