Download project

This commit is contained in:
Roman Pyrih
2024-11-20 09:09:44 +01:00
parent 547a138d6a
commit 5ff041757f
40737 changed files with 7766183 additions and 0 deletions

View File

@@ -0,0 +1,109 @@
<?php
namespace GuzzleHttp\Post;
use GuzzleHttp\Stream\AppendStream;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Stream\StreamDecoratorTrait;
use GuzzleHttp\Stream\StreamInterface;
/**
* Stream that when read returns bytes for a streaming multipart/form-data body
*/
class MultipartBody implements StreamInterface
{
use StreamDecoratorTrait;
private $boundary;
/**
* @param array $fields Associative array of field names to values where
* each value is a string or array of strings.
* @param array $files Associative array of PostFileInterface objects
* @param string $boundary You can optionally provide a specific boundary
* @throws \InvalidArgumentException
*/
public function __construct(
array $fields = [],
array $files = [],
$boundary = null
) {
$this->boundary = $boundary ?: uniqid();
$this->stream = $this->createStream($fields, $files);
}
/**
* Get the boundary
*
* @return string
*/
public function getBoundary()
{
return $this->boundary;
}
public function isWritable()
{
return false;
}
/**
* Get the string needed to transfer a POST field
*/
private function getFieldString($name, $value)
{
return sprintf(
"--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n",
$this->boundary,
$name,
$value
);
}
/**
* Get the headers needed before transferring the content of a POST file
*/
private function getFileHeaders(PostFileInterface $file)
{
$headers = '';
foreach ($file->getHeaders() as $key => $value) {
$headers .= "{$key}: {$value}\r\n";
}
return "--{$this->boundary}\r\n" . trim($headers) . "\r\n\r\n";
}
/**
* Create the aggregate stream that will be used to upload the POST data
*/
protected function createStream(array $fields, array $files)
{
$stream = new AppendStream();
foreach ($fields as $name => $fieldValues) {
foreach ((array) $fieldValues as $value) {
$stream->addStream(
Stream::factory($this->getFieldString($name, $value))
);
}
}
foreach ($files as $file) {
if (!$file instanceof PostFileInterface) {
throw new \InvalidArgumentException('All POST fields must '
. 'implement PostFieldInterface');
}
$stream->addStream(
Stream::factory($this->getFileHeaders($file))
);
$stream->addStream($file->getContent());
$stream->addStream(Stream::factory("\r\n"));
}
// Add the trailing boundary with CRLF
$stream->addStream(Stream::factory("--{$this->boundary}--\r\n"));
return $stream;
}
}

View File

