/**
  * Set the body of the request
  *
  * @param string|resource|EntityBody $body Body to use in the entity body
  *      of the request
  * @param string $contentType (optional) Content-Type to set.  Leave null
  *      to use an existing Content-Type or to guess the Content-Type
  * @param bool $tryChunkedTransfer (optional) Set to TRUE to try to use
  *      Tranfer-Encoding chunked
  *
  * @return EntityEnclosingRequest
  * @throws RequestException if the protocol is < 1.1 and Content-Length can
  *      not be determined
  */
 public function setBody($body, $contentType = null, $tryChunkedTransfer = false)
 {
     $this->body = EntityBody::factory($body);
     $this->removeHeader('Content-Length');
     $this->setHeader('Expect', '100-Continue');
     if ($contentType) {
         $this->setHeader('Content-Type', (string) $contentType);
     }
     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);
         } else {
             if ('1.1' == $this->protocolVersion) {
                 $this->setHeader('Transfer-Encoding', 'chunked');
             } else {
                 throw new RequestException('Cannot determine entity body ' . 'size and cannot use chunked Transfer-Encoding when ' . 'using HTTP/' . $this->protocolVersion);
             }
         }
     }
     return $this;
 }
Example #2
0
 /**
  * @return int
  */
 public function getContentLength()
 {
     return $this->contentLength !== null ? $this->contentLength : $this->content->getContentLength();
 }
Example #3
0
 /**
  * @return int
  */
 public function getContentLength()
 {
     return $this->contentLength ?: $this->content->getContentLength();
 }