예제 #1
0
 /**
  * {@inheritdoc}
  */
 protected function read($length = null)
 {
     if (is_null($length)) {
         return trim(Utils::readLine($this->inputStream));
     }
     return $this->inputStream->read($length);
 }
예제 #2
0
 /**
  * Create color profile object from filename.
  *
  * @param string $filename
  *
  * @throws RuntimeException
  *
  * @return $this
  */
 public static function fromFilename($filename)
 {
     if (!file_exists($filename) || !is_file($filename) || !is_readable($filename)) {
         throw new RuntimeException(sprintf('Filename %s is an invalid profile file or is not readable', $filename));
     }
     return new static(StreamUtils::create(StreamUtils::open($filename, 'r')));
 }
예제 #3
0
 public function writeContainerLogs(Container $container)
 {
     $stream = $this->restClient->get(sprintf('/containers/%s/logs?stderr=1&stdout=1&follow=1', $container->getId()), ['stream' => true]);
     while (!$stream->getBody()->eof()) {
         $line = \GuzzleHttp\Stream\Utils::readLine($stream->getBody());
         $line = substr($line, 8);
         $container->addToLog($line);
     }
 }
예제 #4
0
 public function __invoke($retries, AbstractTransferEvent $event)
 {
     if (!($response = $event->getResponse())) {
         return RetrySubscriber::DEFER;
     }
     if (!$response->hasHeader('x-amz-crc32')) {
         return RetrySubscriber::DEFER;
     }
     $hash = hexdec(Utils::hash($response->getBody(), 'crc32b'));
     return (int) $response->getHeader('x-amz-crc32') !== $hash ? RetrySubscriber::RETRY : RetrySubscriber::DEFER;
 }
예제 #5
0
 public function fetch(RequestInterface $request)
 {
     $key = $this->getCacheKey($request);
     $entry = $this->cache->fetch($key);
     if (!$entry) {
         return null;
     }
     $response = new Response($entry['code'], $entry['headers'], Stream\Utils::create($entry['body']));
     $request->getConfig()->set('cache.key', $key);
     return $response;
 }
 public function getStatuses($param, $callback)
 {
     $response = $this->client->post('statuses/filter.json', ['body' => $param]);
     $body = $response->getBody();
     while (!$body->eof()) {
         $line = Utils::readLine($body);
         $data = json_decode($line, true);
         if (is_null($data)) {
             continue;
         }
         call_user_func($callback, $data);
         if (ob_get_level() > 0) {
             ob_flush();
         }
         flush();
     }
 }
예제 #7
0
 /**
  * listen endpoint
  *
  * @param $channels list of channels
  * @param null $callback
  */
 public function listen($channels, $callback = null)
 {
     $endpoint = $this->makeEndpoint($this->listenServerOptions);
     if (substr($this->listenServerOptions['path'], -1) != '/') {
         $endpoint .= '/';
     }
     $endpoint .= implode(',', (array) $channels);
     $response = $this->client->get($endpoint, ['debug' => $this->debug, 'stream' => true]);
     $body = $response->getBody();
     while (!$body->eof()) {
         if (is_callable($callback)) {
             call_user_func($callback, Utils::readline($body));
         } else {
             echo Utils::readline($body);
         }
     }
 }
예제 #8
0
파일: HttpFetcher.php 프로젝트: Tawreh/mtg
 /**
  * {@inheritdoc}
  */
 public function fetch(FeedInterface $feed, StateInterface $state)
 {
     $response = $this->get($feed->getSource(), $this->getCacheKey($feed));
     $feed->setSource($response->getEffectiveUrl());
     // 304, nothing to see here.
     if ($response->getStatusCode() == 304) {
         $state->setMessage($this->t('The feed has not been updated.'));
         throw new EmptyFeedException();
     }
     // Copy the temp stream to a real file.
     $download_file = drupal_tempnam('temporary://', 'feeds_http_fetcher');
     $dest_stream = Utils::create(fopen($download_file, 'w+'));
     Utils::copyToStream($response->getBody(), $dest_stream);
     $response->getBody()->close();
     $dest_stream->close();
     return new HttpFetcherResult($download_file, $response->getHeaders());
 }
