From 4fd093a1ec3d0429a4966665a5377745749bea35 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sat, 12 Apr 2014 02:44:58 -0700 Subject: [PATCH] Add preliminary support for HHVM --- src/Curl.class.php | 15 +++++++++++++-- tests/PHPCurlClass/PHPCurlClassTest.php | 17 +++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Curl.class.php b/src/Curl.class.php index fd77a99..503e53e 100644 --- a/src/Curl.class.php +++ b/src/Curl.class.php @@ -93,6 +93,10 @@ class Curl public function post($url, $data = array()) { + if (is_array($data) && empty($data)) { + $this->setHeader('Content-Length'); + } + $this->setOpt(CURLOPT_URL, $this->buildURL($url)); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'POST'); $this->setOpt(CURLOPT_POST, true); @@ -104,12 +108,17 @@ class Curl { $this->setOpt(CURLOPT_URL, $url); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'PUT'); - $this->setOpt(CURLOPT_POSTFIELDS, http_build_query($data)); + $put_data = http_build_query($data); + if (empty($this->options[CURLOPT_INFILE]) && empty($this->options[CURLOPT_INFILESIZE])) { + $this->setHeader('Content-Length', strlen($put_data)); + } + $this->setOpt(CURLOPT_POSTFIELDS, $put_data); return $this->exec(); } public function patch($url, $data = array()) { + $this->setHeader('Content-Length'); $this->setOpt(CURLOPT_URL, $this->buildURL($url)); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'PATCH'); $this->setOpt(CURLOPT_POSTFIELDS, $data); @@ -118,6 +127,7 @@ class Curl public function delete($url, $data = array()) { + $this->setHeader('Content-Length'); $this->setOpt(CURLOPT_URL, $this->buildURL($url, $data)); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'DELETE'); return $this->exec(); @@ -133,6 +143,7 @@ class Curl public function options($url, $data = array()) { + $this->setHeader('Content-Length'); $this->setOpt(CURLOPT_URL, $this->buildURL($url, $data)); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'OPTIONS'); return $this->exec(); @@ -144,7 +155,7 @@ class Curl $this->setOpt(CURLOPT_USERPWD, $username . ':' . $password); } - public function setHeader($key, $value) + public function setHeader($key, $value = '') { $this->headers[$key] = $key . ': ' . $value; $this->setOpt(CURLOPT_HTTPHEADER, array_values($this->headers)); diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index ae231a3..c42bea0 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -278,9 +278,16 @@ class CurlTest extends PHPUnit_Framework_TestCase { } public function testMultipleCookieResponse() { + $expected_response = 'cookie1=scrumptious,cookie2=mouthwatering'; + + // github.com/facebook/hhvm/issues/2345 + if (defined('HHVM_VERSION')) { + $expected_response = 'cookie2=mouthwatering,cookie1=scrumptious'; + } + $test = new Test(); $test->server('multiple_cookie', 'GET'); - $this->assertEquals($test->curl->response_headers['Set-Cookie'], 'cookie1=scrumptious,cookie2=mouthwatering'); + $this->assertEquals($test->curl->response_headers['Set-Cookie'], $expected_response); } public function testError() { @@ -295,7 +302,13 @@ class CurlTest extends PHPUnit_Framework_TestCase { public function testErrorMessage() { $test = new Test(); $test->server('error_message', 'GET'); - $this->assertTrue($test->curl->error_message === 'HTTP/1.1 401 Unauthorized'); + + $expected_response = 'HTTP/1.1 401 Unauthorized'; + if (defined('HHVM_VERSION')) { + $expected_response = 'HTTP/1.1 401'; + } + + $this->assertEquals($test->curl->error_message, $expected_response); } public function testHeaders() {