Esempio n. 1
0
 /**
  * @return resource
  */
 public function getContentsAsStream()
 {
     if (!$this->stream) {
         $this->stream = $this->buildRequest()->send()->getBody(false);
     }
     return $this->stream->getStream();
 }
Esempio n. 2
0
 protected function addExpectHeader(EntityEnclosingRequestInterface $request, EntityBodyInterface $body, $expect)
 {
     if ($expect === false) {
         $request->removeHeader('Expect');
     } elseif ($expect !== true) {
         $expect = $expect ?: 1048576;
         if (is_numeric($expect) && $body->getSize()) {
             if ($body->getSize() < $expect) {
                 $request->removeHeader('Expect');
             } else {
                 $request->setHeader('Expect', '100-Continue');
             }
         }
     }
 }
Esempio n. 3
0
 /**
  * Add the appropriate expect header to a request
  *
  * @param EntityEnclosingRequestInterface $request Request to update
  * @param EntityBodyInterface             $body    Entity body of the request
  * @param string|int                      $expect  Expect header setting
  */
 protected function addExpectHeader(EntityEnclosingRequestInterface $request, EntityBodyInterface $body, $expect)
 {
     // Allow the `expect` data parameter to be set to remove the Expect header from the request
     if ($expect === false) {
         $request->removeHeader('Expect');
     } elseif ($expect !== true) {
         // Default to using a MB as the point in which to start using the expect header
         $expect = $expect ?: 1048576;
         // If the expect_header value is numeric then only add if the size is greater than the cutoff
         if (is_numeric($expect) && $body->getSize()) {
             if ($body->getSize() < $expect) {
                 $request->removeHeader('Expect');
             } else {
                 $request->setHeader('Expect', '100-Continue');
             }
         }
     }
 }
 /**
  * @return array|null
  */
 protected function getLineData()
 {
     $line = $this->body->readLine();
     if (is_string($line)) {
         return json_decode($line, JSON_OBJECT_AS_ARRAY);
     } else {
         return null;
     }
 }
 /**
  * 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 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);
         } 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;
 }
 private function readPart(EntityBodyInterface $body, $max = Size::MB)
 {
     return $body->read(min($this->partSize, $max));
 }
Esempio n. 8
0
 public static function calculateMd5(EntityBodyInterface $body, $rawOutput = false, $base64Encode = false)
 {
     Version::warn(__CLASS__ . ' is deprecated. Use getContentMd5()');
     return $body->getContentMd5($rawOutput, $base64Encode);
 }
 /**
  * Create a cache key for a response's body
  *
  * @param string              $url  URL of the entry
  * @param EntityBodyInterface $body Response body
  *
  * @return string
  */
 protected function getBodyKey($url, EntityBodyInterface $body)
 {
     return $this->keyPrefix . md5($url) . $body->getContentMd5();
 }
Esempio n. 10
0
 /**
  * Performs the work of reading the body stream, creating tree hashes, and creating UploadPartContext objects
  *
  * @param EntityBodyInterface $body The body to create parts from
  */
 protected function generateUploadParts(EntityBodyInterface $body)
 {
     // Rewind the body stream
     $body->seek(0);
     // Initialize variables for tracking data for upload
     $uploadContext = new UploadPartContext($this->partSize, $body->ftell());
     // Read the data from the streamed body in 1MB chunks
     while ($data = $body->read(min($this->partSize, Size::MB))) {
         // Add data to the hashes and size calculations
         $uploadContext->addData($data);
         // If the upload part is complete, generate an upload object and reset the currently tracked upload data
         if ($uploadContext->isFull()) {
             $this->updateTotals($uploadContext->generatePart());
             $uploadContext = new UploadPartContext($this->partSize, $body->ftell());
         }
     }
     // Handle any leftover data
     if (!$uploadContext->isEmpty()) {
         $this->updateTotals($uploadContext->generatePart());
     }
     // Rewind the body stream
     $body->seek(0);
 }