From 6e02eee7114c6828a7ed6863d177e7192f75125f Mon Sep 17 00:00:00 2001 From: Michael Dowling Date: Mon, 28 Mar 2011 22:50:19 -0500 Subject: [PATCH] [Http] Removing the MultipartFormData class cURL will handle this better --- .../Http/Message/EntityEnclosingRequest.php | 1 - library/Guzzle/Http/MultipartFormData.php | 134 ------------------ .../Tests/Http/MultipartFormDataTest.php | 108 -------------- 3 files changed, 243 deletions(-) delete mode 100644 library/Guzzle/Http/MultipartFormData.php delete mode 100644 tests/Guzzle/Tests/Http/MultipartFormDataTest.php diff --git a/library/Guzzle/Http/Message/EntityEnclosingRequest.php b/library/Guzzle/Http/Message/EntityEnclosingRequest.php index 10e2c3ad..de8e77ec 100644 --- a/library/Guzzle/Http/Message/EntityEnclosingRequest.php +++ b/library/Guzzle/Http/Message/EntityEnclosingRequest.php @@ -11,7 +11,6 @@ use Guzzle\Common\Event\Subject; use Guzzle\Common\Event\Observer; use Guzzle\Http\EntityBody; use Guzzle\Http\QueryString; -use Guzzle\Http\MultipartFormData; /** * HTTP request that sends an entity-body in the request message (POST, PUT) diff --git a/library/Guzzle/Http/MultipartFormData.php b/library/Guzzle/Http/MultipartFormData.php deleted file mode 100644 index 48a21b4f..00000000 --- a/library/Guzzle/Http/MultipartFormData.php +++ /dev/null @@ -1,134 +0,0 @@ - - * @license See the LICENSE file that was distributed with this source code. - */ - -namespace Guzzle\Http; - -use Guzzle\Http\Message\EntityEnclosingRequestInterface; - -/** - * Class for building multipart/form-data entity bodies - * - * @link http://www.ietf.org/rfc/rfc1867.txt - * @link http://www.ietf.org/rfc/rfc2388.txt - * @author Michael Dowling - */ -class MultipartFormData -{ - /** - * @var EntityBody - */ - protected $entityBody; - - /** - * Factory to build anew MultipartFormData object using the post fields of - * set on a request object - * - * @param EntityEnclosingRequestInterface $request Request - * - * @return MultipartFormData - */ - public static function fromRequestfactory(EntityEnclosingRequestInterface $request) - { - return new self($request->getPostFields()->urlEncode(), $request->getPostFiles(), true); - } - - /** - * Constructor to set the POST data - * - * @param array $fields (optional) Associative array of form fields - * @param array $files (optional) Associative Array of filenames to use in - * the upload, the key of the array is the field name of the file - */ - public function __construct(array $fields = array(), array $files = array()) - { - $this->setPostData($fields, $files); - } - - /** - * Get the POST body as a string - * - * @return string - */ - public function __toString() - { - return (string)$this->entityBody; - } - - /** - * Get the POST multipart/form-data entity body as an EntityBody for use - * with an HTTP POST request - */ - public function getEntityBody() - { - $this->entityBody->seek(0); - - return $this->entityBody; - } - - /** - * Rebuild the entity body to send in a POST multipart/form-data request - * - * @param array $fields Associative array of form fields - * @param array $files Associative Array of filenames to use in the upload, - * the key of the array is the field name to send in the POST data - * - * @return MultipartFormData - */ - public function setPostData(array $fields, array $files = array()) - { - $this->entityBody = EntityBody::factory(''); - - // Try to create a boundary that is not part of the fields or files - $boundary = uniqid(md5(microtime())); - $written = 0; - - // Add the form fields to the POST entity body - foreach ($fields as $fieldName => $value) { - - if ($written) { - $this->entityBody->write("\r\n"); - } - - $this->entityBody->write(sprintf( - "--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s", - $boundary, $fieldName, $value - )); - $written++; - } - - // Add the files to the POST entity body - foreach ($files as $fieldName => $file) { - - if (!is_readable($file)) { - throw new HttpException('Unable to open file ' . $file); - } - - // Get the mime type of the file - $finfo = finfo_open(FILEINFO_MIME_TYPE); - $mimeType = finfo_file($finfo, $file); - finfo_close($finfo); - - if ($written) { - $this->entityBody->write("\r\n"); - } - - $this->entityBody->write(sprintf( - "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: %s\r\n\r\n", - $boundary, $fieldName, basename($file), $mimeType - )); - - // Add the contents of the file to the entity body - $fp = fopen($file, 'r'); - while ($data = fread($fp, 8192)) { - $this->entityBody->write($data); - } - - $written++; - } - - return $this; - } -} \ No newline at end of file diff --git a/tests/Guzzle/Tests/Http/MultipartFormDataTest.php b/tests/Guzzle/Tests/Http/MultipartFormDataTest.php deleted file mode 100644 index c26d3575..00000000 --- a/tests/Guzzle/Tests/Http/MultipartFormDataTest.php +++ /dev/null @@ -1,108 +0,0 @@ - - * @license See the LICENSE file that was distributed with this source code. - */ - -namespace Guzzle\Tests\Http; - -use Guzzle\Http\QueryString; -use Guzzle\Http\MultipartFormData; -use Guzzle\Http\Message\EntityEnclosingRequest; - -/** - * @author Michael Dowling - */ -class MultipartFormDataTest extends \Guzzle\Tests\GuzzleTestCase -{ - /** - * @covers Guzzle\Http\MultipartFormData::__construct - */ - public function testConstructorSetsInitialFields() - { - $e = new MultipartFormData(array( - 'a' => 'b', - 'b' => 'c' - ), array( - 'file[0]' => __FILE__ - )); - - $this->validateTest($e); - } - - /** - * @covers Guzzle\Http\MultipartFormData::setPostData - */ - public function testPostDataCanBeChangedAtAnyTime() - { - $e = new MultipartFormData(); - $e->setPostData(array( - 'a' => 'b', - 'b' => 'c' - ), array( - 'file[0]' => __FILE__ - )); - - $this->validateTest($e); - } - - /** - * @covers Guzzle\Http\MultipartFormData::__toString - * @covers Guzzle\Http\MultipartFormData::getEntityBody - */ - public function testManagesEntityBody() - { - $e = new MultipartFormData(array( - 'a' => 'b', - 'b' => 'c' - ), array( - 'file[0]' => __FILE__ - )); - - $this->assertInternalType('string', (string)$e); - $this->assertEquals((string) $e->getEntityBody(), (string)$e); - } - - /** - * @covers Guzzle\Http\MultipartFormData::fromRequestfactory - */ - public function testCanCreatesEntityBodyUsingRequest() - { - $r = new EntityEnclosingRequest('POST', 'http://www.test.com/'); - $r->addPostFields(array( - 'a' => 'b', - 'b' => 'c' - ))->addPostFiles(array( - 'file[0]' => __FILE__ - )); - - $e = MultipartFormData::fromRequestfactory($r); - - $this->validateTest($e); - } - - /** - * @covers Guzzle\Http\MultipartFormData::setPostData - * @expectedException Guzzle\Http\HttpException - * @expectedExceptionMessage Unable to open file blee_bloo_blop.not_there - */ - public function testValidatesFilesAreReadable() - { - $e = new MultipartFormData(array(), array( - 'test_file' => 'blee_bloo_blop.not_there' - )); - } - - /** - * Create a multipart upload object and validate it - */ - protected function validateTest($m) - { - $eb = (string) $m->getEntityBody(); - - $this->assertContains("Content-Disposition: form-data; name=\"a\"\r\n\r\nb\r\n", $eb); - $this->assertContains("Content-Disposition: form-data; name=\"b\"\r\n\r\nc\r\n", $eb); - $this->assertContains("Content-Disposition: form-data; name=\"file[0]\"; filename=\"MultipartFormDataTest.php\"\r\nContent-Type: text/x-php\r\n\r\n", $eb); - $this->assertContains(file_get_contents(__FILE__), $eb); - } -} \ No newline at end of file