Skip to content

Add the Uri\Rfc3986\Uri class to ext/uri without wither support #18836

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kocsismate
Copy link
Member

@kocsismate kocsismate commented Jun 11, 2025

There are a few WIP parts but the PR can already be reviewed.

Relates to #14461 and https://wiki.php.net/rfc/url_parsing_api

@kocsismate kocsismate changed the title Add Uri\Rfc3986\Uri class to ext/uri Add the Uri\Rfc3986\Uri class to ext/uri without wither support Jun 11, 2025
Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

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

Cursory glance, I see there's some todos wrt memory management as well so maybe I'll wait a bit on that

Copy link
Member

@TimWolla TimWolla left a comment

Choose a reason for hiding this comment

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

Some first remarks from just looking at the diff on GitHub.

UriUriA *uri, zend_string *uri_str,
UriUriA *normalized_uri, zend_string *normalized_uri_str
) {
uriparser_uris_t *uriparser_uris = emalloc(sizeof(uriparser_uris_t));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
uriparser_uris_t *uriparser_uris = emalloc(sizeof(uriparser_uris_t));
uriparser_uris_t *uriparser_uris = emalloc(sizeof(*uriparser_uris));

Prevents the two types from going out of sync (same elsewhere).

URIPARSER_READ_URI(uriparser_uri, uriparser_uris, read_mode);

if (uriparser_uri->userInfo.first != NULL && uriparser_uri->userInfo.afterLast != NULL) {
ZVAL_STRINGL(retval, uriparser_uri->userInfo.first, uriparser_uri->userInfo.afterLast - uriparser_uri->userInfo.first);
Copy link
Member

Choose a reason for hiding this comment

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

This is very verbose, it would probably make sense to add:

static inline zend_string *text_range_to_zend_string(const UriTextRangeA *range)
{
    return zend_string_init(range->first, range->afterLast - range->first, false);
}

and then:

ZVAL_NEW_STR(retval, text_range_to_zend_string(uriparser_uri->userInfo));

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively (seeing the read_user() function that needs to access substrings), perhaps a text_range_len() function that just calculates the correct length to avoid the repetition.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea!

Copy link
Member

Choose a reason for hiding this comment

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

Something I'm only noticing now: I think this should rather be called ext/uri/parser_rfc3986.c and php_lexbor.c would be ext/uri/parser_whatwg.c. The php_uriparser.c name is confusing to me, because uriparser is an extremely generic term.

To give another comparison with ext/random, since it is architecturally similar: Each engine has its own engine_enginename.c file, e.g. engine_xoshiro256starstar.c.

efree(uriparser_uris);
}

const uri_handler_t uriparser_uri_handler = {
Copy link
Member

Choose a reason for hiding this comment

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

And similarly the public symbols in the file should also be prefixed with php_uri_. Just uriparser_ can conflict with the uriparser library itself.

Here I would suggest php_uri_rfc3986_handler.

But I'm also happy to leave this to a follow-up to not introduce too much churn.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this and the file renaming is something that I'm happy to do after most parts are merged.

zend_string *normalized_uri_str;
} uriparser_uris_t;

void uriparser_module_init(void);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
void uriparser_module_init(void);
PHP_MINIT_FUNCTION(uri_uriparser);

return FAILURE;
}

uriparser_module_init();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
uriparser_module_init();
if (FAILURE == PHP_MINIT(uri_uriparser)(INIT_FUNC_ARGS_PASSTHRU)) {
return FAILURE;
}

For consistency with e.g. spl or sodium.

Comment on lines +849 to +851
if (uri_handler_register(&uriparser_uri_handler) == FAILURE) {
return FAILURE;
}
Copy link
Member

Choose a reason for hiding this comment

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

Registering the handler can be part of PHP_MINIT(uri_uriparser) then.

uriparser_copy_text_range(&uriparser_uri->query, &new_uriparser_uri->query, false);
uriparser_copy_text_range(&uriparser_uri->fragment, &new_uriparser_uri->fragment, false);
new_uriparser_uri->absolutePath = uriparser_uri->absolutePath;
new_uriparser_uri->owner = true;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
new_uriparser_uri->owner = true;
new_uriparser_uri->owner = URI_TRUE;

}
}

static UriUriA *uriparser_copy_uri(UriUriA *uriparser_uri) // TODO add to uriparser
Copy link
Member

