class EntityBody

Entity body used with an HTTP request or response

Hierarchy

Expanded class hierarchy of EntityBody

4 files declare their use of EntityBody
EntityEnclosingRequest.php in drupal/core/vendor/guzzle/http/Guzzle/Http/Message/EntityEnclosingRequest.php
Request.php in drupal/core/vendor/guzzle/http/Guzzle/Http/Message/Request.php
RequestFactory.php in drupal/core/vendor/guzzle/http/Guzzle/Http/Message/RequestFactory.php
Response.php in drupal/core/vendor/guzzle/http/Guzzle/Http/Message/Response.php

File

drupal/core/vendor/guzzle/http/Guzzle/Http/EntityBody.php, line 12

Namespace

Guzzle\Http
View source
class EntityBody extends Stream implements EntityBodyInterface {

  /**
   * @var bool Content-Encoding of the entity body if known
   */
  protected $contentEncoding = false;

  /**
   * @var callable Method to invoke for rewinding a stream
   */
  protected $rewindFunction;

  /**
   * Create a new EntityBody based on the input type
   *
   * @param resource|string|EntityBody $resource Entity body data
   * @param int                        $size     Size of the data contained in the resource
   *
   * @return EntityBody
   * @throws InvalidArgumentException if the $resource arg is not a resource or string
   */
  public static function factory($resource = '', $size = null) {
    if ($resource instanceof EntityBodyInterface) {
      return $resource;
    }
    switch (gettype($resource)) {
      case 'string':
        return self::fromString($resource);
      case 'resource':
        return new static($resource, $size);
      case 'object':
        if (method_exists($resource, '__toString')) {
          return self::fromString((string) $resource);
        }
        break;
      case 'array':
        return self::fromString(http_build_query($resource));
    }
    throw new InvalidArgumentException('Invalid resource type');
  }

