From 5c64824b2e55ad79ba362f064c086010e8c0743f Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Sun, 17 Jan 2010 09:03:56 +0000 Subject: [PATCH] Allow WP_HTTP_Fopen to send extra headers and custom user-agents. See #11888 git-svn-id: https://develop.svn.wordpress.org/trunk@12748 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/http.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/wp-includes/http.php b/wp-includes/http.php index 60c2da1355..2bb08d5800 100644 --- a/wp-includes/http.php +++ b/wp-includes/http.php @@ -837,6 +837,25 @@ class WP_Http_Fopen { if ( 'http' != $arrURL['scheme'] && 'https' != $arrURL['scheme'] ) $url = str_replace($arrURL['scheme'], 'http', $url); + if ( is_null( $r['headers'] ) ) + $r['headers'] = array(); + + if ( is_string($r['headers']) ) { + $processedHeaders = WP_Http::processHeaders($r['headers']); + $r['headers'] = $processedHeaders['headers']; + } + + $initial_user_agent = ini_get('user_agent'); + + if ( !empty($r['headers']) && is_array($r['headers']) ) { + $user_agent_extra_headers = ''; + foreach ( $r['headers'] as $header => $value ) + $user_agent_extra_headers .= "\r\n$header: $value"; + @ini_set('user_agent', $r['user-agent'] . $user_agent_extra_headers); + } else { + @ini_set('user_agent', $r['user-agent']); + } + if ( !WP_DEBUG ) $handle = @fopen($url, 'r'); else @@ -851,6 +870,7 @@ class WP_Http_Fopen { if ( ! $r['blocking'] ) { fclose($handle); + @ini_set('user_agent', $initial_user_agent); //Clean up any extra headers added return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() ); } @@ -871,6 +891,8 @@ class WP_Http_Fopen { fclose($handle); + @ini_set('user_agent', $initial_user_agent); //Clean up any extra headers added + $processedHeaders = WP_Http::processHeaders($theHeaders); if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] )