diff --git a/src/Httpful/Mime.php b/src/Httpful/Mime.php index 1a7c456..cc28c85 100644 --- a/src/Httpful/Mime.php +++ b/src/Httpful/Mime.php @@ -8,15 +8,16 @@ */ class Mime { - const JSON = 'application/json'; - const XML = 'application/xml'; - const XHTML = 'application/html+xml'; - const FORM = 'application/x-www-form-urlencoded'; - const PLAIN = 'text/plain'; - const JS = 'text/javascript'; - const HTML = 'text/html'; - const YAML = 'application/x-yaml'; - const CSV = 'text/csv'; + const JSON = 'application/json'; + const XML = 'application/xml'; + const XHTML = 'application/html+xml'; + const FORM = 'application/x-www-form-urlencoded'; + const UPLOAD = 'multipart/form-data'; + const PLAIN = 'text/plain'; + const JS = 'text/javascript'; + const HTML = 'text/html'; + const YAML = 'application/x-yaml'; + const CSV = 'text/csv'; /** * Map short name for a mime type @@ -28,6 +29,7 @@ class Mime 'form' => self::FORM, 'plain' => self::PLAIN, 'text' => self::PLAIN, + 'upload' => self::UPLOAD, 'html' => self::HTML, 'xhtml' => self::XHTML, 'js' => self::JS, diff --git a/src/Httpful/Request.php b/src/Httpful/Request.php index c66ff09..0b9eb20 100644 --- a/src/Httpful/Request.php +++ b/src/Httpful/Request.php @@ -199,7 +199,7 @@ public function send() $this->_curlPrep(); $result = curl_exec($this->_ch); - + if ($result === false) { $this->_error(curl_error($this->_ch)); throw new ConnectionErrorException('Unable to connect.'); @@ -333,6 +333,9 @@ public function mime($mime) { if (empty($mime)) return $this; $this->content_type = $this->expected_type = Mime::getFullMime($mime); + if($this->isUpload()) { + $this->neverSerializePayload(); + } return $this; } // @alias of mime @@ -374,6 +377,15 @@ public function expectsType($mime) { return $this->expects($mime); } + + public function attach($files) { + foreach ($files as $key => $file) { + $this->payload[$key] = "@{$file}"; + } + + $this->sendsType(Mime::UPLOAD); + return $this; + } /** * @return Request this @@ -383,6 +395,9 @@ public function contentType($mime) { if (empty($mime)) return $this; $this->content_type = Mime::getFullMime($mime); + if($this->isUpload()) { + $this->neverSerializePayload(); + } return $this; } // @alias of contentType @@ -437,7 +452,7 @@ public function useProxy($proxy_host, $proxy_port = 80, $auth_type = null, $auth * @return is this request setup for using proxy? */ public function hasProxy(){ - return is_string($this->additional_curl_opts[CURLOPT_PROXY]); + return isset($this->additional_curl_opts[CURLOPT_PROXY]) && is_string($this->additional_curl_opts[CURLOPT_PROXY]); } /** @@ -794,7 +809,8 @@ public function _curlPrep() if (isset($this->payload)) { $this->serialized_payload = $this->_serializePayload($this->payload); curl_setopt($ch, CURLOPT_POSTFIELDS, $this->serialized_payload); - $this->headers['Content-Length'] = strlen($this->serialized_payload); + if(!$this->isUpload()) + $this->headers['Content-Length'] = strlen($this->serialized_payload) ; } $headers = array(); @@ -821,7 +837,7 @@ public function _curlPrep() } // Solve a bug on squid proxy, NONE/411 when miss content length - if (!isset($this->headers['Content-Length'])) { + if (!isset($this->headers['Content-Length']) && !$this->isUpload()) { $this->headers['Content-Length'] = 0; } @@ -850,11 +866,15 @@ public function _curlPrep() foreach ($this->additional_curl_opts as $curlopt => $curlval) { curl_setopt($ch, $curlopt, $curlval); } - + $this->_ch = $ch; return $this; } + + public function isUpload() { + return Mime::UPLOAD == $this->content_type; + } public function buildUserAgent() { $user_agent = 'User-Agent: Httpful/' . Httpful::VERSION . ' (cURL/'; diff --git a/tests/Httpful/HttpfulTest.php b/tests/Httpful/HttpfulTest.php index ac2ab54..8a3115c 100644 --- a/tests/Httpful/HttpfulTest.php +++ b/tests/Httpful/HttpfulTest.php @@ -91,12 +91,14 @@ function testShortMime() $this->assertEquals(Mime::XML, Mime::getFullMime('xml')); $this->assertEquals(Mime::HTML, Mime::getFullMime('html')); $this->assertEquals(Mime::CSV, Mime::getFullMime('csv')); + $this->assertEquals(Mime::UPLOAD, Mime::getFullMime('upload')); // Valid long ones $this->assertEquals(Mime::JSON, Mime::getFullMime(Mime::JSON)); $this->assertEquals(Mime::XML, Mime::getFullMime(Mime::XML)); $this->assertEquals(Mime::HTML, Mime::getFullMime(Mime::HTML)); $this->assertEquals(Mime::CSV, Mime::getFullMime(Mime::CSV)); + $this->assertEquals(Mime::UPLOAD, Mime::getFullMime(Mime::UPLOAD)); // No false positives $this->assertNotEquals(Mime::XML, Mime::getFullMime(Mime::HTML)); @@ -293,7 +295,36 @@ function testParsingContentTypeCharset() $this->assertEquals($response->content_type, 'text/plain'); $this->assertEquals($response->charset, 'utf-8'); } - + + function testParsingContentTypeUpload() + { + $req = Request::init(); + + $req->sendsType(Mime::UPLOAD); + // $response = new Response(SAMPLE_JSON_RESPONSE, "", $req); + // // Check default content type of iso-8859-1 + $this->assertEquals($req->content_type, 'multipart/form-data'); + } + + function testAttach() { + $req = Request::init(); + + $req->attach(array('index' => '/dir/filename')); + // $response = new Response(SAMPLE_JSON_RESPONSE, "", $req); + // // Check default content type of iso-8859-1 + $this->assertEquals($req->payload['index'], '@/dir/filename'); + $this->assertEquals($req->content_type, Mime::UPLOAD); + $this->assertEquals($req->serialize_payload_method, Request::SERIALIZE_PAYLOAD_NEVER); + } + + function testIsUpload() { + $req = Request::init(); + + $req->sendsType(Mime::UPLOAD); + + $this->assertTrue($req->isUpload()); + } + function testEmptyResponseParse() { $req = Request::init()->sendsAndExpects(Mime::JSON);