Initial import.

This commit is contained in:
ekes 2015-02-24 16:25:12 +01:00
commit 86383280c9
428 changed files with 68738 additions and 0 deletions

View file

@ -0,0 +1,220 @@
<?php
namespace Guzzle\Http\Message;
use Guzzle\Common\Version;
use Guzzle\Common\Collection;
use Guzzle\Http\Message\Header\HeaderCollection;
use Guzzle\Http\Message\Header\HeaderFactory;
use Guzzle\Http\Message\Header\HeaderFactoryInterface;
use Guzzle\Http\Message\Header\HeaderInterface;
/**
* Abstract HTTP request/response message
*/
abstract class AbstractMessage implements MessageInterface
{
/** @var array HTTP header collection */
protected $headers;
/** @var HeaderFactoryInterface $headerFactory */
protected $headerFactory;
/** @var Collection Custom message parameters that are extendable by plugins */
protected $params;
/** @var string Message protocol */
protected $protocol = 'HTTP';
/** @var string HTTP protocol version of the message */
protected $protocolVersion = '1.1';
public function __construct()
{
$this->params = new Collection();
$this->headerFactory = new HeaderFactory();
$this->headers = new HeaderCollection();
}
/**
* Set the header factory to use to create headers
*
* @param HeaderFactoryInterface $factory
*
* @return self
*/
public function setHeaderFactory(HeaderFactoryInterface $factory)
{
$this->headerFactory = $factory;
return $this;
}
public function getParams()
{
return $this->params;
}
public function addHeader($header, $value)
{
if (isset($this->headers[$header])) {
$this->headers[$header]->add($value);
} elseif ($value instanceof HeaderInterface) {
$this->headers[$header] = $value;
} else {
$this->headers[$header] = $this->headerFactory->createHeader($header, $value);
}
return $this;
}
public function addHeaders(array $headers)
{
foreach ($headers as $key => $value) {
$this->addHeader($key, $value);
}
return $this;
}
public function getHeader($header)
{
return $this->headers[$header];
}
public function getHeaders()
{
return $this->headers;
}
public function getHeaderLines()
{
$headers = array();
foreach ($this->headers as $value) {
$headers[] = $value->getName() . ': ' . $value;
}
return $headers;
}
public function setHeader($header, $value)
{
unset($this->headers[$header]);
$this->addHeader($header, $value);
return $this;
}
public function setHeaders(array $headers)
{
$this->headers->clear();
foreach ($headers as $key => $value) {
$this->addHeader($key, $value);
}
return $this;
}
public function hasHeader($header)
{
return isset($this->headers[$header]);
}
public function removeHeader($header)
{
unset($this->headers[$header]);
return $this;
}
/**
* @deprecated Use $message->getHeader()->parseParams()
* @codeCoverageIgnore
*/
public function getTokenizedHeader($header, $token = ';')
{
Version::warn(__METHOD__ . ' is deprecated. Use $message->getHeader()->parseParams()');
if ($this->hasHeader($header)) {
$data = new Collection();
foreach ($this->getHeader($header)->parseParams() as $values) {
foreach ($values as $key => $value) {
if ($value === '') {
$data->set($data->count(), $key);
} else {
$data->add($key, $value);
}
}
}
return $data;
}
}
/**
* @deprecated
* @codeCoverageIgnore
*/
public function setTokenizedHeader($header, $data, $token = ';')
{
Version::warn(__METHOD__ . ' is deprecated.');
return $this;
}
/**
* @deprecated
* @codeCoverageIgnore
*/
public function getCacheControlDirective($directive)
{
Version::warn(__METHOD__ . ' is deprecated. Use $message->getHeader(\'Cache-Control\')->getDirective()');
if (!($header = $this->getHeader('Cache-Control'))) {
return null;
}
return $header->getDirective($directive);
}
/**
* @deprecated
* @codeCoverageIgnore
*/
public function hasCacheControlDirective($directive)
{
Version::warn(__METHOD__ . ' is deprecated. Use $message->getHeader(\'Cache-Control\')->hasDirective()');
if ($header = $this->getHeader('Cache-Control')) {
return $header->hasDirective($directive);
} else {
return false;
}
}
/**
* @deprecated
* @codeCoverageIgnore
*/
public function addCacheControlDirective($directive, $value = true)
{
Version::warn(__METHOD__ . ' is deprecated. Use $message->getHeader(\'Cache-Control\')->addDirective()');
if (!($header = $this->getHeader('Cache-Control'))) {
$this->addHeader('Cache-Control', '');
$header = $this->getHeader('Cache-Control');
}
$header->addDirective($directive, $value);
return $this;
}
/**
* @deprecated
* @codeCoverageIgnore
*/
public function removeCacheControlDirective($directive)
{
Version::warn(__METHOD__ . ' is deprecated. Use $message->getHeader(\'Cache-Control\')->removeDirective()');
if ($header = $this->getHeader('Cache-Control')) {
$header->removeDirective($directive);
}
return $this;
}
}

View file

@ -0,0 +1,247 @@
<?php
namespace Guzzle\Http\Message;
use Guzzle\Http\EntityBody;
use Guzzle\Http\EntityBodyInterface;
use Guzzle\Http\QueryString;
use Guzzle\Http\RedirectPlugin;
use Guzzle\Http\Exception\RequestException;
/**
* HTTP request that sends an entity-body in the request message (POST, PUT, PATCH, DELETE)
*/
class EntityEnclosingRequest extends Request implements EntityEnclosingRequestInterface
{
/** @var int When the size of the body is greater than 1MB, then send Expect: 100-Continue */
protected $expectCutoff = 1048576;
/** @var EntityBodyInterface $body Body of the request */
protected $body;
/** @var QueryString POST fields to use in the EntityBody */
protected $postFields;
/** @var array POST files to send with the request */
protected $postFiles = array();
public function __construct($method, $url, $headers = array())
{
$this->postFields = new QueryString();
parent::__construct($method, $url, $headers);
}
/**
* @return string
*/
public function __toString()
{
// Only attempt to include the POST data if it's only fields
if (count($this->postFields) && empty($this->postFiles)) {
return parent::__toString() . (string) $this->postFields;
}
return parent::__toString() . $this->body;
}
public function setState($state, array $context = array())
{
parent::setState($state, $context);
if ($state == self::STATE_TRANSFER && !$this->body && !count($this->postFields) && !count($this->postFiles)) {
$this->setHeader('Content-Length', 0)->removeHeader('Transfer-Encoding');
}
return $this->state;
}
public function setBody($body, $contentType = null)
{
$this->body = EntityBody::factory($body);
// Auto detect the Content-Type from the path of the request if possible
if ($contentType === null && !$this->hasHeader('Content-Type')) {
$contentType = $this->body->getContentType();
}
if ($contentType) {
$this->setHeader('Content-Type', $contentType);
}
// Always add the Expect 100-Continue header if the body cannot be rewound. This helps with redirects.
if (!$this->body->isSeekable() && $this->expectCutoff !== false) {
$this->setHeader('Expect', '100-Continue');
}
// Set the Content-Length header if it can be determined
$size = $this->body->getContentLength();
if ($size !== null && $size !== false) {
$this->setHeader('Content-Length', $size);
if ($size > $this->expectCutoff) {
$this->setHeader('Expect', '100-Continue');
}
} elseif (!$this->hasHeader('Content-Length')) {
if ('1.1' == $this->protocolVersion) {
$this->setHeader('Transfer-Encoding', 'chunked');
} else {
throw new RequestException(
'Cannot determine Content-Length and cannot use chunked Transfer-Encoding when using HTTP/1.0'
);
}
}
return $this;
}
public function getBody()
{
return $this->body;
}
/**
* Set the size that the entity body of the request must exceed before adding the Expect: 100-Continue header.
*
* @param int|bool $size Cutoff in bytes. Set to false to never send the expect header (even with non-seekable data)
*
* @return self
*/
public function setExpectHeaderCutoff($size)
{
$this->expectCutoff = $size;
if ($size === false || !$this->body) {
$this->removeHeader('Expect');
} elseif ($this->body && $this->body->getSize() && $this->body->getSize() > $size) {
$this->setHeader('Expect', '100-Continue');
}
return $this;
}
public function configureRedirects($strict = false, $maxRedirects = 5)
{
$this->getParams()->set(RedirectPlugin::STRICT_REDIRECTS, $strict);
if ($maxRedirects == 0) {
$this->getParams()->set(RedirectPlugin::DISABLE, true);
} else {
$this->getParams()->set(RedirectPlugin::MAX_REDIRECTS, $maxRedirects);
}
return $this;
}
public function getPostField($field)
{
return $this->postFields->get($field);
}
public function getPostFields()
{
return $this->postFields;
}
public function setPostField($key, $value)
{
$this->postFields->set($key, $value);
$this->processPostFields();
return $this;
}
public function addPostFields($fields)
{
$this->postFields->merge($fields);
$this->processPostFields();
return $this;
}
public function removePostField($field)
{
$this->postFields->remove($field);
$this->processPostFields();
return $this;
}
public function getPostFiles()
{
return $this->postFiles;
}
public function getPostFile($fieldName)
{
return isset($this->postFiles[$fieldName]) ? $this->postFiles[$fieldName] : null;
}
public function removePostFile($fieldName)
{
unset($this->postFiles[$fieldName]);
$this->processPostFields();
return $this;
}
public function addPostFile($field, $filename = null, $contentType = null, $postname = null)
{
$data = null;
if ($field instanceof PostFileInterface) {
$data = $field;
} elseif (is_array($filename)) {
// Allow multiple values to be set in a single key
foreach ($filename as $file) {
$this->addPostFile($field, $file, $contentType);
}
return $this;
} elseif (!is_string($filename)) {
throw new RequestException('The path to a file must be a string');
} elseif (!empty($filename)) {
// Adding an empty file will cause cURL to error out
$data = new PostFile($field, $filename, $contentType, $postname);
}
if ($data) {
if (!isset($this->postFiles[$data->getFieldName()])) {
$this->postFiles[$data->getFieldName()] = array($data);
} else {
$this->postFiles[$data->getFieldName()][] = $data;
}
$this->processPostFields();
}
return $this;
}
public function addPostFiles(array $files)
{
foreach ($files as $key => $file) {
if ($file instanceof PostFileInterface) {
$this->addPostFile($file, null, null, false);
} elseif (is_string($file)) {
// Convert non-associative array keys into 'file'
if (is_numeric($key)) {
$key = 'file';
}
$this->addPostFile($key, $file, null, false);
} else {
throw new RequestException('File must be a string or instance of PostFileInterface');
}
}
return $this;
}
/**
* Determine what type of request should be sent based on post fields
*/
protected function processPostFields()
{
if (!$this->postFiles) {
$this->removeHeader('Expect')->setHeader('Content-Type', self::URL_ENCODED);
} else {
$this->setHeader('Content-Type', self::MULTIPART);
if ($this->expectCutoff !== false) {
$this->setHeader('Expect', '100-Continue');
}
}
}
}

