/** * {@inheritdoc} */ protected function transfer() { while (!$this->stopped && !$this->source->isConsumed()) { if ($this->source->getContentLength() && $this->source->isSeekable()) { // If the stream is seekable and the Content-Length known, then stream from the data source $body = new ReadLimitEntityBody($this->source, $this->partSize, $this->source->ftell()); } else { // We need to read the data source into a temporary buffer before streaming $body = EntityBody::factory(); while ($body->getContentLength() < $this->partSize && $body->write($this->source->read(max(1, min(10 * Size::KB, $this->partSize - $body->getContentLength()))))) { } } // @codeCoverageIgnoreStart if ($body->getContentLength() == 0) { break; } // @codeCoverageIgnoreEnd $params = $this->state->getUploadId()->toParams(); $command = $this->client->getCommand('UploadPart', array_replace($params, array('PartNumber' => count($this->state) + 1, 'Body' => $body, 'ContentMD5' => (bool) $this->options['part_md5'], Ua::OPTION => Ua::MULTIPART_UPLOAD))); // Notify observers that the part is about to be uploaded $eventData = $this->getEventData(); $eventData['command'] = $command; $this->dispatch(self::BEFORE_PART_UPLOAD, $eventData); // Allow listeners to stop the transfer if needed if ($this->stopped) { break; } $response = $command->getResponse(); $this->state->addPart(UploadPart::fromArray(array('PartNumber' => count($this->state) + 1, 'ETag' => $response->getHeader('ETag', true), 'Size' => $body->getContentLength(), 'LastModified' => gmdate(DateFormat::RFC2822)))); // Notify observers that the part was uploaded $this->dispatch(self::AFTER_PART_UPLOAD, $eventData); } }
/** * 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; }
/** * {@inheritdoc} */ public function create($method, $url, $headers = null, $body = null) { if ($method != 'POST' && $method != 'PUT' && $method != 'PATCH') { $c = $this->requestClass; $request = new $c($method, $url, $headers); if ($body) { $request->setResponseBody(EntityBody::factory($body)); } return $request; } $c = $this->entityEnclosingRequestClass; $request = new $c($method, $url, $headers); if ($body) { $isChunked = (string) $request->getHeader('Transfer-Encoding') == 'chunked'; if ($method == 'POST' && (is_array($body) || $body instanceof Collection)) { $request->addPostFields($body); if ($isChunked) { $request->setHeader('Transfer-Encoding', 'chunked'); } } elseif (is_resource($body) || $body instanceof EntityBody) { $request->setBody($body, (string) $request->getHeader('Content-Type'), $isChunked); } else { $request->setBody((string) $body, (string) $request->getHeader('Content-Type'), $isChunked); } } return $request; }
/** * @covers Guzzle\Http\Message\RequestFactory::create */ public function testCreatesPutRequests() { // Test using a string $request = RequestFactory::getInstance()->create('PUT', 'http://www.google.com/path?q=1&v=2', null, 'Data'); $this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request); $this->assertEquals('PUT', $request->getMethod()); $this->assertEquals('http', $request->getScheme()); $this->assertEquals('http://www.google.com/path?q=1&v=2', $request->getUrl()); $this->assertEquals('www.google.com', $request->getHost()); $this->assertEquals('/path', $request->getPath()); $this->assertEquals('/path?q=1&v=2', $request->getResource()); $this->assertInstanceOf('Guzzle\\Http\\EntityBody', $request->getBody()); $this->assertEquals('Data', (string) $request->getBody()); unset($request); // Test using an EntityBody $request = RequestFactory::getInstance()->create('PUT', 'http://www.google.com/path?q=1&v=2', null, EntityBody::factory('Data')); $this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request); $this->assertEquals('Data', (string) $request->getBody()); // Test using a resource $resource = fopen('php://temp', 'w+'); fwrite($resource, 'Data'); $request = RequestFactory::getInstance()->create('PUT', 'http://www.google.com/path?q=1&v=2', null, $resource); $this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request); $this->assertEquals('Data', (string) $request->getBody()); // Test using an object that can be cast as a string $request = RequestFactory::getInstance()->create('PUT', 'http://www.google.com/path?q=1&v=2', null, Url::factory('http://www.example.com/')); $this->assertInstanceOf('Guzzle\\Http\\Message\\EntityEnclosingRequest', $request); $this->assertEquals('http://www.example.com/', (string) $request->getBody()); }
/** * Receive a response header from curl * * @param resource $curl Curl handle * @param string $header Received header * * @return int */ public function receiveResponseHeader($curl, $header) { static $normalize = array("\r", "\n"); $length = strlen($header); $header = str_replace($normalize, '', $header); if (strpos($header, 'HTTP/') === 0) { $startLine = explode(' ', $header, 3); $code = $startLine[1]; $status = isset($startLine[2]) ? $startLine[2] : ''; // Only download the body of the response to the specified response // body when a successful response is received. if ($code >= 200 && $code < 300) { $body = $this->request->getResponseBody(); } else { $body = EntityBody::factory(); } $response = new Response($code, null, $body); $response->setStatus($code, $status); $this->request->startResponse($response); $this->request->dispatch('request.receive.status_line', array('request' => $this, 'line' => $header, 'status_code' => $code, 'reason_phrase' => $status)); } elseif ($pos = strpos($header, ':')) { $this->request->getResponse()->addHeader(trim(substr($header, 0, $pos)), trim(substr($header, $pos + 1))); } return $length; }
/** * {@inheritdoc} */ public function create($method, $url, $headers = null, $body = '', array $options = array()) { $c = $this->entityEnclosingRequestClass; $request = new $c($method, $url, $headers); $request->setBody(EntityBody::factory($body)); return $request; }
public function setBody($body, $contentType = null) { $this->body = EntityBody::factory($body); if ($contentType === null && !$this->hasHeader('Content-Type')) { $contentType = $this->body->getContentType(); } if ($contentType) { $this->setHeader('Content-Type', $contentType); } if (!$this->body->isSeekable() && $this->expectCutoff !== false) { $this->setHeader('Expect', '100-Continue'); } $size = $this->body->getContentLength(); if ($size !== null && $size !== false) { $this->setHeader('Content-Length', $size); if ($size > $this->expectCutoff) { $this->setHeader('Expect', '100-Continue'); } } elseif (!$this->hasHeader('Content-Length')) { if ('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; }
protected function buildCoreRequest(HttpRequest $request) { $headers = $request->getHeaders(); $contentLength = 0; if (!$request->isParameterInUrl()) { $body = $request->getParameterString(); $contentLength = strlen($body); } else { $body = $request->getContent(); if ($body !== null) { AssertUtils::assertSet(HttpHeaders::CONTENT_LENGTH, $headers); $contentLength = (int) $headers[HttpHeaders::CONTENT_LENGTH]; } } $entity = null; $headers[HttpHeaders::CONTENT_LENGTH] = (string) $contentLength; if ($body !== null) { $entity = new ReadLimitEntityBody(EntityBody::factory($body), $contentLength, $request->getOffset() !== false ? $request->getOffset() : 0); } $coreRequest = $this->client->createRequest($request->getMethod(), $request->getFullUrl(), $headers, $entity); if ($request->getResponseBody() != null) { $coreRequest->setResponseBody($request->getResponseBody()); } return $coreRequest; }
public function testProperlyValidatesWhenUsingContentEncoding() { $plugin = new Md5ValidatorPlugin(true); $request = RequestFactory::getInstance()->create('GET', 'http://www.test.com/'); $request->getEventDispatcher()->addSubscriber($plugin); // Content-MD5 is the MD5 hash of the canonical content after all // content-encoding has been applied. Because cURL will automatically // decompress entity bodies, we need to re-compress it to calculate. $body = EntityBody::factory('abc'); $body->compress(); $hash = $body->getContentMd5(); $body->uncompress(); $response = new Response(200, array('Content-MD5' => $hash, 'Content-Encoding' => 'gzip'), 'abc'); $request->dispatch('request.complete', array('response' => $response)); $this->assertEquals('abc', $response->getBody(true)); // Try again with an unknown encoding $response = new Response(200, array('Content-MD5' => $hash, 'Content-Encoding' => 'foobar'), 'abc'); $request->dispatch('request.complete', array('response' => $response)); // Try again with compress $body->compress('bzip2.compress'); $response = new Response(200, array('Content-MD5' => $body->getContentMd5(), 'Content-Encoding' => 'compress'), 'abc'); $request->dispatch('request.complete', array('response' => $response)); // Try again with encoding and disabled content-encoding checks $request->getEventDispatcher()->removeSubscriber($plugin); $plugin = new Md5ValidatorPlugin(false); $request->getEventDispatcher()->addSubscriber($plugin); $request->dispatch('request.complete', array('response' => $response)); }
protected function createTransferAction(\SplFileInfo $file) { // Open the file for reading $filename = $file->getPathName(); if (!($resource = fopen($filename, 'r'))) { // @codeCoverageIgnoreStart throw new RuntimeException("Could not open {$filename} for reading"); // @codeCoverageIgnoreEnd } $key = $this->options['source_converter']->convert($filename); $body = EntityBody::factory($resource); // Determine how the ACL should be applied if ($acl = $this->options['acl']) { $aclType = is_string($this->options['acl']) ? 'ACL' : 'ACP'; } else { $acl = 'private'; $aclType = 'ACL'; } // Use a multi-part upload if the file is larger than the cutoff size and is a regular file if ($body->getWrapper() == 'plainfile' && $file->getSize() >= $this->options['multipart_upload_size']) { $builder = UploadBuilder::newInstance()->setBucket($this->options['bucket'])->setKey($key)->setMinPartSize($this->options['multipart_upload_size'])->setOption($aclType, $acl)->setClient($this->options['client'])->setSource($body)->setConcurrency($this->options['concurrency']); $this->dispatch(self::BEFORE_MULTIPART_BUILD, array('builder' => $builder, 'file' => $file)); return $builder->build(); } return $this->options['client']->getCommand('PutObject', array('Bucket' => $this->options['bucket'], 'Key' => $key, 'Body' => $body, $aclType => $acl)); }
public function save($vaultName, $file, $description = null) { if (!$this->vaultExist($vaultName)) { $this->createVault($vaultName); } if (!is_file($file)) { throw new \Exception('File not exist'); } $options = array('vaultName' => $vaultName, 'body' => EntityBody::factory(fopen($file, 'r'))); /*if (filesize($file) > 1024000) { $multiupload = $this->glacier->initiateMultipartUpload(array( 'vaultName' => $vaultName, 'archiveDescription' => $description, 'partSize' => '4194304' )); $options['uploadId'] = $multiupload->get('uploadId'); $options['range'] = filesize($file); $this->glacier->uploadMultipartPart($options); $result = $this->glacier->completeMultipartUpload(array( 'vaultName' => $vaultName, 'uploadId' => $multiupload->get('uploadId'), )); } else {*/ if ($description) { $options['archiveDescription'] = $description; } $result = $this->glacier->uploadArchive($options); //} $archiveId = $result->get('archiveId'); return $archiveId ? true : false; }
public function putObject($bucket, $key, $filePath, $options) { $options['Bucket'] = $bucket; $options['Key'] = $key; $options['Body'] = EntityBody::factory(fopen($filePath, 'r+')); return $this->s3->putObject($options); }
public function testSettingBody() { $request = \Guzzle\Http\Message\RequestFactory::getInstance()->create('PUT', 'http://www.test.com/'); $request->setBody(EntityBody::factory('test')); $this->assertEquals(4, (string) $request->getHeader('Content-Length')); $this->assertFalse($request->hasHeader('Transfer-Encoding')); }
/** * {@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; }
public function setBody($body, $contentType = null) { $this->body = EntityBody::factory($body); // Auto detect the Content-Type from the path of the request if possible if ($contentType === null && !$this->hasHeader('Content-Type')) { $contentType = $this->body->getContentType() ?: Mimetypes::getInstance()->fromFilename($this->getPath()); } if ($contentType) { $this->setHeader('Content-Type', $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'); } // 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 (!$this->hasHeader('Content-Length')) { if ('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 create($method, $url, $headers = null, $body = null) { $method = strtoupper($method); if ($method == 'GET' || $method == 'HEAD' || $method == 'TRACE' || $method == 'OPTIONS') { $c = $this->requestClass; $request = new $c($method, $url, $headers); if ($body) { // The body is where the response body will be stored $request->setResponseBody(EntityBody::factory($body)); } return $request; } $c = $this->entityEnclosingRequestClass; $request = new $c($method, $url, $headers); if ($body) { $isChunked = (string) $request->getHeader('Transfer-Encoding') == 'chunked'; if ($method == 'POST' && (is_array($body) || $body instanceof Collection)) { // Normalize PHP style cURL uploads with a leading '@' symbol foreach ($body as $key => $value) { if (is_string($value) && substr($value, 0, 1) == '@') { $request->addPostFile($key, $value); unset($body[$key]); } } // Add the fields if they are still present and not all files $request->addPostFields($body); } elseif (is_resource($body) || $body instanceof EntityBody) { $request->setBody($body, (string) $request->getHeader('Content-Type'), $isChunked); } else { $request->setBody((string) $body, (string) $request->getHeader('Content-Type'), $isChunked); } } return $request; }
private function getResponse(array $headers = array(), $body = '', $status = 200) { $data = array('Body' => \Guzzle\Http\EntityBody::factory($body)); foreach ($headers as $header => $value) { $data[str_replace('-', '', $header)] = $value; } return new \Guzzle\Service\Resource\Model($data); }
public function setUp() { $this->mockRequest = new \Guzzle\Plugin\Mock\MockPlugin(); $body = \Guzzle\Http\EntityBody::fromString($this->responseBodyString); $this->mockRequest->addResponse(new Guzzle\Http\Message\Response(200, $this->responseHeaders, $body)); $this->client = $this->getClientInstance(); $this->client->addClientSubscriber($this->mockRequest); }
public static function save($file_path, $file_name = null, $upload_type = 'img', $is_private_acl = false) { if (!file_exists($file_path)) { throw new FuelException('File not exists.'); } static::set_s3_instanse(); return self::$s3_instantse->putObject(array('Bucket' => FBD_AWS_S3_BUCKET, 'Key' => static::get_key($file_name ?: \Site_Upload::get_file_name_from_file_path($file_path), $upload_type), 'Body' => EntityBody::factory(fopen($file_path, 'r')), 'ACL' => $is_private_acl ? CannedAcl::PRIVATE_ACCESS : CannedAcl::PUBLIC_READ)); }
public function testHasChainableSetterMethods() { $client = $this->getServiceBuilder()->get('s3'); $body = EntityBody::factory('foo'); $b = $this->mockBuilder->resumeFrom('foo')->setClient($client)->setSource($body)->setHeaders(array('Foo' => 'Bar')); $this->assertEquals('foo', $this->readAttribute($b, 'state')); $this->assertSame($client, $this->readAttribute($b, 'client')); $this->assertSame($body, $this->readAttribute($b, 'source')); }
public function signRequest(RequestInterface $request, CredentialsInterface $credentials) { if ($request instanceof EntityEnclosingRequestInterface && $request->getBody()) { $request->setHeader('X-Amz-Content-Sha256', EntityBody::getHash($request->getBody(), 'sha256')); } else { $request->setHeader('X-Amz-Content-Sha256', hash('sha256', '')); } parent::signRequest($request, $credentials); }
/** * S3の指定パスへファイルをアップロードする * * @param string $from_path * @param string $to_path * @return void **/ public function upload($from_path, $to_path) { try { $this->client->putObject(array('Bucket' => $this->bucket, 'Key' => $to_path, 'Body' => EntityBody::factory(fopen($from_path, 'r')))); } catch (\Exception $e) { throw $e; } return true; }
public function testAddsContentEncodingWhenSetOnBody() { $visitor = new Visitor(); $param = $this->getNestedCommand('body')->getParam('foo')->setSentAs('Foo'); $body = EntityBody::factory('foo'); $body->compress(); $visitor->visit($this->command, $this->request, $param, $body); $this->assertEquals('gzip', (string) $this->request->getHeader('Content-Encoding')); }
/** * {@inheritdoc} */ public function create($method, $url, $headers = null, $body = null) { $c = $this->entityEnclosingRequestClass; $request = new $c($method, $url, $headers); if ($body) { $request->setBody(EntityBody::factory($body)); } return $request; }
protected function getMockedTransfer(\Closure $closure = null) { $state = $this->getMockBuilder('Aws\\Common\\Model\\MultipartUpload\\AbstractTransferState')->disableOriginalConstructor()->getMockForAbstractClass(); $transfer = $this->getMockBuilder('Aws\\Common\\Model\\MultipartUpload\\AbstractTransfer')->setConstructorArgs(array($this->getMockBuilder('Aws\\Common\\Client\\AbstractClient')->disableOriginalConstructor()->getMockForAbstractClass(), $state, EntityBody::factory(), array('foo' => 'bar')))->getMockForAbstractClass(); if ($closure) { $closure($transfer, $state); } return $transfer; }
/** * @expectedException \Aws\Common\Exception\UnexpectedValueException * @expectedExceptionMessage Message integrity check failed. Expected 5032561e973f16047f3109e6a3f7f173 but got 4ba36d23a78c7393b4900ef38019d8ff */ public function testEnsuresMd5Match() { $client = $this->getServiceBuilder()->get('s3'); $mock = new MockPlugin(array(new Response(200, array('Content-Length' => 15, 'Content-MD5' => '5032561e973f16047f3109e6a3f7f173')), new Response(200, array('Content-Length' => 1), '1'))); $client->addSubscriber($mock); $target = EntityBody::factory('11111111111111'); $target->seek(0, SEEK_END); $resumable = new ResumableDownload($client, 'test', 'key', $target); $resumable(); }
/** * {@inheritdoc} */ public function visit(CommandInterface $command, RequestInterface $request, Parameter $param, $value) { $entityBody = EntityBody::factory($value); $request->setBody($entityBody); $this->addExpectHeader($request, $entityBody, $param->getData('expect_header')); // Add the Content-Encoding header if one is set on the EntityBody if ($encoding = $entityBody->getContentEncoding()) { $request->setHeader('Content-Encoding', $encoding); } }
/** * Creates a single upload part (up to 4GB) useful for the UploadArchive operation * * @param string|resource|EntityBodyInterface $body The upload body * * @return UploadPart * @throws RuntimeException if the body ends up being larger than 4GB */ public static function createSingleUploadPart($body) { $generator = new static(EntityBody::factory($body), 4 * Size::GB); if (count($generator) > 1) { // @codeCoverageIgnoreStart throw new RuntimeException('You cannot create a single upload that is larger than 4 GB.'); // @codeCoverageIgnoreEnd } return $generator->getUploadPart(1); }
/** * @param $xml * @return Response */ public function generateEbaySuccessResponse($xml) { $mockReponse = new Response(200); if (file_exists($xml)) { $mockReponse->setBody(EntityBody::factory(file_get_contents($xml))); } else { $mockReponse->setBody(EntityBody::factory($xml)); } return $mockReponse; }
/** * {@inheritdoc} */ protected function build() { $feedContent = $this->get('feed_content'); $this->remove('feed_content'); parent::build(); $this->body = EntityBody::factory($feedContent); $this->request->setBody($this->body); $this->request->setHeader('Content-Type', $this->body->getContentType()); $this->request->setHeader('Content-MD5', $this->body->getContentMd5(true, true)); }