@@ -0,0 +1,287 @@
<?php
namespace GuzzleHttp\Post;
use GuzzleHttp\Message\RequestInterface;
use GuzzleHttp\Stream\Exception\CannotAttachException;
use GuzzleHttp\Stream\StreamInterface;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Query;
/**
* Holds POST fields and files and creates a streaming body when read methods
* are called on the object.
*/
class PostBody implements PostBodyInterface
{
/** @var StreamInterface */
private $body;
/** @var callable */
private $aggregator;
private $fields = [];
/** @var PostFileInterface[] */
private $files = [];
private $forceMultipart = false;
private $detached = false;
/**
* Applies request headers to a request based on the POST state
*
* @param RequestInterface $request Request to update
*/
public function applyRequestHeaders(RequestInterface $request)
{
if ($this->files || $this->forceMultipart) {
$request->setHeader(
'Content-Type',
'multipart/form-data; boundary=' . $this->getBody()->getBoundary()
);
} elseif ($this->fields && !$request->hasHeader('Content-Type')) {
$request->setHeader(
'Content-Type',
'application/x-www-form-urlencoded'
);
}
if ($size = $this->getSize()) {
$request->setHeader('Content-Length', $size);
}
}
public function forceMultipartUpload($force)
{
$this->forceMultipart = $force;
}
public function setAggregator(callable $aggregator)
{
$this->aggregator = $aggregator;
}
public function setField($name, $value)
{
$this->fields[$name] = $value;
$this->mutate();
}
public function replaceFields(array $fields)
{
$this->fields = $fields;
$this->mutate();
}
public function getField($name)
{
return isset($this->fields[$name]) ? $this->fields[$name] : null;
}
public function removeField($name)
{
unset($this->fields[$name]);
$this->mutate();
}
public function getFields($asString = false)
{
if (!$asString) {
return $this->fields;
}
$query = new Query($this->fields);
$query->setEncodingType(Query::RFC1738);
$query->setAggregator($this->getAggregator());
return (string) $query;
}
public function hasField($name)
{
return isset($this->fields[$name]);
}
public function getFile($name)
{
foreach ($this->files as $file) {
if ($file->getName() == $name) {
return $file;
}
}
return null;
}
public function getFiles()
{
return $this->files;
}
public function addFile(PostFileInterface $file)
{
$this->files[] = $file;
$this->mutate();
}
public function clearFiles()
{
$this->files = [];
$this->mutate();
}
/**
* Returns the numbers of fields + files
*
* @return int
*/
public function count()
{
return count($this->files) + count($this->fields);
}
public function __toString()
{
return (string) $this->getBody();
}
public function getContents($maxLength = -1)
{
return $this->getBody()->getContents();
}
public function close()
{
$this->detach();
}
public function detach()
{
$this->detached = true;
$this->fields = $this->files = [];
if ($this->body) {
$this->body->close();
$this->body = null;
}
}
public function attach($stream)
{
throw new CannotAttachException();
}
public function eof()
{
return $this->getBody()->eof();
}
public function tell()
{
return $this->body ? $this->body->tell() : 0;
}
public function isSeekable()
{
return true;
}
public function isReadable()
{
return true;
}
public function isWritable()
{
return false;
}
public function getSize()
{
return $this->getBody()->getSize();
}
public function seek($offset, $whence = SEEK_SET)
{
return $this->getBody()->seek($offset, $whence);
}
public function read($length)
{
return $this->getBody()->read($length);
}
public function write($string)
{
return false;
}
public function getMetadata($key = null)
{
return $key ? null : [];
}
/**
* Return a stream object that is built from the POST fields and files.
*
* If one has already been created, the previously created stream will be
* returned.
*/
private function getBody()
{
if ($this->body) {
return $this->body;
} elseif ($this->files || $this->forceMultipart) {
return $this->body = $this->createMultipart();
} elseif ($this->fields) {
return $this->body = $this->createUrlEncoded();
} else {
return $this->body = Stream::factory();
}
}
/**
* Get the aggregator used to join multi-valued field parameters
*
* @return callable
*/
final protected function getAggregator()
{
if (!$this->aggregator) {
$this->aggregator = Query::phpAggregator();
}
return $this->aggregator;
}
/**
* Creates a multipart/form-data body stream
*
* @return MultipartBody
*/
private function createMultipart()
{
// Flatten the nested query string values using the correct aggregator
return new MultipartBody(
call_user_func($this->getAggregator(), $this->fields),
$this->files
);
}
/**
* Creates an application/x-www-form-urlencoded stream body
*
* @return StreamInterface
*/
private function createUrlEncoded()
{
return Stream::factory($this->getFields(true));
}
/**
* Get rid of any cached data
*/
private function mutate()
{
$this->body = null;
}
}

View File

@@ -0,0 +1,109 @@
<?php
namespace GuzzleHttp\Post;
use GuzzleHttp\Message\AppliesHeadersInterface;
use GuzzleHttp\Stream\StreamInterface;
/**
* Represents a POST body that is sent as either a multipart/form-data stream
* or application/x-www-urlencoded stream.
*/
interface PostBodyInterface extends StreamInterface, \Countable, AppliesHeadersInterface
{
/**
* Set a specific field
*
* @param string $name Name of the field to set
* @param string|array $value Value to set
*/
public function setField($name, $value);
/**
* Set the aggregation strategy that will be used to turn multi-valued
* fields into a string.
*
* The aggregation function accepts a deeply nested array of query string
* values and returns a flattened associative array of key value pairs.
*
* @param callable $aggregator
*/
public function setAggregator(callable $aggregator);
/**
* Set to true to force a multipart upload even if there are no files.
*
* @param bool $force Set to true to force multipart uploads or false to
* remove this flag.
*/
public function forceMultipartUpload($force);
/**
* Replace all existing form fields with an array of fields
*
* @param array $fields Associative array of fields to set
*/
public function replaceFields(array $fields);
/**
* Get a specific field by name
*
* @param string $name Name of the POST field to retrieve
*
* @return string|null
*/
public function getField($name);
/**
* Remove a field by name
*
* @param string $name Name of the field to remove
*/
public function removeField($name);
/**
* Returns an associative array of names to values or a query string.
*
* @param bool $asString Set to true to retrieve the fields as a query
* string.
*
* @return array|string
*/
public function getFields($asString = false);
/**
* Returns true if a field is set
*
* @param string $name Name of the field to set
*
* @return bool
*/
public function hasField($name);
/**
* Get all of the files
*
* @return array Returns an array of PostFileInterface objects
*/
public function getFiles();
/**
* Get a POST file by name.
*
* @param string $name Name of the POST file to retrieve
*
* @return PostFileInterface|null
*/
public function getFile($name);
/**
* Add a file to the POST
*
* @param PostFileInterface $file File to add
*/
public function addFile(PostFileInterface $file);
/**
* Remove all files from the collection
*/
public function clearFiles();
}