예제 #9
0
 /**
  * Drains the stream into the "save_to" client option.
  *
  * @param resource                        $stream
  * @param string|resource|StreamInterface $dest
  *
  * @return Stream
  * @throws \RuntimeException when the save_to option is invalid.
  */
 private function drain($stream, $dest)
 {
     if (is_resource($stream)) {
         if (!is_resource($dest)) {
             $stream = Stream::factory($stream);
         } else {
             stream_copy_to_stream($stream, $dest);
             fclose($stream);
             rewind($dest);
             return $dest;
         }
     }
     // Stream the response into the destination stream
     $dest = is_string($dest) ? new Stream(Utils::open($dest, 'r+')) : Stream::factory($dest);
     Utils::copyToStream($stream, $dest);
     $dest->seek(0);
     $stream->close();
     return $dest;
 }
예제 #10
0
 public function setMd5(PreparedEvent $event)
 {
     $client = $event->getClient();
     $command = $event->getCommand();
     $body = $event->getRequest()->getBody();
     // If ContentMD5 is set or there is no body, there is nothing to do.
     if ($command['ContentMD5'] || !$body) {
         return;
     }
     // If and MD5 is required or enabled, add one.
     $optional = $client->getConfig('calculate_md5') && in_array($command->getName(), self::$canMd5);
     if (in_array($command->getName(), self::$requireMd5) || $optional) {
         // Throw exception is calculating and MD5 would result in an error.
         if (!$body->isSeekable()) {
             throw new CouldNotCreateChecksumException('md5');
         }
         // Set the Content-MD5 header.
         $event->getRequest()->setHeader('Content-MD5', base64_encode(Utils::hash($body, 'md5', true)));
     }
 }
예제 #11
0
 /**
  * Drain the stream into the destination stream
  */
 private function getSaveToBody(RequestInterface $request, StreamInterface $stream)
 {
     if ($saveTo = $request->getConfig()['save_to']) {
         // Stream the response into the destination stream
         $saveTo = is_string($saveTo) ? new Stream(Utils::open($saveTo, 'r+')) : Stream::factory($saveTo);
     } else {
         // Stream into the default temp stream
         $saveTo = Stream::factory();
     }
     Utils::copyToStream($stream, $saveTo);
     $saveTo->seek(0);
     $stream->close();
     return $saveTo;
 }
예제 #12
0
 public function __toString()
 {
     return Utils::copyToString($this);
 }
예제 #13
0
 /**
  * Create a cache key for a response's body
  *
  * @param string          $url  URL of the entry
  * @param StreamInterface $body Response body
  *
  * @return string
  */
 private function getBodyKey($url, StreamInterface $body)
 {
     return $this->keyPrefix . md5($url) . Stream\Utils::hash($body, 'md5');
 }
예제 #14
0
 public function getContents()
 {
     return Utils::copyToString($this);
 }
예제 #15
0
 public function getContents($maxLength = -1)
 {
     return Utils::copyToString($this, $maxLength);
 }
예제 #16
0
 /**
  * {@inheritdoc}
  */
 public function getColorProfile()
 {
     try {
         $data = $this->gmagick->getimageprofile('ICM');
     } catch (\GmagickException $exception) {
         return null;
     }
     return new ColorProfile(StreamUtils::create($data));
 }
 /**
  * This method opens a connection to the Twitter streaming API and applies the given callback function to each Tweet
  * @param $track
  * @param $callback
  */
 public function getStream($track, $callback)
 {
     $client = $this->createClient(self::TWITTER_STREAM_URL);
     $response = $client->post(self::STATUSES_ENDPOINT, ['body' => $track]);
     $body = $response->getBody();
     while (!$body->eof()) {
         //Read a line of the response
         $line = Utils::readLine($body);
         if (!empty($line)) {
             //callback
             call_user_func($callback, $line);
         }
         if (ob_get_level() > 0) {
             ob_flush();
         }
         flush();
     }
 }
