-class WP_Http {
-
- /**
- * PHP4 style Constructor - Calls PHP5 Style Constructor
- *
- * @since 2.7
- * @return WP_Http
- */
- function WP_Http() {
- $this->__construct();
- }
-
- /**
- * PHP5 style Constructor - Setup available transport if not available.
- *
- * PHP4 does not have the 'self' keyword and since WordPress supports PHP4,
- * the class needs to be used for the static call.
- *
- * The transport are setup to save time. This should only be called once, so
- * the overhead should be fine.
- *
- * @since 2.7
- * @return WP_Http
- */
- function __construct() {
- WP_Http::_getTransport();
- WP_Http::_postTransport();
- }
-
- /**
- * Tests the WordPress HTTP objects for an object to use and returns it.
- *
- * Tests all of the objects and returns the object that passes. Also caches
- * that object to be used later.
- *
- * The order for the GET/HEAD requests are Streams, HTTP Extension, Fopen,
- * and finally Fsockopen. fsockopen() is used last, because it has the most
- * overhead in its implementation. There isn't any real way around it, since
- * redirects have to be supported, much the same way the other transports
- * also handle redirects.
- *
- * There are currently issues with "localhost" not resolving correctly with
- * DNS. This may cause an error "failed to open stream: A connection attempt
- * failed because the connected party did not properly respond after a
- * period of time, or established connection failed because connected host
- * has failed to respond."
- *
- * @since 2.7
- * @access private
- *
- * @param array $args Request args, default us an empty array
- * @return object|null Null if no transports are available, HTTP transport object.
- */
- function &_getTransport( $args = array() ) {
- static $working_transport, $blocking_transport, $nonblocking_transport;
-
- if ( is_null($working_transport) ) {
- if ( true === WP_Http_ExtHttp::test() && apply_filters('use_http_extension_transport', true) ) {
- $working_transport['exthttp'] = new WP_Http_ExtHttp();
- $blocking_transport[] = &$working_transport['exthttp'];
- } else if ( true === WP_Http_Curl::test() && apply_filters('use_curl_transport', true) ) {
- $working_transport['curl'] = new WP_Http_Curl();
- $blocking_transport[] = &$working_transport['curl'];
- } else if ( true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true) ) {
- $working_transport['streams'] = new WP_Http_Streams();
- $blocking_transport[] = &$working_transport['streams'];
- } else if ( true === WP_Http_Fopen::test() && apply_filters('use_fopen_transport', true) ) {
- $working_transport['fopen'] = new WP_Http_Fopen();
- $blocking_transport[] = &$working_transport['fopen'];
- } else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) ) {
- $working_transport['fsockopen'] = new WP_Http_Fsockopen();
- $blocking_transport[] = &$working_transport['fsockopen'];
- }
-
- foreach ( array('curl', 'streams', 'fopen', 'fsockopen', 'exthttp') as $transport ) {
- if ( isset($working_transport[$transport]) )
- $nonblocking_transport[] = &$working_transport[$transport];
- }
- }
-
- if ( isset($args['blocking']) && !$args['blocking'] )
- return $nonblocking_transport;
- else
- return $blocking_transport;
- }
-
- /**
- * Tests the WordPress HTTP objects for an object to use and returns it.
- *
- * Tests all of the objects and returns the object that passes. Also caches
- * that object to be used later. This is for posting content to a URL and
- * is used when there is a body. The plain Fopen Transport can not be used
- * to send content, but the streams transport can. This is a limitation that
- * is addressed here, by just not including that transport.
- *
- * @since 2.7
- * @access private
- *
- * @param array $args Request args, default us an empty array
- * @return object|null Null if no transports are available, HTTP transport object.
- */
- function &_postTransport( $args = array() ) {
- static $working_transport, $blocking_transport, $nonblocking_transport;
-
- if ( is_null($working_transport) ) {
- if ( true === WP_Http_ExtHttp::test() && apply_filters('use_http_extension_transport', true) ) {
- $working_transport['exthttp'] = new WP_Http_ExtHttp();
- $blocking_transport[] = &$working_transport['exthttp'];
- } else if ( true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true) ) {
- $working_transport['streams'] = new WP_Http_Streams();
- $blocking_transport[] = &$working_transport['streams'];
- } else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) ) {
- $working_transport['fsockopen'] = new WP_Http_Fsockopen();
- $blocking_transport[] = &$working_transport['fsockopen'];
- }
-
- foreach ( array('streams', 'fsockopen', 'exthttp') as $transport ) {
- if ( isset($working_transport[$transport]) )
- $nonblocking_transport[] = &$working_transport[$transport];
- }
- }
-
- if ( isset($args['blocking']) && !$args['blocking'] )
- return $nonblocking_transport;
- else
- return $blocking_transport;
- }
-
- /**
- * Send a HTTP request to a URI.
- *
- * The body and headers are part of the arguments. The 'body' argument is
- * for the body and will accept either a string or an array. The 'headers'
- * argument should be an array, but a string is acceptable. If the 'body'
- * argument is an array, then it will automatically be escaped using
- * http_build_query().
- *
- * The only URI that are supported in the HTTP Transport implementation are
- * the HTTP and HTTPS protocols. HTTP and HTTPS are assumed so the server
- * might not know how to handle the send headers. Other protocols are
- * unsupported and most likely will fail.
- *
- * The defaults are 'method', 'timeout', 'redirection', 'httpversion',
- * 'blocking' and 'user-agent'.
- *
- * Accepted 'method' values are 'GET', 'POST', and 'HEAD', some transports
- * technically allow others, but should not be assumed. The 'timeout' is
- * used to sent how long the connection should stay open before failing when
- * no response. 'redirection' is used to track how many redirects were taken
- * and used to sent the amount for other transports, but not all transports
- * accept setting that value.
- *
- * The 'httpversion' option is used to sent the HTTP version and accepted
- * values are '1.0', and '1.1' and should be a string. Version 1.1 is not
- * supported, because of chunk response. The 'user-agent' option is the
- * user-agent and is used to replace the default user-agent, which is
- * 'WordPress/WP_Version', where WP_Version is the value from $wp_version.
- *
- * 'blocking' is the default, which is used to tell the transport, whether
- * it should halt PHP while it performs the request or continue regardless.
- * Actually, that isn't entirely correct. Blocking mode really just means
- * whether the fread should just pull what it can whenever it gets bytes or
- * if it should wait until it has enough in the buffer to read or finishes
- * reading the entire content. It doesn't actually always mean that PHP will
- * continue going after making the request.
- *
- * @access public
- * @since 2.7
- *
- * @param string $url URI resource.
- * @param str|array $args Optional. Override the defaults.
- * @return boolean
- */
- function request( $url, $args = array() ) {
- global $wp_version;
-
- $defaults = array(
- 'method' => 'GET',
- 'timeout' => apply_filters( 'http_request_timeout', 5),
- 'redirection' => apply_filters( 'http_request_redirection_count', 5),
- 'httpversion' => apply_filters( 'http_request_version', '1.0'),
- 'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version ),
- 'blocking' => true,
- 'headers' => array(), 'body' => null
- );
-
- $r = wp_parse_args( $args, $defaults );
- $r = apply_filters( 'http_request_args', $r );
-
- if ( is_null( $r['headers'] ) )
- $r['headers'] = array();
-
- if ( ! is_array($r['headers']) ) {
- $processedHeaders = WP_Http::processHeaders($r['headers']);
- $r['headers'] = $processedHeaders['headers'];
- }
-
- if ( isset($r['headers']['User-Agent']) ) {
- $r['user-agent'] = $r['headers']['User-Agent'];
- unset($r['headers']['User-Agent']);
- }
-
- if ( isset($r['headers']['user-agent']) ) {
- $r['user-agent'] = $r['headers']['user-agent'];
- unset($r['headers']['user-agent']);
- }
-
- if ( is_null($r['body']) ) {
- // Some servers fail when sending content without the content-length
- // header being set.
- $r['headers']['Content-Length'] = 0;
- $transports = WP_Http::_getTransport($r);
- } else {
- if ( is_array( $r['body'] ) || is_object( $r['body'] ) ) {
- $r['body'] = http_build_query($r['body'], null, '&');
- $r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset');
- $r['headers']['Content-Length'] = strlen($r['body']);
- }
-
- if ( ! isset( $r['headers']['Content-Length'] ) && ! isset( $r['headers']['content-length'] ) )
- $r['headers']['Content-Length'] = strlen($r['body']);
-
- $transports = WP_Http::_postTransport($r);
- }
-
- $response = array( 'headers' => array(), 'body' => '', 'response' => array('code', 'message') );
- foreach( (array) $transports as $transport ) {
- $response = $transport->request($url, $r);
-
- if( !is_wp_error($response) )
- return $response;
- }
-
- return $response;
- }
-
- /**
- * Uses the POST HTTP method.
- *
- * Used for sending data that is expected to be in the body.
- *
- * @access public
- * @since 2.7
- *
- * @param string $url URI resource.
- * @param str|array $args Optional. Override the defaults.
- * @return boolean
- */
- function post($url, $args = array()) {
- $defaults = array('method' => 'POST');
- $r = wp_parse_args( $args, $defaults );
- return $this->request($url, $r);
- }
-
- /**
- * Uses the GET HTTP method.
- *
- * Used for sending data that is expected to be in the body.
- *
- * @access public
- * @since 2.7
- *
- * @param string $url URI resource.
- * @param str|array $args Optional. Override the defaults.
- * @return boolean
- */
- function get($url, $args = array()) {
- $defaults = array('method' => 'GET');
- $r = wp_parse_args( $args, $defaults );
- return $this->request($url, $r);
- }
-
- /**
- * Uses the HEAD HTTP method.
- *
- * Used for sending data that is expected to be in the body.
- *
- * @access public
- * @since 2.7
- *
- * @param string $url URI resource.
- * @param str|array $args Optional. Override the defaults.
- * @return boolean
- */
- function head($url, $args = array()) {
- $defaults = array('method' => 'HEAD');
- $r = wp_parse_args( $args, $defaults );
- return $this->request($url, $r);
- }
-
- /**
- * Parses the responses and splits the parts into headers and body.
- *
- * @access public
- * @static
- * @since 2.7
- *
- * @param string $strResponse The full response string
- * @return array Array with 'headers' and 'body' keys.
- */
- function processResponse($strResponse) {
- list($theHeaders, $theBody) = explode("\r\n\r\n", $strResponse, 2);
- return array('headers' => $theHeaders, 'body' => $theBody);
- }
-
- /**
- * Transform header string into an array.
- *
- * If an array is given then it is assumed to be raw header data with
- * numeric keys with the headers as the values. No headers must be passed
- * that were already processed.
- *
- * @access public
- * @static
- * @since 2.7
- *
- * @param string|array $headers
- * @return array Processed string headers
- */
- function processHeaders($headers) {
- if ( is_string($headers) )
- $headers = explode("\n", str_replace(array("\r\n", "\r"), "\n", $headers) );
-
- $response = array('code' => 0, 'message' => '');
-
- $newheaders = array();
- foreach ( $headers as $tempheader ) {
- if ( empty($tempheader) )
- continue;
-
- if ( false === strpos($tempheader, ':') ) {
- list( , $iResponseCode, $strResponseMsg) = explode(' ', $tempheader, 3);
- $response['code'] = $iResponseCode;
- $response['message'] = $strResponseMsg;
- continue;
- }
-
- list($key, $value) = explode(':', $tempheader, 2);
-
- if ( ! empty($value) )
- $newheaders[strtolower($key)] = trim($value);
- }
-
- return array('response' => $response, 'headers' => $newheaders);
- }
-
- /**
- * Decodes chunk transfer-encoding, based off the HTTP 1.1 specification.
- *
- * Based off the HTTP http_encoding_dechunk function. Does not support
- * UTF-8. Does not support returning footer headers. Shouldn't be too
- * difficult to support it though.
- *
- * @todo Add support for footer chunked headers.
- * @access public
- * @since 2.7
- * @static
- *
- * @param string $body Body content
- * @return string Chunked decoded body on success or raw body on failure.
- */
- function chunkTransferDecode($body) {
- $body = str_replace(array("\r\n", "\r"), "\n", $body);
- // The body is not chunked encoding or is malformed.
- if ( ! preg_match( '/^[0-9a-f]+(\s|\n)+/mi', trim($body) ) )
- return $body;
-
- $parsedBody = '';
- //$parsedHeaders = array(); Unsupported
-
- while ( true ) {
- $hasChunk = (bool) preg_match( '/^([0-9a-f]+)(\s|\n)+/mi', $body, $match );
-
- if ( $hasChunk ) {
- if ( empty($match[1]) )
- return $body;
-
- $length = hexdec( $match[1] );
- $chunkLength = strlen( $match[0] );
-
- $strBody = substr($body, $chunkLength, $length);
- $parsedBody .= $strBody;