Skip to content

Implement "Followup improvements for ext/uri" RFC - WHATWG URL building - #22268

Open
kocsismate wants to merge 8 commits into
php:masterfrom
kocsismate:uri-followup4
Open

Implement "Followup improvements for ext/uri" RFC - WHATWG URL building#22268
kocsismate wants to merge 8 commits into
php:masterfrom
kocsismate:uri-followup4

Conversation

@kocsismate

Copy link
Copy Markdown
Member

@kocsismate
kocsismate requested a review from TimWolla as a code owner June 10, 2026 15:10
@kocsismate kocsismate changed the title IImplement "Followup improvements for ext/uri" RFC - WHATWG URL building Implement "Followup improvements for ext/uri" RFC - WHATWG URL building Jun 10, 2026
@kocsismate
kocsismate marked this pull request as draft June 10, 2026 16:12
goto failure;
}

if (lexbor_base_url != NULL) {

@kocsismate kocsismate Jun 25, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What's a big shame is that apparently it's not possible to properly use the builder with a base URL :(

  • if we try to add the base URL after the input URL is built, then legitimate relative URLs are rejected (e.g. /foo + https://example.com), because /foo is not a valid URL on its own
  • If we try to build the input URL with the base URL in the same time, then the parsing algorithm must be used (currently, only setters are used with a hack on line 821). Then the complication is to find some URL component that is suitable for parsing:
    • for special, full URLs: scheme + host is needed at least (e.g. https://example.com)
    • for non-special full URLs: scheme is needed at least (e.g. https://)
    • for relative URLs: the path is needed at least (e.g. /foo/bar)

And in the 2nd case, the question arises if it's ok to parse only the minimally required components and then set the rest of the components, or the whole input URL must be built and parsed all at once.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have limited knowledge of lexbor, but can we build a string out of the components we have in the builder, and then call the parser with that? Assuming that we can build the string unambiguously, any errors from missing components would be reported by the parser.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

thanks for the idea! Unfortunately, - as far as I can see the situation - the main blocker is achiveving unambiguous recomposition. E.g.

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setScheme("git");
$builder->setPath("//refs/heads/main");
$builder->build();

This would be recomposed as git://refs/heads/main, however it should rather be git:////refs/heads/main. So overall, we would end up reimplementing the WHATWG URL spec to prevent some cases. Since it's very long specification with a lot of special cases, I wouldn't even dare to attempt this. :(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just brainstorming, but would that work?

  • base = parse base url
  • url = clone base
  • for each component in builder:
    • lxb_url_parse_basic(..., url, base, ..., override_state, ...) (with override_state the state corresponding to this component, e.g. LXB_URL_STATE_PATH_START_STATE for the path)

@kocsismate kocsismate Aug 15, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Very clever idea again, but I'm not exactly sure that "inverting" parsing would work correctly either..

  • let base be https://example.com:80/foo
  • let url be scheme://example.net/bar

Then this should be scheme://example.net/bar. However, it would be scheme://example.net:80/bar. I guess, the algorithm could be improved by discarding base if url has a scheme.

@lexborisov do you maybe have some insights whether the above idea would work to support base URLs with the WHATWG URL Builder that was proposed in https://wiki.php.net/rfc/uri_followup#builder ? The complexity is that we store the components separately, and we need to build them together (via the setters) + use the base URL - but the setters obviously don't support the base URL.

The build() is implemented here: https://github.com/php/php-src/pull/22268/changes#diff-1ac5fecaced76e4592af01f30d246ce17edfd60c29ebe0e01d57b2988ec10fbdR892

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@arnaud-lb can we merge what we have so far? and then I can go back to this question a bit later? I'm worried a little bit, because quite a few of the URI followup RFC haven't been merged yet :(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi @kocsismate

I've started looking into this thread, but I'll need some time. I have a lot on my plate right now.
At first glance, it seems like this can be resolved with minimal effort.

@kocsismate
kocsismate marked this pull request as ready for review June 25, 2026 08:19
@kocsismate
kocsismate requested a review from ndossche June 25, 2026 13:32
@kocsismate

Copy link
Copy Markdown
Member Author

May I have a review soon so that this can potentially be included into alpha 2 at least? :)

Comment thread ext/uri/php_uri.c Outdated
Comment thread ext/uri/uri_parser_whatwg.c
Comment thread ext/uri/uri_parser_whatwg.c Outdated
Comment thread ext/uri/uri_parser_whatwg.c Outdated
* The URL is initialized as LXB_URL_SCHEMEL_TYPE__UNDEF but this would prevent the scheme to be updated
* in case of non-special schemes due to https://github.com/php/php-src/blob/27d7b799c0a13578ee0506b428b8ddc209ffb010/ext/lexbor/lexbor/url/url.c#L1402
*/
if (!php_uri_parser_whatwg_is_special_scheme(scheme)) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'll try to check if it's something that we can omit somehow...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I didn't find any way how this workaround could be eliminated...

Comment thread ext/uri/php_uri.c
Comment thread ext/uri/uri_parser_whatwg.c Outdated
Comment thread ext/uri/uri_parser_whatwg.c Outdated

@TimWolla TimWolla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Had a very superficial first look.

Comment thread ext/uri/uri_parser_whatwg.c Outdated
Comment thread ext/uri/uri_parser_whatwg.c Outdated
Comment thread ext/uri/uri_parser_whatwg.c
@TimWolla
TimWolla self-requested a review July 17, 2026 18:25

@TimWolla TimWolla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looked at some tests and some of the C files. Not yet through the PR, but you can likely already make some changes in response to this review.

Comment thread ext/uri/uri_parser_whatwg.c Outdated
Comment thread ext/uri/tests/whatwg/builder/scheme_error_c0_control_space_char.phpt Outdated
Comment thread ext/uri/tests/whatwg/builder/scheme_error_empty_string.phpt Outdated
Comment thread ext/uri/tests/whatwg/builder/scheme_success_non_special.phpt Outdated
Comment thread ext/uri/tests/whatwg/builder/username_error_missing_host.phpt Outdated
Comment thread ext/uri/tests/whatwg/builder/username_success_empty_opaque_host.phpt Outdated
Comment thread ext/uri/tests/whatwg/builder/fragment_error_unicode_char.phpt Outdated

@TimWolla TimWolla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Went through all tests now. Didn't deeply look at the C code yet, but I'm not super stoked about needing to reimplement the component validation ourselves.

Comment thread ext/uri/tests/whatwg/builder/fragment_success_tab_newline.phpt Outdated
Comment thread ext/uri/tests/whatwg/builder/host_error_ipv6_closing_brace_opaque.phpt Outdated
Comment thread ext/uri/tests/whatwg/builder/host_error_percent_encoding3.phpt
Comment thread ext/uri/tests/whatwg/builder/password_error_empty_opaque_host.phpt Outdated
Comment thread ext/uri/tests/whatwg/builder/port_success_default.phpt Outdated
Comment thread ext/uri/tests/whatwg/builder/port_success_non_default.phpt Outdated
$builder->setHost("example.com");
$builder->setFragment("\tfoo");
$errors = [];
$url = $builder->build(errors: $errors);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems this is the only test testing $errors.

Comment thread ext/uri/tests/whatwg/builder/username_success_special_char.phpt
Comment thread ext/uri/uri_parser_whatwg.c Outdated

@arnaud-lb arnaud-lb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've only done a partial review yet

Comment thread ext/uri/php_uri.c Outdated
kocsismate added a commit to kocsismate/php-src that referenced this pull request Aug 8, 2026
kocsismate added a commit that referenced this pull request Aug 10, 2026
lexborisov added a commit to lexbor/lexbor that referenced this pull request Aug 12, 2026
Added lxb_url_parse_host_ipv6() — a public entry point to the IPv6
parser from the WHATWG specification:
https://url.spec.whatwg.org/#concept-ipv6-parser

The address is accepted both with and without the surrounding square
brackets: "::1" and "[::1]" give the same result.

#402

The API was requested in #402 for use by php/php-src#22268.

Suggested-by: Máté Kocsis (@kocsismate)

@arnaud-lb arnaud-lb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks good to me (except a few nits), but we should throw when a $baseUrl parameter is passed to ::build() until this is implemented.

Comment thread ext/uri/uri_parser_whatwg.c Outdated
Comment on lines +717 to +719
if (php_uri_whatwg_is_ascii_tab_or_newline(uc)) {
continue;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is consistent with Url::withScheme(), but also non-standard as per https://url.spec.whatwg.org/#url-writing:

A URL-scheme string must be one ASCII alpha, followed by zero or more of ASCII alphanumeric, U+002B (+), U+002D (-), and U+002E (.).

Comment on lines +826 to +827
if (php_uri_whatwg_is_ascii_tab_or_newline(uc)) {
continue;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: This can skip tabs and newlines in the middle of the scheme

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, this is intentional because of step 3 in https://url.spec.whatwg.org/#url-parsing:

Remove all ASCII tab or newline from input.

Or do you think this step shouldn't apply for the Builder?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There's also a special case for leading/trailing C0 or space characters:

If url is not given:

  • Set url to a new URL
  • If input contains any leading or trailing C0 control or space, invalid-URL-unit validation error.
  • Remove any leading and trailing C0 control or space from input.

The url parameter is given when we are using the setters. However, IMO the Builder should rather adhere to parsing rules (#22268 (comment)), so this section could apply to our use-case.

But at last, I didn't implement the Remove any leading and trailing C0 control or space from input., since we don't really know where the end of the URL is. Although, the scheme validator could ignore leading CO control or space characters at least...

But I don't really know what the best solution could be :(.

Comment thread ext/uri/php_uri.c
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.

5 participants