View file

@ -0,0 +1,137 @@
<?php
namespace Guzzle\Http\Message;
use Guzzle\Http\Exception\RequestException;
use Guzzle\Http\EntityBodyInterface;
use Guzzle\Http\QueryString;
/**
* HTTP request that sends an entity-body in the request message (POST, PUT)
*/
interface EntityEnclosingRequestInterface extends RequestInterface
{
const URL_ENCODED = 'application/x-www-form-urlencoded; charset=utf-8';
const MULTIPART = 'multipart/form-data';
/**
* Set the body of the request
*
* @param string|resource|EntityBodyInterface $body Body to use in the entity body of the request
* @param string $contentType Content-Type to set. Leave null to use an existing
* Content-Type or to guess the Content-Type
* @return self
* @throws RequestException if the protocol is < 1.1 and Content-Length can not be determined
*/
public function setBody($body, $contentType = null);
/**
* Get the body of the request if set
*
* @return EntityBodyInterface|null
*/
public function getBody();
/**
* Get a POST field from the request
*
* @param string $field Field to retrieve
*
* @return mixed|null
*/
public function getPostField($field);
/**
* Get the post fields that will be used in the request
*
* @return QueryString
*/
public function getPostFields();
/**
* Set a POST field value
*
* @param string $key Key to set
* @param string $value Value to set
*
* @return self
*/
public function setPostField($key, $value);
/**
* Add POST fields to use in the request
*
* @param QueryString|array $fields POST fields
*
* @return self
*/
public function addPostFields($fields);
/**
* Remove a POST field or file by name
*
* @param string $field Name of the POST field or file to remove
*
* @return self
*/
public function removePostField($field);
/**
* Returns an associative array of POST field names to PostFileInterface objects
*
* @return array
*/
public function getPostFiles();
/**
* Get a POST file from the request
*
* @param string $fieldName POST fields to retrieve
*
* @return array|null Returns an array wrapping an array of PostFileInterface objects
*/
public function getPostFile($fieldName);
/**
* Remove a POST file from the request
*
* @param string $fieldName POST file field name to remove
*
* @return self
*/
public function removePostFile($fieldName);
/**
* Add a POST file to the upload
*
* @param string $field POST field to use (e.g. file). Used to reference content from the server.
* @param string $filename Full path to the file. Do not include the @ symbol.
* @param string $contentType Optional Content-Type to add to the Content-Disposition.
* Default behavior is to guess. Set to false to not specify.
* @param string $postname The name of the file, when posted. (e.g. rename the file)
* @return self
*/
public function addPostFile($field, $filename = null, $contentType = null, $postname = null);
/**
* Add POST files to use in the upload
*
* @param array $files An array of POST fields => filenames where filename can be a string or PostFileInterface
*
* @return self
*/
public function addPostFiles(array $files);
/**
* Configure how redirects are handled for the request
*
* @param bool $strict Set to true to follow strict RFC compliance when redirecting POST requests. Most
* browsers with follow a 301-302 redirect for a POST request with a GET request. This is
* the default behavior of Guzzle. Enable strict redirects to redirect these responses
* with a POST rather than a GET request.
* @param int $maxRedirects Specify the maximum number of allowed redirects. Set to 0 to disable redirects.
*
* @return self
*/
public function configureRedirects($strict = false, $maxRedirects = 5);
}

View file

@ -0,0 +1,182 @@
<?php
namespace Guzzle\Http\Message;
use Guzzle\Common\Version;
use Guzzle\Http\Message\Header\HeaderInterface;
/**
* Represents a header and all of the values stored by that header
*/
class Header implements HeaderInterface
{
protected $values = array();
protected $header;
protected $glue;
/**
* @param string $header Name of the header
* @param array|string $values Values of the header as an array or a scalar
* @param string $glue Glue used to combine multiple values into a string
*/
public function __construct($header, $values = array(), $glue = ',')
{
$this->header = trim($header);
$this->glue = $glue;
foreach ((array) $values as $value) {
foreach ((array) $value as $v) {
$this->values[] = $v;
}
}
}
public function __toString()
{
return implode($this->glue . ' ', $this->toArray());
}
public function add($value)
{
$this->values[] = $value;
return $this;
}
public function getName()
{
return $this->header;
}
public function setName($name)
{
$this->header = $name;
return $this;
}
public function setGlue($glue)
{
$this->glue = $glue;
return $this;
}
public function getGlue()
{
return $this->glue;
}
/**
* Normalize the header to be a single header with an array of values.
*
* If any values of the header contains the glue string value (e.g. ","), then the value will be exploded into
* multiple entries in the header.
*
* @return self
*/
public function normalize()
{
$values = $this->toArray();
for ($i = 0, $total = count($values); $i < $total; $i++) {
if (strpos($values[$i], $this->glue) !== false) {
// Explode on glue when the glue is not inside of a comma
foreach (preg_split('/' . preg_quote($this->glue) . '(?=([^"]*"[^"]*")*[^"]*$)/', $values[$i]) as $v) {
$values[] = trim($v);
}
unset($values[$i]);
}
}
$this->values = array_values($values);
return $this;
}
public function hasValue($searchValue)
{
return in_array($searchValue, $this->toArray());
}
public function removeValue($searchValue)
{
$this->values = array_values(array_filter($this->values, function ($value) use ($searchValue) {
return $value != $searchValue;
}));
return $this;
}
public function toArray()
{
return $this->values;
}
public function count()
{
return count($this->toArray());
}
public function getIterator()
{
return new \ArrayIterator($this->toArray());
}
public function parseParams()
{
$params = $matches = array();
$callback = array($this, 'trimHeader');
// Normalize the header into a single array and iterate over all values
foreach ($this->normalize()->toArray() as $val) {
$part = array();
foreach (preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) as $kvp) {
if (!preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches)) {
continue;
}
$pieces = array_map($callback, $matches[0]);
$part[$pieces[0]] = isset($pieces[1]) ? $pieces[1] : '';
}
if ($part) {
$params[] = $part;
}
}
return $params;
}
/**
* @deprecated
* @codeCoverageIgnore
*/
public function hasExactHeader($header)
{
Version::warn(__METHOD__ . ' is deprecated');
return $this->header == $header;
}
/**
* @deprecated
* @codeCoverageIgnore
*/
public function raw()
{
Version::warn(__METHOD__ . ' is deprecated. Use toArray()');
return $this->toArray();
}
/**
* Trim a header by removing excess spaces and wrapping quotes
*
* @param $str
*
* @return string
*/
protected function trimHeader($str)
{
static $trimmed = "\"' \n\t";
return trim($str, $trimmed);
}
}

View file

@ -0,0 +1,121 @@
<?php
namespace Guzzle\Http\Message\Header;
use Guzzle\Http\Message\Header;
/**
* Provides helpful functionality for Cache-Control headers
*/
class CacheControl extends Header
{
/** @var array */
protected $directives;
public function add($value)
{
parent::add($value);
$this->directives = null;
}
public function removeValue($searchValue)
{
parent::removeValue($searchValue);
$this->directives = null;
}
/**
* Check if a specific cache control directive exists
*
* @param string $param Directive to retrieve
*
* @return bool
*/
public function hasDirective($param)
{
$directives = $this->getDirectives();
return isset($directives[$param]);
}
/**
* Get a specific cache control directive
*
* @param string $param Directive to retrieve
*
* @return string|bool|null
*/
public function getDirective($param)
{
$directives = $this->getDirectives();
return isset($directives[$param]) ? $directives[$param] : null;
}
/**
* Add a cache control directive
*
* @param string $param Directive to add
* @param string $value Value to set
*
* @return self
*/
public function addDirective($param, $value)
{
$directives = $this->getDirectives();
$directives[$param] = $value;
$this->updateFromDirectives($directives);
return $this;
}
/**
* Remove a cache control directive by name
*
* @param string $param Directive to remove
*
* @return self
*/
public function removeDirective($param)
{
$directives = $this->getDirectives();
unset($directives[$param]);
$this->updateFromDirectives($directives);
return $this;
}
/**
* Get an associative array of cache control directives
*
* @return array
*/
public function getDirectives()
{
if ($this->directives === null) {
$this->directives = array();
foreach ($this->parseParams() as $collection) {
foreach ($collection as $key => $value) {
$this->directives[$key] = $value === '' ? true : $value;
}
}
}
return $this->directives;
}
/**
* Updates the header value based on the parsed directives
*
* @param array $directives Array of cache control directives
*/
protected function updateFromDirectives(array $directives)
{
$this->directives = $directives;
$this->values = array();
foreach ($directives as $key => $value) {
$this->values[] = $value === true ? $key : "{$key}={$value}";
}
}
}

View file

