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:
Justin Boswell
2021-03-19 13:29:14 -07:00
committed by GitHub
parent 73ecd3bc8a
commit 974fe3d74a
11 changed files with 441 additions and 49 deletions
+34 -16
View File
@@ -16,36 +16,54 @@ function aws_crt_event_loop_group_options_release(int $elg_options): void {}
function aws_crt_event_loop_group_options_set_max_threads(int $elg_options, int $max_threads): void {}
function aws_crt_event_loop_group_new(int $options): int {}
function aws_crt_event_loop_group_new(object $options): object {}
function aws_crt_event_loop_group_release(int $event_loop_group): void {}
function aws_crt_event_loop_group_release(object $event_loop_group): void {}
function aws_crt_credentials_options_new(): int {}
function aws_crt_input_stream_options_new(): object {}
function aws_crt_credentials_options_release(int $options): void {}
function aws_crt_input_stream_options_release(object $options): void {}
function aws_crt_credentials_options_set_access_key_id(int $options, string $access_key_id): void {}
function aws_crt_input_stream_options_set_user_data(object $options, object $user_data): void {}
function aws_crt_credentials_options_set_secret_access_key(int $options, string $secret_access_key): void {}
function aws_crt_input_stream_new(object $options): object {}
function aws_crt_credentials_options_set_session_token(int $options, string $session_token): void {}
function aws_crt_input_stream_release(int $stream): void {}
function aws_crt_credentials_options_set_expiration_timepoint_seconds(int $options, int $expiration_timepoint_seconds): void {}
function aws_crt_input_stream_seek(int $stream, int $offset, int $basis): int {}
function aws_crt_credentials_new(int $options): int {}
function aws_crt_input_stream_read(int $stream, int $length): string {}
function aws_crt_credentials_release(int $credentials): void {}
function aws_crt_input_stream_eof(int $stream): bool {}
function aws_crt_input_stream_get_length(int $stream): int {}
function aws_crt_credentials_options_new(): object {}
function aws_crt_credentials_options_release(object $options): void {}
function aws_crt_credentials_options_set_access_key_id(object $options, string $access_key_id): void {}
function aws_crt_credentials_options_set_secret_access_key(object $options, string $secret_access_key): void {}
function aws_crt_credentials_options_set_session_token(object $options, string $session_token): void {}
function aws_crt_credentials_options_set_expiration_timepoint_seconds(object $options, int $expiration_timepoint_seconds): void {}
function aws_crt_credentials_new(object $options): object {}
function aws_crt_credentials_release(object $credentials): void {}
function aws_crt_credentials_provider_release(int $credentials): void {}
function aws_crt_credentials_provider_static_options_new(): int {}
function aws_crt_credentials_provider_static_options_new(): object {}
function aws_crt_credentials_provider_static_options_release(int $options): void {}
function aws_crt_credentials_provider_static_options_release(object $options): void {}
function aws_crt_credentials_provider_static_options_set_access_key_id(int $options, string $access_key_id): void {}
function aws_crt_credentials_provider_static_options_set_access_key_id(object $options, string $access_key_id): void {}
function aws_crt_credentials_provider_static_options_set_secret_access_key(int $options, string $secret_access_key): void {}
function aws_crt_credentials_provider_static_options_set_secret_access_key(object $options, string $secret_access_key): void {}
function aws_crt_credentials_provider_static_options_set_session_token(int $options, string $session_token): void {}
function aws_crt_credentials_provider_static_options_set_session_token(object $options, string $session_token): void {}
function aws_crt_credentials_provider_static_new(int $options): int {}
function aws_crt_credentials_provider_static_new(object $options): object {}
+71 -16
View File
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: c681786667e5b7755b2874e4862ea7f162ef61ee */
* Stub hash: 5549e1214a9a419075f5cb561adb2d7a340c5916 */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_last_error, 0, 0, IS_LONG, 0)
ZEND_END_ARG_INFO()
@@ -23,51 +23,88 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_event_loop_group_options
ZEND_ARG_TYPE_INFO(0, max_threads, IS_LONG, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_event_loop_group_new, 0, 1, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_event_loop_group_new, 0, 1, IS_OBJECT, 0)
ZEND_ARG_TYPE_INFO(0, options, IS_OBJECT, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_event_loop_group_release, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, event_loop_group, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, event_loop_group, IS_OBJECT, 0)
ZEND_END_ARG_INFO()
#define arginfo_aws_crt_credentials_options_new arginfo_aws_crt_last_error
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_credentials_options_release, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_input_stream_options_new, 0, 0, IS_OBJECT, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_input_stream_options_release, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, options, IS_OBJECT, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_input_stream_options_set_user_data, 0, 2, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, options, IS_OBJECT, 0)
ZEND_ARG_TYPE_INFO(0, user_data, IS_OBJECT, 0)
ZEND_END_ARG_INFO()
#define arginfo_aws_crt_input_stream_new arginfo_aws_crt_event_loop_group_new
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_input_stream_release, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, stream, IS_LONG, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_input_stream_seek, 0, 3, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, stream, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, offset, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, basis, IS_LONG, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_input_stream_read, 0, 2, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, stream, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, length, IS_LONG, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_input_stream_eof, 0, 1, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, stream, IS_LONG, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_input_stream_get_length, 0, 1, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, stream, IS_LONG, 0)
ZEND_END_ARG_INFO()
#define arginfo_aws_crt_credentials_options_new arginfo_aws_crt_input_stream_options_new
#define arginfo_aws_crt_credentials_options_release arginfo_aws_crt_input_stream_options_release
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_credentials_options_set_access_key_id, 0, 2, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, options, IS_OBJECT, 0)
ZEND_ARG_TYPE_INFO(0, access_key_id, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_credentials_options_set_secret_access_key, 0, 2, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, options, IS_OBJECT, 0)
ZEND_ARG_TYPE_INFO(0, secret_access_key, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_credentials_options_set_session_token, 0, 2, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, options, IS_OBJECT, 0)
ZEND_ARG_TYPE_INFO(0, session_token, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_credentials_options_set_expiration_timepoint_seconds, 0, 2, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, options, IS_OBJECT, 0)
ZEND_ARG_TYPE_INFO(0, expiration_timepoint_seconds, IS_LONG, 0)
ZEND_END_ARG_INFO()
#define arginfo_aws_crt_credentials_new arginfo_aws_crt_event_loop_group_new
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_credentials_release, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, credentials, IS_OBJECT, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_credentials_provider_release, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, credentials, IS_LONG, 0)
ZEND_END_ARG_INFO()
#define arginfo_aws_crt_credentials_provider_release arginfo_aws_crt_credentials_release
#define arginfo_aws_crt_credentials_provider_static_options_new arginfo_aws_crt_input_stream_options_new
#define arginfo_aws_crt_credentials_provider_static_options_new arginfo_aws_crt_last_error
#define arginfo_aws_crt_credentials_provider_static_options_release arginfo_aws_crt_credentials_options_release
#define arginfo_aws_crt_credentials_provider_static_options_release arginfo_aws_crt_input_stream_options_release
#define arginfo_aws_crt_credentials_provider_static_options_set_access_key_id arginfo_aws_crt_credentials_options_set_access_key_id
@@ -87,6 +124,15 @@ ZEND_FUNCTION(aws_crt_event_loop_group_options_release);
ZEND_FUNCTION(aws_crt_event_loop_group_options_set_max_threads);
ZEND_FUNCTION(aws_crt_event_loop_group_new);
ZEND_FUNCTION(aws_crt_event_loop_group_release);
ZEND_FUNCTION(aws_crt_input_stream_options_new);
ZEND_FUNCTION(aws_crt_input_stream_options_release);
ZEND_FUNCTION(aws_crt_input_stream_options_set_user_data);
ZEND_FUNCTION(aws_crt_input_stream_new);
ZEND_FUNCTION(aws_crt_input_stream_release);
ZEND_FUNCTION(aws_crt_input_stream_seek);
ZEND_FUNCTION(aws_crt_input_stream_read);
ZEND_FUNCTION(aws_crt_input_stream_eof);
ZEND_FUNCTION(aws_crt_input_stream_get_length);
ZEND_FUNCTION(aws_crt_credentials_options_new);
ZEND_FUNCTION(aws_crt_credentials_options_release);
ZEND_FUNCTION(aws_crt_credentials_options_set_access_key_id);
@@ -114,6 +160,15 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(aws_crt_event_loop_group_options_set_max_threads, arginfo_aws_crt_event_loop_group_options_set_max_threads)
ZEND_FE(aws_crt_event_loop_group_new, arginfo_aws_crt_event_loop_group_new)
ZEND_FE(aws_crt_event_loop_group_release, arginfo_aws_crt_event_loop_group_release)
ZEND_FE(aws_crt_input_stream_options_new, arginfo_aws_crt_input_stream_options_new)
ZEND_FE(aws_crt_input_stream_options_release, arginfo_aws_crt_input_stream_options_release)
ZEND_FE(aws_crt_input_stream_options_set_user_data, arginfo_aws_crt_input_stream_options_set_user_data)
ZEND_FE(aws_crt_input_stream_new, arginfo_aws_crt_input_stream_new)
ZEND_FE(aws_crt_input_stream_release, arginfo_aws_crt_input_stream_release)
ZEND_FE(aws_crt_input_stream_seek, arginfo_aws_crt_input_stream_seek)
ZEND_FE(aws_crt_input_stream_read, arginfo_aws_crt_input_stream_read)
ZEND_FE(aws_crt_input_stream_eof, arginfo_aws_crt_input_stream_eof)
ZEND_FE(aws_crt_input_stream_get_length, arginfo_aws_crt_input_stream_get_length)
ZEND_FE(aws_crt_credentials_options_new, arginfo_aws_crt_credentials_options_new)
ZEND_FE(aws_crt_credentials_options_release, arginfo_aws_crt_credentials_options_release)
ZEND_FE(aws_crt_credentials_options_set_access_key_id, arginfo_aws_crt_credentials_options_set_access_key_id)
+167 -1
View File
@@ -10,6 +10,15 @@
#include "awscrt_arginfo.h"
/* Helpful references for this file:
* zend_parse_parameters and friends -
* https://git.php.net/?p=php-src.git;a=blob;f=docs/parameter-parsing-api.md;h=c962fc6ee58cc756aaac9e65759b7d5ea5c18fc4;hb=HEAD
* https://git.php.net/?p=php-src.git;a=blob;f=docs/self-contained-extensions.md;h=47f4c636baca8ca195118e2cc234ac7fd2842c1b;hb=HEAD
* Examples:
* Curl extension: https://github.com/php/php-src/blob/PHP-5.6/ext/curl/interface.c
* libuv extension: https://github.com/amphp/ext-uv/blob/master/php_uv.c
*/
ZEND_DECLARE_MODULE_GLOBALS(awscrt);
PHP_INI_BEGIN()
@@ -117,7 +126,7 @@ PHP_FUNCTION(aws_crt_event_loop_group_new) {
}
aws_crt_event_loop_group_options *options = (void *)php_options;
struct aws_event_loop_group *elg = aws_crt_event_loop_group_new(options);
aws_crt_event_loop_group *elg = aws_crt_event_loop_group_new(options);
RETURN_LONG((zend_ulong)elg);
}
@@ -132,6 +141,163 @@ PHP_FUNCTION(aws_crt_event_loop_group_release) {
aws_crt_event_loop_group_release(elg);
}
PHP_FUNCTION(aws_crt_input_stream_options_new) {
if (zend_parse_parameters_none() == FAILURE) {
RETURN_NULL();
}
aws_crt_input_stream_options *options = aws_crt_input_stream_options_new();
RETURN_LONG((zend_ulong)options);
}
PHP_FUNCTION(aws_crt_input_stream_options_release) {
zend_ulong php_options = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &php_options) == FAILURE) {
return;
}
aws_crt_input_stream_options *options = (void *)php_options;
aws_crt_input_stream_options_release(options);
}
/* PHP streams info:
* https://git.php.net/?p=php-src.git;a=blob;f=docs/streams.md;h=0ec3846d68bf70067297d8a6c691d2591c49b48a;hb=HEAD
* https://github.com/php/php-src/blob/PHP-5.6.0/main/php_streams.h
*/
PHP_FUNCTION(aws_crt_input_stream_options_set_user_data) {
zend_ulong php_options = 0;
zval *user_data = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz", &php_options, &user_data) == FAILURE) {
return;
}
aws_crt_input_stream_options *options = (void *)php_options;
php_stream *stream = NULL;
AWS_PHP_STREAM_FROM_ZVAL(stream, user_data);
aws_crt_input_stream_options_set_user_data(options, stream);
}
static int s_php_stream_seek(void *user_data, int64_t offset, aws_crt_input_stream_seek_basis basis) {
php_stream *stream = user_data;
return php_stream_seek(stream, offset, basis);
}
static int s_php_stream_read(void *user_data, uint8_t *dest, size_t dest_length) {
php_stream *stream = user_data;
return php_stream_read(stream, dest, dest_length) != 0;
}
static int s_php_stream_get_length(void *user_data, int64_t *out_length) {
php_stream *stream = user_data;
size_t pos = php_stream_tell(stream);
php_stream_seek(stream, 0, SEEK_END);
*out_length = php_stream_tell(stream);
php_stream_seek(stream, pos, SEEK_SET);
return 0;
}
static int s_php_stream_get_status(void *user_data, aws_crt_input_stream_status *out_status) {
php_stream *stream = user_data;
out_status->is_valid = stream != NULL;
/* We would like to use php_stream_eof here, but certain streams (notably php://memory)
* are not actually capable of EOF, so we get to do it the hard way */
size_t length = 0, pos = 0;
s_php_stream_get_length(stream, &length);
pos = php_stream_tell(stream);
out_status->is_end_of_stream = pos == length;
return 0;
}
static void s_php_stream_destroy(void *user_data) {
(void)user_data;
/* no op, stream will be freed by PHP refcount dropping from InputStream::stream */
}
PHP_FUNCTION(aws_crt_input_stream_new) {
zend_ulong php_options = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &php_options) == FAILURE) {
RETURN_NULL();
}
aws_crt_input_stream_options *options = (void *)php_options;
aws_crt_input_stream_options_set_seek(options, s_php_stream_seek);
aws_crt_input_stream_options_set_read(options, s_php_stream_read);
aws_crt_input_stream_options_set_get_status(options, s_php_stream_get_status);
aws_crt_input_stream_options_set_get_length(options, s_php_stream_get_length);
aws_crt_input_stream_options_set_destroy(options, s_php_stream_destroy);
aws_crt_input_stream *stream = aws_crt_input_stream_new(options);
RETURN_LONG((zend_ulong)stream);
}
PHP_FUNCTION(aws_crt_input_stream_release) {
zend_ulong php_stream = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &php_stream) == FAILURE) {
RETURN_NULL();
}
aws_crt_input_stream *stream = (void *)php_stream;
aws_crt_input_stream_release(stream);
}
PHP_FUNCTION(aws_crt_input_stream_seek) {
zend_ulong php_stream = 0;
zend_ulong offset = 0;
zend_ulong basis = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &php_stream, &offset, &basis) == FAILURE) {
RETURN_NULL();
}
aws_crt_input_stream *stream = (void *)php_stream;
RETURN_LONG(aws_crt_input_stream_seek(stream, offset, basis));
}
PHP_FUNCTION(aws_crt_input_stream_read) {
zend_ulong php_stream = 0;
zend_ulong length = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &php_stream, &length) == FAILURE) {
RETURN_NULL();
}
aws_crt_input_stream *stream = (void *)php_stream;
uint8_t *buf = emalloc(length);
int ret = aws_crt_input_stream_read(stream, buf, length);
AWS_RETURN_STRINGL(buf, length);
efree(buf);
}
PHP_FUNCTION(aws_crt_input_stream_eof) {
zend_ulong php_stream = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &php_stream) == FAILURE) {
RETURN_NULL();
}
aws_crt_input_stream *stream = (void *)php_stream;
aws_crt_input_stream_status status = {0};
aws_crt_input_stream_get_status(stream, &status);
RETURN_BOOL(status.is_end_of_stream);
}
PHP_FUNCTION(aws_crt_input_stream_get_length) {
zend_ulong php_stream = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &php_stream) == FAILURE) {
RETURN_NULL();
}
aws_crt_input_stream *stream = (void *)php_stream;
size_t length = 0;
aws_crt_input_stream_get_length(stream, &length);
RETURN_LONG(length);
}
PHP_FUNCTION(aws_crt_credentials_options_new) {
aws_crt_credentials_options *options = aws_crt_credentials_options_new();
RETURN_LONG((zend_ulong)options);
+8 -1
View File
@@ -29,11 +29,18 @@ ZEND_EXTERN_MODULE_GLOBALS(awscrt)
#define AWSCRT_GLOBAL(v) ZEND_MODULE_GLOBALS_ACCESSOR(awscrt, v)
/* PHP 7 removed the string duplicate parameter */
#if AWS_PHP_AT_LEAST_7
/* PHP 7 removed the string duplicate parameter */
# define AWS_RETURN_STRING(s) RETURN_STRING(s)
# define AWS_RETURN_STRINGL(s, l) RETURN_STRINGL(s, l)
/* PHP 7 takes a zval*, PHP5 takes a zval** */
# define AWS_PHP_STREAM_FROM_ZVAL(s, z) php_stream_from_zval(s, z)
#else
# define AWS_RETURN_STRING(s) RETURN_STRING(s, 1)
# define AWS_RETURN_STRINGL(s, l) RETURN_STRINGL(s, l, 1)
# define AWS_PHP_STREAM_FROM_ZVAL(s, z) php_stream_from_zval(s, &z)
/* definitions for ZEND API macros taken from PHP7 and backported to 5.6 */
# define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
static const zend_arg_info name[] = {{NULL, 0, NULL, required_num_args, return_reference, 0, 0},