1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-01-18 05:48:15 +01:00

Lots of fixes to multipart/form-data

Using the AppendStream to vastly clean up how a multipart POST is created.
This changes also cleans up how the body is sent over the wire, eliminating
some bugs I discovered when fields are sent before files.
This commit is contained in:
Michael Dowling 2014-07-15 15:50:36 -07:00
parent b909bd8bef
commit 14f0873442
4 changed files with 73 additions and 214 deletions

View File

@ -17,7 +17,7 @@
"require": {
"php": ">=5.4.0",
"ext-json": "*",
"guzzlehttp/streams": "~1.0"
"guzzlehttp/streams": "~1.3"
},
"suggest": {
@ -34,7 +34,7 @@
"psr-4": {
"GuzzleHttp\\Tests\\": "tests/"
}
},
},
"require-dev": {
"ext-curl": "*",
"psr/log": "~1.0",

View File

@ -10,15 +10,7 @@ use GuzzleHttp\Stream;
class MultipartBody implements Stream\StreamInterface
{
/** @var Stream\StreamInterface */
private $files;
private $fields;
private $size;
private $buffer;
private $bufferedHeaders = [];
private $pos = 0;
private $currentFile = 0;
private $currentField = 0;
private $sentLast;
private $stream;
private $boundary;
/**
@ -34,43 +26,17 @@ class MultipartBody implements Stream\StreamInterface
$boundary = null
) {
$this->boundary = $boundary ?: uniqid();
$this->fields = $fields;
$this->files = $files;
// Ensure each file is a PostFileInterface
foreach ($this->files as $file) {
if (!$file instanceof PostFileInterface) {
throw new \InvalidArgumentException('All POST fields must '
. 'implement PostFieldInterface');
}
}
$this->createStream($fields, $files);
}
public function __toString()
{
$this->seek(0);
return $this->getContents();
return (string) $this->stream;
}
public function getContents($maxLength = -1)
{
$buffer = '';
while (!$this->eof()) {
if ($maxLength === -1) {
$read = 1048576;
} else {
$len = strlen($buffer);
if ($len == $maxLength) {
break;
}
$read = min(1048576, $maxLength - $len);
}
$buffer .= $this->read($read);
}
return $buffer;
return $this->stream->getContents($maxLength);
}
/**
@ -85,28 +51,24 @@ class MultipartBody implements Stream\StreamInterface
public function close()
{
$this->stream->close();
$this->detach();
}
public function detach()
{
$this->fields = $this->files = [];
$this->stream->detach();
$this->size = 0;
}
/**
* The stream has reached an EOF when all of the fields and files have been
* read.
* {@inheritdoc}
*/
public function eof()
{
return $this->currentField == count($this->fields) &&
$this->currentFile == count($this->files);
return $this->stream->eof();
}
public function tell()
{
return $this->pos;
return $this->stream->tell();
}
public function isReadable()
@ -119,77 +81,24 @@ class MultipartBody implements Stream\StreamInterface
return false;
}
/**
* The steam is seekable by default, but all attached files must be
* seekable too.
* {@inheritdoc}
*/
public function isSeekable()
{
foreach ($this->files as $file) {
if (!$file->getContent()->isSeekable()) {
return false;
}
}
return true;
return $this->stream->isSeekable();
}
public function getSize()
{
if ($this->size === null) {
foreach ($this->files as $file) {
// We must be able to ascertain the size of each attached file
if (null === ($size = $file->getContent()->getSize())) {
return null;
}
$this->size += strlen($this->getFileHeaders($file)) + $size;
}
foreach (array_keys($this->fields) as $key) {
$this->size += strlen($this->getFieldString($key));
}
$this->size += strlen("\r\n--{$this->boundary}--");
}
return $this->size;
return $this->stream->getSize();
}
public function read($length)
{
$content = '';
if ($this->buffer && !$this->buffer->eof()) {
$content .= $this->buffer->read($length);
}
if ($delta = $length - strlen($content)) {
$content .= $this->readData($delta);
}
if ($content === '' && !$this->sentLast) {
$this->sentLast = true;
$content = "\r\n--{$this->boundary}--";
}
return $content;
return $this->stream->read($length);
}
public function seek($offset, $whence = SEEK_SET)
{
if ($offset != 0 || $whence != SEEK_SET || !$this->isSeekable()) {
return false;
}
foreach ($this->files as $file) {
if (!$file->getContent()->seek(0)) {
throw new \RuntimeException('Rewind on multipart file failed '
. 'even though it shouldn\'t have');
}
}
$this->buffer = $this->sentLast = null;
$this->pos = $this->currentField = $this->currentFile = 0;
$this->bufferedHeaders = [];
return true;
return $this->stream->seek($offset, $whence);
}
public function write($string)
@ -198,88 +107,21 @@ class MultipartBody implements Stream\StreamInterface
}
/**
* No data is in the read buffer, so more needs to be pulled in from fields
* and files.
*
* @param int $length Amount of data to read
*
* @return string
* Get the string needed to transfer a POST field
*/
private function readData($length)
{
$result = '';
if ($this->currentField < count($this->fields)) {
$result = $this->readField($length);
}
if ($result === '' && $this->currentFile < count($this->files)) {
$result = $this->readFile($length);
}
return $result;
}
/**
* Create a new stream buffer and inject form-data
*
* @param int $length Amount of data to read from the stream buffer
*
* @return string
*/
private function readField($length)
{
$name = array_keys($this->fields)[++$this->currentField - 1];
$this->buffer = Stream\create($this->getFieldString($name));
return $this->buffer->read($length);
}
/**
* Read data from a POST file, fill the read buffer with any overflow
*
* @param int $length Amount of data to read from the file
*
* @return string
*/
private function readFile($length)
{
$current = $this->files[$this->currentFile];
// Got to the next file and recursively return the read value, or bail
// if no more data can be read.
if ($current->getContent()->eof()) {
return ++$this->currentFile == count($this->files)
? ''
: $this->readFile($length);
}
// If this is the start of a file, then send the headers to the read
// buffer.
if (!isset($this->bufferedHeaders[$this->currentFile])) {
$this->buffer = Stream\create($this->getFileHeaders($current));
$this->bufferedHeaders[$this->currentFile] = true;
}
// More data needs to be read to meet the limit, so pull from the file
$content = $this->buffer ? $this->buffer->read($length) : '';
if (($remaining = $length - strlen($content)) > 0) {
$content .= $current->getContent()->read($remaining);
}
return $content;
}
private function getFieldString($key)
private function getFieldString($name, $value)
{
return sprintf(
"--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n",
$this->boundary,
$key,
$this->fields[$key]
$name,
$value
);
}
/**
* Get the headers needed before transferring the content of a POST file
*/
private function getFileHeaders(PostFileInterface $file)
{
$headers = '';
@ -289,4 +131,35 @@ class MultipartBody implements Stream\StreamInterface
return "--{$this->boundary}\r\n" . trim($headers) . "\r\n\r\n";
}
/**
* Create the aggregate stream that will be used to upload the POST data
*/
private function createStream(array $fields, array $files)
{
$this->stream = new Stream\AppendStream();
foreach ($fields as $name => $field) {
$this->stream->addStream(
Stream\create($this->getFieldString($name, $field))
);
}
foreach ($files as $file) {
if (!$file instanceof PostFileInterface) {
throw new \InvalidArgumentException('All POST fields must '
. 'implement PostFieldInterface');
}
$this->stream->addStream(
Stream\create($this->getFileHeaders($file))
);
$this->stream->addStream($file->getContent());
$this->stream->addStream(Stream\create("\r\n"));
}
// Add the trailing boundary
$this->stream->addStream(Stream\create("--{$this->boundary}--"));
}
}

