Make WordPress Core

Ticket #10588: 10588.diff

File 10588.diff, 4.4 KB (added by ryan, 15 years ago)

Refreshed patch

  • wp-includes/class-IXR.php

     
    514514        if (!$path) {
    515515            // Assume we have been given a URL instead
    516516            $bits = parse_url($server);
     517            $this->scheme = $bits['scheme'];
    517518            $this->server = $bits['host'];
    518519            $this->port = isset($bits['port']) ? $bits['port'] : 80;
    519520            $this->path = isset($bits['path']) ? $bits['path'] : '/';
     
    522523                $this->path = '/';
    523524            }
    524525        } else {
     526            $this->scheme = 'http';
    525527            $this->server = $server;
    526528            $this->path = $path;
    527529            $this->port = $port;
     
    533535        $args = func_get_args();
    534536        $method = array_shift($args);
    535537        $request = new IXR_Request($method, $args);
    536         $length = $request->getLength();
    537538        $xml = $request->getXml();
    538         $r = "\r\n";
    539         $request  = "POST {$this->path} HTTP/1.0$r";
    540539
    541                 $this->headers['Host']                  = $this->server;
    542                 $this->headers['Content-Type']  = 'text/xml';
    543                 $this->headers['User-Agent']    = $this->useragent;
    544                 $this->headers['Content-Length']= $length;
     540        $url = $this->scheme . '://' . $this->server . ':' . $this->port . $this->path;
     541        $args = array(
     542            'headers'    => array('Content-Type' => 'text/xml'),
     543            'user-agent' => $this->useragent,
     544            'body'       => $xml,
     545        );
    545546
    546                 foreach( $this->headers as $header => $value ) {
    547                         $request .= "{$header}: {$value}{$r}";
    548                 }
    549                 $request .= $r;
     547        foreach ( $this->headers as $header => $value )
     548                    $args['headers'][$header] = $value;
    550549
    551         $request .= $xml;
     550        if ( $this->timeout !== false )
     551                    $args['timeout'] = $this->timeout;
     552
    552553        // Now send the request
    553554        if ($this->debug) {
    554             echo '<pre class="ixr_request">'.htmlspecialchars($request)."\n</pre>\n\n";
     555            echo '<pre class="ixr_request">'.htmlspecialchars($xml)."\n</pre>\n\n";
    555556        }
    556         if ($this->timeout) {
    557             $fp = @fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout);
    558         } else {
    559             $fp = @fsockopen($this->server, $this->port, $errno, $errstr);
     557
     558        $response = wp_remote_post($url, $args);
     559
     560        if ( is_wp_error($response) ) {
     561            $errno    = $response->get_error_code();
     562            $errorstr = $response->get_error_message();
     563            $this->error = new IXR_Error(-32300, "transport error: $errno $errstr");
     564            return false;
    560565        }
    561         if (!$fp) {
    562             $this->error = new IXR_Error(-32300, "transport error - could not open socket: $errno $errstr");
     566 
     567        $code = $response['response']['code'];
     568        if ( $code != 200 ) {
     569            $this->error = new IXR_Error(-32301, "transport error - HTTP status code was not 200 ($code)");
    563570            return false;
    564571        }
    565         fputs($fp, $request);
    566         $contents = '';
    567         $debug_contents = '';
    568         $gotFirstLine = false;
    569         $gettingHeaders = true;
    570         while (!feof($fp)) {
    571             $line = fgets($fp, 4096);
    572             if (!$gotFirstLine) {
    573                 // Check line for '200'
    574                 if (strstr($line, '200') === false) {
    575                     $this->error = new IXR_Error(-32301, 'transport error - HTTP status code was not 200');
    576                     return false;
    577                 }
    578                 $gotFirstLine = true;
    579             }
    580             if (trim($line) == '') {
    581                 $gettingHeaders = false;
    582             }
    583             if (!$gettingHeaders) {
    584                 // WP#12559 remove trim so as to not strip newlines from received response.
    585                 $contents .= $line;
    586             }
    587             if ($this->debug) {
    588                 $debug_contents .= $line;
    589             }
    590         }
     572
    591573        if ($this->debug) {
    592             echo '<pre class="ixr_response">'.htmlspecialchars($debug_contents)."\n</pre>\n\n";
     574            echo '<pre class="ixr_response">'.htmlspecialchars($response['body'])."\n</pre>\n\n";
    593575        }
    594576        // Now parse what we've got back
    595         $this->message = new IXR_Message($contents);
     577        $this->message = new IXR_Message($response['body']);
    596578        if (!$this->message->parse()) {
    597579            // XML error
    598580            $this->error = new IXR_Error(-32700, 'parse error. not well formed');