WP_Http

Core class used for managing HTTP transports and making HTTP requests.

Defined (1)

The class is defined in the following location(s).

/bp-forums/bbpress/bb-includes/backpress/class.wp-http.php  
  1. class WP_Http { 
  2.  
  3. /** 
  4. * PHP4 style Constructor - Calls PHP5 Style Constructor 
  5. * @since 2.7.0 
  6. * @return WP_Http 
  7. */ 
  8. function WP_Http() { 
  9. $this->__construct(); 
  10.  
  11. /** 
  12. * PHP5 style Constructor - Setup available transport if not available. 
  13. * PHP4 does not have the 'self' keyword and since WordPress supports PHP4,  
  14. * the class needs to be used for the static call. 
  15. * The transport are setup to save time. This should only be called once, so 
  16. * the overhead should be fine. 
  17. * @since 2.7.0 
  18. * @return WP_Http 
  19. */ 
  20. function __construct() { 
  21. WP_Http::_getTransport(); 
  22. WP_Http::_postTransport(); 
  23.  
  24. /** 
  25. * Tests the WordPress HTTP objects for an object to use and returns it. 
  26. * Tests all of the objects and returns the object that passes. Also caches 
  27. * that object to be used later. 
  28. * The order for the GET/HEAD requests are HTTP Extension, FSockopen Streams,  
  29. * Fopen, and finally cURL. Whilst Fsockopen has the highest overhead, Its 
  30. * used 2nd due to high compatibility with most hosts, The HTTP Extension is 
  31. * tested first due to hosts which have it enabled, are likely to work 
  32. * correctly with it. 
  33. * There are currently issues with "localhost" not resolving correctly with 
  34. * DNS. This may cause an error "failed to open stream: A connection attempt 
  35. * failed because the connected party did not properly respond after a 
  36. * period of time, or established connection failed because connected host 
  37. * has failed to respond." 
  38. * @since 2.7.0 
  39. * @access private 
  40. * @param array $args Request args, default us an empty array 
  41. * @return object|null Null if no transports are available, HTTP transport object. 
  42. */ 
  43. function &_getTransport( $args = array() ) { 
  44. static $working_transport, $blocking_transport, $nonblocking_transport; 
  45.  
  46. if ( is_null($working_transport) ) { 
  47. if ( true === WP_Http_ExtHttp::test($args) ) { 
  48. $working_transport['exthttp'] = new WP_Http_ExtHttp(); 
  49. $blocking_transport[] = &$working_transport['exthttp']; 
  50. } else if ( true === WP_Http_Fsockopen::test($args) ) { 
  51. $working_transport['fsockopen'] = new WP_Http_Fsockopen(); 
  52. $blocking_transport[] = &$working_transport['fsockopen']; 
  53. } else if ( true === WP_Http_Streams::test($args) ) { 
  54. $working_transport['streams'] = new WP_Http_Streams(); 
  55. $blocking_transport[] = &$working_transport['streams']; 
  56. } else if ( true === WP_Http_Fopen::test($args) ) { 
  57. $working_transport['fopen'] = new WP_Http_Fopen(); 
  58. $blocking_transport[] = &$working_transport['fopen']; 
  59. } else if ( true === WP_Http_Curl::test($args) ) { 
  60. $working_transport['curl'] = new WP_Http_Curl(); 
  61. $blocking_transport[] = &$working_transport['curl']; 
  62.  
  63. foreach ( array('curl', 'streams', 'fopen', 'fsockopen', 'exthttp') as $transport ) { 
  64. if ( isset($working_transport[$transport]) ) 
  65. $nonblocking_transport[] = &$working_transport[$transport]; 
  66.  
  67. if ( has_filter('http_transport_get_debug') ) 
  68. do_action('http_transport_get_debug', $working_transport, $blocking_transport, $nonblocking_transport); 
  69.  
  70. if ( isset($args['blocking']) && !$args['blocking'] ) 
  71. return $nonblocking_transport; 
  72. else 
  73. return $blocking_transport; 
  74.  
  75. /** 
  76. * Tests the WordPress HTTP objects for an object to use and returns it. 
  77. * Tests all of the objects and returns the object that passes. Also caches 
  78. * that object to be used later. This is for posting content to a URL and 
  79. * is used when there is a body. The plain Fopen Transport can not be used 
  80. * to send content, but the streams transport can. This is a limitation that 
  81. * is addressed here, by just not including that transport. 
  82. * @since 2.7.0 
  83. * @access private 
  84. * @param array $args Request args, default us an empty array 
  85. * @return object|null Null if no transports are available, HTTP transport object. 
  86. */ 
  87. function &_postTransport( $args = array() ) { 
  88. static $working_transport, $blocking_transport, $nonblocking_transport; 
  89.  
  90. if ( is_null($working_transport) ) { 
  91. if ( true === WP_Http_ExtHttp::test($args) ) { 
  92. $working_transport['exthttp'] = new WP_Http_ExtHttp(); 
  93. $blocking_transport[] = &$working_transport['exthttp']; 
  94. } else if ( true === WP_Http_Fsockopen::test($args) ) { 
  95. $working_transport['fsockopen'] = new WP_Http_Fsockopen(); 
  96. $blocking_transport[] = &$working_transport['fsockopen']; 
  97. } else if ( true === WP_Http_Streams::test($args) ) { 
  98. $working_transport['streams'] = new WP_Http_Streams(); 
  99. $blocking_transport[] = &$working_transport['streams']; 
  100. } else if ( true === WP_Http_Curl::test($args) ) { 
  101. $working_transport['curl'] = new WP_Http_Curl(); 
  102. $blocking_transport[] = &$working_transport['curl']; 
  103.  
  104. foreach ( array('curl', 'streams', 'fsockopen', 'exthttp') as $transport ) { 
  105. if ( isset($working_transport[$transport]) ) 
  106. $nonblocking_transport[] = &$working_transport[$transport]; 
  107.  
  108. if ( has_filter('http_transport_post_debug') ) 
  109. do_action('http_transport_post_debug', $working_transport, $blocking_transport, $nonblocking_transport); 
  110.  
  111. if ( isset($args['blocking']) && !$args['blocking'] ) 
  112. return $nonblocking_transport; 
  113. else 
  114. return $blocking_transport; 
  115.  
  116. /** 
  117. * Send a HTTP request to a URI. 
  118. * The body and headers are part of the arguments. The 'body' argument is for the body and will 
  119. * accept either a string or an array. The 'headers' argument should be an array, but a string 
  120. * is acceptable. If the 'body' argument is an array, then it will automatically be escaped 
  121. * using http_build_query(). 
  122. * The only URI that are supported in the HTTP Transport implementation are the HTTP and HTTPS 
  123. * protocols. HTTP and HTTPS are assumed so the server might not know how to handle the send 
  124. * headers. Other protocols are unsupported and most likely will fail. 
  125. * The defaults are 'method', 'timeout', 'redirection', 'httpversion', 'blocking' and 
  126. * 'user-agent'. 
  127. * Accepted 'method' values are 'GET', 'POST', and 'HEAD', some transports technically allow 
  128. * others, but should not be assumed. The 'timeout' is used to sent how long the connection 
  129. * should stay open before failing when no response. 'redirection' is used to track how many 
  130. * redirects were taken and used to sent the amount for other transports, but not all transports 
  131. * accept setting that value. 
  132. * The 'httpversion' option is used to sent the HTTP version and accepted values are '1.0', and 
  133. * '1.1' and should be a string. Version 1.1 is not supported, because of chunk response. The 
  134. * 'user-agent' option is the user-agent and is used to replace the default user-agent, which is 
  135. * 'WordPress/WP_Version', where WP_Version is the value from $wp_version. 
  136. * 'blocking' is the default, which is used to tell the transport, whether it should halt PHP 
  137. * while it performs the request or continue regardless. Actually, that isn't entirely correct. 
  138. * Blocking mode really just means whether the fread should just pull what it can whenever it 
  139. * gets bytes or if it should wait until it has enough in the buffer to read or finishes reading 
  140. * the entire content. It doesn't actually always mean that PHP will continue going after making 
  141. * the request. 
  142. * @access public 
  143. * @since 2.7.0 
  144. * @param string $url URI resource. 
  145. * @param str|array $args Optional. Override the defaults. 
  146. * @return array containing 'headers', 'body', 'response', 'cookies' 
  147. */ 
  148. function request( $url, $args = array() ) { 
  149. $defaults = array( 
  150. 'method' => 'GET',  
  151. 'timeout' => apply_filters( 'http_request_timeout', 5),  
  152. 'redirection' => apply_filters( 'http_request_redirection_count', 5),  
  153. 'httpversion' => apply_filters( 'http_request_version', '1.0'),  
  154. 'user-agent' => apply_filters( 'http_headers_useragent', backpress_get_option( 'wp_http_version' ) ),  
  155. 'blocking' => true,  
  156. 'headers' => array(),  
  157. 'cookies' => array(),  
  158. 'body' => null,  
  159. 'compress' => false,  
  160. 'decompress' => true,  
  161. 'sslverify' => true 
  162. ); 
  163.  
  164. $r = wp_parse_args( $args, $defaults ); 
  165. $r = apply_filters( 'http_request_args', $r, $url ); 
  166.  
  167. $arrURL = parse_url($url); 
  168.  
  169. if ( $this->block_request( $url ) ) 
  170. return new WP_Error('http_request_failed', 'User has blocked requests through HTTP.'); 
  171.  
  172. // Determine if this is a https call and pass that on to the transport functions 
  173. // so that we can blacklist the transports that do not support ssl verification 
  174. $r['ssl'] = $arrURL['scheme'] == 'https' || $arrURL['scheme'] == 'ssl'; 
  175.  
  176. // Determine if this request is to OUR install of WordPress 
  177. $homeURL = parse_url( backpress_get_option( 'application_uri' ) ); 
  178. $r['local'] = $homeURL['host'] == $arrURL['host'] || 'localhost' == $arrURL['host']; 
  179. unset($homeURL); 
  180.  
  181. if ( is_null( $r['headers'] ) ) 
  182. $r['headers'] = array(); 
  183.  
  184. if ( ! is_array($r['headers']) ) { 
  185. $processedHeaders = WP_Http::processHeaders($r['headers']); 
  186. $r['headers'] = $processedHeaders['headers']; 
  187.  
  188. if ( isset($r['headers']['User-Agent']) ) { 
  189. $r['user-agent'] = $r['headers']['User-Agent']; 
  190. unset($r['headers']['User-Agent']); 
  191.  
  192. if ( isset($r['headers']['user-agent']) ) { 
  193. $r['user-agent'] = $r['headers']['user-agent']; 
  194. unset($r['headers']['user-agent']); 
  195.  
  196. // Construct Cookie: header if any cookies are set 
  197. WP_Http::buildCookieHeader( $r ); 
  198.  
  199. if ( WP_Http_Encoding::is_available() ) 
  200. $r['headers']['Accept-Encoding'] = WP_Http_Encoding::accept_encoding(); 
  201.  
  202. if ( is_null($r['body']) ) { 
  203. // Some servers fail when sending content without the content-length 
  204. // header being set. 
  205. $r['headers']['Content-Length'] = 0; 
  206. $transports = WP_Http::_getTransport($r); 
  207. } else { 
  208. if ( is_array( $r['body'] ) || is_object( $r['body'] ) ) { 
  209. if ( ! version_compare(phpversion(), '5.1.2', '>=') ) 
  210. $r['body'] = _http_build_query($r['body'], null, '&'); 
  211. else 
  212. $r['body'] = http_build_query($r['body'], null, '&'); 
  213. $r['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . backpress_get_option( 'charset' ); 
  214. $r['headers']['Content-Length'] = strlen($r['body']); 
  215.  
  216. if ( ! isset( $r['headers']['Content-Length'] ) && ! isset( $r['headers']['content-length'] ) ) 
  217. $r['headers']['Content-Length'] = strlen($r['body']); 
  218.  
  219. $transports = WP_Http::_postTransport($r); 
  220.  
  221. if ( has_action('http_api_debug') ) 
  222. do_action('http_api_debug', $transports, 'transports_list'); 
  223.  
  224. $response = array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() ); 
  225. foreach ( (array) $transports as $transport ) { 
  226. $response = $transport->request($url, $r); 
  227.  
  228. if ( has_action('http_api_debug') ) 
  229. do_action( 'http_api_debug', $response, 'response', get_class($transport) ); 
  230.  
  231. if ( ! is_wp_error($response) ) 
  232. return $response; 
  233.  
  234. return $response; 
  235.  
  236. /** 
  237. * Uses the POST HTTP method. 
  238. * Used for sending data that is expected to be in the body. 
  239. * @access public 
  240. * @since 2.7.0 
  241. * @param string $url URI resource. 
  242. * @param str|array $args Optional. Override the defaults. 
  243. * @return boolean 
  244. */ 
  245. function post($url, $args = array()) { 
  246. $defaults = array('method' => 'POST'); 
  247. $r = wp_parse_args( $args, $defaults ); 
  248. return $this->request($url, $r); 
  249.  
  250. /** 
  251. * Uses the GET HTTP method. 
  252. * Used for sending data that is expected to be in the body. 
  253. * @access public 
  254. * @since 2.7.0 
  255. * @param string $url URI resource. 
  256. * @param str|array $args Optional. Override the defaults. 
  257. * @return boolean 
  258. */ 
  259. function get($url, $args = array()) { 
  260. $defaults = array('method' => 'GET'); 
  261. $r = wp_parse_args( $args, $defaults ); 
  262. return $this->request($url, $r); 
  263.  
  264. /** 
  265. * Uses the HEAD HTTP method. 
  266. * Used for sending data that is expected to be in the body. 
  267. * @access public 
  268. * @since 2.7.0 
  269. * @param string $url URI resource. 
  270. * @param str|array $args Optional. Override the defaults. 
  271. * @return boolean 
  272. */ 
  273. function head($url, $args = array()) { 
  274. $defaults = array('method' => 'HEAD'); 
  275. $r = wp_parse_args( $args, $defaults ); 
  276. return $this->request($url, $r); 
  277.  
  278. /** 
  279. * Parses the responses and splits the parts into headers and body. 
  280. * @access public 
  281. * @static 
  282. * @since 2.7.0 
  283. * @param string $strResponse The full response string 
  284. * @return array Array with 'headers' and 'body' keys. 
  285. */ 
  286. function processResponse($strResponse) { 
  287. list($theHeaders, $theBody) = explode("\r\n\r\n", $strResponse, 2); 
  288. return array('headers' => $theHeaders, 'body' => $theBody); 
  289.  
  290. /** 
  291. * Transform header string into an array. 
  292. * If an array is given then it is assumed to be raw header data with numeric keys with the 
  293. * headers as the values. No headers must be passed that were already processed. 
  294. * @access public 
  295. * @static 
  296. * @since 2.7.0 
  297. * @param string|array $headers 
  298. * @return array Processed string headers. If duplicate headers are encountered,  
  299. * Then a numbered array is returned as the value of that header-key. 
  300. */ 
  301. function processHeaders($headers) { 
  302. // split headers, one per array element 
  303. if ( is_string($headers) ) { 
  304. // tolerate line terminator: CRLF = LF (RFC 2616 19.3) 
  305. $headers = str_replace("\r\n", "\n", $headers); 
  306. // unfold folded header fields. LWS = [CRLF] 1*( SP | HT ) <US-ASCII SP, space (32)>, <US-ASCII HT, horizontal-tab (9)> (RFC 2616 2.2) 
  307. $headers = preg_replace('/\n[ \t]/', ' ', $headers); 
  308. // create the headers array 
  309. $headers = explode("\n", $headers); 
  310.  
  311. $response = array('code' => 0, 'message' => ''); 
  312.  
  313. $cookies = array(); 
  314. $newheaders = array(); 
  315. foreach ( $headers as $tempheader ) { 
  316. if ( empty($tempheader) ) 
  317. continue; 
  318.  
  319. if ( false === strpos($tempheader, ':') ) { 
  320. list( , $iResponseCode, $strResponseMsg) = explode(' ', $tempheader, 3); 
  321. $response['code'] = $iResponseCode; 
  322. $response['message'] = $strResponseMsg; 
  323. continue; 
  324.  
  325. list($key, $value) = explode(':', $tempheader, 2); 
  326.  
  327. if ( !empty( $value ) ) { 
  328. $key = strtolower( $key ); 
  329. if ( isset( $newheaders[$key] ) ) { 
  330. $newheaders[$key] = array( $newheaders[$key], trim( $value ) ); 
  331. } else { 
  332. $newheaders[$key] = trim( $value ); 
  333. if ( 'set-cookie' == strtolower( $key ) ) 
  334. $cookies[] = new WP_Http_Cookie( $value ); 
  335.  
  336. return array('response' => $response, 'headers' => $newheaders, 'cookies' => $cookies); 
  337.  
  338. /** 
  339. * Takes the arguments for a ::request() and checks for the cookie array. 
  340. * If it's found, then it's assumed to contain WP_Http_Cookie objects, which are each parsed 
  341. * into strings and added to the Cookie: header (within the arguments array). Edits the array by 
  342. * reference. 
  343. * @access public 
  344. * @version 2.8.0 
  345. * @static 
  346. * @param array $r Full array of args passed into ::request() 
  347. */ 
  348. function buildCookieHeader( &$r ) { 
  349. if ( ! empty($r['cookies']) ) { 
  350. $cookies_header = ''; 
  351. foreach ( (array) $r['cookies'] as $cookie ) { 
  352. $cookies_header .= $cookie->getHeaderValue() . '; '; 
  353. $cookies_header = substr( $cookies_header, 0, -2 ); 
  354. $r['headers']['cookie'] = $cookies_header; 
  355.  
  356. /** 
  357. * Decodes chunk transfer-encoding, based off the HTTP 1.1 specification. 
  358. * Based off the HTTP http_encoding_dechunk function. Does not support UTF-8. Does not support 
  359. * returning footer headers. Shouldn't be too difficult to support it though. 
  360. * @todo Add support for footer chunked headers. 
  361. * @access public 
  362. * @since 2.7.0 
  363. * @static 
  364. * @param string $body Body content 
  365. * @return string Chunked decoded body on success or raw body on failure. 
  366. */ 
  367. function chunkTransferDecode($body) { 
  368. $body = str_replace(array("\r\n", "\r"), "\n", $body); 
  369. // The body is not chunked encoding or is malformed. 
  370. if ( ! preg_match( '/^[0-9a-f]+(\s|\n)+/mi', trim($body) ) ) 
  371. return $body; 
  372.  
  373. $parsedBody = ''; 
  374. //$parsedHeaders = array(); Unsupported 
  375.  
  376. while ( true ) { 
  377. $hasChunk = (bool) preg_match( '/^([0-9a-f]+)(\s|\n)+/mi', $body, $match ); 
  378.  
  379. if ( $hasChunk ) { 
  380. if ( empty( $match[1] ) ) 
  381. return $body; 
  382.  
  383. $length = hexdec( $match[1] ); 
  384. $chunkLength = strlen( $match[0] ); 
  385.  
  386. $strBody = substr($body, $chunkLength, $length); 
  387. $parsedBody .= $strBody; 
  388.  
  389. $body = ltrim(str_replace(array($match[0], $strBody), '', $body), "\n"); 
  390.  
  391. if ( "0" == trim($body) ) 
  392. return $parsedBody; // Ignore footer headers. 
  393. } else { 
  394. return $body; 
  395.  
  396. /** 
  397. * Block requests through the proxy. 
  398. * Those who are behind a proxy and want to prevent access to certain hosts may do so. This will 
  399. * prevent plugins from working and core functionality, if you don't include api.wordpress.org. 
  400. * You block external URL requests by defining WP_HTTP_BLOCK_EXTERNAL in your wp-config.php file 
  401. * and this will only allow localhost and your blog to make requests. The constant 
  402. * WP_ACCESSIBLE_HOSTS will allow additional hosts to go through for requests. The format of the 
  403. * WP_ACCESSIBLE_HOSTS constant is a comma separated list of hostnames to allow. 
  404. * @since 2.8.0 
  405. * @link http://core.trac.wordpress.org/ticket/8927 Allow preventing external requests. 
  406. * @param string $uri URI of url. 
  407. * @return bool True to block, false to allow. 
  408. */ 
  409. function block_request($uri) { 
  410. // We don't need to block requests, because nothing is blocked. 
  411. if ( ! defined('WP_HTTP_BLOCK_EXTERNAL') || ( defined('WP_HTTP_BLOCK_EXTERNAL') && WP_HTTP_BLOCK_EXTERNAL == false ) ) 
  412. return false; 
  413.  
  414. // parse_url() only handles http, https type URLs, and will emit E_WARNING on failure. 
  415. // This will be displayed on blogs, which is not reasonable. 
  416. $check = @parse_url($uri); 
  417.  
  418. /** Malformed URL, can not process, but this could mean ssl, so let through anyway. 
  419. * This isn't very security sound. There are instances where a hacker might attempt 
  420. * to bypass the proxy and this check. However, the reason for this behavior is that 
  421. * WordPress does not do any checking currently for non-proxy requests, so it is keeps with 
  422. * the default unsecure nature of the HTTP request. 
  423. */ 
  424. if ( $check === false ) 
  425. return false; 
  426.  
  427. $home = parse_url( backpress_get_option( 'application_uri' ) ); 
  428.  
  429. // Don't block requests back to ourselves by default 
  430. if ( $check['host'] == 'localhost' || $check['host'] == $home['host'] ) 
  431. return apply_filters('block_local_requests', false); 
  432.  
  433. if ( !defined('WP_ACCESSIBLE_HOSTS') ) 
  434. return true; 
  435.  
  436. static $accessible_hosts; 
  437. if ( null == $accessible_hosts ) 
  438. $accessible_hosts = preg_split('|, \s*|', WP_ACCESSIBLE_HOSTS); 
  439.  
  440. return !in_array( $check['host'], $accessible_hosts ); //Inverse logic, If its in the array, then we can't access it.