@ -0,0 +1,108 @@
<?php
namespace Guzzle\Http\Message\Header;
use Guzzle\Common\ToArrayInterface;
/**
* Provides a case-insensitive collection of headers
*/
class HeaderCollection implements \IteratorAggregate, \Countable, \ArrayAccess, ToArrayInterface
{
/** @var array */
protected $headers;
public function __construct($headers = array())
{
$this->headers = $headers;
}
public function __clone()
{
foreach ($this->headers as &$header) {
$header = clone $header;
}
}
/**
* Clears the header collection
*/
public function clear()
{
$this->headers = array();
}
/**
* Set a header on the collection
*
* @param HeaderInterface $header Header to add
*
* @return self
*/
public function add(HeaderInterface $header)
{
$this->headers[strtolower($header->getName())] = $header;
return $this;
}
/**
* Get an array of header objects
*
* @return array
*/
public function getAll()
{
return $this->headers;
}
/**
* Alias of offsetGet
*/
public function get($key)
{
return $this->offsetGet($key);
}
public function count()
{
return count($this->headers);
}
public function offsetExists($offset)
{
return isset($this->headers[strtolower($offset)]);
}
public function offsetGet($offset)
{
$l = strtolower($offset);
return isset($this->headers[$l]) ? $this->headers[$l] : null;
}
public function offsetSet($offset, $value)
{
$this->add($value);
}
public function offsetUnset($offset)
{
unset($this->headers[strtolower($offset)]);
}
public function getIterator()
{
return new \ArrayIterator($this->headers);
}
public function toArray()
{
$result = array();
foreach ($this->headers as $header) {
$result[$header->getName()] = $header->toArray();
}
return $result;
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace Guzzle\Http\Message\Header;
use Guzzle\Http\Message\Header;
/**
* Default header factory implementation
*/
class HeaderFactory implements HeaderFactoryInterface
{
/** @var array */
protected $mapping = array(
'cache-control' => 'Guzzle\Http\Message\Header\CacheControl',
'link' => 'Guzzle\Http\Message\Header\Link',
);
public function createHeader($header, $value = null)
{
$lowercase = strtolower($header);
return isset($this->mapping[$lowercase])
? new $this->mapping[$lowercase]($header, $value)
: new Header($header, $value);
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace Guzzle\Http\Message\Header;
/**
* Interface for creating headers
*/
interface HeaderFactoryInterface
{
/**
* Create a header from a header name and a single value
*
* @param string $header Name of the header to create
* @param string $value Value to set on the header
*
* @return HeaderInterface
*/
public function createHeader($header, $value = null);
}

View file

@ -0,0 +1,83 @@
<?php
namespace Guzzle\Http\Message\Header;
use Guzzle\Common\ToArrayInterface;
interface HeaderInterface extends ToArrayInterface, \Countable, \IteratorAggregate
{
/**
* Convert the header to a string
*
* @return string
*/
public function __toString();
/**
* Add a value to the list of header values
*
* @param string $value Value to add to the header
*
* @return self
*/
public function add($value);
/**
* Get the name of the header
*
* @return string
*/
public function getName();
/**
* Change the name of the header
*
* @param string $name Name to change to
*
* @return self
*/
public function setName($name);
/**
* Change the glue used to implode the values
*
* @param string $glue Glue used to implode multiple values
*
* @return self
*/
public function setGlue($glue);
/**
* Get the glue used to implode multiple values into a string
*
* @return string
*/
public function getGlue();
/**
* Check if the collection of headers has a particular value
*
* @param string $searchValue Value to search for
*
* @return bool
*/
public function hasValue($searchValue);
/**
* Remove a specific value from the header
*
* @param string $searchValue Value to remove
*
* @return self
*/
public function removeValue($searchValue);
/**
* Parse a header containing ";" separated data into an array of associative arrays representing the header
* key value pair data of the header. When a parameter does not contain a value, but just contains a key, this
* function will inject a key with a '' string value.
*
* @return array
*/
public function parseParams();
}

View file

@ -0,0 +1,93 @@
<?php
namespace Guzzle\Http\Message\Header;
use Guzzle\Http\Message\Header;
/**
* Provides helpful functionality for link headers
*/
class Link extends Header
{
/**
* Add a link to the header
*
* @param string $url Link URL
* @param string $rel Link rel
* @param array $params Other link parameters
*
* @return self
*/
public function addLink($url, $rel, array $params = array())
{
$values = array("<{$url}>", "rel=\"{$rel}\"");
foreach ($params as $k => $v) {
$values[] = "{$k}=\"{$v}\"";
}
return $this->add(implode('; ', $values));
}
/**
* Check if a specific link exists for a given rel attribute
*
* @param string $rel rel value
*
* @return bool
*/
public function hasLink($rel)
{
return $this->getLink($rel) !== null;
}
/**
* Get a specific link for a given rel attribute
*
* @param string $rel Rel value
*
* @return array|null
*/
public function getLink($rel)
{
foreach ($this->getLinks() as $link) {
if (isset($link['rel']) && $link['rel'] == $rel) {
return $link;
}
}
return null;
}
/**
* Get an associative array of links
*
* For example:
* Link: <http:/.../front.jpeg>; rel=front; type="image/jpeg", <http://.../back.jpeg>; rel=back; type="image/jpeg"
*
* <code>
* var_export($response->getLinks());
* array(
* array(
* 'url' => 'http:/.../front.jpeg',
* 'rel' => 'back',
* 'type' => 'image/jpeg',
* )
* )
* </code>
*
* @return array
*/
public function getLinks()
{
$links = $this->parseParams();
foreach ($links as &$link) {
$key = key($link);
unset($link[$key]);
$link['url'] = trim($key, '<> ');
}
return $links;
}
}

View file

@ -0,0 +1,102 @@
<?php
namespace Guzzle\Http\Message;
/**
* Request and response message interface
*/
interface MessageInterface
{
/**
* Get application and plugin specific parameters set on the message.
*
* @return \Guzzle\Common\Collection
*/
public function getParams();
/**
* Add a header to an existing collection of headers.
*
* @param string $header Header name to add
* @param string $value Value of the header
*
* @return self
*/
public function addHeader($header, $value);
/**
* Add and merge in an array of HTTP headers.
*
* @param array $headers Associative array of header data.
*
* @return self
*/
public function addHeaders(array $headers);
/**
* Retrieve an HTTP header by name. Performs a case-insensitive search of all headers.
*
* @param string $header Header to retrieve.
*
* @return Header|null
*/
public function getHeader($header);
/**
* Get all headers as a collection
*
* @return \Guzzle\Http\Message\Header\HeaderCollection
*/
public function getHeaders();
/**
* Check if the specified header is present.
*
* @param string $header The header to check.
*
* @return bool
*/
public function hasHeader($header);
/**
* Remove a specific HTTP header.
*
* @param string $header HTTP header to remove.
*
* @return self
*/
public function removeHeader($header);
/**
* Set an HTTP header and overwrite any existing value for the header
*
* @param string $header Name of the header to set.
* @param mixed $value Value to set.
*
* @return self
*/
public function setHeader($header, $value);
/**
* Overwrite all HTTP headers with the supplied array of headers
*
* @param array $headers Associative array of header data.
*
* @return self
*/
public function setHeaders(array $headers);
/**
* Get an array of message header lines (e.g. ["Host: example.com", ...])
*
* @return array
*/
public function getHeaderLines();
/**
* Get the raw message headers as a string
*
* @return string
*/
public function getRawHeaders();
}

View file

@ -0,0 +1,124 @@
<?php
namespace Guzzle\Http\Message;
use Guzzle\Common\Version;
use Guzzle\Common\Exception\InvalidArgumentException;
use Guzzle\Http\Mimetypes;
/**
* POST file upload
*/
class PostFile implements PostFileInterface
{
protected $fieldName;
protected $contentType;
protected $filename;
protected $postname;
/**
* @param string $fieldName Name of the field
* @param string $filename Local path to the file
* @param string $postname Remote post file name
* @param string $contentType Content-Type of the upload
*/
public function __construct($fieldName, $filename, $contentType = null, $postname = null)
{
$this->fieldName = $fieldName;
$this->setFilename($filename);
$this->postname = $postname ? $postname : basename($filename);
$this->contentType = $contentType ?: $this->guessContentType();
}
public function setFieldName($name)
{
$this->fieldName = $name;
return $this;
}
public function getFieldName()
{
return $this->fieldName;
}
public function setFilename($filename)
{
// Remove leading @ symbol
if (strpos($filename, '@') === 0) {
$filename = substr($filename, 1);
}
if (!is_readable($filename)) {
throw new InvalidArgumentException("Unable to open {$filename} for reading");
}
$this->filename = $filename;
return $this;
}
public function setPostname($postname)
{
$this->postname = $postname;
return $this;
}
public function getFilename()
{
return $this->filename;
}
public function getPostname()
{
return $this->postname;
}
public function setContentType($type)
{
$this->contentType = $type;
return $this;
}
public function getContentType()
{
return $this->contentType;
}
public function getCurlValue()
{
// PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
// See: https://wiki.php.net/rfc/curl-file-upload
if (function_exists('curl_file_create')) {
return curl_file_create($this->filename, $this->contentType, $this->postname);
}
// Use the old style if using an older version of PHP
$value = "@{$this->filename};filename=" . $this->postname;
if ($this->contentType) {
$value .= ';type=' . $this->contentType;
}
return $value;
}
/**
* @deprecated
* @codeCoverageIgnore
*/
public function getCurlString()
{
Version::warn(__METHOD__ . ' is deprecated. Use getCurlValue()');
return $this->getCurlValue();
}
/**
* Determine the Content-Type of the file
*/
protected function guessContentType()
{
return Mimetypes::getInstance()->fromFilename($this->filename) ?: 'application/octet-stream';
}
}

View file

@ -0,0 +1,83 @@
<?php
namespace Guzzle\Http\Message;
use Guzzle\Common\Exception\InvalidArgumentException;
/**
* POST file upload
*/
interface PostFileInterface
{
/**
* Set the name of the field
*
* @param string $name Field name
*
* @return self
*/
public function setFieldName($name);
/**
* Get the name of the field
*
* @return string
*/
public function getFieldName();
/**
* Set the path to the file
*
* @param string $path Full path to the file
*
* @return self
* @throws InvalidArgumentException if the file cannot be read
*/
public function setFilename($path);
/**
* Set the post name of the file
*
* @param string $name The new name of the file
*
* @return self
*/
public function setPostname($name);
/**
* Get the full path to the file
*
* @return string
*/
public function getFilename();
/**
* Get the post name of the file
*
* @return string
*/
public function getPostname();
/**
* Set the Content-Type of the file
*
* @param string $type Content type
*
* @return self
*/
public function setContentType($type);
/**
* Get the Content-Type of the file
*
* @return string
*/
public function getContentType();
/**
* Get a cURL ready string or CurlFile object for the upload
*
* @return string
*/
public function getCurlValue();
}

View file

@ -0,0 +1,638 @@
<?php
namespace Guzzle\Http\Message;
use Guzzle\Common\Version;
use Guzzle\Common\Event;
use Guzzle\Common\Collection;
use Guzzle\Common\Exception\RuntimeException;
use Guzzle\Common\Exception\InvalidArgumentException;
use Guzzle\Http\Exception\RequestException;
use Guzzle\Http\Exception\BadResponseException;
use Guzzle\Http\ClientInterface;
use Guzzle\Http\EntityBody;
use Guzzle\Http\EntityBodyInterface;
use Guzzle\Http\Message\Header\HeaderInterface;
use Guzzle\Http\Url;
use Guzzle\Parser\ParserRegistry;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* HTTP request class to send requests
*/
class Request extends AbstractMessage implements RequestInterface
{
/** @var EventDispatcherInterface */
protected $eventDispatcher;
/** @var Url HTTP Url */
protected $url;
/** @var string HTTP method (GET, PUT, POST, DELETE, HEAD, OPTIONS, TRACE) */
protected $method;
/** @var ClientInterface */
protected $client;
/** @var Response Response of the request */
protected $response;
/** @var EntityBodyInterface Response body */
protected $responseBody;
/** @var string State of the request object */
protected $state;
/** @var string Authentication username */
protected $username;
/** @var string Auth password */
protected $password;
/** @var Collection cURL specific transfer options */
protected $curlOptions;
/** @var bool */
protected $isRedirect = false;
public static function getAllEvents()
{
return array(
// Called when receiving or uploading data through cURL
'curl.callback.read', 'curl.callback.write', 'curl.callback.progress',
// Cloning a request
'request.clone',
// About to send the request, sent request, completed transaction
'request.before_send', 'request.sent', 'request.complete',
// A request received a successful response
'request.success',
// A request received an unsuccessful response
'request.error',
// An exception is being thrown because of an unsuccessful response
'request.exception',
// Received response status line
'request.receive.status_line'
);
}
/**
* @param string $method HTTP method
* @param string|Url $url HTTP URL to connect to. The URI scheme, host header, and URI are parsed from the
* full URL. If query string parameters are present they will be parsed as well.
* @param array|Collection $headers HTTP headers
*/
public function __construct($method, $url, $headers = array())
{
parent::__construct();
$this->method = strtoupper($method);
$this->curlOptions = new Collection();
$this->setUrl($url);
if ($headers) {
// Special handling for multi-value headers
foreach ($headers as $key => $value) {
// Deal with collisions with Host and Authorization
if ($key == 'host' || $key == 'Host') {
$this->setHeader($key, $value);
} elseif ($value instanceof HeaderInterface) {
$this->addHeader($key, $value);
} else {
foreach ((array) $value as $v) {
$this->addHeader($key, $v);
}
}
}
}
$this->setState(self::STATE_NEW);
}
public function __clone()
{
if ($this->eventDispatcher) {
$this->eventDispatcher = clone $this->eventDispatcher;
}
$this->curlOptions = clone $this->curlOptions;
$this->params = clone $this->params;
$this->url = clone $this->url;
$this->response = $this->responseBody = null;
$this->headers = clone $this->headers;
$this->setState(RequestInterface::STATE_NEW);
$this->dispatch('request.clone', array('request' => $this));
}
/**
* Get the HTTP request as a string
*
* @return string
*/
public function __toString()
{
return $this->getRawHeaders() . "\r\n\r\n";
}
/**
* Default method that will throw exceptions if an unsuccessful response is received.
*
* @param Event $event Received
* @throws BadResponseException if the response is not successful
*/
public static function onRequestError(Event $event)
{
$e = BadResponseException::factory($event['request'], $event['response']);
$event['request']->setState(self::STATE_ERROR, array('exception' => $e) + $event->toArray());
throw $e;
}
public function setClient(ClientInterface $client)
{
$this->client = $client;
return $this;
}
public function getClient()
{
return $this->client;
}
public function getRawHeaders()
{
$protocolVersion = $this->protocolVersion ?: '1.1';
return trim($this->method . ' ' . $this->getResource()) . ' '
. strtoupper(str_replace('https', 'http', $this->url->getScheme()))
. '/' . $protocolVersion . "\r\n" . implode("\r\n", $this->getHeaderLines());
}
public function setUrl($url)
{
if ($url instanceof Url) {
$this->url = $url;
} else {
$this->url = Url::factory($url);
}
// Update the port and host header
$this->setPort($this->url->getPort());
if ($this->url->getUsername() || $this->url->getPassword()) {
$this->setAuth($this->url->getUsername(), $this->url->getPassword());
// Remove the auth info from the URL
$this->url->setUsername(null);
$this->url->setPassword(null);
}
return $this;
}
public function send()
{
if (!$this->client) {
throw new RuntimeException('A client must be set on the request');
}
return $this->client->send($this);
}
public function getResponse()
{
return $this->response;
}
public function getQuery($asString = false)
{
return $asString
? (string) $this->url->getQuery()
: $this->url->getQuery();
}
public function getMethod()
{
return $this->method;
}
public function getScheme()
{
return $this->url->getScheme();
}
public function setScheme($scheme)
{
$this->url->setScheme($scheme);
return $this;
}
public function getHost()
{
return $this->url->getHost();
}
public function setHost($host)
{
$this->url->setHost($host);
$this->setPort($this->url->getPort());
return $this;
}
public function getProtocolVersion()
{
return $this->protocolVersion;
}
public function setProtocolVersion($protocol)
{
$this->protocolVersion = $protocol;
return $this;
}
public function getPath()
{
return '/' . ltrim($this->url->getPath(), '/');
}
public function setPath($path)
{
$this->url->setPath($path);
return $this;
}
public function getPort()
{
return $this->url->getPort();
}
public function setPort($port)
{
$this->url->setPort($port);
// Include the port in the Host header if it is not the default port for the scheme of the URL
$scheme = $this->url->getScheme();
if ($port && (($scheme == 'http' && $port != 80) || ($scheme == 'https' && $port != 443))) {
$this->headers['host'] = $this->headerFactory->createHeader('Host', $this->url->getHost() . ':' . $port);
} else {
$this->headers['host'] = $this->headerFactory->createHeader('Host', $this->url->getHost());
}
return $this;
}
public function getUsername()
{
return $this->username;
}
public function getPassword()
{
return $this->password;
}
public function setAuth($user, $password = '', $scheme = CURLAUTH_BASIC)
{
static $authMap = array(
'basic' => CURLAUTH_BASIC,
'digest' => CURLAUTH_DIGEST,
'ntlm' => CURLAUTH_NTLM,
'any' => CURLAUTH_ANY
);
// If we got false or null, disable authentication
if (!$user) {
$this->password = $this->username = null;
$this->removeHeader('Authorization');
$this->getCurlOptions()->remove(CURLOPT_HTTPAUTH);
return $this;
}
if (!is_numeric($scheme)) {
$scheme = strtolower($scheme);
if (!isset($authMap[$scheme])) {
throw new InvalidArgumentException($scheme . ' is not a valid authentication type');
}
$scheme = $authMap[$scheme];
}
$this->username = $user;
$this->password = $password;
// Bypass CURL when using basic auth to promote connection reuse
if ($scheme == CURLAUTH_BASIC) {
$this->getCurlOptions()->remove(CURLOPT_HTTPAUTH);
$this->setHeader('Authorization', 'Basic ' . base64_encode($this->username . ':' . $this->password));
} else {
$this->getCurlOptions()
->set(CURLOPT_HTTPAUTH, $scheme)
->set(CURLOPT_USERPWD, $this->username . ':' . $this->password);
}
return $this;
}
public function getResource()
{
$resource = $this->getPath();
if ($query = (string) $this->url->getQuery()) {
$resource .= '?' . $query;
}
return $resource;
}
public function getUrl($asObject = false)
{
return $asObject ? clone $this->url : (string) $this->url;
}
public function getState()
{
return $this->state;
}
public function setState($state, array $context = array())
{
$oldState = $this->state;
$this->state = $state;
switch ($state) {
case self::STATE_NEW:
$this->response = null;
break;
case self::STATE_TRANSFER:
if ($oldState !== $state) {
// Fix Content-Length and Transfer-Encoding collisions
if ($this->hasHeader('Transfer-Encoding') && $this->hasHeader('Content-Length')) {
$this->removeHeader('Transfer-Encoding');
}
$this->dispatch('request.before_send', array('request' => $this));
}
break;
case self::STATE_COMPLETE:
if ($oldState !== $state) {
$this->processResponse($context);
$this->responseBody = null;
}
break;
case self::STATE_ERROR:
if (isset($context['exception'])) {
$this->dispatch('request.exception', array(
'request' => $this,
'response' => isset($context['response']) ? $context['response'] : $this->response,
'exception' => isset($context['exception']) ? $context['exception'] : null
));
}
}
return $this->state;
}
public function getCurlOptions()
{
return $this->curlOptions;
}
public function startResponse(Response $response)
{
$this->state = self::STATE_TRANSFER;
$response->setEffectiveUrl((string) $this->getUrl());
$this->response = $response;
return $this;
}
public function setResponse(Response $response, $queued = false)
{
$response->setEffectiveUrl((string) $this->url);
if ($queued) {
$ed = $this->getEventDispatcher();
$ed->addListener('request.before_send', $f = function ($e) use ($response, &$f, $ed) {
$e['request']->setResponse($response);
$ed->removeListener('request.before_send', $f);
}, -9999);
} else {
$this->response = $response;
// If a specific response body is specified, then use it instead of the response's body
if ($this->responseBody && !$this->responseBody->getCustomData('default') && !$response->isRedirect()) {
$this->getResponseBody()->write((string) $this->response->getBody());
} else {
$this->responseBody = $this->response->getBody();
}
$this->setState(self::STATE_COMPLETE);
}
return $this;
}
public function setResponseBody($body)
{
// Attempt to open a file for writing if a string was passed
if (is_string($body)) {
// @codeCoverageIgnoreStart
if (!($body = fopen($body, 'w+'))) {
throw new InvalidArgumentException('Could not open ' . $body . ' for writing');
}
// @codeCoverageIgnoreEnd
}
$this->responseBody = EntityBody::factory($body);
return $this;
}
public function getResponseBody()
{
if ($this->responseBody === null) {
$this->responseBody = EntityBody::factory()->setCustomData('default', true);
}
return $this->responseBody;
}
/**
* Determine if the response body is repeatable (readable + seekable)
*
* @return bool
* @deprecated Use getResponseBody()->isSeekable()
* @codeCoverageIgnore
*/
public function isResponseBodyRepeatable()
{
Version::warn(__METHOD__ . ' is deprecated. Use $request->getResponseBody()->isRepeatable()');
return !$this->responseBody ? true : $this->responseBody->isRepeatable();
}
public function getCookies()
{
if ($cookie = $this->getHeader('Cookie')) {
$data = ParserRegistry::getInstance()->getParser('cookie')->parseCookie($cookie);
return $data['cookies'];
}
return array();
}
public function getCookie($name)
{
$cookies = $this->getCookies();
return isset($cookies[$name]) ? $cookies[$name] : null;
}
public function addCookie($name, $value)
{
if (!$this->hasHeader('Cookie')) {
$this->setHeader('Cookie', "{$name}={$value}");
} else {
$this->getHeader('Cookie')->add("{$name}={$value}");
}
// Always use semicolons to separate multiple cookie headers
$this->getHeader('Cookie')->setGlue(';');
return $this;
}
public function removeCookie($name)
{
if ($cookie = $this->getHeader('Cookie')) {
foreach ($cookie as $cookieValue) {
if (strpos($cookieValue, $name . '=') === 0) {
$cookie->removeValue($cookieValue);
}
}
}
return $this;
}
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
$this->eventDispatcher->addListener('request.error', array(__CLASS__, 'onRequestError'), -255);
return $this;
}
public function getEventDispatcher()
{
if (!$this->eventDispatcher) {
$this->setEventDispatcher(new EventDispatcher());
}
return $this->eventDispatcher;
}
public function dispatch($eventName, array $context = array())
{
$context['request'] = $this;
return $this->getEventDispatcher()->dispatch($eventName, new Event($context));
}
public function addSubscriber(EventSubscriberInterface $subscriber)
{
$this->getEventDispatcher()->addSubscriber($subscriber);
return $this;
}
/**
* Get an array containing the request and response for event notifications
*
* @return array
*/
protected function getEventArray()
{
return array(
'request' => $this,
'response' => $this->response
);
}
/**
* Process a received response
*
* @param array $context Contextual information
* @throws RequestException|BadResponseException on unsuccessful responses
*/
protected function processResponse(array $context = array())
{
if (!$this->response) {
// If no response, then processResponse shouldn't have been called
$e = new RequestException('Error completing request');
$e->setRequest($this);
throw $e;
}
$this->state = self::STATE_COMPLETE;
// A request was sent, but we don't know if we'll send more or if the final response will be successful
$this->dispatch('request.sent', $this->getEventArray() + $context);
// Some response processors will remove the response or reset the state (example: ExponentialBackoffPlugin)
if ($this->state == RequestInterface::STATE_COMPLETE) {
// The request completed, so the HTTP transaction is complete
$this->dispatch('request.complete', $this->getEventArray());
// If the response is bad, allow listeners to modify it or throw exceptions. You can change the response by
// modifying the Event object in your listeners or calling setResponse() on the request
if ($this->response->isError()) {
$event = new Event($this->getEventArray());
$this->getEventDispatcher()->dispatch('request.error', $event);
// Allow events of request.error to quietly change the response
if ($event['response'] !== $this->response) {
$this->response = $event['response'];
}
}
// If a successful response was received, dispatch an event
if ($this->response->isSuccessful()) {
$this->dispatch('request.success', $this->getEventArray());
}
}
}
/**
* @deprecated Use Guzzle\Plugin\Cache\DefaultCanCacheStrategy
* @codeCoverageIgnore
*/
public function canCache()
{
Version::warn(__METHOD__ . ' is deprecated. Use Guzzle\Plugin\Cache\DefaultCanCacheStrategy.');
if (class_exists('Guzzle\Plugin\Cache\DefaultCanCacheStrategy')) {
$canCache = new \Guzzle\Plugin\Cache\DefaultCanCacheStrategy();
return $canCache->canCacheRequest($this);
} else {
return false;
}
}
/**
* @deprecated Use the history plugin (not emitting a warning as this is built-into the RedirectPlugin for now)
* @codeCoverageIgnore
*/
public function setIsRedirect($isRedirect)
{
$this->isRedirect = $isRedirect;
return $this;
}
/**
* @deprecated Use the history plugin
* @codeCoverageIgnore
*/
public function isRedirect()
{
Version::warn(__METHOD__ . ' is deprecated. Use the HistoryPlugin to track this.');
return $this->isRedirect;
}
}

View file

@ -0,0 +1,359 @@
<?php
namespace Guzzle\Http\Message;
use Guzzle\Common\Collection;
use Guzzle\Common\Exception\InvalidArgumentException;
use Guzzle\Http\RedirectPlugin;
use Guzzle\Http\Url;
use Guzzle\Parser\ParserRegistry;
/**
* Default HTTP request factory used to create the default {@see Request} and {@see EntityEnclosingRequest} objects.
*/
class RequestFactory implements RequestFactoryInterface
{
/** @var RequestFactory Singleton instance of the default request factory */
protected static $instance;
/** @var array Hash of methods available to the class (provides fast isset() lookups) */
protected $methods;
/** @var string Class to instantiate for requests with no body */
protected $requestClass = 'Guzzle\\Http\\Message\\Request';
/** @var string Class to instantiate for requests with a body */
protected $entityEnclosingRequestClass = 'Guzzle\\Http\\Message\\EntityEnclosingRequest';
/**
* Get a cached instance of the default request factory
*
* @return RequestFactory
*/
public static function getInstance()
{
// @codeCoverageIgnoreStart
if (!static::$instance) {
static::$instance = new static();
}
// @codeCoverageIgnoreEnd
return static::$instance;
}
public function __construct()
{
$this->methods = array_flip(get_class_methods(__CLASS__));
}
public function fromMessage($message)
{
$parsed = ParserRegistry::getInstance()->getParser('message')->parseRequest($message);
if (!$parsed) {
return false;
}
$request = $this->fromParts($parsed['method'], $parsed['request_url'],
$parsed['headers'], $parsed['body'], $parsed['protocol'],
$parsed['version']);
// EntityEnclosingRequest adds an "Expect: 100-Continue" header when using a raw request body for PUT or POST
// requests. This factory method should accurately reflect the message, so here we are removing the Expect
// header if one was not supplied in the message.
if (!isset($parsed['headers']['Expect']) && !isset($parsed['headers']['expect'])) {
$request->removeHeader('Expect');
}
return $request;
}
public function fromParts(
$method,
array $urlParts,
$headers = null,
$body = null,
$protocol = 'HTTP',
$protocolVersion = '1.1'
) {
return $this->create($method, Url::buildUrl($urlParts), $headers, $body)
->setProtocolVersion($protocolVersion);
}
public function create($method, $url, $headers = null, $body = null, array $options = array())
{
$method = strtoupper($method);
if ($method == 'GET' || $method == 'HEAD' || $method == 'TRACE') {
// Handle non-entity-enclosing request methods
$request = new $this->requestClass($method, $url, $headers);
if ($body) {
// The body is where the response body will be stored
$type = gettype($body);
if ($type == 'string' || $type == 'resource' || $type == 'object') {
$request->setResponseBody($body);
}
}
} else {
// Create an entity enclosing request by default
$request = new $this->entityEnclosingRequestClass($method, $url, $headers);
if ($body || $body === '0') {
// Add POST fields and files to an entity enclosing request if an array is used
if (is_array($body) || $body instanceof Collection) {
// Normalize PHP style cURL uploads with a leading '@' symbol
foreach ($body as $key => $value) {
if (is_string($value) && substr($value, 0, 1) == '@') {
$request->addPostFile($key, $value);
unset($body[$key]);
}
}
// Add the fields if they are still present and not all files
$request->addPostFields($body);
} else {
// Add a raw entity body body to the request
$request->setBody($body, (string) $request->getHeader('Content-Type'));
if ((string) $request->getHeader('Transfer-Encoding') == 'chunked') {
$request->removeHeader('Content-Length');
}
}
}
}
if ($options) {
$this->applyOptions($request, $options);
}
return $request;
}
/**
* Clone a request while changing the method. Emulates the behavior of
* {@see Guzzle\Http\Message\Request::clone}, but can change the HTTP method.
*
* @param RequestInterface $request Request to clone
* @param string $method Method to set
*
* @return RequestInterface
*/
public function cloneRequestWithMethod(RequestInterface $request, $method)
{
// Create the request with the same client if possible
if ($request->getClient()) {
$cloned = $request->getClient()->createRequest($method, $request->getUrl(), $request->getHeaders());
} else {
$cloned = $this->create($method, $request->getUrl(), $request->getHeaders());
}
$cloned->getCurlOptions()->replace($request->getCurlOptions()->toArray());
$cloned->setEventDispatcher(clone $request->getEventDispatcher());
// Ensure that that the Content-Length header is not copied if changing to GET or HEAD
if (!($cloned instanceof EntityEnclosingRequestInterface)) {
$cloned->removeHeader('Content-Length');
} elseif ($request instanceof EntityEnclosingRequestInterface) {
$cloned->setBody($request->getBody());
}
$cloned->getParams()->replace($request->getParams()->toArray());
$cloned->dispatch('request.clone', array('request' => $cloned));
return $cloned;
}
public function applyOptions(RequestInterface $request, array $options = array(), $flags = self::OPTIONS_NONE)
{
// Iterate over each key value pair and attempt to apply a config using function visitors
foreach ($options as $key => $value) {
$method = "visit_{$key}";
if (isset($this->methods[$method])) {
$this->{$method}($request, $value, $flags);
}
}
}
protected function visit_headers(RequestInterface $request, $value, $flags)
{
if (!is_array($value)) {
throw new InvalidArgumentException('headers value must be an array');
}
if ($flags & self::OPTIONS_AS_DEFAULTS) {
// Merge headers in but do not overwrite existing values
foreach ($value as $key => $header) {
if (!$request->hasHeader($key)) {
$request->setHeader($key, $header);
}
}
} else {
$request->addHeaders($value);
}
}
protected function visit_body(RequestInterface $request, $value, $flags)
{
if ($request instanceof EntityEnclosingRequestInterface) {
$request->setBody($value);
} else {
throw new InvalidArgumentException('Attempting to set a body on a non-entity-enclosing request');
}
}
protected function visit_allow_redirects(RequestInterface $request, $value, $flags)
{
if ($value === false) {
$request->getParams()->set(RedirectPlugin::DISABLE, true);
}
}
protected function visit_auth(RequestInterface $request, $value, $flags)
{
if (!is_array($value)) {
throw new InvalidArgumentException('auth value must be an array');
}
$request->setAuth($value[0], isset($value[1]) ? $value[1] : null, isset($value[2]) ? $value[2] : 'basic');
}
protected function visit_query(RequestInterface $request, $value, $flags)
{
if (!is_array($value)) {
throw new InvalidArgumentException('query value must be an array');
}
if ($flags & self::OPTIONS_AS_DEFAULTS) {
// Merge query string values in but do not overwrite existing values
$query = $request->getQuery();
$query->overwriteWith(array_diff_key($value, $query->toArray()));
} else {
$request->getQuery()->overwriteWith($value);
}
}
protected function visit_cookies(RequestInterface $request, $value, $flags)
{
if (!is_array($value)) {
throw new InvalidArgumentException('cookies value must be an array');
}
foreach ($value as $name => $v) {
$request->addCookie($name, $v);
}
}
protected function visit_events(RequestInterface $request, $value, $flags)
{
if (!is_array($value)) {
throw new InvalidArgumentException('events value must be an array');
}
foreach ($value as $name => $method) {
if (is_array($method)) {
$request->getEventDispatcher()->addListener($name, $method[0], $method[1]);
} else {
$request->getEventDispatcher()->addListener($name, $method);
}
}
}
protected function visit_plugins(RequestInterface $request, $value, $flags)
{
if (!is_array($value)) {
throw new InvalidArgumentException('plugins value must be an array');
}
foreach ($value as $plugin) {
$request->addSubscriber($plugin);
}
}
protected function visit_exceptions(RequestInterface $request, $value, $flags)
{
if ($value === false || $value === 0) {
$dispatcher = $request->getEventDispatcher();
foreach ($dispatcher->getListeners('request.error') as $listener) {
if (is_array($listener) && $listener[0] == 'Guzzle\Http\Message\Request' && $listener[1] = 'onRequestError') {
$dispatcher->removeListener('request.error', $listener);
break;
}
}
}
}
protected function visit_save_to(RequestInterface $request, $value, $flags)
{
$request->setResponseBody($value);
}
protected function visit_params(RequestInterface $request, $value, $flags)
{
if (!is_array($value)) {
throw new InvalidArgumentException('params value must be an array');
}
$request->getParams()->overwriteWith($value);
}
protected function visit_timeout(RequestInterface $request, $value, $flags)
{
if (defined('CURLOPT_TIMEOUT_MS')) {
$request->getCurlOptions()->set(CURLOPT_TIMEOUT_MS, $value * 1000);
} else {
$request->getCurlOptions()->set(CURLOPT_TIMEOUT, $value);
}
}
protected function visit_connect_timeout(RequestInterface $request, $value, $flags)
{
if (defined('CURLOPT_CONNECTTIMEOUT_MS')) {
$request->getCurlOptions()->set(CURLOPT_CONNECTTIMEOUT_MS, $value * 1000);
} else {
$request->getCurlOptions()->set(CURLOPT_CONNECTTIMEOUT, $value);
}
}
protected function visit_debug(RequestInterface $request, $value, $flags)
{
if ($value) {
$request->getCurlOptions()->set(CURLOPT_VERBOSE, true);
}
}
protected function visit_verify(RequestInterface $request, $value, $flags)
{
$curl = $request->getCurlOptions();
if ($value === true || is_string($value)) {
$curl[CURLOPT_SSL_VERIFYHOST] = 2;
$curl[CURLOPT_SSL_VERIFYPEER] = true;
if ($value !== true) {
$curl[CURLOPT_CAINFO] = $value;
}
} elseif ($value === false) {
unset($curl[CURLOPT_CAINFO]);
$curl[CURLOPT_SSL_VERIFYHOST] = 0;
$curl[CURLOPT_SSL_VERIFYPEER] = false;
}
}
protected function visit_proxy(RequestInterface $request, $value, $flags)
{
$request->getCurlOptions()->set(CURLOPT_PROXY, $value, $flags);
}
protected function visit_cert(RequestInterface $request, $value, $flags)
{
if (is_array($value)) {
$request->getCurlOptions()->set(CURLOPT_SSLCERT, $value[0]);
$request->getCurlOptions()->set(CURLOPT_SSLCERTPASSWD, $value[1]);
} else {
$request->getCurlOptions()->set(CURLOPT_SSLCERT, $value);
}
}
protected function visit_ssl_key(RequestInterface $request, $value, $flags)
{
if (is_array($value)) {
$request->getCurlOptions()->set(CURLOPT_SSLKEY, $value[0]);
$request->getCurlOptions()->set(CURLOPT_SSLKEYPASSWD, $value[1]);
} else {
$request->getCurlOptions()->set(CURLOPT_SSLKEY, $value);
}
}
}

View file

@ -0,0 +1,105 @@
<?php
namespace Guzzle\Http\Message;
use Guzzle\Common\Collection;
use Guzzle\Http\EntityBodyInterface;
use Guzzle\Http\Url;
/**
* Request factory used to create HTTP requests
*/
interface RequestFactoryInterface
{
const OPTIONS_NONE = 0;
const OPTIONS_AS_DEFAULTS = 1;
/**
* Create a new request based on an HTTP message
*
* @param string $message HTTP message as a string
*
* @return RequestInterface
*/
public function fromMessage($message);
/**
* Create a request from URL parts as returned from parse_url()
*
* @param string $method HTTP method (GET, POST, PUT, HEAD, DELETE, etc)
*
* @param array $urlParts URL parts containing the same keys as parse_url()
* - scheme: e.g. http
* - host: e.g. www.guzzle-project.com
* - port: e.g. 80
* - user: e.g. michael
* - pass: e.g. rocks
* - path: e.g. / OR /index.html
* - query: after the question mark ?
* @param array|Collection $headers HTTP headers
* @param string|resource|array|EntityBodyInterface $body Body to send in the request
* @param string $protocol Protocol (HTTP, SPYDY, etc)
* @param string $protocolVersion 1.0, 1.1, etc
*
* @return RequestInterface
*/
public function fromParts(
$method,
array $urlParts,
$headers = null,
$body = null,
$protocol = 'HTTP',
$protocolVersion = '1.1'
);
/**
* Create a new request based on the HTTP method
*
* @param string $method HTTP method (GET, POST, PUT, PATCH, HEAD, DELETE, ...)
* @param string|Url $url HTTP URL to connect to
* @param array|Collection $headers HTTP headers
* @param string|resource|array|EntityBodyInterface $body Body to send in the request
* @param array $options Array of options to apply to the request
*
* @return RequestInterface
*/
public function create($method, $url, $headers = null, $body = null, array $options = array());
/**
* Apply an associative array of options to the request
*
* @param RequestInterface $request Request to update
* @param array $options Options to use with the request. Available options are:
* "headers": Associative array of headers
* "query": Associative array of query string values to add to the request
* "body": Body of a request, including an EntityBody, string, or array when sending POST requests.
* "auth": Array of HTTP authentication parameters to use with the request. The array must contain the
* username in index [0], the password in index [2], and can optionally contain the authentication type
* in index [3]. The authentication types are: "Basic", "Digest", "NTLM", "Any" (defaults to "Basic").
* "cookies": Associative array of cookies
* "allow_redirects": Set to false to disable redirects
* "save_to": String, fopen resource, or EntityBody object used to store the body of the response
* "events": Associative array mapping event names to a closure or array of (priority, closure)
* "plugins": Array of plugins to add to the request
* "exceptions": Set to false to disable throwing exceptions on an HTTP level error (e.g. 404, 500, etc)
* "params": Set custom request data parameters on a request. (Note: these are not query string parameters)
* "timeout": Float describing the timeout of the request in seconds
* "connect_timeout": Float describing the number of seconds to wait while trying to connect. Use 0 to wait
* indefinitely.
* "verify": Set to true to enable SSL cert validation (the default), false to disable, or supply the path
* to a CA bundle to enable verification using a custom certificate.
* "cert": Set to a string to specify the path to a file containing a PEM formatted certificate. If a
* password is required, then set an array containing the path to the PEM file followed by the the
* password required for the certificate.
* "ssl_key": Specify the path to a file containing a private SSL key in PEM format. If a password is
* required, then set an array containing the path to the SSL key followed by the password required for
* the certificate.
* "proxy": Specify an HTTP proxy (e.g. "http://username:password@192.168.16.1:10")
* "debug": Set to true to display all data sent over the wire
* @param int $flags Bitwise flags to apply when applying the options to the request. Defaults to no special
* options. `1` (OPTIONS_AS_DEFAULTS): When specified, options will only update a request when
* the value does not already exist on the request. This is only supported by "query" and
* "headers". Other bitwise options may be added in the future.
*/
public function applyOptions(RequestInterface $request, array $options = array(), $flags = self::OPTIONS_NONE);
}

View file

@ -0,0 +1,318 @@
<?php
namespace Guzzle\Http\Message;
use Guzzle\Common\Collection;
use Guzzle\Common\HasDispatcherInterface;
use Guzzle\Http\Exception\RequestException;
use Guzzle\Http\ClientInterface;
use Guzzle\Http\EntityBodyInterface;
use Guzzle\Http\Url;
use Guzzle\Http\QueryString;
/**
* Generic HTTP request interface
*/
interface RequestInterface extends MessageInterface, HasDispatcherInterface
{
const STATE_NEW = 'new';
const STATE_COMPLETE = 'complete';
const STATE_TRANSFER = 'transfer';
const STATE_ERROR = 'error';
const GET = 'GET';
const PUT = 'PUT';
const POST = 'POST';
const DELETE = 'DELETE';
const HEAD = 'HEAD';
const CONNECT = 'CONNECT';
const OPTIONS = 'OPTIONS';
const TRACE = 'TRACE';
const PATCH = 'PATCH';
/**
* @return string
*/
public function __toString();
/**
* Send the request
*
* @return Response
* @throws RequestException on a request error
*/
public function send();
/**
* Set the client used to transport the request
*
* @param ClientInterface $client
*
* @return self
*/
public function setClient(ClientInterface $client);
/**
* Get the client used to transport the request
*
* @return ClientInterface $client
*/
public function getClient();
/**
* Set the URL of the request
*
* @param string $url|Url Full URL to set including query string
*
* @return self
*/
public function setUrl($url);
/**
* Get the full URL of the request (e.g. 'http://www.guzzle-project.com/')
*
* @param bool $asObject Set to TRUE to retrieve the URL as a clone of the URL object owned by the request.
*
* @return string|Url
*/
public function getUrl($asObject = false);
/**
* Get the resource part of the the request, including the path, query string, and fragment
*
* @return string
*/
public function getResource();
/**
* Get the collection of key value pairs that will be used as the query string in the request
*
* @return QueryString
*/
public function getQuery();
/**
* Get the HTTP method of the request
*
* @return string
*/
public function getMethod();
/**
* Get the URI scheme of the request (http, https, ftp, etc)
*
* @return string
*/
public function getScheme();
/**
* Set the URI scheme of the request (http, https, ftp, etc)
*
* @param string $scheme Scheme to set
*
* @return self
*/
public function setScheme($scheme);
/**
* Get the host of the request
*
* @return string
*/
public function getHost();
/**
* Set the host of the request. Including a port in the host will modify the port of the request.
*
* @param string $host Host to set (e.g. www.yahoo.com, www.yahoo.com:80)
*
* @return self
*/
public function setHost($host);
/**
* Get the path of the request (e.g. '/', '/index.html')
*
* @return string
*/
public function getPath();
/**
* Set the path of the request (e.g. '/', '/index.html')
*
* @param string|array $path Path to set or array of segments to implode
*
* @return self
*/
public function setPath($path);
/**
* Get the port that the request will be sent on if it has been set
*
* @return int|null
*/
public function getPort();
/**
* Set the port that the request will be sent on
*
* @param int $port Port number to set
*
* @return self
*/
public function setPort($port);
/**
* Get the username to pass in the URL if set
*
* @return string|null
*/
public function getUsername();
/**
* Get the password to pass in the URL if set
*
* @return string|null
*/
public function getPassword();
/**
* Set HTTP authorization parameters
*
* @param string|bool $user User name or false disable authentication
* @param string $password Password
* @param string $scheme Authentication scheme ('Basic', 'Digest', or a CURLAUTH_* constant (deprecated))
*
* @return self
* @link http://www.ietf.org/rfc/rfc2617.txt
* @link http://php.net/manual/en/function.curl-setopt.php See the available options for CURLOPT_HTTPAUTH
* @throws RequestException
*/
public function setAuth($user, $password = '', $scheme = 'Basic');
/**
* Get the HTTP protocol version of the request
*
* @return string
*/
public function getProtocolVersion();
/**
* Set the HTTP protocol version of the request (e.g. 1.1 or 1.0)
*
* @param string $protocol HTTP protocol version to use with the request
*
* @return self
*/
public function setProtocolVersion($protocol);
/**
* Get the previously received {@see Response} or NULL if the request has not been sent
*
* @return Response|null
*/
public function getResponse();
/**
* Manually set a response for the request.
*
* This method is useful for specifying a mock response for the request or setting the response using a cache.
* Manually setting a response will bypass the actual sending of a request.
*
* @param Response $response Response object to set
* @param bool $queued Set to TRUE to keep the request in a state of not having been sent, but queue the
* response for send()
*
* @return self Returns a reference to the object.
*/
public function setResponse(Response $response, $queued = false);
/**
* The start of a response has been received for a request and the request is still in progress
*
* @param Response $response Response that has been received so far
*
* @return self
*/
public function startResponse(Response $response);
/**
* Set the EntityBody that will hold a successful response message's entity body.
*
* This method should be invoked when you need to send the response's entity body somewhere other than the normal
* php://temp buffer. For example, you can send the entity body to a socket, file, or some other custom stream.
*
* @param EntityBodyInterface|string|resource $body Response body object. Pass a string to attempt to store the
* response body in a local file.
* @return Request
*/
public function setResponseBody($body);
/**
* Get the EntityBody that will hold the resulting response message's entity body. This response body will only
* be used for successful responses. Intermediate responses (e.g. redirects) will not use the targeted response
* body.
*
* @return EntityBodyInterface
*/
public function getResponseBody();
/**
* Get the state of the request. One of 'complete', 'transfer', 'new', 'error'
*
* @return string
*/
public function getState();
/**
* Set the state of the request
*
* @param string $state State of the request ('complete', 'transfer', 'new', 'error')
* @param array $context Contextual information about the state change
*
* @return string Returns the current state of the request (which may have changed due to events being fired)
*/
public function setState($state, array $context = array());
/**
* Get the cURL options that will be applied when the cURL handle is created
*
* @return Collection
*/
public function getCurlOptions();
/**
* Get an array of Cookies
*
* @return array
*/
public function getCookies();
/**
* Get a cookie value by name
*
* @param string $name Cookie to retrieve
*
* @return null|string
*/
public function getCookie($name);
/**
* Add a Cookie value by name to the Cookie header
*
* @param string $name Name of the cookie to add
* @param string $value Value to set
*
* @return self
*/
public function addCookie($name, $value);
/**
* Remove a specific cookie value by name
*
* @param string $name Cookie to remove by name
*
* @return self
*/
public function removeCookie($name);
}

View file

@ -0,0 +1,968 @@
<?php
namespace Guzzle\Http\Message;
use Guzzle\Common\Version;
use Guzzle\Common\ToArrayInterface;
use Guzzle\Common\Exception\RuntimeException;
use Guzzle\Http\EntityBodyInterface;
use Guzzle\Http\EntityBody;
use Guzzle\Http\Exception\BadResponseException;
use Guzzle\Http\RedirectPlugin;
use Guzzle\Parser\ParserRegistry;
/**
* Guzzle HTTP response object
*/
class Response extends AbstractMessage implements \Serializable
{
/**
* @var array Array of reason phrases and their corresponding status codes
*/
private static $statusTexts = array(
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status',
208 => 'Already Reported',
226 => 'IM Used',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
308 => 'Permanent Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
425 => 'Reserved for WebDAV advanced collections expired proposal',
426 => 'Upgrade required',
428 => 'Precondition Required',
429 => 'Too Many Requests',
431 => 'Request Header Fields Too Large',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
506 => 'Variant Also Negotiates (Experimental)',
507 => 'Insufficient Storage',
508 => 'Loop Detected',
510 => 'Not Extended',
511 => 'Network Authentication Required',
);
/** @var EntityBodyInterface The response body */
protected $body;
/** @var string The reason phrase of the response (human readable code) */
protected $reasonPhrase;
/** @var string The status code of the response */
protected $statusCode;
/** @var array Information about the request */
protected $info = array();
/** @var string The effective URL that returned this response */
protected $effectiveUrl;
/** @var array Cacheable response codes (see RFC 2616:13.4) */
protected static $cacheResponseCodes = array(200, 203, 206, 300, 301, 410);
/**
* Create a new Response based on a raw response message
*
* @param string $message Response message
*
* @return self|bool Returns false on error
*/
public static function fromMessage($message)
{
$data = ParserRegistry::getInstance()->getParser('message')->parseResponse($message);
if (!$data) {
return false;
}
$response = new static($data['code'], $data['headers'], $data['body']);
$response->setProtocol($data['protocol'], $data['version'])
->setStatus($data['code'], $data['reason_phrase']);
// Set the appropriate Content-Length if the one set is inaccurate (e.g. setting to X)
$contentLength = (string) $response->getHeader('Content-Length');
$actualLength = strlen($data['body']);
if (strlen($data['body']) > 0 && $contentLength != $actualLength) {
$response->setHeader('Content-Length', $actualLength);
}
return $response;
}
/**
* Construct the response
*
* @param string $statusCode The response status code (e.g. 200, 404, etc)
* @param ToArrayInterface|array $headers The response headers
* @param string|resource|EntityBodyInterface $body The body of the response
*
* @throws BadResponseException if an invalid response code is given
*/
public function __construct($statusCode, $headers = null, $body = null)
{
parent::__construct();
$this->setStatus($statusCode);
$this->body = EntityBody::factory($body !== null ? $body : '');
if ($headers) {
if (is_array($headers)) {
$this->setHeaders($headers);
} elseif ($headers instanceof ToArrayInterface) {
$this->setHeaders($headers->toArray());
} else {
throw new BadResponseException('Invalid headers argument received');
}
}
}
/**
* @return string
*/
public function __toString()
{
return $this->getMessage();
}
public function serialize()
{
return json_encode(array(
'status' => $this->statusCode,
'body' => (string) $this->body,
'headers' => $this->headers->toArray()
));
}
public function unserialize($serialize)
{
$data = json_decode($serialize, true);
$this->__construct($data['status'], $data['headers'], $data['body']);
}
/**
* Get the response entity body
*
* @param bool $asString Set to TRUE to return a string of the body rather than a full body object
*
* @return EntityBodyInterface|string
*/
public function getBody($asString = false)
{
return $asString ? (string) $this->body : $this->body;
}
/**
* Set the response entity body
*
* @param EntityBodyInterface|string $body Body to set
*
* @return self
*/
public function setBody($body)
{
$this->body = EntityBody::factory($body);
return $this;
}
/**
* Set the protocol and protocol version of the response
*
* @param string $protocol Response protocol
* @param string $version Protocol version
*
* @return self
*/
public function setProtocol($protocol, $version)
{
$this->protocol = $protocol;
$this->protocolVersion = $version;
return $this;
}
/**
* Get the protocol used for the response (e.g. HTTP)
*
* @return string
*/
public function getProtocol()
{
return $this->protocol;
}
/**
* Get the HTTP protocol version
*
* @return string
*/
public function getProtocolVersion()
{
return $this->protocolVersion;
}
/**
* Get a cURL transfer information
*
* @param string $key A single statistic to check
*
* @return array|string|null Returns all stats if no key is set, a single stat if a key is set, or null if a key
* is set and not found
* @link http://www.php.net/manual/en/function.curl-getinfo.php
*/
public function getInfo($key = null)
{
if ($key === null) {
return $this->info;
} elseif (array_key_exists($key, $this->info)) {
return $this->info[$key];
} else {
return null;
}
}
/**
* Set the transfer information
*
* @param array $info Array of cURL transfer stats
*
* @return self
*/
public function setInfo(array $info)
{
$this->info = $info;
return $this;
}
/**
* Set the response status
*
* @param int $statusCode Response status code to set
* @param string $reasonPhrase Response reason phrase
*
* @return self
* @throws BadResponseException when an invalid response code is received
*/
public function setStatus($statusCode, $reasonPhrase = '')
{
$this->statusCode = (int) $statusCode;
if (!$reasonPhrase && isset(self::$statusTexts[$this->statusCode])) {
$this->reasonPhrase = self::$statusTexts[$this->statusCode];
} else {
$this->reasonPhrase = $reasonPhrase;
}
return $this;
}
/**
* Get the response status code
*
* @return integer
*/
public function getStatusCode()
{
return $this->statusCode;
}
/**
* Get the entire response as a string
*
* @return string
*/
public function getMessage()
{
$message = $this->getRawHeaders();
// Only include the body in the message if the size is < 2MB
$size = $this->body->getSize();
if ($size < 2097152) {
$message .= (string) $this->body;
}
return $message;
}
/**
* Get the the raw message headers as a string
*
* @return string
*/
public function getRawHeaders()
{
$headers = 'HTTP/1.1 ' . $this->statusCode . ' ' . $this->reasonPhrase . "\r\n";
$lines = $this->getHeaderLines();
if (!empty($lines)) {
$headers .= implode("\r\n", $lines) . "\r\n";
}
return $headers . "\r\n";
}
/**
* Get the response reason phrase- a human readable version of the numeric
* status code
*
* @return string
*/
public function getReasonPhrase()
{
return $this->reasonPhrase;
}
/**
* Get the Accept-Ranges HTTP header
*
* @return string Returns what partial content range types this server supports.
*/
public function getAcceptRanges()
{
return (string) $this->getHeader('Accept-Ranges');
}
/**
* Calculate the age of the response
*
* @return integer
*/
public function calculateAge()
{
$age = $this->getHeader('Age');
if ($age === null && $this->getDate()) {
$age = time() - strtotime($this->getDate());
}
return $age === null ? null : (int) (string) $age;
}
/**
* Get the Age HTTP header
*
* @return integer|null Returns the age the object has been in a proxy cache in seconds.
*/
public function getAge()
{
return (string) $this->getHeader('Age');
}
/**
* Get the Allow HTTP header
*
* @return string|null Returns valid actions for a specified resource. To be used for a 405 Method not allowed.
*/
public function getAllow()
{
return (string) $this->getHeader('Allow');
}
/**
* Check if an HTTP method is allowed by checking the Allow response header
*
* @param string $method Method to check
*
* @return bool
*/
public function isMethodAllowed($method)
{
$allow = $this->getHeader('Allow');
if ($allow) {
foreach (explode(',', $allow) as $allowable) {
if (!strcasecmp(trim($allowable), $method)) {
return true;
}
}
}
return false;
}
/**
* Get the Cache-Control HTTP header
*
* @return string
*/
public function getCacheControl()
{
return (string) $this->getHeader('Cache-Control');
}
/**
* Get the Connection HTTP header
*
* @return string
*/
public function getConnection()
{
return (string) $this->getHeader('Connection');
}
/**
* Get the Content-Encoding HTTP header
*
* @return string|null
*/
public function getContentEncoding()
{
return (string) $this->getHeader('Content-Encoding');
}
/**
* Get the Content-Language HTTP header
*
* @return string|null Returns the language the content is in.
*/
public function getContentLanguage()
{
return (string) $this->getHeader('Content-Language');
}
/**
* Get the Content-Length HTTP header
*
* @return integer Returns the length of the response body in bytes
*/
public function getContentLength()
{
return (int) (string) $this->getHeader('Content-Length');
}
/**
* Get the Content-Location HTTP header
*
* @return string|null Returns an alternate location for the returned data (e.g /index.htm)
*/
public function getContentLocation()
{
return (string) $this->getHeader('Content-Location');
}
/**
* Get the Content-Disposition HTTP header
*
* @return string|null Returns the Content-Disposition header
*/
public function getContentDisposition()
{
return (string) $this->getHeader('Content-Disposition');
}
/**
* Get the Content-MD5 HTTP header
*
* @return string|null Returns a Base64-encoded binary MD5 sum of the content of the response.
*/
public function getContentMd5()
{
return (string) $this->getHeader('Content-MD5');
}
/**
* Get the Content-Range HTTP header
*
* @return string Returns where in a full body message this partial message belongs (e.g. bytes 21010-47021/47022).
*/
public function getContentRange()
{
return (string) $this->getHeader('Content-Range');
}
/**
* Get the Content-Type HTTP header
*
* @return string Returns the mime type of this content.
*/
public function getContentType()
{
return (string) $this->getHeader('Content-Type');
}
/**
* Checks if the Content-Type is of a certain type. This is useful if the
* Content-Type header contains charset information and you need to know if
* the Content-Type matches a particular type.
*
* @param string $type Content type to check against
*
* @return bool
*/
public function isContentType($type)
{
return stripos($this->getHeader('Content-Type'), $type) !== false;
}
/**
* Get the Date HTTP header
*
* @return string|null Returns the date and time that the message was sent.
*/
public function getDate()
{
return (string) $this->getHeader('Date');
}
/**
* Get the ETag HTTP header
*
* @return string|null Returns an identifier for a specific version of a resource, often a Message digest.
*/
public function getEtag()
{
return (string) $this->getHeader('ETag');
}
/**
* Get the Expires HTTP header
*
* @return string|null Returns the date/time after which the response is considered stale.
*/
public function getExpires()
{
return (string) $this->getHeader('Expires');
}
/**
* Get the Last-Modified HTTP header
*
* @return string|null Returns the last modified date for the requested object, in RFC 2822 format
* (e.g. Tue, 15 Nov 1994 12:45:26 GMT)
*/
public function getLastModified()
{
return (string) $this->getHeader('Last-Modified');
}
/**
* Get the Location HTTP header
*
* @return string|null Used in redirection, or when a new resource has been created.
*/
public function getLocation()
{
return (string) $this->getHeader('Location');
}
/**
* Get the Pragma HTTP header
*
* @return Header|null Returns the implementation-specific headers that may have various effects anywhere along
* the request-response chain.
*/
public function getPragma()
{
return (string) $this->getHeader('Pragma');
}
/**
* Get the Proxy-Authenticate HTTP header
*
* @return string|null Authentication to access the proxy (e.g. Basic)
*/
public function getProxyAuthenticate()
{
return (string) $this->getHeader('Proxy-Authenticate');
}
/**
* Get the Retry-After HTTP header
*
* @return int|null If an entity is temporarily unavailable, this instructs the client to try again after a
* specified period of time.
*/
public function getRetryAfter()
{
return (string) $this->getHeader('Retry-After');
}
/**
* Get the Server HTTP header
*
* @return string|null A name for the server
*/
public function getServer()
{
return (string) $this->getHeader('Server');
}
/**
* Get the Set-Cookie HTTP header
*
* @return string|null An HTTP cookie.
*/
public function getSetCookie()
{
return (string) $this->getHeader('Set-Cookie');
}
/**
* Get the Trailer HTTP header
*
* @return string|null The Trailer general field value indicates that the given set of header fields is present in
* the trailer of a message encoded with chunked transfer-coding.
*/
public function getTrailer()
{
return (string) $this->getHeader('Trailer');
}
/**
* Get the Transfer-Encoding HTTP header
*
* @return string|null The form of encoding used to safely transfer the entity to the user
*/
public function getTransferEncoding()
{
return (string) $this->getHeader('Transfer-Encoding');
}
/**
* Get the Vary HTTP header
*
* @return string|null Tells downstream proxies how to match future request headers to decide whether the cached
* response can be used rather than requesting a fresh one from the origin server.
*/
public function getVary()
{
return (string) $this->getHeader('Vary');
}
/**
* Get the Via HTTP header
*
* @return string|null Informs the client of proxies through which the response was sent.
*/
public function getVia()
{
return (string) $this->getHeader('Via');
}
/**
* Get the Warning HTTP header
*
* @return string|null A general warning about possible problems with the entity body
*/
public function getWarning()
{
return (string) $this->getHeader('Warning');
}
/**
* Get the WWW-Authenticate HTTP header
*
* @return string|null Indicates the authentication scheme that should be used to access the requested entity
*/
public function getWwwAuthenticate()
{
return (string) $this->getHeader('WWW-Authenticate');
}
/**
* Checks if HTTP Status code is a Client Error (4xx)
*
* @return bool
*/
public function isClientError()
{
return $this->statusCode >= 400 && $this->statusCode < 500;
}
/**
* Checks if HTTP Status code is Server OR Client Error (4xx or 5xx)
*
* @return boolean
*/
public function isError()
{
return $this->isClientError() || $this->isServerError();
}
/**
* Checks if HTTP Status code is Information (1xx)
*
* @return bool
*/
public function isInformational()
{
return $this->statusCode < 200;
}
/**
* Checks if HTTP Status code is a Redirect (3xx)
*
* @return bool
*/
public function isRedirect()
{
return $this->statusCode >= 300 && $this->statusCode < 400;
}
/**
* Checks if HTTP Status code is Server Error (5xx)
*
* @return bool
*/
public function isServerError()
{
return $this->statusCode >= 500 && $this->statusCode < 600;
}
/**
* Checks if HTTP Status code is Successful (2xx | 304)
*
* @return bool
*/
public function isSuccessful()
{
return ($this->statusCode >= 200 && $this->statusCode < 300) || $this->statusCode == 304;
}
/**
* Check if the response can be cached based on the response headers
*
* @return bool Returns TRUE if the response can be cached or false if not
*/
public function canCache()
{
// Check if the response is cacheable based on the code
if (!in_array((int) $this->getStatusCode(), self::$cacheResponseCodes)) {
return false;
}
// Make sure a valid body was returned and can be cached
if ((!$this->getBody()->isReadable() || !$this->getBody()->isSeekable())
&& ($this->getContentLength() > 0 || $this->getTransferEncoding() == 'chunked')) {
return false;
}
// Never cache no-store resources (this is a private cache, so private
// can be cached)
if ($this->getHeader('Cache-Control') && $this->getHeader('Cache-Control')->hasDirective('no-store')) {
return false;
}
return $this->isFresh() || $this->getFreshness() === null || $this->canValidate();
}
/**
* Gets the number of seconds from the current time in which this response is still considered fresh
*
* @return int|null Returns the number of seconds
*/
public function getMaxAge()
{
if ($header = $this->getHeader('Cache-Control')) {
// s-max-age, then max-age, then Expires
if ($age = $header->getDirective('s-maxage')) {
return $age;
}
if ($age = $header->getDirective('max-age')) {
return $age;
}
}
if ($this->getHeader('Expires')) {
return strtotime($this->getExpires()) - time();
}
return null;
}
/**
* Check if the response is considered fresh.
*
* A response is considered fresh when its age is less than or equal to the freshness lifetime (maximum age) of the
* response.
*
* @return bool|null
*/
public function isFresh()
{
$fresh = $this->getFreshness();
return $fresh === null ? null : $fresh >= 0;
}
/**
* Check if the response can be validated against the origin server using a conditional GET request.
*
* @return bool
*/
public function canValidate()
{
return $this->getEtag() || $this->getLastModified();
}
/**
* Get the freshness of the response by returning the difference of the maximum lifetime of the response and the
* age of the response (max-age - age).
*
* Freshness values less than 0 mean that the response is no longer fresh and is ABS(freshness) seconds expired.
* Freshness values of greater than zero is the number of seconds until the response is no longer fresh. A NULL
* result means that no freshness information is available.
*
* @return int
*/
public function getFreshness()
{
$maxAge = $this->getMaxAge();
$age = $this->calculateAge();
return $maxAge && $age ? ($maxAge - $age) : null;
}
/**
* Parse the JSON response body and return an array
*
* @return array|string|int|bool|float
* @throws RuntimeException if the response body is not in JSON format
*/
public function json()
{
$data = json_decode((string) $this->body, true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new RuntimeException('Unable to parse response body into JSON: ' . json_last_error());
}
return $data === null ? array() : $data;
}
/**
* Parse the XML response body and return a \SimpleXMLElement.
*
* In order to prevent XXE attacks, this method disables loading external
* entities. If you rely on external entities, then you must parse the
* XML response manually by accessing the response body directly.
*
* @return \SimpleXMLElement
* @throws RuntimeException if the response body is not in XML format
* @link http://websec.io/2012/08/27/Preventing-XXE-in-PHP.html
*/
public function xml()
{
$errorMessage = null;
$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
libxml_clear_errors();
try {
$xml = new \SimpleXMLElement((string) $this->body ?: '<root />', LIBXML_NONET);
if ($error = libxml_get_last_error()) {
$errorMessage = $error->message;
}
} catch (\Exception $e) {
$errorMessage = $e->getMessage();
}
libxml_clear_errors();
libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities);
if ($errorMessage) {
throw new RuntimeException('Unable to parse response body into XML: ' . $errorMessage);
}
return $xml;
}
/**
* Get the redirect count of this response
*
* @return int
*/
public function getRedirectCount()
{
return (int) $this->params->get(RedirectPlugin::REDIRECT_COUNT);
}
/**
* Set the effective URL that resulted in this response (e.g. the last redirect URL)
*
* @param string $url The effective URL
*
* @return self
*/
public function setEffectiveUrl($url)
{
$this->effectiveUrl = $url;
return $this;
}
/**
* Get the effective URL that resulted in this response (e.g. the last redirect URL)
*
* @return string
*/
public function getEffectiveUrl()
{
return $this->effectiveUrl;
}
/**
* @deprecated
* @codeCoverageIgnore
*/
public function getPreviousResponse()
{
Version::warn(__METHOD__ . ' is deprecated. Use the HistoryPlugin.');
return null;
}
/**
* @deprecated
* @codeCoverageIgnore
*/
public function setRequest($request)
{
Version::warn(__METHOD__ . ' is deprecated');
return $this;
}
/**
* @deprecated
* @codeCoverageIgnore
*/
public function getRequest()
{
Version::warn(__METHOD__ . ' is deprecated');
return null;
}
}