View File

@ -253,15 +253,19 @@ class PostBody implements PostBodyInterface
private function createMultipart()
{
// Flatten the nested query string values using the correct aggregator
$query = (string) (new Query($this->fields))
->setEncodingType(false)
->setAggregator($this->getAggregator());
if (!$this->fields) {
$fields = [];
} else {
$query = (string) (new Query($this->fields))
->setEncodingType(false)
->setAggregator($this->getAggregator());
// Convert the flattened query string back into an array
$fields = [];
foreach (explode('&', $query, 2) as $kvp) {
$parts = explode('=', $kvp);
$fields[$parts[0]] = isset($parts[1]) ? $parts[1] : null;
// Convert the flattened query string back into an array
$fields = [];
foreach (explode('&', $query, 2) as $kvp) {
$parts = explode('=', $kvp);
$fields[$parts[0]] = isset($parts[1]) ? $parts[1] : null;
}
}
return new MultipartBody($fields, $this->files);

View File

@ -70,44 +70,23 @@ class MultipartBodyTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('', (string) $b);
}
public function testCanOnlySeekTo0()
{
$b = new MultipartBody();
$this->assertFalse($b->seek(10));
}
public function testIsSeekableReturnsTrueIfAllAreSeekable()
{
$s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
->setMethods(['isSeekable'])
->setMethods(['isSeekable', 'isReadable'])
->getMockForAbstractClass();
$s->expects($this->once())
->method('isSeekable')
->will($this->returnValue(false));
$s->expects($this->once())
->method('isReadable')
->will($this->returnValue(true));
$p = new PostFile('foo', $s, 'foo.php');
$b = new MultipartBody([], [$p]);
$this->assertFalse($b->isSeekable());
$this->assertFalse($b->seek(10));
}
/**
* @expectedException \RuntimeException
*/
public function testThrowsExceptionWhenStreamFailsToRewind()
{
$s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
->setMethods(['seek', 'isSeekable'])
->getMockForAbstractClass();
$s->expects($this->once())
->method('isSeekable')
->will($this->returnValue(true));
$s->expects($this->once())
->method('seek')
->will($this->returnValue(false));
$b = new MultipartBody([], [new PostFile('foo', $s, 'foo.php')]);
$b->seek(0);
}
public function testGetContentsCanCap()
{
$b = $this->getTestBody();
@ -136,11 +115,14 @@ class MultipartBodyTest extends \PHPUnit_Framework_TestCase
public function testCalculatesSizeAndReturnsNullForUnknown()
{
$s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
->setMethods(['getSize'])
->setMethods(['getSize', 'isReadable'])
->getMockForAbstractClass();
$s->expects($this->once())
->method('getSize')
->will($this->returnValue(null));
$s->expects($this->once())
->method('isReadable')
->will($this->returnValue(true));
$b = new MultipartBody([], [new PostFile('foo', $s, 'foo.php')]);
$this->assertNull($b->getSize());
}