<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[17551] trunk/wp-includes/class-http.php: Allow for array('redirection' =&gt;
  0) to bypass WP_Error on redirects being encountered;
  Allows HEAD requests WITH 'redirection' &gt;
  0 specified at call time to follow redirections;
  Standardises on return values from all transports to act the same based on the Unit Tests
 .</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/17551">17551</a></dd>
<dt>Author</dt> <dd>dd32</dd>
<dt>Date</dt> <dd>2011-03-24 05:18:34 +0000 (Thu, 24 Mar 2011)</dd>
</dl>

<h3>Log Message</h3>
<pre>Allow for array('redirection' =&gt; 0) to bypass WP_Error on redirects being encountered; Allows HEAD requests WITH 'redirection' &gt; 0 specified at call time to follow redirections; Standardises on return values from all transports to act the same based on the Unit Tests. Fixes <a href="http://trac.wordpress.org/ticket/16855">#16855</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesclasshttpphp">trunk/wp-includes/class-http.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesclasshttpphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/class-http.php (17550 => 17551)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-http.php        2011-03-24 02:16:13 UTC (rev 17550)
+++ trunk/wp-includes/class-http.php        2011-03-24 05:18:34 UTC (rev 17551)
</span><span class="lines">@@ -100,9 +100,20 @@
</span><span class="cx">                         'sslverify' =&gt; true
</span><span class="cx">                 );
</span><span class="cx"> 
</span><ins>+                
+                // Pre-parse for the HEAD checks.
+                $args = wp_parse_args( $args );
+
+                // By default, Head requests do not cause redirections.
+                if ( isset($args['method']) &amp;&amp; 'HEAD' == $args['method'] )
+                        $defaults['redirection'] = 0;
+
</ins><span class="cx">                 $r = wp_parse_args( $args, $defaults );
</span><span class="cx">                 $r = apply_filters( 'http_request_args', $r, $url );
</span><span class="cx"> 
</span><ins>+                // Certain classes decrement this, store a copy of the original value for loop purposes.
+                $r['_redirection'] = $r['redirection'];
+
</ins><span class="cx">                 // Allow plugins to short-circuit the request
</span><span class="cx">                 $pre = apply_filters( 'pre_http_request', false, $r, $url );
</span><span class="cx">                 if ( false !== $pre )
</span><span class="lines">@@ -664,7 +675,7 @@
</span><span class="cx">                         return new WP_Error('http_request_failed', $arrHeaders['response']['code'] . ': ' . $arrHeaders['response']['message']);
</span><span class="cx"> 
</span><span class="cx">                 // If location is found, then assume redirect and redirect to location.
</span><del>-                if ( 'HEAD' != $r['method'] &amp;&amp; isset($arrHeaders['headers']['location']) ) {
</del><ins>+                if ( isset($arrHeaders['headers']['location']) &amp;&amp; 0 !== $r['_redirection'] ) {
</ins><span class="cx">                         if ( $r['redirection']-- &gt; 0 ) {
</span><span class="cx">                                 return $this-&gt;request($arrHeaders['headers']['location'], $r);
</span><span class="cx">                         } else {
</span><span class="lines">@@ -800,9 +811,6 @@
</span><span class="cx">                                 $arrContext['http']['header'] .= $proxy-&gt;authentication_header() . &quot;\r\n&quot;;
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                if ( 'HEAD' == $r['method'] ) // Disable redirects for HEAD requests
-                        $arrContext['http']['max_redirects'] = 1;
-
</del><span class="cx">                 if ( ! empty($r['body'] ) )
</span><span class="cx">                         $arrContext['http']['content'] = $r['body'];
</span><span class="cx"> 
</span><span class="lines">@@ -837,6 +845,11 @@
</span><span class="cx">                 else
</span><span class="cx">                         $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']);
</span><span class="cx"> 
</span><ins>+                // Streams does not provide an error code which we can use to see why the request stream stoped. 
+                // We can however test to see if a location header is present and return based on that.
+                if ( isset($processedHeaders['headers']['location']) &amp;&amp; 0 !== $args['_redirection'] )
+                        return new WP_Error('http_request_failed', __('Too many redirects.'));
+
</ins><span class="cx">                 if ( ! empty( $strResponse ) &amp;&amp; isset( $processedHeaders['headers']['transfer-encoding'] ) &amp;&amp; 'chunked' == $processedHeaders['headers']['transfer-encoding'] )
</span><span class="cx">                         $strResponse = WP_Http::chunkTransferDecode($strResponse);
</span><span class="cx"> 
</span><span class="lines">@@ -949,9 +962,6 @@
</span><span class="cx">                         )
</span><span class="cx">                 );
</span><span class="cx"> 
</span><del>-                if ( HTTP_METH_HEAD == $r['method'] )
-                        $options['redirect'] = 0; // Assumption: Docs seem to suggest that this means do not follow. Untested.
-
</del><span class="cx">                 // The HTTP extensions offers really easy proxy support.
</span><span class="cx">                 $proxy = new WP_HTTP_Proxy();
</span><span class="cx"> 
</span><span class="lines">@@ -1116,8 +1126,7 @@
</span><span class="cx">                         curl_setopt( $handle, CURLOPT_HEADER, false );
</span><span class="cx"> 
</span><span class="cx">                 // The option doesn't work with safe mode or when open_basedir is set.
</span><del>-                // Disable HEAD when making HEAD requests.
-                if ( !ini_get('safe_mode') &amp;&amp; !ini_get('open_basedir') &amp;&amp; 'HEAD' != $r['method'] )
</del><ins>+                if ( !ini_get('safe_mode') &amp;&amp; !ini_get('open_basedir') &amp;&amp; 0 !== $r['_redirection'] )
</ins><span class="cx">                         curl_setopt( $handle, CURLOPT_FOLLOWLOCATION, true );
</span><span class="cx"> 
</span><span class="cx">                 if ( !empty( $r['headers'] ) ) {
</span><span class="lines">@@ -1176,7 +1185,7 @@
</span><span class="cx">                 curl_close( $handle );
</span><span class="cx"> 
</span><span class="cx">                 // See #11305 - When running under safe mode, redirection is disabled above. Handle it manually.
</span><del>-                if ( !empty($theHeaders['headers']['location']) &amp;&amp; (ini_get('safe_mode') || ini_get('open_basedir')) ) {
</del><ins>+                if ( !empty($theHeaders['headers']['location']) &amp;&amp; (ini_get('safe_mode') || ini_get('open_basedir')) &amp;&amp; 0 !== $r['_redirection'] ) {
</ins><span class="cx">                         if ( $r['redirection']-- &gt; 0 ) {
</span><span class="cx">                                 return $this-&gt;request($theHeaders['headers']['location'], $r);
</span><span class="cx">                         } else {
</span></span></pre>
</div>
</div>

</body>
</html>