예제 #18
0
 public function testSkipsOverwrittenBytes()
 {
     $decorated = Stream::factory(implode("\n", array_map(function ($n) {
         return str_pad($n, 4, '0', STR_PAD_LEFT);
     }, range(0, 25))));
     $body = new CachingStream($decorated);
     $this->assertEquals("0000\n", Utils::readline($body));
     $this->assertEquals("0001\n", Utils::readline($body));
     // Write over part of the body yet to be read, so skip some bytes
     $this->assertEquals(5, $body->write("TEST\n"));
     $this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
     // Read, which skips bytes, then reads
     $this->assertEquals("0003\n", Utils::readline($body));
     $this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
     $this->assertEquals("0004\n", Utils::readline($body));
     $this->assertEquals("0005\n", Utils::readline($body));
     // Overwrite part of the cached body (so don't skip any bytes)
     $body->seek(5);
     $this->assertEquals(5, $body->write("ABCD\n"));
     $this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
     $this->assertEquals("TEST\n", Utils::readline($body));
     $this->assertEquals("0003\n", Utils::readline($body));
     $this->assertEquals("0004\n", Utils::readline($body));
     $this->assertEquals("0005\n", Utils::readline($body));
     $this->assertEquals("0006\n", Utils::readline($body));
     $this->assertEquals(5, $body->write("1234\n"));
     $this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
     // Seek to 0 and ensure the overwritten bit is replaced
     $body->seek(0);
     $this->assertEquals("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", $body->read(50));
     // Ensure that casting it to a string does not include the bit that was overwritten
     $this->assertContains("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", (string) $body);
 }
예제 #19
0
 protected function getPayload(RequestInterface $request)
 {
     // Calculate the request signature payload
     if ($request->hasHeader('x-amz-content-sha256')) {
         // Handle streaming operations (e.g. Glacier.UploadArchive)
         return (string) $request->getHeader('x-amz-content-sha256');
     }
     if ($body = $request->getBody()) {
         if (!$body->isSeekable()) {
             throw new CouldNotCreateChecksumException('sha256');
         }
         return Utils::hash($body, 'sha256');
     }
     return self::EMPTY_PAYLOAD;
 }
예제 #20
0
 /**
  * Stream processor
  *
  * @access  protected
  * @param   string                             $endpoint Streaming API endpoint
  * @param   Closure                            $handler  Data handler
  * @param   \GuzzleHttp\Stream\StreamInterface $stream
  * @return  void
  */
 protected function processor($endpoint, Closure $handler, StreamInterface $stream)
 {
     $stalled = time();
     while (($line = Utils::readline($stream)) !== false) {
         if (empty($line)) {
             if (time() - $stalled > $this->stallTimeout) {
                 break;
             }
             continue;
         }
         // pass each line to the data handler
         $handler(json_decode($line, $this->messageAsArray));
         if ($this->triggerReconnection($endpoint)) {
             break;
         }
         $stalled = time();
     }
 }
예제 #21
0
 public function testTwitterStreamingIntegration()
 {
     if (empty($_SERVER['OAUTH_CONSUMER_SECRET'])) {
         $this->markTestSkipped('No OAUTH_CONSUMER_SECRET provided in phpunit.xml');
         return;
     }
     $client = new Client(['base_url' => 'https://stream.twitter.com/1.1/', 'defaults' => ['auth' => 'oauth']]);
     $oauth = new Oauth1(['consumer_key' => $_SERVER['OAUTH_CONSUMER_KEY'], 'consumer_secret' => $_SERVER['OAUTH_CONSUMER_SECRET'], 'token' => $_SERVER['OAUTH_TOKEN'], 'token_secret' => $_SERVER['OAUTH_TOKEN_SECRET']]);
     $client->getEmitter()->attach($oauth);
     try {
         $response = $client->post('statuses/filter.json', ['body' => ['track' => 'bieber'], 'stream' => true]);
         $body = $response->getBody();
         $data = Utils::readLine($body);
         $this->assertContains('bieber', strtolower($data));
         $this->assertNotEmpty(json_decode($data, true));
         $body->close();
     } catch (ClientException $e) {
         if ($e->getResponse()->getStatusCode() == 429) {
             $this->markTestIncomplete('You are being throttled');
         } else {
             throw $e;
         }
     }
 }
