class EntityEnclosingRequest

HTTP request that sends an entity-body in the request message (POST, PUT, PATCH, DELETE)

Hierarchy

Expanded class hierarchy of EntityEnclosingRequest

File

drupal/core/vendor/guzzle/http/Guzzle/Http/Message/EntityEnclosingRequest.php, line 14

Namespace

Guzzle\Http\Message
View source
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();

  /**
   * {@inheritdoc}
   */
  public function __construct($method, $url, $headers = array()) {
    $this->postFields = new QueryString();
    $this->postFields
      ->setPrefix('');
    parent::__construct($method, $url, $headers);
  }

  /**
   * Get the HTTP request as a string
   *
   * @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;
  }

  /**
   * {@inheritdoc}
   */
  public function setState($state) {
    parent::setState($state);
    if ($state == self::STATE_TRANSFER && !$this->body && !count($this->postFields) && !count($this->postFiles)) {
      $this
        ->setHeader('Content-Length', 0)
        ->removeHeader('Transfer-Encoding');
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setBody($body, $contentType = null, $tryChunkedTransfer = false) {
    $this->body = EntityBody::factory($body);
    $this
      ->removeHeader('Content-Length');
    if ($contentType) {
      $this
        ->setHeader('Content-Type', (string) $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');
    }
    if ($tryChunkedTransfer) {
      $this
        ->setHeader('Transfer-Encoding', 'chunked');
    }
    else {
      $this
        ->removeHeader('Transfer-Encoding');

      // 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 ('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;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function getPostField($field) {
    return $this->postFields
      ->get($field);
  }

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

  /**
   * {@inheritdoc}
   */
  public function setPostField($key, $value) {
    $this->postFields
      ->set($key, $value);
    $this
      ->processPostFields();
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function addPostFields($fields) {
    $this->postFields
      ->merge($fields);
    $this
      ->processPostFields();
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function removePostField($field) {
    $this->postFields
      ->remove($field);
    $this
      ->processPostFields();
    return $this;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getPostFile($fieldName) {
    return isset($this->postFiles[$fieldName]) ? $this->postFiles[$fieldName] : null;
  }

  /**
   * {@inheritdoc}
   */
  public function removePostFile($fieldName) {
    unset($this->postFiles[$fieldName]);
    $this
      ->processPostFields();
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function addPostFile($field, $filename = null, $contentType = null) {
    $data = null;
    if ($field instanceof PostFileInterface) {
      $data = $field;
    }
    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);
    }
    if ($data) {
      if (!isset($this->postFiles[$data
        ->getFieldName()])) {
        $this->postFiles[$data
          ->getFieldName()] = array(
          $data,
        );
      }
      else {
        $this->postFiles[$data
          ->getFieldName()][] = $data;
      }
      $this
        ->processPostFields();
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  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 (empty($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');
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractMessage::$cacheControl private property
AbstractMessage::$headers protected property
AbstractMessage::$params protected property
AbstractMessage::$protocolVersion protected property
AbstractMessage::addCacheControlDirective public function Add a Cache-Control directive on the message Overrides MessageInterface::addCacheControlDirective
AbstractMessage::addHeader public function Add a header to an existing collection of headers. Overrides MessageInterface::addHeader
AbstractMessage::addHeaders public function Add and merge in an array of HTTP headers. Overrides MessageInterface::addHeaders
AbstractMessage::getCacheControlDirective public function Get a Cache-Control directive from the message Overrides MessageInterface::getCacheControlDirective
AbstractMessage::getHeader public function Retrieve an HTTP header by name. Performs a case-insensitive search of all headers. Overrides MessageInterface::getHeader
AbstractMessage::getHeaderLines public function Get an array of message header lines Overrides MessageInterface::getHeaderLines
AbstractMessage::getHeaders public function Get all headers as a collection Overrides MessageInterface::getHeaders
AbstractMessage::getParams public function Get application and plugin specific parameters set on the message. Overrides MessageInterface::getParams
AbstractMessage::getTokenizedHeader public function Get a tokenized header as a Collection Overrides MessageInterface::getTokenizedHeader
AbstractMessage::hasCacheControlDirective public function Check if the message has a Cache-Control directive Overrides MessageInterface::hasCacheControlDirective
AbstractMessage::hasHeader public function Check if the specified header is present. Overrides MessageInterface::hasHeader
AbstractMessage::parseCacheControlDirective private function Parse the Cache-Control HTTP header into an array
AbstractMessage::rebuildCacheControlDirective private function Rebuild the Cache-Control HTTP header using the user-specified values
AbstractMessage::removeCacheControlDirective public function Remove a Cache-Control directive from the message Overrides MessageInterface::removeCacheControlDirective
AbstractMessage::removeHeader public function Remove a specific HTTP header. Overrides MessageInterface::removeHeader
AbstractMessage::setHeader public function Set an HTTP header Overrides MessageInterface::setHeader
AbstractMessage::setHeaders public function Overwrite all HTTP headers with the supplied array of headers Overrides MessageInterface::setHeaders
AbstractMessage::setTokenizedHeader public function Set a tokenized header on the request that implodes a Collection of data into a string separated by a token. Overrides MessageInterface::setTokenizedHeader
EntityEnclosingRequest::$body protected property
EntityEnclosingRequest::$expectCutoff protected property
EntityEnclosingRequest::$postFields protected property
EntityEnclosingRequest::$postFiles protected property
EntityEnclosingRequest::addPostFields public function Add POST fields to use in the request Overrides EntityEnclosingRequestInterface::addPostFields
EntityEnclosingRequest::addPostFile public function Add a POST file to the upload Overrides EntityEnclosingRequestInterface::addPostFile
EntityEnclosingRequest::addPostFiles public function Add POST files to use in the upload Overrides EntityEnclosingRequestInterface::addPostFiles
EntityEnclosingRequest::configureRedirects public function Configure how redirects are handled for the request Overrides EntityEnclosingRequestInterface::configureRedirects
EntityEnclosingRequest::getBody public function Get the body of the request if set Overrides EntityEnclosingRequestInterface::getBody
EntityEnclosingRequest::getPostField public function Get a POST field from the request Overrides EntityEnclosingRequestInterface::getPostField
EntityEnclosingRequest::getPostFields public function Get the post fields that will be used in the request Overrides EntityEnclosingRequestInterface::getPostFields
EntityEnclosingRequest::getPostFile public function Get a POST file from the request Overrides EntityEnclosingRequestInterface::getPostFile
EntityEnclosingRequest::getPostFiles public function Returns an associative array of POST field names to PostFileInterface objects Overrides EntityEnclosingRequestInterface::getPostFiles
EntityEnclosingRequest::processPostFields protected function Determine what type of request should be sent based on post fields
EntityEnclosingRequest::removePostField public function Remove a POST field or file by name Overrides EntityEnclosingRequestInterface::removePostField
EntityEnclosingRequest::removePostFile public function Remove a POST file from the request Overrides EntityEnclosingRequestInterface::removePostFile
EntityEnclosingRequest::setBody public function Set the body of the request Overrides EntityEnclosingRequestInterface::setBody
EntityEnclosingRequest::setExpectHeaderCutoff public function Set the size that the entity body of the request must exceed before adding the Expect: 100-Continue header.
EntityEnclosingRequest::setPostField public function Set a POST field value Overrides EntityEnclosingRequestInterface::setPostField
EntityEnclosingRequest::setState public function Set the state of the request Overrides Request::setState
EntityEnclosingRequest::__construct public function Create a new request Overrides Request::__construct
EntityEnclosingRequest::__toString public function Get the HTTP request as a string Overrides Request::__toString
EntityEnclosingRequestInterface::MULTIPART constant
EntityEnclosingRequestInterface::URL_ENCODED constant
Request::$client protected property
Request::$curlOptions protected property
Request::$eventDispatcher protected property
Request::$method protected property
Request::$password protected property
Request::$response protected property
Request::$responseBody protected property
Request::$state protected property
Request::$url protected property
Request::$username protected property
Request::addCookie public function Add a Cookie value by name to the Cookie header Overrides RequestInterface::addCookie
Request::addSubscriber public function @codeCoverageIgnore Overrides HasDispatcherInterface::addSubscriber
Request::canCache public function Returns whether or not the request can be cached Overrides RequestInterface::canCache
Request::changedHeader protected function Check to see if the modified headers need to reset any of the managed headers like cache-control Overrides AbstractMessage::changedHeader
Request::dispatch public function Helper to dispatch Guzzle events and set the event name on the event Overrides HasDispatcherInterface::dispatch
Request::getAllEvents public static function Get a list of all of the events emitted from the class Overrides HasDispatcherInterface::getAllEvents
Request::getClient public function Get the client used to transport the request Overrides RequestInterface::getClient
Request::getCookie public function Get a cookie value by name Overrides RequestInterface::getCookie
Request::getCookies public function Get an array of Cookies Overrides RequestInterface::getCookies
Request::getCurlOptions public function Get the cURL options that will be applied when the cURL handle is created Overrides RequestInterface::getCurlOptions
Request::getEventArray protected function Get an array containing the request and response for event notifications
Request::getEventDispatcher public function Get the EventDispatcher of the request Overrides HasDispatcherInterface::getEventDispatcher
Request::getHost public function Get the host of the request Overrides RequestInterface::getHost
Request::getMethod public function Get the HTTP method of the request Overrides RequestInterface::getMethod
Request::getPassword public function Get the password to pass in the URL if set Overrides RequestInterface::getPassword
Request::getPath public function Get the path of the request (e.g. '/', '/index.html') Overrides RequestInterface::getPath
Request::getPort public function Get the port that the request will be sent on if it has been set Overrides RequestInterface::getPort
Request::getProtocolVersion public function Get the HTTP protocol version of the request Overrides RequestInterface::getProtocolVersion
Request::getQuery public function Get the collection of key value pairs that will be used as the query string in the request Overrides RequestInterface::getQuery
Request::getRawHeaders public function Get the raw message headers as a string Overrides MessageInterface::getRawHeaders
Request::getResource public function Get the resource part of the the request, including the path, query string, and fragment Overrides RequestInterface::getResource
Request::getResponse public function Get the previously received {not been sent Overrides RequestInterface::getResponse
Request::getResponseBody protected function
Request::getScheme public function Get the URI scheme of the request (http, https, ftp, etc) Overrides RequestInterface::getScheme
Request::getState public function Get the state of the request. One of 'complete', 'sending', 'new' Overrides RequestInterface::getState
Request::getUrl public function Get the full URL of the request (e.g. 'http://www.guzzle-project.com/') scheme://username:password@domain:port/path?query_string#fragment Overrides RequestInterface::getUrl
Request::getUsername public function Get the username to pass in the URL if set Overrides RequestInterface::getUsername
Request::isResponseBodyRepeatable public function Determine if the response body is repeatable (readable + seekable) Overrides RequestInterface::isResponseBodyRepeatable
Request::onRequestError public static function Default method that will throw exceptions if an unsuccessful response is received.
Request::processResponse protected function Process a received response
Request::receiveResponseHeader public function Method to receive HTTP response headers as they are retrieved Overrides RequestInterface::receiveResponseHeader
Request::removeCookie public function Remove a specific cookie value by name Overrides RequestInterface::removeCookie
Request::send public function Send the request Overrides RequestInterface::send
Request::setAuth public function Set HTTP authorization parameters Overrides RequestInterface::setAuth
Request::setClient public function Set the client used to transport the request Overrides RequestInterface::setClient
Request::setEventDispatcher public function Set the EventDispatcher of the request Overrides HasDispatcherInterface::setEventDispatcher
Request::setHost public function Set the host of the request. Including a port in the host will modify the port of the request. Overrides RequestInterface::setHost
Request::setPath public function Set the path of the request (e.g. '/', '/index.html') Overrides RequestInterface::setPath
Request::setPort public function Set the port that the request will be sent on Overrides RequestInterface::setPort
Request::setProtocolVersion public function Set the HTTP protocol version of the request (e.g. 1.1 or 1.0) Overrides RequestInterface::setProtocolVersion
Request::setResponse public function Manually set a response for the request. Overrides RequestInterface::setResponse
Request::setResponseBody public function Set the EntityBody that will hold the response message's entity body. Overrides RequestInterface::setResponseBody
Request::setScheme public function Set the URI scheme of the request (http, https, ftp, etc) Overrides RequestInterface::setScheme
Request::setUrl public function Set the URL of the request Overrides RequestInterface::setUrl
Request::__clone public function Clone the request object, leaving off any response that was received
RequestInterface::CONNECT constant
RequestInterface::DELETE constant
RequestInterface::GET constant
RequestInterface::HEAD constant
RequestInterface::OPTIONS constant
RequestInterface::PATCH constant
RequestInterface::POST constant
RequestInterface::PUT constant
RequestInterface::STATE_COMPLETE constant
RequestInterface::STATE_ERROR constant
RequestInterface::STATE_NEW constant
RequestInterface::STATE_TRANSFER constant
RequestInterface::TRACE constant