Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

uri: Remove php_uri_parse() and php_uri_free() #19624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
TimWolla wants to merge 2 commits into php:master
base: master
Choose a base branch
Loading
from TimWolla:uri-parse-api
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
View file Open in desktop
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,
Copy link
Member

@kocsismate kocsismate Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed these design details a while ago here: #18672 (comment) Back then, you suggested a simple API for internal usage (that's why php_uri_parse() doesn't support the base_url parameter), but now all the capabilities of the raw parse_uri handler would be directly exposed for 3rd parties.

In the last sentence of my response (#18672 (comment)), I mentioned the reason why I think these API functions are important: they can hide the implementation details. E.g. if we remove the PHPAPI functions then we won't be able to change the signature (e.g. add new parameters) without causing a serious BC break for any caller of the API.

What made you change your mind in this question? What kind of future changes do you have in mind?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note how my last reply said "I'll look into this more at a later time" and this is what happened here. I still believe that simplifying the handler itself would be the right decision.

When I worked my way through the code, I realized that the php_uri_parse() function is not much simpler than using the parse_uri handler directly (its main benefit is not having the errors parameter that is inapplicable to a generic API), but the way it works it comes with quite a bit of overhead due to the dynamic allocation (as mentioned in the commit message).

What kind of future changes do you have in mind?

I'm hoping to overall reduce the amount of stuff passed by pointer, since this adds quite a bit of overhead and makes the code harder to reason about. Specifically uri_internal_t is currently often a dynamic allocation that is passed by pointer, which results in a 8 byte pointer being passed to a function just to look up a 16 byte struct containing two pointers, one of which is already known in many cases.


However I see that this PR by itself might not be particularly convincing. I can offer to prepare a little larger PR for you to see the "full picture" that I can then split into its individual parts, how does that sound?

Copy link
Member

@kocsismate kocsismate Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note how my last reply said "I'll look into this more at a later time" and this is what happened here. I still believe that simplifying the handler itself would be the right decision.

Yeah, when rereading it, I was sure that the time had come now ^^

I'm hoping to overall reduce the amount of stuff passed by pointer, since this adds quite a bit of overhead and makes the code harder to reason about. Specifically uri_internal_t is currently often a dynamic allocation that is passed by pointer, which results in a 8 byte pointer being passed to a function just to look up a 16 byte struct containing two pointers, one of which is already known in many cases.

Thanks for the efforts!

However I see that this PR by itself might not be particularly convincing. I can offer to prepare a little larger PR for you to see the "full picture" that I can then split into its individual parts, how does that sound?

Yes, that sounds much better! I'm happy about performance/code quality improvements as long as the code continues to offer its users some kind of protection against future BC breaks. Basically, that's my main - and probably only - cornerstone :)

/* 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
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,6 @@ PHPAPI const 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 @@ -178,96 +164,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 @@ -343,7 +328,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
View file Open in desktop
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 const 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
View file Open in desktop
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

AltStyle によって変換されたページ (->オリジナル) /