Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions ext/openssl/xp_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2640,14 +2640,19 @@ static char *php_openssl_get_url_name(const char *resourcename,
return NULL;
}

uri_internal_t *internal_uri = php_uri_parse(uri_parser, resourcename, resourcenamelen, true);
if (internal_uri == NULL) {
void *parsed = uri_parser->parse_uri(resourcename, resourcenamelen,
/* base_url */ NULL, /* errors */ NULL, /* silent */ true);
if (parsed == NULL) {
return NULL;
}
uri_internal_t internal_uri = {
.parser = uri_parser,
.uri = parsed,
};

char * url_name = NULL;
zval host_zv;
zend_result result = php_uri_get_host(internal_uri, URI_COMPONENT_READ_RAW, &host_zv);
zend_result result = php_uri_get_host(&internal_uri, URI_COMPONENT_READ_RAW, &host_zv);
if (result == SUCCESS && Z_TYPE(host_zv) == IS_STRING) {
const char * host = Z_STRVAL(host_zv);
size_t len = Z_STRLEN(host_zv);
Expand All @@ -2662,7 +2667,7 @@ static char *php_openssl_get_url_name(const char *resourcename,
}
}

php_uri_free(internal_uri);
uri_parser->free_uri(parsed);
zval_ptr_dtor(&host_zv);

return url_name;
Expand Down
56 changes: 21 additions & 35 deletions ext/uri/php_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,6 @@ PHPAPI uri_parser_t *php_uri_get_parser(const zend_string *uri_parser_name)
return uri_parser_by_name(ZSTR_VAL(uri_parser_name), ZSTR_LEN(uri_parser_name));
}

