diff --git a/src/HPCloud/Transport/PHPStreamTransport.php b/src/HPCloud/Transport/PHPStreamTransport.php index b9c3662..bbc8e3e 100644 --- a/src/HPCloud/Transport/PHPStreamTransport.php +++ b/src/HPCloud/Transport/PHPStreamTransport.php @@ -84,6 +84,10 @@ class PHPStreamTransport implements Transporter { } $metadata = stream_get_meta_data($res); + if (\HPCloud\Bootstrap::hasConfig('transport.debug')) { + fwrite(STDOUT, implode(PHP_EOL, $metadata['wrapper_data'])); + fprintf(STDOUT, "\nWaiting to read %d bytes.\n", $metadata['unread_bytes']); + } $response = new Response($res, $metadata); @@ -217,7 +221,6 @@ class PHPStreamTransport implements Transporter { * stream context. This builds the context. */ protected function buildStreamContext($method, $headers, $body) { - // Construct the stream options. $config = array( 'http' => array( @@ -239,7 +242,12 @@ class PHPStreamTransport implements Transporter { // Set the params. (Currently there is only one.) $params = array(); if (!empty($this->notificationCallback)) { - $params['notification_callback'] = $this->notificationCallback; + $params['notification'] = $this->notificationCallback; + } + // Enable debugging: + elseif (\HPCloud\Bootstrap::hasConfig('transport.debug')) { + fwrite(STDOUT, "Sending debug messages to STDOUT\n"); + $params['notification'] = array($this, 'printNotifications'); } // Build the context. @@ -248,4 +256,41 @@ class PHPStreamTransport implements Transporter { return $context; } + public function printNotifications($code, $severity, $msg, $msgcode, $bytes, $len) { + static $filesize = 'Unknown'; + + switch ($code) { + case STREAM_NOTIFY_RESOLVE: + fprintf(STDOUT, "Resolved. %s\n", $msg); + break; + case STREAM_NOTIFY_FAILURE: + fprintf(STDOUT, "socket-level failure: %s\n", $msg); + break; + case STREAM_NOTIFY_COMPLETED: + fprintf(STDOUT, "Transaction complete. %s\n", $msg); + break; + //case STREAM_NOTIFY_REDIRECT: + // fprintf(STDOUT, "Redirect... %s\n", $msg); + // break; + case STREAM_NOTIFY_CONNECT: + fprintf(STDOUT, "Connect... %s\n", $msg); + break; + case STREAM_NOTIFY_FILE_SIZE_IS: + fprintf(STDOUT, "Content-length: %d\n", $len); + $filesize = $len; + break; + case STREAM_NOTIFY_MIME_TYPE_IS: + fprintf(STDOUT, "Content-Type: %s\n", $msg); + break; + case STREAM_NOTIFY_PROGRESS: + fwrite(STDOUT, $msg . PHP_EOL); + fprintf(STDOUT, "%d bytes of %s\n", $bytes, $filesize); + break; + default: + fprintf(STDOUT, "Code: %d, Message: %s\n", $code, $msg); + break; + } + + } + } diff --git a/src/HPCloud/Transport/Response.php b/src/HPCloud/Transport/Response.php index 0020899..f39aa32 100644 --- a/src/HPCloud/Transport/Response.php +++ b/src/HPCloud/Transport/Response.php @@ -167,28 +167,24 @@ class Response { public function content() { $out = ''; - // This should always be set... but... - if (isset($this->metadata['unread_bytes'])) { - $bytes = (int) $this->metadata['unread_bytes']; - if ($bytes == 0 && $this->header('Content-Length', 0) > 0) { - throw new \HPCloud\Exception(sprintf( - 'Content length %d doesn\'t match byte count %d.', - $this->header('Content-Length', 0), - $bytes - )); - throw new \Exception(print_r($this->metadata, TRUE)); + // XXX: The addition of the Content-Length check is a workaround + // for an issue with using PHP Stream Wrappers to communicate with + // Identity Service. Apparently, the remote does not provide + // an EOF marker, and PHP is too dumb to truncate at Content-Length, + // so we have to do it manually. + $max = $this->header('Content-Length', NULL); + if (isset($this->metadata['unread_bytes']) && isset($max)) { + while (!feof($this->handle) && strlen($out) < $max) { + $out .= fread($this->handle, 8192); } - $out = fread($this->handle, $bytes); - //$out = stream_get_contents($this->handle); } - // XXX: In cases of large files, isn't this the safer default? else { - $out = ''; while (!feof($this->handle)) { $out .= fread($this->handle, 8192); } } + // Should we close or rewind? // Cannot rewind PHP HTTP streams. fclose($this->handle); diff --git a/test/Tests/CURLTransportTest.php b/test/Tests/CURLTransportTest.php index 7d0dfb2..7272770 100644 --- a/test/Tests/CURLTransportTest.php +++ b/test/Tests/CURLTransportTest.php @@ -32,7 +32,7 @@ require_once 'test/TestCase.php'; use \HPCloud\Transport; use \HPCloud\Transport\CURLTransport; -class ObjectTest extends \HPCloud\Tests\TestCase { +class CURLTransportTest extends \HPCloud\Tests\TestCase { public static function tearDownAfterClass() { $transport = NULL;