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

Merged
merged 15 commits into from
Jul 5, 2025

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 requested a review from dstogov as a code owner June 11, 2025 17:57
@kocsismate kocsismate requested review from TimWolla and nielsdos and removed request for dstogov June 11, 2025 17:58
@kocsismate kocsismate marked this pull request as draft June 11, 2025 17:58
@kocsismate kocsismate force-pushed the ext-url6 branch 2 times, most recently from 43c1d9f to 155e070 Compare June 11, 2025 18:07
@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.

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.

@TimWolla

This comment was marked as resolved.

@kocsismate
Copy link
Member Author

The following patch fixes the leaks / memory management for me:

@TimWolla Thanks for the patch, it's awesome! TBH I didn't even think about using the UriMakeOwnerA() function, because I wanted to avoid its performance penalty. But you are right, this is the right call, it simplifies the code so much. I love it!

@TimWolla
Copy link
Member

@kocsismate When stack allocating the struct the code should become even simpler (and possibly faster, due to fewer pointer indirection).

@kocsismate kocsismate marked this pull request as ready for review June 16, 2025 07:59
@kocsismate kocsismate force-pushed the ext-url6 branch 2 times, most recently from 0b3cfd8 to 062dc4d Compare June 20, 2025 06:04
@kocsismate kocsismate force-pushed the ext-url6 branch 3 times, most recently from 226735b to 7541b33 Compare July 1, 2025 20:08
@DanielEScherzer
Copy link
Member

I have no further suggestions, but don't feel qualified to approve this

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.

Looks mostly okay for an initial merge now. The additional clean-up can happen in a follow-up to move forward. The leak should be fixed, though.

Comment on lines -107 to -166
PHP_METHOD(Uri_WhatWg_InvalidUrlException, __construct)
{
zend_string *message = NULL;
zval *errors = NULL;
zend_long code = 0;
zval *previous = NULL;

ZEND_PARSE_PARAMETERS_START(0, 4)
Z_PARAM_OPTIONAL
Z_PARAM_STR(message)
Z_PARAM_ARRAY(errors)
Z_PARAM_LONG(code)
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(previous, zend_ce_throwable)
ZEND_PARSE_PARAMETERS_END();

if (zend_update_exception_properties(INTERNAL_FUNCTION_PARAM_PASSTHRU, message, code, previous) == FAILURE) {
RETURN_THROWS();
}

if (errors == NULL) {
zval tmp;
ZVAL_EMPTY_ARRAY(&tmp);
zend_update_property(uri_whatwg_invalid_url_exception_ce, Z_OBJ_P(ZEND_THIS), ZEND_STRL("errors"), &tmp);
} else {
zend_update_property(uri_whatwg_invalid_url_exception_ce, Z_OBJ_P(ZEND_THIS), ZEND_STRL("errors"), errors);
}
if (EG(exception)) {
RETURN_THROWS();
}
}

PHP_METHOD(Uri_WhatWg_UrlValidationError, __construct)
{
zend_string *context;
zval *type;
bool failure;

ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_STR(context)
Z_PARAM_OBJECT_OF_CLASS(type, uri_whatwg_url_validation_error_type_ce)
Z_PARAM_BOOL(failure)
ZEND_PARSE_PARAMETERS_END();

zend_update_property_str(uri_whatwg_url_validation_error_ce, Z_OBJ_P(ZEND_THIS), ZEND_STRL("context"), context);
if (EG(exception)) {
RETURN_THROWS();
}

zend_update_property_ex(uri_whatwg_url_validation_error_ce, Z_OBJ_P(ZEND_THIS), ZSTR_KNOWN(ZEND_STR_TYPE), type);
if (EG(exception)) {
RETURN_THROWS();
}

zval failure_zv;
ZVAL_BOOL(&failure_zv, failure);
zend_update_property(uri_whatwg_url_validation_error_ce, Z_OBJ_P(ZEND_THIS), ZEND_STRL("failure"), &failure_zv);
if (EG(exception)) {
RETURN_THROWS();
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't particularly like that you reordered these. It makes the diff hard to read.

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. I should have waited a little bit, sorry! A few methods had to be removed due to the added implementation aliases, and that was the point I think when I moved a few methods.

Comment on lines +443 to +455
PHP_METHOD(Uri_Rfc3986_Uri, equals)
{
zend_object *that_object;
zend_object *comparison_mode = NULL;

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_OBJ_OF_CLASS(that_object, uri_rfc3986_uri_ce)
Z_PARAM_OPTIONAL
Z_PARAM_OBJ_OF_CLASS(comparison_mode, uri_comparison_mode_ce)
ZEND_PARSE_PARAMETERS_END();

uri_equals(INTERNAL_FUNCTION_PARAM_PASSTHRU, that_object, comparison_mode);
}
Copy link
Member

Choose a reason for hiding this comment

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

Similarly to the other remark about file naming and organization, it might make sense to move the PHP_METHOD implementations into the matching source file to keep php_uri.c slim.

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.

Besides the things that Tim said, here some small remarks

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 minor suggestions, but this is good to merge for me now. Anything else can be done in follow-ups.

Comment on lines 165 to 167
static size_t str_to_int(const char *str, size_t len)
{
int result = 0;
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
static size_t str_to_int(const char *str, size_t len)
{
int result = 0;
static zend_long str_to_int(const char *str, size_t len)
{
zend_long result = 0;

It's being stored in a zend_long, so should return a zend_long. But perhaps @nielsdos has other suggestions.

Copy link
Member

Choose a reason for hiding this comment

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

No your suggestion is fine

@kocsismate kocsismate merged commit 5a9f5a6 into php:master Jul 5, 2025
9 checks passed
@kocsismate kocsismate deleted the ext-url6 branch July 5, 2025 08:00
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.

None yet

4 participants