Fixed fread() usage in Request_HTTP not properly handling incomplete responses
authorDan
Fri, 02 Apr 2010 14:42:15 -0400
changeset 1234 903bb153c265
parent 1233 202b25e2d0e6
child 1235 25e13d35cd76
Fixed fread() usage in Request_HTTP not properly handling incomplete responses
includes/http.php
--- a/includes/http.php	Thu Apr 01 19:57:23 2010 -0500
+++ b/includes/http.php	Fri Apr 02 14:42:15 2010 -0400
@@ -370,10 +370,8 @@
 		$newline = "\r\n";
 		
 		if ( $this->debug )
+		{
 			echo '<p>Connection opened. Writing main request to socket. Raw socket data follows.</p><pre>';
-		
-		if ( $this->debug )
-		{
 			echo '<hr /><div style="white-space: nowrap;">';
 			echo '<p><b>' . __CLASS__ . ': Sending request</b></p><p>Request parameters:</p>';
 			echo "<p><b>Headers:</b></p><pre>$headers</pre>";
@@ -386,7 +384,7 @@
 		$portline = ( $this->port == 80 ) ? '' : ":$this->port";
 		
 		$this->_fputs($connection, "{$this->method} {$this->uri}{$get} HTTP/1.1{$newline}");
-		$this->_fputs($connection, "Host: {$this->host}$portline{$newline}");
+		$this->_fputs($connection, "Host: {$this->host}{$portline}{$newline}");
 		$this->_fputs($connection, $headers);
 		$this->_fputs($connection, $cookies);
 		
@@ -528,7 +526,17 @@
 				{
 					echo "Pulling response using fread(), size $size\n";
 				}
-				$this->response .= fread($this->socket, $size);
+				$basesize = strlen($this->response);
+				while ( strlen($this->response) - $basesize < $size )
+				{
+					$remaining_bytes = $size - (strlen($this->response) - $basesize);
+					$this->response .= fread($this->socket, $remaining_bytes);
+					if ( $this->debug )
+					{
+						$remaining_bytes = $size - (strlen($this->response) - $basesize);
+						echo "<br />Received " . (strlen($this->response) - $basesize) . " of $size bytes ($remaining_bytes remaining)...\n";
+					}
+				}
 			}
 			else
 			{