View File

@@ -0,0 +1,135 @@
<?php
namespace GuzzleHttp\Post;
use GuzzleHttp\Mimetypes;
use GuzzleHttp\Stream\StreamInterface;
use GuzzleHttp\Stream\Stream;
/**
* Post file upload
*/
class PostFile implements PostFileInterface
{
private $name;
private $filename;
private $content;
private $headers = [];
/**
* @param string $name Name of the form field
* @param mixed $content Data to send
* @param string|null $filename Filename content-disposition attribute
* @param array $headers Array of headers to set on the file
* (can override any default headers)
* @throws \RuntimeException when filename is not passed or can't be determined
*/
public function __construct(
$name,
$content,
$filename = null,
array $headers = []
) {
$this->headers = $headers;
$this->name = $name;
$this->prepareContent($content);
$this->prepareFilename($filename);
$this->prepareDefaultHeaders();
}
public function getName()
{
return $this->name;
}
public function getFilename()
{
return $this->filename;
}
public function getContent()
{
return $this->content;
}
public function getHeaders()
{
return $this->headers;
}
/**
* Prepares the contents of a POST file.
*
* @param mixed $content Content of the POST file
*/
private function prepareContent($content)
{
$this->content = $content;
if (!($this->content instanceof StreamInterface)) {
$this->content = Stream::factory($this->content);
} elseif ($this->content instanceof MultipartBody) {
if (!$this->hasHeader('Content-Disposition')) {
$disposition = 'form-data; name="' . $this->name .'"';
$this->headers['Content-Disposition'] = $disposition;
}
if (!$this->hasHeader('Content-Type')) {
$this->headers['Content-Type'] = sprintf(
"multipart/form-data; boundary=%s",
$this->content->getBoundary()
);
}
}
}
/**
* Applies a file name to the POST file based on various checks.
*
* @param string|null $filename Filename to apply (or null to guess)
*/
private function prepareFilename($filename)
{
$this->filename = $filename;
if (!$this->filename) {
$this->filename = $this->content->getMetadata('uri');
}
if (!$this->filename || substr($this->filename, 0, 6) === 'php://') {
$this->filename = $this->name;
}
}
/**
* Applies default Content-Disposition and Content-Type headers if needed.
*/
private function prepareDefaultHeaders()
{
// Set a default content-disposition header if one was no provided
if (!$this->hasHeader('Content-Disposition')) {
$this->headers['Content-Disposition'] = sprintf(
'form-data; name="%s"; filename="%s"',
$this->name,
basename($this->filename)
);
}
// Set a default Content-Type if one was not supplied
if (!$this->hasHeader('Content-Type')) {
$this->headers['Content-Type'] = Mimetypes::getInstance()
->fromFilename($this->filename) ?: 'text/plain';
}
}
/**
* Check if a specific header exists on the POST file by name.
*
* @param string $name Case-insensitive header to check
*
* @return bool
*/
private function hasHeader($name)
{
return isset(array_change_key_case($this->headers)[strtolower($name)]);
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace GuzzleHttp\Post;
use GuzzleHttp\Stream\StreamInterface;
/**
* Post file upload interface
*/
interface PostFileInterface
{
/**
* Get the name of the form field
*
* @return string
*/
public function getName();
/**
* Get the full path to the file
*
* @return string
*/
public function getFilename();
/**
* Get the content
*
* @return StreamInterface
*/
public function getContent();
/**
* Gets all POST file headers.
*
* The keys represent the header name as it will be sent over the wire, and
* each value is a string.
*
* @return array Returns an associative array of the file's headers.
*/
public function getHeaders();
}