From ce8cdd8f673edcfab79256c01dc6d79fce1fa82b Mon Sep 17 00:00:00 2001 From: Michael Dowling Date: Wed, 8 Aug 2012 17:16:55 -0700 Subject: [PATCH] [Http] Attempting to seek to the beginning of a stream when sending EntityEnclosingRequest objects so that they can be reused without having to seek manually --- src/Guzzle/Http/Curl/CurlHandle.php | 2 ++ .../Guzzle/Tests/Http/Curl/CurlHandleTest.php | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/Guzzle/Http/Curl/CurlHandle.php b/src/Guzzle/Http/Curl/CurlHandle.php index 771dc4b2..d8c6c4a4 100644 --- a/src/Guzzle/Http/Curl/CurlHandle.php +++ b/src/Guzzle/Http/Curl/CurlHandle.php @@ -153,6 +153,8 @@ class CurlHandle // Add a callback for curl to read data to send with the request // only if a body was specified $curlOptions[CURLOPT_READFUNCTION] = array($mediator, 'readRequestBody'); + // Attempt to seek to the start of the stream + $request->getBody()->seek(0); } elseif ($request->getMethod() == 'POST') { // Need to remove CURLOPT_POST to prevent chunked encoding unset($curlOptions[CURLOPT_POST]); diff --git a/tests/Guzzle/Tests/Http/Curl/CurlHandleTest.php b/tests/Guzzle/Tests/Http/Curl/CurlHandleTest.php index 4608bdaf..d24f037d 100644 --- a/tests/Guzzle/Tests/Http/Curl/CurlHandleTest.php +++ b/tests/Guzzle/Tests/Http/Curl/CurlHandleTest.php @@ -929,4 +929,23 @@ class CurlHandleTest extends \Guzzle\Tests\GuzzleTestCase $this->assertEquals(2, count($received)); $this->assertEquals($method, $received[1]->getMethod()); } + + public function testSeeksToBeginningOfStreamWhenSending() + { + $this->getServer()->flush(); + $this->getServer()->enqueue(array( + "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", + "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" + )); + + $client = new Client($this->getServer()->getUrl()); + $request = $client->put('/', null, 'test'); + $request->send(); + $request->send(); + + $received = $this->getServer()->getReceivedRequests(true); + $this->assertEquals(2, count($received)); + $this->assertEquals('test', (string) $received[0]->getBody()); + $this->assertEquals('test', (string) $received[1]->getBody()); + } }