ZEND_ATTRIBUTE_NONNULL PHPAPI uri_internal_t *php_uri_parse(const uri_parser_t *uri_parser, const char *uri_str, size_t uri_str_len, bool silent)
{
uri_internal_t *internal_uri = emalloc(sizeof(*internal_uri));
internal_uri->parser = uri_parser;
internal_uri->uri = uri_parser->parse_uri(uri_str, uri_str_len, NULL, NULL, silent);

if (UNEXPECTED(internal_uri->uri == NULL)) {
efree(internal_uri);
return NULL;
}

return internal_uri;
}

ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_get_property(const uri_internal_t *internal_uri, uri_property_name_t property_name, uri_component_read_mode_t read_mode, zval *zv)
{
const uri_property_handler_t *property_handler = uri_property_handler_from_internal_uri(internal_uri, property_name);
Expand Down Expand Up @@ -180,96 +166,95 @@ ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_uri_get_fragment(const uri_interna
return php_uri_get_property(internal_uri, URI_PROPERTY_NAME_FRAGMENT, read_mode, zv);
}

ZEND_ATTRIBUTE_NONNULL PHPAPI void php_uri_free(uri_internal_t *internal_uri)
{
internal_uri->parser->free_uri(internal_uri->uri);
internal_uri->uri = NULL;
internal_uri->parser = NULL;
efree(internal_uri);
}

ZEND_ATTRIBUTE_NONNULL PHPAPI php_uri *php_uri_parse_to_struct(
const uri_parser_t *uri_parser, const char *uri_str, size_t uri_str_len, uri_component_read_mode_t read_mode, bool silent
) {
uri_internal_t *uri_internal = php_uri_parse(uri_parser, uri_str, uri_str_len, silent);
if (uri_internal == NULL) {
void *parsed = uri_parser->parse_uri(uri_str, uri_str_len,
/* base_url */ NULL, /* errors */ NULL, silent);
if (parsed == NULL) {
return NULL;
}

uri_internal_t uri_internal = {
.parser = uri_parser,
.uri = parsed,
};

php_uri *uri = ecalloc(1, sizeof(*uri));
zval tmp;
zend_result result;

result = php_uri_get_scheme(uri_internal, read_mode, &tmp);
result = php_uri_get_scheme(&uri_internal, read_mode, &tmp);
if (result == FAILURE) {
goto error;
}
if (Z_TYPE(tmp) == IS_STRING) {
uri->scheme = Z_STR(tmp);
}

result = php_uri_get_username(uri_internal, read_mode, &tmp);
result = php_uri_get_username(&uri_internal, read_mode, &tmp);
if (result == FAILURE) {
goto error;
}
if (Z_TYPE(tmp) == IS_STRING) {
uri->user = Z_STR(tmp);
}

result = php_uri_get_password(uri_internal, read_mode, &tmp);
result = php_uri_get_password(&uri_internal, read_mode, &tmp);
if (result == FAILURE) {
goto error;
}
if (Z_TYPE(tmp) == IS_STRING) {
uri->password = Z_STR(tmp);
}

result = php_uri_get_host(uri_internal, read_mode, &tmp);
result = php_uri_get_host(&uri_internal, read_mode, &tmp);
if (result == FAILURE) {
goto error;
}
if (Z_TYPE(tmp) == IS_STRING) {
uri->host = Z_STR(tmp);
}

result = php_uri_get_port(uri_internal, read_mode, &tmp);
result = php_uri_get_port(&uri_internal, read_mode, &tmp);
if (result == FAILURE) {
goto error;
}
if (Z_TYPE(tmp) == IS_LONG) {
uri->port = Z_LVAL(tmp);
}

result = php_uri_get_path(uri_internal, read_mode, &tmp);
result = php_uri_get_path(&uri_internal, read_mode, &tmp);
if (result == FAILURE) {
goto error;
}
if (Z_TYPE(tmp) == IS_STRING) {
uri->path = Z_STR(tmp);
}

result = php_uri_get_query(uri_internal, read_mode, &tmp);
result = php_uri_get_query(&uri_internal, read_mode, &tmp);
if (result == FAILURE) {
goto error;
}
if (Z_TYPE(tmp) == IS_STRING) {
uri->query = Z_STR(tmp);
}

result = php_uri_get_fragment(uri_internal, read_mode, &tmp);
result = php_uri_get_fragment(&uri_internal, read_mode, &tmp);
if (result == FAILURE) {
goto error;
}
if (Z_TYPE(tmp) == IS_STRING) {
uri->fragment = Z_STR(tmp);
}

php_uri_free(uri_internal);
uri_parser->free_uri(parsed);

return uri;

error:
php_uri_free(uri_internal);

uri_parser->free_uri(parsed);
php_uri_struct_free(uri);

return NULL;
Expand Down Expand Up @@ -346,7 +331,8 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 2) PHPAPI void php_uri_instantiate_uri(
base_url = internal_base_url->uri;
}

void *uri = uri_parser->parse_uri(ZSTR_VAL(uri_str), ZSTR_LEN(uri_str), base_url, should_throw || errors_zv != NULL ? &errors : NULL, !should_throw);
void *uri = uri_parser->parse_uri(ZSTR_VAL(uri_str), ZSTR_LEN(uri_str),
base_url, should_throw || errors_zv != NULL ? &errors : NULL, !should_throw);
if (UNEXPECTED(uri == NULL)) {
if (should_throw) {
zval_ptr_dtor(&errors);
Expand Down
9 changes: 0 additions & 9 deletions ext/uri/php_uri.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ PHPAPI zend_result php_uri_parser_register(const uri_parser_t *uri_parser);
*/
PHPAPI uri_parser_t *php_uri_get_parser(const zend_string *uri_parser_name);

ZEND_ATTRIBUTE_NONNULL PHPAPI uri_internal_t *php_uri_parse(const uri_parser_t *uri_parser, const char *uri_str, size_t uri_str_len, bool silent);

/**
* Retrieves the scheme component based on the read_mode and passes it to the zv ZVAL in case of success.
*
Expand Down Expand Up @@ -171,13 +169,6 @@ ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_uri_get_query(const uri_internal_t
*/
ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_uri_get_fragment(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *zv);

/**
* Frees the uri member within the provided internal URI.
*
* @param internal_uri The internal URI
*/
ZEND_ATTRIBUTE_NONNULL PHPAPI void php_uri_free(uri_internal_t *internal_uri);

/**
* Creates a new php_uri struct containing all the URI components. The components are retrieved based on the read_mode parameter.
*
Expand Down
44 changes: 25 additions & 19 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -741,54 +741,60 @@ static ZEND_FUNCTION(zend_test_uri_parser)
RETURN_THROWS();
}

uri_internal_t *uri = php_uri_parse(parser, ZSTR_VAL(uri_string), ZSTR_LEN(uri_string), false);
if (uri == NULL) {
void *parsed = parser->parse_uri(ZSTR_VAL(uri_string), ZSTR_LEN(uri_string),
/* base_url */ NULL, /* errors */ NULL, /* silent */ false);
if (parsed == NULL) {
RETURN_THROWS();
}

uri_internal_t uri = {
.parser = parser,
.uri = parsed,
};

zval value;

array_init(return_value);
zval normalized;
array_init(&normalized);
php_uri_get_scheme(uri, URI_COMPONENT_READ_NORMALIZED_ASCII, &value);
php_uri_get_scheme(&uri, URI_COMPONENT_READ_NORMALIZED_ASCII, &value);
zend_hash_add(Z_ARR(normalized), ZSTR_KNOWN(ZEND_STR_SCHEME), &value);
php_uri_get_username(uri, URI_COMPONENT_READ_NORMALIZED_ASCII, &value);
php_uri_get_username(&uri, URI_COMPONENT_READ_NORMALIZED_ASCII, &value);
zend_hash_add(Z_ARR(normalized), ZSTR_KNOWN(ZEND_STR_USERNAME), &value);
php_uri_get_password(uri, URI_COMPONENT_READ_NORMALIZED_ASCII, &value);
php_uri_get_password(&uri, URI_COMPONENT_READ_NORMALIZED_ASCII, &value);
zend_hash_add(Z_ARR(normalized), ZSTR_KNOWN(ZEND_STR_PASSWORD), &value);
php_uri_get_host(uri, URI_COMPONENT_READ_NORMALIZED_ASCII, &value);
php_uri_get_host(&uri, URI_COMPONENT_READ_NORMALIZED_ASCII, &value);
zend_hash_add(Z_ARR(normalized), ZSTR_KNOWN(ZEND_STR_HOST), &value);
php_uri_get_port(uri, URI_COMPONENT_READ_NORMALIZED_ASCII, &value);
php_uri_get_port(&uri, URI_COMPONENT_READ_NORMALIZED_ASCII, &value);
zend_hash_add(Z_ARR(normalized), ZSTR_KNOWN(ZEND_STR_PORT), &value);
php_uri_get_path(uri, URI_COMPONENT_READ_NORMALIZED_ASCII, &value);
php_uri_get_path(&uri, URI_COMPONENT_READ_NORMALIZED_ASCII, &value);
zend_hash_add(Z_ARR(normalized), ZSTR_KNOWN(ZEND_STR_PATH), &value);
php_uri_get_query(uri, URI_COMPONENT_READ_NORMALIZED_ASCII, &value);
php_uri_get_query(&uri, URI_COMPONENT_READ_NORMALIZED_ASCII, &value);
zend_hash_add(Z_ARR(normalized), ZSTR_KNOWN(ZEND_STR_QUERY), &value);
php_uri_get_fragment(uri, URI_COMPONENT_READ_NORMALIZED_ASCII, &value);
php_uri_get_fragment(&uri, URI_COMPONENT_READ_NORMALIZED_ASCII, &value);
zend_hash_add(Z_ARR(normalized), ZSTR_KNOWN(ZEND_STR_FRAGMENT), &value);
zend_hash_str_add(Z_ARR_P(return_value), "normalized", strlen("normalized"), &normalized);
zval raw;
array_init(&raw);
php_uri_get_scheme(uri, URI_COMPONENT_READ_RAW, &value);
php_uri_get_scheme(&uri, URI_COMPONENT_READ_RAW, &value);
zend_hash_add(Z_ARR(raw), ZSTR_KNOWN(ZEND_STR_SCHEME), &value);
php_uri_get_username(uri, URI_COMPONENT_READ_RAW, &value);
php_uri_get_username(&uri, URI_COMPONENT_READ_RAW, &value);
zend_hash_add(Z_ARR(raw), ZSTR_KNOWN(ZEND_STR_USERNAME), &value);
php_uri_get_password(uri, URI_COMPONENT_READ_RAW, &value);
php_uri_get_password(&uri, URI_COMPONENT_READ_RAW, &value);
zend_hash_add(Z_ARR(raw), ZSTR_KNOWN(ZEND_STR_PASSWORD), &value);
php_uri_get_host(uri, URI_COMPONENT_READ_RAW, &value);
php_uri_get_host(&uri, URI_COMPONENT_READ_RAW, &value);
zend_hash_add(Z_ARR(raw), ZSTR_KNOWN(ZEND_STR_HOST), &value);
php_uri_get_port(uri, URI_COMPONENT_READ_RAW, &value);
php_uri_get_port(&uri, URI_COMPONENT_READ_RAW, &value);
zend_hash_add(Z_ARR(raw), ZSTR_KNOWN(ZEND_STR_PORT), &value);
php_uri_get_path(uri, URI_COMPONENT_READ_RAW, &value);
php_uri_get_path(&uri, URI_COMPONENT_READ_RAW, &value);
zend_hash_add(Z_ARR(raw), ZSTR_KNOWN(ZEND_STR_PATH), &value);
php_uri_get_query(uri, URI_COMPONENT_READ_RAW, &value);
php_uri_get_query(&uri, URI_COMPONENT_READ_RAW, &value);
zend_hash_add(Z_ARR(raw), ZSTR_KNOWN(ZEND_STR_QUERY), &value);
php_uri_get_fragment(uri, URI_COMPONENT_READ_RAW, &value);
php_uri_get_fragment(&uri, URI_COMPONENT_READ_RAW, &value);
zend_hash_add(Z_ARR(raw), ZSTR_KNOWN(ZEND_STR_FRAGMENT), &value);
zend_hash_str_add(Z_ARR_P(return_value), "raw", strlen("raw"), &raw);

php_uri_free(uri);
parser->free_uri(parsed);
}

static bool has_opline(zend_execute_data *execute_data)
Expand Down