*\r
* LICENSE:\r
*\r
- * Copyright (c) 2008-2011, Alexey Borzov <avb@php.net>\r
+ * Copyright (c) 2008-2012, Alexey Borzov <avb@php.net>\r
* All rights reserved.\r
*\r
* Redistribution and use in source and binary forms, with or without\r
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
*\r
- * @category HTTP\r
- * @package HTTP_Request2\r
- * @author Alexey Borzov <avb@php.net>\r
- * @license http://opensource.org/licenses/bsd-license.php New BSD License\r
- * @version SVN: $Id: MultipartBody.php 308322 2011-02-14 13:58:03Z avb $\r
- * @link http://pear.php.net/package/HTTP_Request2\r
+ * @category HTTP\r
+ * @package HTTP_Request2\r
+ * @author Alexey Borzov <avb@php.net>\r
+ * @license http://opensource.org/licenses/bsd-license.php New BSD License\r
+ * @version SVN: $Id: MultipartBody.php 324415 2012-03-21 10:50:50Z avb $\r
+ * @link http://pear.php.net/package/HTTP_Request2\r
*/\r
\r
/**\r
* The class helps to reduce memory consumption by streaming large file uploads\r
* from disk, it also allows monitoring of upload progress (see request #7630)\r
*\r
- * @category HTTP\r
- * @package HTTP_Request2\r
- * @author Alexey Borzov <avb@php.net>\r
- * @version Release: 2.0.0RC1\r
- * @link http://tools.ietf.org/html/rfc1867\r
+ * @category HTTP\r
+ * @package HTTP_Request2\r
+ * @author Alexey Borzov <avb@php.net>\r
+ * @license http://opensource.org/licenses/bsd-license.php New BSD License\r
+ * @version Release: 2.1.1\r
+ * @link http://pear.php.net/package/HTTP_Request2\r
+ * @link http://tools.ietf.org/html/rfc1867\r
*/\r
class HTTP_Request2_MultipartBody\r
{\r
- /**\r
- * MIME boundary\r
- * @var string\r
- */\r
+ /**\r
+ * MIME boundary\r
+ * @var string\r
+ */\r
private $_boundary;\r
\r
- /**\r
- * Form parameters added via {@link HTTP_Request2::addPostParameter()}\r
- * @var array\r
- */\r
+ /**\r
+ * Form parameters added via {@link HTTP_Request2::addPostParameter()}\r
+ * @var array\r
+ */\r
private $_params = array();\r
\r
- /**\r
- * File uploads added via {@link HTTP_Request2::addUpload()}\r
- * @var array\r
- */\r
+ /**\r
+ * File uploads added via {@link HTTP_Request2::addUpload()}\r
+ * @var array\r
+ */\r
private $_uploads = array();\r
\r
- /**\r
- * Header for parts with parameters\r
- * @var string\r
- */\r
+ /**\r
+ * Header for parts with parameters\r
+ * @var string\r
+ */\r
private $_headerParam = "--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n";\r
\r
- /**\r
- * Header for parts with uploads\r
- * @var string\r
- */\r
+ /**\r
+ * Header for parts with uploads\r
+ * @var string\r
+ */\r
private $_headerUpload = "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: %s\r\n\r\n";\r
\r
- /**\r
- * Current position in parameter and upload arrays\r
- *\r
- * First number is index of "current" part, second number is position within\r
- * "current" part\r
- *\r
- * @var array\r
- */\r
+ /**\r
+ * Current position in parameter and upload arrays\r
+ *\r
+ * First number is index of "current" part, second number is position within\r
+ * "current" part\r
+ *\r
+ * @var array\r
+ */\r
private $_pos = array(0, 0);\r
\r
\r
- /**\r
- * Constructor. Sets the arrays with POST data.\r
- *\r
- * @param array values of form fields set via {@link HTTP_Request2::addPostParameter()}\r
- * @param array file uploads set via {@link HTTP_Request2::addUpload()}\r
- * @param bool whether to append brackets to array variable names\r
- */\r
+ /**\r
+ * Constructor. Sets the arrays with POST data.\r
+ *\r
+ * @param array $params values of form fields set via\r
+ * {@link HTTP_Request2::addPostParameter()}\r
+ * @param array $uploads file uploads set via\r
+ * {@link HTTP_Request2::addUpload()}\r
+ * @param bool $useBrackets whether to append brackets to array variable names\r
+ */\r
public function __construct(array $params, array $uploads, $useBrackets = true)\r
{\r
$this->_params = self::_flattenArray('', $params, $useBrackets);\r
}\r
}\r
\r
- /**\r
- * Returns the length of the body to use in Content-Length header\r
- *\r
- * @return integer\r
- */\r
+ /**\r
+ * Returns the length of the body to use in Content-Length header\r
+ *\r
+ * @return integer\r
+ */\r
public function getLength()\r
{\r
$boundaryLength = strlen($this->getBoundary());\r
return $length;\r
}\r
\r
- /**\r
- * Returns the boundary to use in Content-Type header\r
- *\r
- * @return string\r
- */\r
+ /**\r
+ * Returns the boundary to use in Content-Type header\r
+ *\r
+ * @return string\r
+ */\r
public function getBoundary()\r
{\r
if (empty($this->_boundary)) {\r
return $this->_boundary;\r
}\r
\r
- /**\r
- * Returns next chunk of request body\r
- *\r
- * @param integer Amount of bytes to read\r
- * @return string Up to $length bytes of data, empty string if at end\r
- */\r
+ /**\r
+ * Returns next chunk of request body\r
+ *\r
+ * @param integer $length Number of bytes to read\r
+ *\r
+ * @return string Up to $length bytes of data, empty string if at end\r
+ */\r
public function read($length)\r
{\r
$ret = '';\r
while ($length > 0 && $this->_pos[0] <= $paramCount + $uploadCount) {\r
$oldLength = $length;\r
if ($this->_pos[0] < $paramCount) {\r
- $param = sprintf($this->_headerParam, $boundary,\r
- $this->_params[$this->_pos[0]][0]) .\r
- $this->_params[$this->_pos[0]][1] . "\r\n";\r
+ $param = sprintf(\r
+ $this->_headerParam, $boundary, $this->_params[$this->_pos[0]][0]\r
+ ) . $this->_params[$this->_pos[0]][1] . "\r\n";\r
$ret .= substr($param, $this->_pos[1], $length);\r
$length -= min(strlen($param) - $this->_pos[1], $length);\r
\r
} elseif ($this->_pos[0] < $paramCount + $uploadCount) {\r
$pos = $this->_pos[0] - $paramCount;\r
- $header = sprintf($this->_headerUpload, $boundary,\r
- $this->_uploads[$pos]['name'],\r
- $this->_uploads[$pos]['filename'],\r
- $this->_uploads[$pos]['type']);\r
+ $header = sprintf(\r
+ $this->_headerUpload, $boundary, $this->_uploads[$pos]['name'],\r
+ $this->_uploads[$pos]['filename'], $this->_uploads[$pos]['type']\r
+ );\r
if ($this->_pos[1] < strlen($header)) {\r
$ret .= substr($header, $this->_pos[1], $length);\r
$length -= min(strlen($header) - $this->_pos[1], $length);\r
return $ret;\r
}\r
\r
- /**\r
- * Sets the current position to the start of the body\r
- *\r
- * This allows reusing the same body in another request\r
- */\r
+ /**\r
+ * Sets the current position to the start of the body\r
+ *\r
+ * This allows reusing the same body in another request\r
+ */\r
public function rewind()\r
{\r
$this->_pos = array(0, 0);\r
}\r
}\r
\r
- /**\r
- * Returns the body as string\r
- *\r
- * Note that it reads all file uploads into memory so it is a good idea not\r
- * to use this method with large file uploads and rely on read() instead.\r
- *\r
- * @return string\r
- */\r
+ /**\r
+ * Returns the body as string\r
+ *\r
+ * Note that it reads all file uploads into memory so it is a good idea not\r
+ * to use this method with large file uploads and rely on read() instead.\r
+ *\r
+ * @return string\r
+ */\r
public function __toString()\r
{\r
$this->rewind();\r
}\r
\r
\r
- /**\r
- * Helper function to change the (probably multidimensional) associative array\r
- * into the simple one.\r
- *\r
- * @param string name for item\r
- * @param mixed item's values\r
- * @param bool whether to append [] to array variables' names\r
- * @return array array with the following items: array('item name', 'item value');\r
- */\r
+ /**\r
+ * Helper function to change the (probably multidimensional) associative array\r
+ * into the simple one.\r
+ *\r
+ * @param string $name name for item\r
+ * @param mixed $values item's values\r
+ * @param bool $useBrackets whether to append [] to array variables' names\r
+ *\r
+ * @return array array with the following items: array('item name', 'item value');\r
+ */\r
private static function _flattenArray($name, $values, $useBrackets)\r
{\r
if (!is_array($values)) {\r