  /**
   * {@inheritdoc}
   */
  public function setRewindFunction($callable) {
    if (!is_callable($callable)) {
      throw new InvalidArgumentException('Must specify a callable');
    }
    $this->rewindFunction = $callable;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function rewind() {
    return $this->rewindFunction ? call_user_func($this->rewindFunction, $this) : parent::rewind();
  }

  /**
   * Create a new EntityBody from a string
   *
   * @param string $string String of data
   *
   * @return EntityBody
   */
  public static function fromString($string) {
    $stream = fopen('php://temp', 'r+');
    fwrite($stream, $string);
    rewind($stream);
    return new static($stream);
  }

  /**
   * {@inheritdoc}
   */
  public function compress($filter = 'zlib.deflate') {
    $result = $this
      ->handleCompression($filter);
    $this->contentEncoding = $result ? $filter : false;
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function uncompress($filter = 'zlib.inflate') {
    $offsetStart = 0;

    // When inflating gzipped data, the first 10 bytes must be stripped
    // if a gzip header is present
    if ($filter == 'zlib.inflate') {

      // @codeCoverageIgnoreStart
      if (!$this
        ->isReadable() || $this
        ->isConsumed() && !$this
        ->isSeekable()) {
        return false;
      }

      // @codeCoverageIgnoreEnd
      if (stream_get_contents($this->stream, 3, 0) === "") {
        $offsetStart = 10;
      }
    }
    $this->contentEncoding = false;
    return $this
      ->handleCompression($filter, $offsetStart);
  }

  /**
   * {@inheritdoc}
   */
  public function getContentLength() {
    return $this
      ->getSize();
  }

  /**
   * {@inheritdoc}
   */
  public function getContentType() {
    if (!($this
      ->isLocal() && $this
      ->getWrapper() == 'plainfile' && file_exists($this
      ->getUri()))) {
      return 'application/octet-stream';
    }
    return Mimetypes::getInstance()
      ->fromFilename($this
      ->getUri()) ?: 'application/octet-stream';
  }

  /**
   * {@inheritdoc}
   */
  public function getContentMd5($rawOutput = false, $base64Encode = false) {
    $hash = self::getHash($this, 'md5', $rawOutput);
    return $hash && $base64Encode ? base64_encode($hash) : $hash;
  }

  /**
   * Calculate the MD5 hash of an entity body
   *
   * @param EntityBodyInterface $body         Entity body to calculate the hash for
   * @param bool                $rawOutput    Whether or not to use raw output
   * @param bool                $base64Encode Whether or not to base64 encode raw output (only if raw output is true)
   *
   * @return bool|string Returns an MD5 string on success or FALSE on failure
   * @deprecated This will be deprecated soon
   * @codeCoverageIgnore
   */
  public static function calculateMd5(EntityBodyInterface $body, $rawOutput = false, $base64Encode = false) {
    return $body
      ->getContentMd5($rawOutput, $base64Encode);
  }

  /**
   * {@inheritdoc}
   */
  public function setStreamFilterContentEncoding($streamFilterContentEncoding) {
    $this->contentEncoding = $streamFilterContentEncoding;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getContentEncoding() {
    return strtr($this->contentEncoding, array(
      'zlib.deflate' => 'gzip',
      'bzip2.compress' => 'compress',
    )) ?: false;
  }

  /**
   * {@inheritdoc}
   */
  protected function handleCompression($filter, $offsetStart = 0) {

    // @codeCoverageIgnoreStart
    if (!$this
      ->isReadable() || $this
      ->isConsumed() && !$this
      ->isSeekable()) {
      return false;
    }

    // @codeCoverageIgnoreEnd
    $handle = fopen('php://temp', 'r+');
    $filter = @stream_filter_append($handle, $filter, STREAM_FILTER_WRITE);
    if (!$filter) {
      return false;
    }

    // Seek to the offset start if possible
    $this
      ->seek($offsetStart);
    while ($data = fread($this->stream, 8096)) {
      fwrite($handle, $data);
    }
    fclose($this->stream);
    $this->stream = $handle;
    stream_filter_remove($filter);
    $stat = fstat($this->stream);
    $this->size = $stat['size'];
    $this
      ->rebuildCache();
    $this
      ->seek(0);

    // Remove any existing rewind function as the underlying stream has been replaced
    $this->rewindFunction = null;
    return true;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityBody::$contentEncoding protected property
EntityBody::$rewindFunction protected property
EntityBody::calculateMd5 Deprecated public static function Calculate the MD5 hash of an entity body
EntityBody::compress public function If the stream is readable, compress the data in the stream using deflate compression. The uncompressed stream is then closed, and the compressed stream then becomes the wrapped stream. Overrides EntityBodyInterface::compress
EntityBody::factory public static function Create a new EntityBody based on the input type
EntityBody::fromString public static function Create a new EntityBody from a string
EntityBody::getContentEncoding public function Get the Content-Encoding of the EntityBody Overrides EntityBodyInterface::getContentEncoding
EntityBody::getContentLength public function Get the Content-Length of the entity body if possible (alias of getSize) Overrides EntityBodyInterface::getContentLength
EntityBody::getContentMd5 public function Get an MD5 checksum of the stream's contents Overrides EntityBodyInterface::getContentMd5
EntityBody::getContentType public function Guess the Content-Type or return the default application/octet-stream Overrides EntityBodyInterface::getContentType
EntityBody::handleCompression protected function
EntityBody::rewind public function Rewind to the beginning of the stream Overrides Stream::rewind
EntityBody::setRewindFunction public function Specify a custom callback used to rewind a non-seekable stream. This can be useful entity enclosing requests that are redirected. Overrides EntityBodyInterface::setRewindFunction
EntityBody::setStreamFilterContentEncoding public function
EntityBody::uncompress public function Decompress a deflated string. Once uncompressed, the uncompressed string is then used as the wrapped stream. Overrides EntityBodyInterface::uncompress
Stream::$cache protected property
Stream::$readWriteHash protected static property
Stream::$size protected property
Stream::$stream protected property
Stream::ftell public function Returns the current position of the file read/write pointer Overrides StreamInterface::ftell
Stream::getHash public static function Calculate a hash of a Stream
Stream::getMetaData public function Get stream metadata Overrides StreamInterface::getMetaData
Stream::getSize public function Get the size of the stream if able Overrides StreamInterface::getSize
Stream::getStream public function Get the stream resource Overrides StreamInterface::getStream
Stream::getStreamType public function Get a label describing the underlying implementation of the stream Overrides StreamInterface::getStreamType
Stream::getUri public function Get the URI/filename associated with this stream Overrides StreamInterface::getUri
Stream::getWrapper public function Get the stream wrapper type Overrides StreamInterface::getWrapper
Stream::getWrapperData public function Wrapper specific data attached to this stream. Overrides StreamInterface::getWrapperData
Stream::isConsumed public function Check if the stream has been consumed Overrides StreamInterface::isConsumed
Stream::isLocal public function Check if the stream is a local stream vs a remote stream Overrides StreamInterface::isLocal
Stream::isReadable public function Check if the stream is readable Overrides StreamInterface::isReadable
Stream::isSeekable public function Check if the string is repeatable Overrides StreamInterface::isSeekable
Stream::isWritable public function Check if the stream is writable Overrides StreamInterface::isWritable
Stream::IS_LOCAL constant
Stream::IS_READABLE constant
Stream::IS_WRITABLE constant
Stream::read public function Read data from the stream Overrides StreamInterface::read
Stream::rebuildCache protected function Reprocess stream metadata
Stream::seek public function Seek to a position in the stream Overrides StreamInterface::seek
Stream::SEEKABLE constant
Stream::setSize public function Specify the size of the stream in bytes Overrides StreamInterface::setSize
Stream::setStream public function Set the stream that is wrapped by the object Overrides StreamInterface::setStream
Stream::STREAM_TYPE constant
Stream::WRAPPER_TYPE constant
Stream::write public function Write data to the stream Overrides StreamInterface::write
Stream::__construct public function Construct a new Stream
Stream::__destruct public function Closes the stream when the helper is destructed
Stream::__toString public function Convert the stream to a string if the stream is readable and the stream is seekable. Overrides StreamInterface::__toString