예제 #22
0
 protected function getCreatePartFn()
 {
     return function ($seekable) {
         $data = [];
         $firstByte = $this->source->tell();
         // Read from the source to create the body stream. This also
         // calculates the linear and tree hashes as the data is read.
         if ($seekable) {
             // Case 1: Stream is seekable, can make stream from new handle.
             $body = Utils::open($this->source->getMetadata('uri'), 'r');
             $body = $this->limitPartStream(Stream::factory($body));
             // Create another stream decorated with hashing streams and read
             // through it, so we can get the hash values for the part.
             $decoratedBody = $this->decorateWithHashes($body, $data);
             while (!$decoratedBody->eof()) {
                 $decoratedBody->read(1048576);
             }
             // Seek the original source forward to the end of the range.
             $this->source->seek($this->source->tell() + $body->getSize());
         } else {
             // Case 2: Stream is not seekable, must store part in temp stream.
             $source = $this->limitPartStream($this->source);
             $source = $this->decorateWithHashes($source, $data);
             $body = Stream::factory();
             Utils::copyToStream($source, $body);
         }
         $body->seek(0);
         $data['body'] = $body;
         $lastByte = $this->source->tell() - 1;
         $data['range'] = "bytes {$firstByte}-{$lastByte}/*";
         return $data;
     };
 }
예제 #23
0
 public function testProxiesToFactory()
 {
     $this->assertEquals('foo', (string) Utils::create('foo'));
 }
예제 #24
0
 /**
  * {@inheritdoc}
  */
 public function getColorProfile()
 {
     if (!in_array('icc', $this->imagick->getImageProfiles('*', false))) {
         return null;
     }
     $data = $this->imagick->getImageProfile('icc');
     return new ColorProfile(StreamUtils::create($data));
 }
예제 #25
0
 protected function getCreatePartFn()
 {
     return function ($seekable, $number) {
         // Initialize the array of part data that will be returned.
         $data = ['PartNumber' => $number];
         // Read from the source to create the body stream.
         if ($seekable) {
             // Case 1: Source is seekable, use lazy stream to defer work.
             $body = $this->limitPartStream(new LazyOpenStream($this->source->getMetadata('uri'), 'r'));
         } else {
             // Case 2: Stream is not seekable; must store in temp stream.
             $source = $this->limitPartStream($this->source);
             $source = $this->decorateWithHashes($source, function ($result, $type) use(&$data) {
                 $data['Content' . strtoupper($type)] = $result;
             });
             $body = Stream::factory();
             Utils::copyToStream($source, $body);
             $data['ContentLength'] = $body->getSize();
         }
         $body->seek(0);
         $data['Body'] = $body;
         return $data;
     };
 }
예제 #26
0
 /**
  * Determines if the body should be uploaded using PutObject or the
  * Multipart Upload System. It also modifies the passed-in $body as needed
  * to support the upload.
  *
  * @param StreamInterface $body      Stream representing the body.
  * @param integer         $threshold Minimum bytes before using Multipart.
  *
  * @return bool
  */
 private function requiresMultipart(StreamInterface &$body, $threshold)
 {
     // If body size known, compare to threshold to determine if Multipart.
     if ($body->getSize() !== null) {
         return $body->getSize() < $threshold ? false : true;
     }
     // Handle the situation where the body size is unknown.
     // Read up to 5MB into a buffer to determine how to upload the body.
     $buffer = Stream::factory();
     Utils::copyToStream($body, $buffer, 5242880);
     // If body < 5MB, use PutObject with the buffer.
     if ($buffer->getSize() < 5242880) {
         $buffer->seek(0);
         $body = $buffer;
         return false;
     }
     // If >= 5 MB, and seekable, use Multipart with rewound body.
     if ($body->isSeekable()) {
         $body->seek(0);
         return true;
     }
     // If >= 5 MB, and non-seekable, use Multipart, but stitch the
     // buffer and the body together into one stream. This avoids
     // needing to seek and unnecessary disc usage, while requiring
     // only the 5 MB buffer to be re-read by the Multipart system.
     $buffer->seek(0);
     $body = new AppendStream([$buffer, $body]);
     return true;
 }