mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-13 03:45:22 +01:00
[Http] Removing the MultipartFormData class cURL will handle this better
This commit is contained in:
parent
55523cf8bf
commit
6e02eee711
@ -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)
|
||||
|
@ -1,134 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Guzzle PHP <http://www.guzzlephp.org>
|
||||
* @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 <michael@guzzlephp.org>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,108 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Guzzle PHP <http://www.guzzlephp.org>
|
||||
* @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 <michael@guzzlephp.org>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user