web/wp-includes/class-http.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
   167 		WP_Http::buildCookieHeader( $r );
   167 		WP_Http::buildCookieHeader( $r );
   168 
   168 
   169 		if ( WP_Http_Encoding::is_available() )
   169 		if ( WP_Http_Encoding::is_available() )
   170 			$r['headers']['Accept-Encoding'] = WP_Http_Encoding::accept_encoding();
   170 			$r['headers']['Accept-Encoding'] = WP_Http_Encoding::accept_encoding();
   171 
   171 
   172 		if ( empty($r['body']) ) {
   172 		if ( ( ! is_null( $r['body'] ) && '' != $r['body'] ) || 'POST' == $r['method'] || 'PUT' == $r['method'] ) {
   173 			$r['body'] = null;
       
   174 			// Some servers fail when sending content without the content-length header being set.
       
   175 			// Also, to fix another bug, we only send when doing POST and PUT and the content-length
       
   176 			// header isn't already set.
       
   177 			if ( ($r['method'] == 'POST' || $r['method'] == 'PUT') && ! isset( $r['headers']['Content-Length'] ) )
       
   178 				$r['headers']['Content-Length'] = 0;
       
   179 		} else {
       
   180 			if ( is_array( $r['body'] ) || is_object( $r['body'] ) ) {
   173 			if ( is_array( $r['body'] ) || is_object( $r['body'] ) ) {
   181 				$r['body'] = http_build_query( $r['body'], null, '&' );
   174 				$r['body'] = http_build_query( $r['body'], null, '&' );
       
   175 
   182 				if ( ! isset( $r['headers']['Content-Type'] ) )
   176 				if ( ! isset( $r['headers']['Content-Type'] ) )
   183 					$r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' );
   177 					$r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' );
   184 				$r['headers']['Content-Length'] = strlen( $r['body'] );
       
   185 			}
   178 			}
       
   179 
       
   180 			if ( '' === $r['body'] )
       
   181 				$r['body'] = null;
   186 
   182 
   187 			if ( ! isset( $r['headers']['Content-Length'] ) && ! isset( $r['headers']['content-length'] ) )
   183 			if ( ! isset( $r['headers']['Content-Length'] ) && ! isset( $r['headers']['content-length'] ) )
   188 				$r['headers']['Content-Length'] = strlen( $r['body'] );
   184 				$r['headers']['Content-Length'] = strlen( $r['body'] );
   189 		}
   185 		}
   190 
   186 
   198 	 * @access private
   194 	 * @access private
   199 	 *
   195 	 *
   200 	 * @param array $args Request arguments
   196 	 * @param array $args Request arguments
   201 	 * @param string $url URL to Request
   197 	 * @param string $url URL to Request
   202 	 *
   198 	 *
   203 	 * @return string|false Class name for the first transport that claims to support the request. False if no transport claims to support the request.
   199 	 * @return string|bool Class name for the first transport that claims to support the request. False if no transport claims to support the request.
   204 	 */
   200 	 */
   205 	public function _get_first_available_transport( $args, $url = null ) {
   201 	public function _get_first_available_transport( $args, $url = null ) {
   206 		$request_order = array( 'curl', 'streams', 'fsockopen' );
   202 		$request_order = array( 'curl', 'streams', 'fsockopen' );
   207 
   203 
   208 		// Loop over each transport on each HTTP request looking for one which will serve this request's needs
   204 		// Loop over each transport on each HTTP request looking for one which will serve this request's needs
   380 				continue;
   376 				continue;
   381 			}
   377 			}
   382 
   378 
   383 			list($key, $value) = explode(':', $tempheader, 2);
   379 			list($key, $value) = explode(':', $tempheader, 2);
   384 
   380 
   385 			if ( !empty( $value ) ) {
   381 			$key = strtolower( $key );
   386 				$key = strtolower( $key );
   382 			$value = trim( $value );
   387 				if ( isset( $newheaders[$key] ) ) {
   383 
   388 					if ( !is_array($newheaders[$key]) )
   384 			if ( isset( $newheaders[ $key ] ) ) {
   389 						$newheaders[$key] = array($newheaders[$key]);
   385 				if ( ! is_array( $newheaders[ $key ] ) )
   390 					$newheaders[$key][] = trim( $value );
   386 					$newheaders[$key] = array( $newheaders[ $key ] );
   391 				} else {
   387 				$newheaders[ $key ][] = $value;
   392 					$newheaders[$key] = trim( $value );
   388 			} else {
   393 				}
   389 				$newheaders[ $key ] = $value;
   394 				if ( 'set-cookie' == $key )
       
   395 					$cookies[] = new WP_Http_Cookie( $value );
       
   396 			}
   390 			}
       
   391 			if ( 'set-cookie' == $key )
       
   392 				$cookies[] = new WP_Http_Cookie( $value );
   397 		}
   393 		}
   398 
   394 
   399 		return array('response' => $response, 'headers' => $newheaders, 'cookies' => $cookies);
   395 		return array('response' => $response, 'headers' => $newheaders, 'cookies' => $cookies);
   400 	}
   396 	}
   401 
   397 
   426 	/**
   422 	/**
   427 	 * Decodes chunk transfer-encoding, based off the HTTP 1.1 specification.
   423 	 * Decodes chunk transfer-encoding, based off the HTTP 1.1 specification.
   428 	 *
   424 	 *
   429 	 * Based off the HTTP http_encoding_dechunk function. Does not support UTF-8. Does not support
   425 	 * Based off the HTTP http_encoding_dechunk function. Does not support UTF-8. Does not support
   430 	 * returning footer headers. Shouldn't be too difficult to support it though.
   426 	 * returning footer headers. Shouldn't be too difficult to support it though.
       
   427 	 *
       
   428 	 * @link http://tools.ietf.org/html/rfc2616#section-19.4.6 Process for chunked decoding.
   431 	 *
   429 	 *
   432 	 * @todo Add support for footer chunked headers.
   430 	 * @todo Add support for footer chunked headers.
   433 	 * @access public
   431 	 * @access public
   434 	 * @since 2.7.0
   432 	 * @since 2.7.0
   435 	 * @static
   433 	 * @static
   804 	 */
   802 	 */
   805 	public static function test( $args = array() ) {
   803 	public static function test( $args = array() ) {
   806 		if ( ! function_exists( 'fsockopen' ) )
   804 		if ( ! function_exists( 'fsockopen' ) )
   807 			return false;
   805 			return false;
   808 
   806 
   809 		if ( false !== ($option = get_option( 'disable_fsockopen' )) && time()-$option < 43200 ) // 12 hours
   807 		if ( false !== ( $option = get_option( 'disable_fsockopen' ) ) && time() - $option < 12 * HOUR_IN_SECONDS )
   810 			return false;
   808 			return false;
   811 
   809 
   812 		$is_ssl = isset( $args['ssl'] ) && $args['ssl'];
   810 		$is_ssl = isset( $args['ssl'] ) && $args['ssl'];
   813 
   811 
   814 		if ( $is_ssl && ! extension_loaded( 'openssl' ) )
   812 		if ( $is_ssl && ! extension_loaded( 'openssl' ) )
   910 			// We only support Basic authentication so this will only work if that is what your proxy supports.
   908 			// We only support Basic authentication so this will only work if that is what your proxy supports.
   911 			if ( $proxy->use_authentication() )
   909 			if ( $proxy->use_authentication() )
   912 				$arrContext['http']['header'] .= $proxy->authentication_header() . "\r\n";
   910 				$arrContext['http']['header'] .= $proxy->authentication_header() . "\r\n";
   913 		}
   911 		}
   914 
   912 
   915 		if ( ! empty($r['body'] ) )
   913 		if ( ! is_null( $r['body'] ) )
   916 			$arrContext['http']['content'] = $r['body'];
   914 			$arrContext['http']['content'] = $r['body'];
   917 
   915 
   918 		$context = stream_context_create($arrContext);
   916 		$context = stream_context_create($arrContext);
   919 
   917 
   920 		if ( !WP_DEBUG )
   918 		if ( !WP_DEBUG )
  1103 				curl_setopt( $handle, CURLOPT_CUSTOMREQUEST, 'PUT' );
  1101 				curl_setopt( $handle, CURLOPT_CUSTOMREQUEST, 'PUT' );
  1104 				curl_setopt( $handle, CURLOPT_POSTFIELDS, $r['body'] );
  1102 				curl_setopt( $handle, CURLOPT_POSTFIELDS, $r['body'] );
  1105 				break;
  1103 				break;
  1106 			default:
  1104 			default:
  1107 				curl_setopt( $handle, CURLOPT_CUSTOMREQUEST, $r['method'] );
  1105 				curl_setopt( $handle, CURLOPT_CUSTOMREQUEST, $r['method'] );
  1108 				if ( ! empty( $r['body'] ) )
  1106 				if ( ! is_null( $r['body'] ) )
  1109 					curl_setopt( $handle, CURLOPT_POSTFIELDS, $r['body'] );
  1107 					curl_setopt( $handle, CURLOPT_POSTFIELDS, $r['body'] );
  1110 				break;
  1108 				break;
  1111 		}
  1109 		}
  1112 
  1110 
  1113 		if ( true === $r['blocking'] )
  1111 		if ( true === $r['blocking'] )
  1114 			curl_setopt( $handle, CURLOPT_HEADERFUNCTION, array( &$this, 'stream_headers' ) );
  1112 			curl_setopt( $handle, CURLOPT_HEADERFUNCTION, array( $this, 'stream_headers' ) );
  1115 
  1113 
  1116 		curl_setopt( $handle, CURLOPT_HEADER, false );
  1114 		curl_setopt( $handle, CURLOPT_HEADER, false );
  1117 
  1115 
  1118 		// If streaming to a file open a file handle, and setup our curl streaming handler
  1116 		// If streaming to a file open a file handle, and setup our curl streaming handler
  1119 		if ( $r['stream'] ) {
  1117 		if ( $r['stream'] ) {
  1390 		if ( $check === false )
  1388 		if ( $check === false )
  1391 			return true;
  1389 			return true;
  1392 
  1390 
  1393 		$home = parse_url( get_option('siteurl') );
  1391 		$home = parse_url( get_option('siteurl') );
  1394 
  1392 
       
  1393 		$result = apply_filters( 'pre_http_send_through_proxy', null, $uri, $check, $home );
       
  1394 		if ( ! is_null( $result ) )
       
  1395 			return $result;
       
  1396 
  1395 		if ( $check['host'] == 'localhost' || $check['host'] == $home['host'] )
  1397 		if ( $check['host'] == 'localhost' || $check['host'] == $home['host'] )
  1396 			return false;
  1398 			return false;
  1397 
  1399 
  1398 		if ( !defined('WP_PROXY_BYPASS_HOSTS') )
  1400 		if ( !defined('WP_PROXY_BYPASS_HOSTS') )
  1399 			return true;
  1401 			return true;
  1544 	 * @param string $url URL you intend to send this cookie to
  1546 	 * @param string $url URL you intend to send this cookie to
  1545 	 * @return boolean true if allowed, false otherwise.
  1547 	 * @return boolean true if allowed, false otherwise.
  1546 	 */
  1548 	 */
  1547 	function test( $url ) {
  1549 	function test( $url ) {
  1548 		// Expires - if expired then nothing else matters
  1550 		// Expires - if expired then nothing else matters
  1549 		if ( time() > $this->expires )
  1551 		if ( isset( $this->expires ) && time() > $this->expires )
  1550 			return false;
  1552 			return false;
  1551 
  1553 
  1552 		// Get details on the URL we're thinking about sending to
  1554 		// Get details on the URL we're thinking about sending to
  1553 		$url = parse_url( $url );
  1555 		$url = parse_url( $url );
  1554 		$url['port'] = isset( $url['port'] ) ? $url['port'] : 80;
  1556 		$url['port'] = isset( $url['port'] ) ? $url['port'] : 80;
  1584 	 * @since 2.8.0
  1586 	 * @since 2.8.0
  1585 	 *
  1587 	 *
  1586 	 * @return string Header encoded cookie name and value.
  1588 	 * @return string Header encoded cookie name and value.
  1587 	 */
  1589 	 */
  1588 	function getHeaderValue() {
  1590 	function getHeaderValue() {
  1589 		if ( empty( $this->name ) || empty( $this->value ) )
  1591 		if ( ! isset( $this->name ) || ! isset( $this->value ) )
  1590 			return '';
  1592 			return '';
  1591 
  1593 
  1592 		return $this->name . '=' . apply_filters( 'wp_http_cookie_value', $this->value, $this->name );
  1594 		return $this->name . '=' . apply_filters( 'wp_http_cookie_value', $this->value, $this->name );
  1593 	}
  1595 	}
  1594 
  1596 
  1671 	}
  1673 	}
  1672 
  1674 
  1673 	/**
  1675 	/**
  1674 	 * Decompression of deflated string while staying compatible with the majority of servers.
  1676 	 * Decompression of deflated string while staying compatible with the majority of servers.
  1675 	 *
  1677 	 *
  1676 	 * Certain Servers will return deflated data with headers which PHP's gziniflate()
  1678 	 * Certain Servers will return deflated data with headers which PHP's gzinflate()
  1677 	 * function cannot handle out of the box. The following function has been created from
  1679 	 * function cannot handle out of the box. The following function has been created from
  1678 	 * various snippets on the gzinflate() PHP documentation.
  1680 	 * various snippets on the gzinflate() PHP documentation.
  1679 	 *
  1681 	 *
  1680 	 * Warning: Magic numbers within. Due to the potential different formats that the compressed
  1682 	 * Warning: Magic numbers within. Due to the potential different formats that the compressed
  1681 	 * data may be returned in, some "magic offsets" are needed to ensure proper decompression
  1683 	 * data may be returned in, some "magic offsets" are needed to ensure proper decompression