<!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" />
<title>[23605] trunk/wp-includes/class-http.php: WP_HTTP: Enable developers to request the first x bytes of a document using the 'limit-response-size' parameter.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { 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 #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#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>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/23605">23605</a></dd>
<dt>Author</dt> <dd>dd32</dd>
<dt>Date</dt> <dd>2013-03-04 04:24:26 +0000 (Mon, 04 Mar 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>WP_HTTP: Enable developers to request the first x bytes of a document using the 'limit-response-size' parameter.
The connection to the remote server will be disconnected after x number of bytes has been received.
See <a href="http://core.trac.wordpress.org/ticket/23472">#23472</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 (23604 => 23605)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-http.php        2013-03-04 04:23:00 UTC (rev 23604)
+++ trunk/wp-includes/class-http.php        2013-03-04 04:24:26 UTC (rev 23605)
</span><span class="lines">@@ -95,7 +95,8 @@
</span><span class="cx">                         'decompress' =&gt; true,
</span><span class="cx">                         'sslverify' =&gt; true,
</span><span class="cx">                         'stream' =&gt; false,
</span><del>-                        'filename' =&gt; null
</del><ins>+                        'filename' =&gt; null,
+                        'limit-response-size' =&gt; null,
</ins><span class="cx">                 );
</span><span class="cx"> 
</span><span class="cx">                 // Pre-parse for the HEAD checks.
</span><span class="lines">@@ -733,6 +734,10 @@
</span><span class="cx"> 
</span><span class="cx">                 $strResponse = '';
</span><span class="cx">                 $bodyStarted = false;
</span><ins>+                $keep_reading = true;
+                $block_size = 4096;
+                if ( isset( $r['limit-response-size'] ) )
+                        $block_size = min( $block_size, $r['limit-response-size'] );
</ins><span class="cx"> 
</span><span class="cx">                 // If streaming to a file setup the file handle
</span><span class="cx">                 if ( $r['stream'] ) {
</span><span class="lines">@@ -743,30 +748,45 @@
</span><span class="cx">                         if ( ! $stream_handle )
</span><span class="cx">                                 return new WP_Error( 'http_request_failed', sprintf( __( 'Could not open handle for fopen() to %s' ), $r['filename'] ) );
</span><span class="cx"> 
</span><del>-                        while ( ! feof($handle) ) {
-                                $block = fread( $handle, 4096 );
-                                if ( $bodyStarted ) {
-                                        fwrite( $stream_handle, $block );
-                                } else {
</del><ins>+                        $bytes_written = 0;
+                        while ( ! feof($handle) &amp;&amp; $keep_reading ) {
+                                $block = fread( $handle, $block_size );
+                                if ( ! $bodyStarted ) {
</ins><span class="cx">                                         $strResponse .= $block;
</span><span class="cx">                                         if ( strpos( $strResponse, &quot;\r\n\r\n&quot; ) ) {
</span><span class="cx">                                                 $process = WP_Http::processResponse( $strResponse );
</span><span class="cx">                                                 $bodyStarted = true;
</span><del>-                                                fwrite( $stream_handle, $process['body'] );
</del><ins>+                                                $block = $process['body'];
</ins><span class="cx">                                                 unset( $strResponse );
</span><span class="cx">                                                 $process['body'] = '';
</span><span class="cx">                                         }
</span><span class="cx">                                 }
</span><ins>+                                
+                                if ( isset( $r['limit-response-size'] ) &amp;&amp; ( $bytes_written + strlen( $block ) ) &gt; $r['limit-response-size'] )
+                                        $block = substr( $block, 0, ( $r['limit-response-size'] - $bytes_written ) );
+
+                                $bytes_written += fwrite( $stream_handle, $block );                                
+                                
+                                $keep_reading = !isset( $r['limit-response-size'] ) || $bytes_written &lt; $r['limit-response-size'];
</ins><span class="cx">                         }
</span><span class="cx"> 
</span><span class="cx">                         fclose( $stream_handle );
</span><span class="cx"> 
</span><span class="cx">                 } else {
</span><del>-                        while ( ! feof($handle) )
-                                $strResponse .= fread( $handle, 4096 );
</del><ins>+                        $header_length = 0;
+                        while ( ! feof( $handle ) &amp;&amp; $keep_reading ) {
+                                $block = fread( $handle, $block_size );
+                                $strResponse .= $block;
+                                if ( ! $bodyStarted &amp;&amp; strpos( $strResponse, &quot;\r\n\r\n&quot; ) ) {
+                                        $header_length = strpos( $strResponse, &quot;\r\n\r\n&quot; ) + 4;
+                                        $bodyStarted = true;
+                                }
+                                $keep_reading = ( ! $bodyStarted || !isset( $r['limit-response-size'] ) || strlen( $strResponse ) &lt; ( $header_length + $r['limit-response-size'] ) );
+                        }
</ins><span class="cx"> 
</span><span class="cx">                         $process = WP_Http::processResponse( $strResponse );
</span><span class="cx">                         unset( $strResponse );
</span><ins>+
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 fclose( $handle );
</span><span class="lines">@@ -792,6 +812,9 @@
</span><span class="cx">                 if ( true === $r['decompress'] &amp;&amp; true === WP_Http_Encoding::should_decode($arrHeaders['headers']) )
</span><span class="cx">                         $process['body'] = WP_Http_Encoding::decompress( $process['body'] );
</span><span class="cx"> 
</span><ins>+                if ( isset( $r['limit-response-size'] ) &amp;&amp; strlen( $process['body'] ) &gt; $r['limit-response-size'] )
+                        $process['body'] = substr( $process['body'], 0, $r['limit-response-size'] );
+
</ins><span class="cx">                 return array( 'headers' =&gt; $arrHeaders['headers'], 'body' =&gt; $process['body'], 'response' =&gt; $arrHeaders['response'], 'cookies' =&gt; $arrHeaders['cookies'], 'filename' =&gt; $r['filename'] );
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -936,6 +959,7 @@
</span><span class="cx">                         return array( 'headers' =&gt; array(), 'body' =&gt; '', 'response' =&gt; array('code' =&gt; false, 'message' =&gt; false), 'cookies' =&gt; array() );
</span><span class="cx">                 }
</span><span class="cx"> 
</span><ins>+                $max_bytes = isset( $r['limit-response-size'] ) ? intval( $r['limit-response-size'] ) : -1;
</ins><span class="cx">                 if ( $r['stream'] ) {
</span><span class="cx">                         if ( ! WP_DEBUG )
</span><span class="cx">                                 $stream_handle = @fopen( $r['filename'], 'w+' );
</span><span class="lines">@@ -945,12 +969,12 @@
</span><span class="cx">                         if ( ! $stream_handle )
</span><span class="cx">                                 return new WP_Error( 'http_request_failed', sprintf( __( 'Could not open handle for fopen() to %s' ), $r['filename'] ) );
</span><span class="cx"> 
</span><del>-                        stream_copy_to_stream( $handle, $stream_handle );
</del><ins>+                        stream_copy_to_stream( $handle, $stream_handle, $max_bytes );
</ins><span class="cx"> 
</span><span class="cx">                         fclose( $stream_handle );
</span><span class="cx">                         $strResponse = '';
</span><span class="cx">                 } else {
</span><del>-                        $strResponse = stream_get_contents( $handle );
</del><ins>+                        $strResponse = stream_get_contents( $handle, $max_bytes );
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 $meta = stream_get_meta_data( $handle );
</span><span class="lines">@@ -1017,7 +1041,7 @@
</span><span class="cx"> class WP_Http_Curl {
</span><span class="cx"> 
</span><span class="cx">         /**
</span><del>-         * Temporary header storage for use with streaming to a file.
</del><ins>+         * Temporary header storage for during requests.
</ins><span class="cx">          *
</span><span class="cx">          * @since 3.2.0
</span><span class="cx">          * @access private
</span><span class="lines">@@ -1026,6 +1050,33 @@
</span><span class="cx">         private $headers = '';
</span><span class="cx"> 
</span><span class="cx">         /**
</span><ins>+         * Temporary body storage for during requests.
+         *
+         * @since 3.6.0
+         * @access private
+         * @var string
+         */
+        private $body = '';
+
+        /**
+         * The maximum amount of data to recieve from the remote server
+         *
+         * @since 3.6.0
+         * @access private
+         * @var int
+         */
+        private $max_body_length = false;
+
+        /**
+         * The file resource used for streaming to file.
+         *
+         * @since 3.6.0
+         * @access private
+         * @var resource
+         */
+        private $stream_handle = false;
+
+        /**
</ins><span class="cx">          * Send a HTTP request to a URI using cURL extension.
</span><span class="cx">          *
</span><span class="cx">          * @access public
</span><span class="lines">@@ -1114,20 +1165,24 @@
</span><span class="cx">                                 break;
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                if ( true === $r['blocking'] )
</del><ins>+                if ( true === $r['blocking'] ) {
</ins><span class="cx">                         curl_setopt( $handle, CURLOPT_HEADERFUNCTION, array( $this, 'stream_headers' ) );
</span><ins>+                        curl_setopt( $handle, CURLOPT_WRITEFUNCTION, array( $this, 'stream_body' ) );
+                }
</ins><span class="cx"> 
</span><span class="cx">                 curl_setopt( $handle, CURLOPT_HEADER, false );
</span><span class="cx"> 
</span><ins>+                if ( isset( $r['limit-response-size'] ) )
+                        $this-&gt;max_body_length = intval( $r['limit-response-size'] );
+
</ins><span class="cx">                 // If streaming to a file open a file handle, and setup our curl streaming handler
</span><span class="cx">                 if ( $r['stream'] ) {
</span><span class="cx">                         if ( ! WP_DEBUG )
</span><del>-                                $stream_handle = @fopen( $r['filename'], 'w+' );
</del><ins>+                                $this-&gt;stream_handle = @fopen( $r['filename'], 'w+' );
</ins><span class="cx">                         else
</span><del>-                                $stream_handle = fopen( $r['filename'], 'w+' );
-                        if ( ! $stream_handle )
</del><ins>+                                $this-&gt;stream_handle = fopen( $r['filename'], 'w+' );
+                        if ( ! $this-&gt;stream_handle )
</ins><span class="cx">                                 return new WP_Error( 'http_request_failed', sprintf( __( 'Could not open handle for fopen() to %s' ), $r['filename'] ) );
</span><del>-                        curl_setopt( $handle, CURLOPT_FILE, $stream_handle );
</del><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 if ( !empty( $r['headers'] ) ) {
</span><span class="lines">@@ -1156,22 +1211,20 @@
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 $theResponse = curl_exec( $handle );
</span><del>-                $theBody = '';
</del><span class="cx">                 $theHeaders = WP_Http::processHeaders( $this-&gt;headers );
</span><ins>+                $theBody = $this-&gt;body;
</ins><span class="cx"> 
</span><del>-                if ( strlen($theResponse) &gt; 0 &amp;&amp; ! is_bool( $theResponse ) ) // is_bool: when using $args['stream'], curl_exec will return (bool)true
-                        $theBody = $theResponse;
</del><ins>+                $this-&gt;headers = '';
+                $this-&gt;body = '';
</ins><span class="cx"> 
</span><span class="cx">                 // If no response
</span><del>-                if ( 0 == strlen( $theResponse ) &amp;&amp; empty( $theHeaders['headers'] ) ) {
</del><ins>+                if ( 0 == strlen( $theBody ) &amp;&amp; empty( $theHeaders['headers'] ) ) {
</ins><span class="cx">                         if ( $curl_error = curl_error( $handle ) )
</span><span class="cx">                                 return new WP_Error( 'http_request_failed', $curl_error );
</span><span class="cx">                         if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ) ) )
</span><span class="cx">                                 return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) );
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                $this-&gt;headers = '';
-
</del><span class="cx">                 $response = array();
</span><span class="cx">                 $response['code'] = curl_getinfo( $handle, CURLINFO_HTTP_CODE );
</span><span class="cx">                 $response['message'] = get_status_header_desc($response['code']);
</span><span class="lines">@@ -1179,7 +1232,7 @@
</span><span class="cx">                 curl_close( $handle );
</span><span class="cx"> 
</span><span class="cx">                 if ( $r['stream'] )
</span><del>-                        fclose( $stream_handle );
</del><ins>+                        fclose( $this-&gt;stream_handle );
</ins><span class="cx"> 
</span><span class="cx">                 // See #11305 - When running under safe mode, redirection is disabled above. Handle it manually.
</span><span class="cx">                 if ( ! empty( $theHeaders['headers']['location'] ) &amp;&amp; 0 !== $r['_redirection'] ) { // _redirection: The requested number of redirections
</span><span class="lines">@@ -1211,6 +1264,28 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><ins>+         * Grab the body of the cURL request
+         *
+         * The contents of the document are passed in chunks, so we append to the $body property for temporary storage.
+         * Returning a length shorter than the length of $data passed in will cause cURL to abort the request as &quot;completed&quot;
+         *
+         * @since 3.6.0
+         * @access private
+         * @return int
+         */
+        private function stream_body( $handle, $data ) {
+                if ( $this-&gt;max_body_length &amp;&amp; ( strlen( $this-&gt;body ) + strlen( $data ) ) &gt; $this-&gt;max_body_length )
+                        $data = substr( $data, 0, ( $this-&gt;max_body_length - strlen( $this-&gt;body ) ) );
+
+                if ( $this-&gt;stream_handle )
+                        fwrite( $this-&gt;stream_handle, $data );
+                else
+                        $this-&gt;body .= $data;
+
+                return strlen( $data );
+        }
+
+        /**
</ins><span class="cx">          * Whether this class can be used for retrieving an URL.
</span><span class="cx">          *
</span><span class="cx">          * @static
</span><span class="lines">@@ -1744,6 +1819,8 @@
</span><span class="cx">                         $compression_enabled = false;
</span><span class="cx">                 elseif ( $args['stream'] ) // disable when streaming to file
</span><span class="cx">                         $compression_enabled = false;
</span><ins>+                elseif ( isset( $args['limit-response-size'] ) ) // If only partial content is being requested, we won't be able to decompress it
+                        $compression_enabled = false;
</ins><span class="cx"> 
</span><span class="cx">                 if ( $compression_enabled ) {
</span><span class="cx">                         if ( function_exists( 'gzinflate' ) )
</span></span></pre>
</div>
</div>

</body>
</html>