From 6b06c03376219b3d608e1f878514ec105ed1b577 Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Mon, 11 May 2015 17:25:57 +0200 Subject: [PATCH] No longer preserving CURLOPT_NOBODY after a HEAD. Fixes #47 Fixes fruux/sabre-dav#649 --- ChangeLog.md | 7 +++++ lib/Client.php | 1 + lib/Version.php | 2 +- tests/HTTP/ClientTest.php | 54 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index f9387e4..8546b27 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,6 +1,13 @@ ChangeLog ========= +3.0.5 (2015-05-11) +------------------ + +* #47: When re-using the client and doing any request after a `HEAD` request, + the client discards the body. + + 3.0.4 (2014-12-10) ------------------ diff --git a/lib/Client.php b/lib/Client.php index 62d51f4..57fd23c 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -73,6 +73,7 @@ function __construct() { $this->curlSettings = [ CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, + CURLOPT_NOBODY => false, ]; } diff --git a/lib/Version.php b/lib/Version.php index c5d5d42..5c66d44 100644 --- a/lib/Version.php +++ b/lib/Version.php @@ -14,6 +14,6 @@ class Version { /** * Full version number */ - const VERSION = '3.0.4'; + const VERSION = '3.0.5'; } diff --git a/tests/HTTP/ClientTest.php b/tests/HTTP/ClientTest.php index 02c0cec..0c661bc 100644 --- a/tests/HTTP/ClientTest.php +++ b/tests/HTTP/ClientTest.php @@ -18,6 +18,7 @@ function testCreateCurlSettingsArrayGET() { CURLOPT_HEADER => true, CURLOPT_POSTREDIR => 0, CURLOPT_HTTPHEADER => ['X-Foo: bar'], + CURLOPT_NOBODY => false, CURLOPT_URL => 'http://example.org/', CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_POSTFIELDS => null, @@ -49,7 +50,42 @@ function testCreateCurlSettingsArrayHEAD() { CURLOPT_CUSTOMREQUEST => 'HEAD', CURLOPT_HTTPHEADER => ['X-Foo: bar'], CURLOPT_URL => 'http://example.org/', - CURLOPT_POSTFIELDS => null, + CURLOPT_POSTFIELDS => '', + CURLOPT_PUT => false, + ]; + + // FIXME: CURLOPT_PROTOCOLS and CURLOPT_REDIR_PROTOCOLS are currently unsupported by HHVM + // at least if this unit test fails in the future we know it is :) + if(defined('HHVM_VERSION') === false) { + $settings[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; + $settings[CURLOPT_REDIR_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; + } + + $this->assertEquals($settings, $client->createCurlSettingsArray($request)); + + } + + function testCreateCurlSettingsArrayGETAfterHEAD() { + + $client = new ClientMock(); + $request = new Request('HEAD','http://example.org/', ['X-Foo' => 'bar']); + + // Parsing the settings for this method, and discarding the result. + // This will cause the client to automatically persist previous + // settings and will help us detect problems. + $client->createCurlSettingsArray($request); + + // This is the real request. + $request = new Request('GET','http://example.org/', ['X-Foo' => 'bar']); + + $settings = [ + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HEADER => true, + CURLOPT_HTTPHEADER => ['X-Foo: bar'], + CURLOPT_NOBODY => false, + CURLOPT_URL => 'http://example.org/', + CURLOPT_POSTFIELDS => '', CURLOPT_PUT => false, ]; @@ -77,6 +113,7 @@ function testCreateCurlSettingsArrayPUTStream() { CURLOPT_HEADER => true, CURLOPT_PUT => true, CURLOPT_INFILE => $h, + CURLOPT_NOBODY => false, CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_HTTPHEADER => ['X-Foo: bar'], CURLOPT_URL => 'http://example.org/', @@ -101,6 +138,7 @@ function testCreateCurlSettingsArrayPUTString() { $settings = [ CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, + CURLOPT_NOBODY => false, CURLOPT_POSTFIELDS => 'boo', CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_HTTPHEADER => ['X-Foo: bar'], @@ -337,12 +375,24 @@ function testDoRequestCurlError() { class ClientMock extends Client { + protected $persistedSettings = []; + /** * Making this method public. + * + * We are also going to persist all settings this method generates. While + * the underlying object doesn't behave exactly the same, it helps us + * simulate what curl does internally, and helps us identify problems with + * settings that are set by _some_ methods and not correctly reset by other + * methods after subsequent use. + * forces */ public function createCurlSettingsArray(RequestInterface $request) { - return parent::createCurlSettingsArray($request); + $settings = parent::createCurlSettingsArray($request); + $settings = $settings + $this->persistedSettings; + $this->persistedSettings = $settings; + return $settings; } /**