Choose a reason for hiding this comment

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

I think this can just be:

UriUriA *copy = emalloc(sizeof(*copy));
memcpy(copy, uriparser_uri, sizeof(*copy));
uriMakeOwnerA(copy);

return copy;

Comment on lines +26 to +31
typedef struct uriparser_uris_t {
UriUriA *uri;
zend_string *uri_str;
UriUriA *normalized_uri;
zend_string *normalized_uri_str;
} uriparser_uris_t;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
typedef struct uriparser_uris_t {
UriUriA *uri;
zend_string *uri_str;
UriUriA *normalized_uri;
zend_string *normalized_uri_str;
} uriparser_uris_t;
typedef struct uriparser_uris_t {
UriUriA uri;
zend_string *uri_str;
UriUriA normalized_uri;
zend_string *normalized_uri_str;
} uriparser_uris_t;

From what I see, the uriparser library does not allocate UriUriA structs itself, but requires you to pass in an OUT-pointer. Thus we can just stack-allocate it / embed it into another struct.

Copy link
Member Author

Choose a reason for hiding this comment

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

The uri field is a good candidate indeed, but the normalized_uri field is initialized on demand (by uriparser_read_uri). So we would have to track its initialization status by a separate bool field because UriUriA doesn't provide any indication. Is it still desirable to allocate on stack even if it causes some maintainability degradation?

Copy link
Member

Choose a reason for hiding this comment

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

Is it still desirable to allocate on stack even if it causes some maintainability degradation?

I feel that a separate bool would also be simpler on the maintenance, since you would have less allocs / frees to deal with, but use whatever “feels comfortable”.

Comment on lines +369 to +416
UriUriA *uriparser_uri = emalloc(sizeof(UriUriA));

/* uriparser keeps the originally passed in string, while lexbor may allocate a new one. */
zend_string *original_uri_str = zend_string_init(ZSTR_VAL(uri_str), ZSTR_LEN(uri_str), false);
if (ZSTR_LEN(original_uri_str) == 0 ||
uriParseSingleUriExA(uriparser_uri, ZSTR_VAL(original_uri_str), ZSTR_VAL(original_uri_str) + ZSTR_LEN(original_uri_str), NULL) != URI_SUCCESS
) {
efree(uriparser_uri);
zend_string_release_ex(original_uri_str, false);
if (!silent) {
throw_invalid_uri_exception();
}

return NULL;
}

if (uriparser_base_urls == NULL) {
return uriparser_create_uris(uriparser_uri, original_uri_str, NULL, NULL);
}

UriUriA *uriparser_base_url = uriparser_copy_uri(uriparser_base_urls->uri);

UriUriA *absolute_uri = emalloc(sizeof(UriUriA));

if (uriAddBaseUriExA(absolute_uri, uriparser_uri, uriparser_base_url, URI_RESOLVE_STRICTLY) != URI_SUCCESS) {
zend_string_release_ex(original_uri_str, false);
uriFreeUriMembersA(uriparser_uri);
efree(uriparser_uri);
uriFreeUriMembersA(uriparser_base_url);
efree(uriparser_base_url);
efree(absolute_uri);

if (!silent) {
throw_invalid_uri_exception();
}

return NULL;
}

/* TODO fix freeing: if the following code runs, then we'll have use-after-free-s because uriparser doesn't
copy the input. If we don't run the following code, then we'll have memory leaks...
uriFreeUriMembersA(uriparser_base_url);
efree(uriparser_base_url);
uriFreeUriMembersA(uriparser_uri);
efree(uriparser_uri);
*/

return uriparser_create_uris(absolute_uri, original_uri_str, NULL, NULL);
Copy link
Member

Choose a reason for hiding this comment

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

Rough untested suggestion. Basically: Keep the UriUriA object stack-allocated for as long as possible.

