mirror of
https://github.com/awslabs/aws-crt-php.git
synced 2026-08-18 01:57:13 +00:00
Input Stream bindings (#18)
* Initial aws_input_stream <-> php_stream implementation * Added comments about useful references * updated aws-crt-ffi to get latest aws-lc, faster builds * Fixed inconsistency between PHP5/7 versions of php_stream_from_zval
This commit is contained in:
@@ -14,7 +14,7 @@ abstract class CredentialsProvider extends NativeResource {
|
||||
}
|
||||
|
||||
function __destruct() {
|
||||
self::$crt->aws_credentials_provider_release($this->release());
|
||||
self::$crt->credentials_provider_release($this->release());
|
||||
parent::__destruct();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,11 +33,11 @@ final class StaticCredentialsProvider extends CredentialsProvider {
|
||||
}
|
||||
$this->credentials = new AwsCredentials($options);
|
||||
|
||||
$provider_options = self::$crt->aws_credentials_provider_static_options_new();
|
||||
self::$crt->aws_credentials_provider_static_options_set_access_key_id($provider_options, $this->credentials->access_key_id);
|
||||
self::$crt->aws_credentials_provider_static_options_set_secret_access_key($provider_options, $this->credentials->secret_access_key);
|
||||
self::$crt->aws_credentials_provider_static_options_set_session_token($provider_options, $this->credentials->session_token);
|
||||
$this->acquire(self::$crt->aws_credentials_provider_static_new($provider_options));
|
||||
self::$crt->aws_credentials_provider_static_options_release($provider_options);
|
||||
$provider_options = self::$crt->credentials_provider_static_options_new();
|
||||
self::$crt->credentials_provider_static_options_set_access_key_id($provider_options, $this->credentials->access_key_id);
|
||||
self::$crt->credentials_provider_static_options_set_secret_access_key($provider_options, $this->credentials->secret_access_key);
|
||||
self::$crt->credentials_provider_static_options_set_session_token($provider_options, $this->credentials->session_token);
|
||||
$this->acquire(self::$crt->credentials_provider_static_new($provider_options));
|
||||
self::$crt->credentials_provider_static_options_release($provider_options);
|
||||
}
|
||||
}
|
||||
|
||||
+43
-7
@@ -140,31 +140,67 @@ final class CRT {
|
||||
self::$impl->aws_crt_credentials_release($credentials);
|
||||
}
|
||||
|
||||
function aws_credentials_provider_release($provider) {
|
||||
function credentials_provider_release($provider) {
|
||||
self::$impl->aws_crt_credentials_provider_release($provider);
|
||||
}
|
||||
|
||||
function aws_credentials_provider_static_options_new() {
|
||||
function credentials_provider_static_options_new() {
|
||||
return self::$impl->aws_crt_credentials_provider_static_options_new();
|
||||
}
|
||||
|
||||
function aws_credentials_provider_static_options_release($options) {
|
||||
function credentials_provider_static_options_release($options) {
|
||||
self::$impl->aws_crt_credentials_provider_static_options_release($options);
|
||||
}
|
||||
|
||||
function aws_credentials_provider_static_options_set_access_key_id($options, $access_key_id) {
|
||||
function credentials_provider_static_options_set_access_key_id($options, $access_key_id) {
|
||||
self::$impl->aws_crt_credentials_provider_static_options_set_access_key_id($options, $access_key_id);
|
||||
}
|
||||
|
||||
function aws_credentials_provider_static_options_set_secret_access_key($options, $secret_access_key) {
|
||||
function credentials_provider_static_options_set_secret_access_key($options, $secret_access_key) {
|
||||
self::$impl->aws_crt_credentials_provider_static_options_set_secret_access_key($options, $secret_access_key);
|
||||
}
|
||||
|
||||
function aws_credentials_provider_static_options_set_session_token($options, $session_token) {
|
||||
function credentials_provider_static_options_set_session_token($options, $session_token) {
|
||||
self::$impl->aws_crt_credentials_provider_static_options_set_session_token($options, $session_token);
|
||||
}
|
||||
|
||||
function aws_credentials_provider_static_new($options) {
|
||||
function credentials_provider_static_new($options) {
|
||||
return self::$impl->aws_crt_credentials_provider_static_new($options);
|
||||
}
|
||||
|
||||
function input_stream_options_new() {
|
||||
return self::$impl->aws_crt_input_stream_options_new();
|
||||
}
|
||||
|
||||
function input_stream_options_release($options) {
|
||||
self::$impl->aws_crt_input_stream_options_release($options);
|
||||
}
|
||||
|
||||
function input_stream_options_set_user_data($options, $user_data) {
|
||||
self::$impl->aws_crt_input_stream_options_set_user_data($options, $user_data);
|
||||
}
|
||||
|
||||
function input_stream_new($options) {
|
||||
return self::$impl->aws_crt_input_stream_new($options);
|
||||
}
|
||||
|
||||
function input_stream_release($stream) {
|
||||
self::$impl->aws_crt_input_stream_release($stream);
|
||||
}
|
||||
|
||||
function input_stream_seek($stream, $offset, $basis) {
|
||||
return self::$impl->aws_crt_input_stream_seek($stream, $offset, $basis);
|
||||
}
|
||||
|
||||
function input_stream_read($stream, $length) {
|
||||
return self::$impl->aws_crt_input_stream_read($stream, $length);
|
||||
}
|
||||
|
||||
function input_stream_eof($stream) {
|
||||
return self::$impl->aws_crt_input_stream_eof($stream);
|
||||
}
|
||||
|
||||
function input_stream_get_length($stream) {
|
||||
return self::$impl->aws_crt_input_stream_get_length($stream);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace AWS\CRT\HTTP;
|
||||
|
||||
abstract class Message {
|
||||
private $path = "";
|
||||
private $query = [];
|
||||
private $headers = [];
|
||||
|
||||
public function __construct($path, $query = [], $headers = []) {
|
||||
$this->path = $path;
|
||||
$this->query = $query;
|
||||
$this->headers = $headers;
|
||||
}
|
||||
}
|
||||
|
||||
class Request extends Message {
|
||||
private $body_stream = null;
|
||||
|
||||
public function __construct($path, $query = [], $headers = [], $body_stream = null) {
|
||||
parent::__construct($path, $query, $headers);
|
||||
$this->body_stream = $body_stream;
|
||||
}
|
||||
}
|
||||
|
||||
class Response extends Message {
|
||||
private $status_code;
|
||||
|
||||
public function __construct($path, $headers, $status_code) {
|
||||
parent::__construct($path, [], $headers);
|
||||
$this->status_code = $status_code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace AWS\CRT\IO;
|
||||
|
||||
use AWS\CRT\NativeResource as NativeResource;
|
||||
|
||||
final class InputStream extends NativeResource {
|
||||
private $stream = null;
|
||||
|
||||
const SEEK_BEGIN = 0;
|
||||
const SEEK_END = 2;
|
||||
|
||||
public function __construct($stream) {
|
||||
$this->stream = $stream;
|
||||
$options = self::$crt->input_stream_options_new();
|
||||
// The stream implementation in native just converts the PHP stream into
|
||||
// a native php_stream* and executes operations entirely in native
|
||||
self::$crt->input_stream_options_set_user_data($options, $stream);
|
||||
$this->acquire(self::$crt->input_stream_new($options));
|
||||
self::$crt->input_stream_options_release($options);
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
self::$crt->input_stream_release($this->release());
|
||||
parent::__destruct();
|
||||
}
|
||||
|
||||
public function eof() {
|
||||
return self::$crt->input_stream_eof($this->native);
|
||||
}
|
||||
|
||||
public function length() {
|
||||
return self::$crt->input_stream_get_length($this->native);
|
||||
}
|
||||
|
||||
public function read($length = 0) {
|
||||
if ($length == 0) {
|
||||
$length = $this->length();
|
||||
}
|
||||
return self::$crt->input_stream_read($this->native, $length);
|
||||
}
|
||||
|
||||
public function seek($offset, $basis) {
|
||||
return self::$crt->input_stream_seek($this->native, $offset, $basis);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use AWS\CRT\IO\InputStream as InputStream;
|
||||
|
||||
require_once('common.inc');
|
||||
|
||||
final class InputStreamTest extends CrtTestCase {
|
||||
|
||||
const MEM_STREAM_CONTENTS = "THIS IS A TEST";
|
||||
|
||||
private function getMemoryStream() {
|
||||
$stream = fopen("php://memory", 'r+');
|
||||
fputs($stream, self::MEM_STREAM_CONTENTS);
|
||||
rewind($stream);
|
||||
return $stream;
|
||||
}
|
||||
|
||||
public function testMemoryStream() {
|
||||
$mem_stream = $this->getMemoryStream();
|
||||
$stream = new InputStream($mem_stream);
|
||||
$this->assertNotNull($stream, "Failed to create InputStream from PHP memory stream");
|
||||
$this->assertEquals(strlen(self::MEM_STREAM_CONTENTS), $stream->length(), "Stream length doesn't match source buffer");
|
||||
$this->assertEquals(self::MEM_STREAM_CONTENTS, $stream->read(), "Stream doesn't match source buffer");
|
||||
$this->assertTrue($stream->eof(), "Stream is not EOF after reading");
|
||||
$this->assertEquals(0, $stream->seek(0, InputStream::SEEK_BEGIN), "Unable to rewind stream");
|
||||
$this->assertFalse($stream->eof(), "Stream is EOF after rewinding");
|
||||
$this->assertEquals(0, $stream->seek(0, InputStream::SEEK_END), "Unable to seek to end of stream");
|
||||
$this->assertTrue($stream->eof(), "Stream is not EOF after seeking to end");
|
||||
$stream = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user