Suggested change
UriUriA *uriparser_uri = emalloc(sizeof(UriUriA));
/* uriparser keeps the originally passed in string, while lexbor may allocate a new one. */
zend_string *original_uri_str = zend_string_init(ZSTR_VAL(uri_str), ZSTR_LEN(uri_str), false);
if (ZSTR_LEN(original_uri_str) == 0 ||
uriParseSingleUriExA(uriparser_uri, ZSTR_VAL(original_uri_str), ZSTR_VAL(original_uri_str) + ZSTR_LEN(original_uri_str), NULL) != URI_SUCCESS
) {
efree(uriparser_uri);
zend_string_release_ex(original_uri_str, false);
if (!silent) {
throw_invalid_uri_exception();
}
return NULL;
}
if (uriparser_base_urls == NULL) {
return uriparser_create_uris(uriparser_uri, original_uri_str, NULL, NULL);
}
UriUriA *uriparser_base_url = uriparser_copy_uri(uriparser_base_urls->uri);
UriUriA *absolute_uri = emalloc(sizeof(UriUriA));
if (uriAddBaseUriExA(absolute_uri, uriparser_uri, uriparser_base_url, URI_RESOLVE_STRICTLY) != URI_SUCCESS) {
zend_string_release_ex(original_uri_str, false);
uriFreeUriMembersA(uriparser_uri);
efree(uriparser_uri);
uriFreeUriMembersA(uriparser_base_url);
efree(uriparser_base_url);
efree(absolute_uri);
if (!silent) {
throw_invalid_uri_exception();
}
return NULL;
}
/* TODO fix freeing: if the following code runs, then we'll have use-after-free-s because uriparser doesn't
copy the input. If we don't run the following code, then we'll have memory leaks...
uriFreeUriMembersA(uriparser_base_url);
efree(uriparser_base_url);
uriFreeUriMembersA(uriparser_uri);
efree(uriparser_uri);
*/
return uriparser_create_uris(absolute_uri, original_uri_str, NULL, NULL);
UriUriA uriparser_uri;
/* uriparser keeps the originally passed in string, while lexbor may allocate a new one. */
zend_string *original_uri_str = zend_string_init(ZSTR_VAL(uri_str), ZSTR_LEN(uri_str), false);
if (ZSTR_LEN(original_uri_str) == 0 ||
uriParseSingleUriExA(&uriparser_uri, ZSTR_VAL(original_uri_str), ZSTR_VAL(original_uri_str) + ZSTR_LEN(original_uri_str), NULL) != URI_SUCCESS
) {
zend_string_release_ex(original_uri_str, false);
if (!silent) {
throw_invalid_uri_exception();
}
return NULL;
}
if (uriparser_base_urls == NULL) {
return uriparser_create_uris(&uriparser_uri, original_uri_str, NULL, NULL);
}
UriUriA absolute_uri;
if (uriAddBaseUriExA(&absolute_uri, uriparser_uri, &uriparser_base_urls->uri, URI_RESOLVE_STRICTLY) != URI_SUCCESS) {
zend_string_release_ex(original_uri_str, false);
uriFreeUriMembersA(uriparser_uri);
uriFreeUriMembersA(uriparser_base_url);
if (!silent) {
throw_invalid_uri_exception();
}
return NULL;
}
/* TODO fix freeing: if the following code runs, then we'll have use-after-free-s because uriparser doesn't
copy the input. If we don't run the following code, then we'll have memory leaks...
uriFreeUriMembersA(uriparser_base_url);
efree(uriparser_base_url);
uriFreeUriMembersA(uriparser_uri);
efree(uriparser_uri);
*/
return uriparser_create_uris(&absolute_uri, original_uri_str, NULL, NULL);

URI_ASSERT_INITIALIZATION(internal_uri);

if (UNEXPECTED(uriparser_read_userinfo(internal_uri, read_mode, return_value) == FAILURE)) {
zend_throw_error(NULL, "%s::$%s property cannot be retrieved", ZSTR_VAL(Z_OBJ_P(ZEND_THIS)->ce->name),
Copy link
Member

Choose a reason for hiding this comment

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

If this is only used for userinfo, why go through the indirection of using a zend_string? I assume the class needs to use ce->name because of subclasses, but you can do

Suggested change
zend_throw_error(NULL, "%s::$%s property cannot be retrieved", ZSTR_VAL(Z_OBJ_P(ZEND_THIS)->ce->name),
zend_throw_error(NULL, "%s::$userinfo property cannot be retrieved", ZSTR_VAL(Z_OBJ_P(ZEND_THIS)->ce->name));

uriparser_uri = uriparser_uris->normalized_uri;
}

int charsRequired;
Copy link
Member

Choose a reason for hiding this comment

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

suggest size_t since you cannot have a negative number here

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately, uriparser needs an int here:

int * charsRequired) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants