Skip to content

[RFC] Add RFC 3986 and WHATWG compliant URL parsing support #14461

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
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
196957b
Create separate lexbor extension
kocsismate May 11, 2025
0334e22
Add RFC 3986 and WHATWG compliant URL parsing support
kocsismate Jun 3, 2024
a09a457
Serialization
kocsismate Oct 22, 2024
3abdb0a
Improve error handling
kocsismate Oct 26, 2024
1445e55
Lot of fixes and added support for equalsTo()
kocsismate Nov 11, 2024
e4de160
Add normalization support
kocsismate Nov 13, 2024
77184ce
SOAP test fixes
kocsismate Nov 13, 2024
3b0449d
Fix some memory leaks
kocsismate Nov 13, 2024
5f90823
Some cleanups
kocsismate Nov 18, 2024
b7011b8
Changes based on discussion
kocsismate Nov 30, 2024
f20842a
Removal of Uri\Uri
kocsismate Dec 30, 2024
c88e92b
A lot of fixes and API changes
kocsismate Jan 6, 2025
c743ead
Updates
kocsismate Feb 5, 2025
19b7180
Add new tests, path fixes
kocsismate Feb 9, 2025
fb0c929
Add more tests for verifying the behavior of withers
kocsismate Feb 15, 2025
cf7ca4e
Fix code review comments
kocsismate Feb 19, 2025
9de1271
A few fixes and improvements after feedback
kocsismate Apr 14, 2025
db3b79d
Test fixes
kocsismate Apr 14, 2025
feefccf
Remove WHATWG non-raw getters
kocsismate Apr 18, 2025
190bbfd
Rename WHATWG getters again
kocsismate Apr 26, 2025
d1df694
Add UriComparisonMode
kocsismate Apr 28, 2025
0d1fc02
Expose $softErrors for Uri\WhatWg\Url::resolve()
kocsismate Apr 30, 2025
df1d0b9
Add SensitiveParameter support
kocsismate May 3, 2025
e8261f7
Proper build support
kocsismate May 19, 2025
7530dd9
Review fixes and serialization update according to the RFC
kocsismate May 20, 2025
b75cc5b
Some extension fixes
kocsismate May 25, 2025
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
Prev Previous commit
Next Next commit
Remove WHATWG non-raw getters
  • Loading branch information
kocsismate committed May 25, 2025
commit feefccfd3a8d688a15481f1d550866158d5621d0
31 changes: 10 additions & 21 deletions ext/uri/php_lexbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,13 @@ const uri_handler_t lexbor_uri_handler = {
return SUCCESS; \
} while (0)

#define LEXBOR_READ_PERCENT_ENCODED_URI_COMPONENT(start, len, read_mode, retval) do { \
#define LEXBOR_READ_ASCII_URI_COMPONENT(start, len, read_mode, retval) do { \
switch (read_mode) { \
case URI_COMPONENT_READ_RAW: \
ZVAL_STRINGL(retval, (const char *) start, len); \
break; \
case URI_COMPONENT_READ_RAW: /* Intentional fallthrough */ \
case URI_COMPONENT_READ_NORMALIZED_UNICODE: /* Intentional fallthrough */ \
case URI_COMPONENT_READ_NORMALIZED_ASCII: { \
lexbor_str_t *str = lexbor_str_create(); \
lxb_url_host_opt_t opt; \
lxb_status_t result = lxb_url_percent_decode(start, start + len, \
str, lexbor_uri->mraw, &opt \
); \
if (result != LXB_STATUS_OK) { \
return FAILURE; \
} \
ZVAL_STRINGL(retval, (const char *) str->data, str->length); \
lexbor_str_destroy(str, lexbor_uri->mraw, true); \
break; \
ZVAL_STRINGL(retval, (const char *) start, len); \
break; \
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
break; \
break; \

} \
EMPTY_SWITCH_DEFAULT_CASE() \
Copy link
Member

Choose a reason for hiding this comment

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

given the EMPTY_SWITCH_DEFAULT_CASE() and the fact that all of the cases fall through, what is the point of even having a switch?

} \
Expand Down Expand Up @@ -291,7 +280,7 @@ static zend_result lexbor_read_username(const uri_internal_t *internal_uri, uri_
lxb_url_t *lexbor_uri = (lxb_url_t *) internal_uri->uri;

if (lexbor_uri->username.length) {
LEXBOR_READ_PERCENT_ENCODED_URI_COMPONENT(lexbor_uri->username.data, lexbor_uri->username.length, read_mode, retval);
LEXBOR_READ_ASCII_URI_COMPONENT(lexbor_uri->username.data, lexbor_uri->username.length, read_mode, retval);
} else {
ZVAL_NULL(retval);
}
Expand All @@ -316,7 +305,7 @@ static zend_result lexbor_read_password(const uri_internal_t *internal_uri, uri_
lxb_url_t *lexbor_uri = (lxb_url_t *) internal_uri->uri;

if (lexbor_uri->password.length > 0) {
LEXBOR_READ_PERCENT_ENCODED_URI_COMPONENT(lexbor_uri->password.data, lexbor_uri->password.length, read_mode, retval);
LEXBOR_READ_ASCII_URI_COMPONENT(lexbor_uri->password.data, lexbor_uri->password.length, read_mode, retval);
} else {
ZVAL_NULL(retval);
}
Expand Down Expand Up @@ -427,9 +416,9 @@ static zend_result lexbor_read_path(const uri_internal_t *internal_uri, uri_comp
lxb_url_t *lexbor_uri = (lxb_url_t *) internal_uri->uri;

if (lexbor_uri->path.opaque) {
LEXBOR_READ_PERCENT_ENCODED_URI_COMPONENT(lexbor_uri->path.str.data, lexbor_uri->path.str.length, read_mode, retval);
LEXBOR_READ_ASCII_URI_COMPONENT(lexbor_uri->path.str.data, lexbor_uri->path.str.length, read_mode, retval);
} else if (lexbor_uri->path.str.length) {
Copy link
Member

Choose a reason for hiding this comment

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

these two blocks look the same - can they be combined?

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, nice catch

LEXBOR_READ_PERCENT_ENCODED_URI_COMPONENT(lexbor_uri->path.str.data, lexbor_uri->path.str.length, read_mode, retval);
LEXBOR_READ_ASCII_URI_COMPONENT(lexbor_uri->path.str.data, lexbor_uri->path.str.length, read_mode, retval);
} else {
ZVAL_EMPTY_STRING(retval);
}
Expand All @@ -454,7 +443,7 @@ static zend_result lexbor_read_query(const uri_internal_t *internal_uri, uri_com
lxb_url_t *lexbor_uri = (lxb_url_t *) internal_uri->uri;

if (lexbor_uri->query.length) {
LEXBOR_READ_PERCENT_ENCODED_URI_COMPONENT(lexbor_uri->query.data, lexbor_uri->query.length, read_mode, retval);
LEXBOR_READ_ASCII_URI_COMPONENT(lexbor_uri->query.data, lexbor_uri->query.length, read_mode, retval);
} else {
ZVAL_NULL(retval);
}
Expand All @@ -479,7 +468,7 @@ static zend_result lexbor_read_fragment(const uri_internal_t *internal_uri, uri_
lxb_url_t *lexbor_uri = (lxb_url_t *) internal_uri->uri;

if (lexbor_uri->fragment.length) {
LEXBOR_READ_PERCENT_ENCODED_URI_COMPONENT(lexbor_uri->fragment.data, lexbor_uri->fragment.length, read_mode, retval);
LEXBOR_READ_ASCII_URI_COMPONENT(lexbor_uri->fragment.data, lexbor_uri->fragment.length, read_mode, retval);
} else {
ZVAL_NULL(retval);
}
Expand Down
15 changes: 0 additions & 15 deletions ext/uri/php_uri.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,11 @@ public function getScheme(): string {}

public function withScheme(string $scheme): static {}

/** @implementation-alias Uri\Rfc3986\Uri::getUsername */
public function getUsername(): ?string {}

/** @implementation-alias Uri\Rfc3986\Uri::getRawUsername */
public function getRawUsername(): ?string {}

public function withUsername(?string $user): static {}

/** @implementation-alias Uri\Rfc3986\Uri::getPassword */
public function getPassword(): ?string {}

/** @implementation-alias Uri\Rfc3986\Uri::getRawPassword */
public function getRawPassword(): ?string {}

Expand All @@ -177,27 +171,18 @@ public function getPort(): ?int {}
/** @implementation-alias Uri\Rfc3986\Uri::withPort */
public function withPort(?int $port): static {}

/** @implementation-alias Uri\Rfc3986\Uri::getPath */
public function getPath(): string {}

/** @implementation-alias Uri\Rfc3986\Uri::getRawPath */
public function getRawPath(): string {}

/** @implementation-alias Uri\Rfc3986\Uri::withPath */
public function withPath(string $path): static {}

/** @implementation-alias Uri\Rfc3986\Uri::getQuery */
public function getQuery(): ?string {}

/** @implementation-alias Uri\Rfc3986\Uri::getRawQuery */
public function getRawQuery(): ?string {}

/** @implementation-alias Uri\Rfc3986\Uri::withQuery */
public function withQuery(?string $query): static {}

/** @implementation-alias Uri\Rfc3986\Uri::getFragment */
public function getFragment(): ?string {}

/** @implementation-alias Uri\Rfc3986\Uri::getRawFragment */
public function getRawFragment(): ?string {}

Expand Down
17 changes: 1 addition & 16 deletions ext/uri/php_uri_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 23 additions & 18 deletions ext/uri/tests/008.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,18 @@ uri
--FILE--
<?php

function callGetters($uri)
function callRfc3986Getters($uri)
{
var_dump($uri->getScheme());
if ($uri instanceof Uri\Rfc3986\Uri) {
var_dump($uri->getRawScheme());
}
var_dump($uri->getRawScheme());
var_dump($uri->getUsername());
var_dump($uri->getRawUsername());
var_dump($uri->getPassword());
var_dump($uri->getRawPassword());
if ($uri instanceof Uri\Rfc3986\Uri) {
var_dump($uri->getHost());
var_dump($uri->getRawHost());
} else {
var_dump($uri->getAsciiHost());
var_dump($uri->getUnicodeHost());
}
var_dump($uri->getUserInfo());
var_dump($uri->getRawUserInfo());
var_dump($uri->getHost());
var_dump($uri->getRawHost());
var_dump($uri->getPort());
var_dump($uri->getPath());
var_dump($uri->getRawPath());
Expand All @@ -31,13 +26,26 @@ function callGetters($uri)
var_dump($uri->getRawFragment());
}

function callWhatWgGetters($uri)
{
var_dump($uri->getScheme());
var_dump($uri->getRawUsername());
var_dump($uri->getRawPassword());
var_dump($uri->getAsciiHost());
var_dump($uri->getUnicodeHost());
var_dump($uri->getPort());
var_dump($uri->getRawPath());
var_dump($uri->getRawQuery());
var_dump($uri->getRawFragment());
}

$uri = Uri\Rfc3986\Uri::parse("https://username:[email protected]:8080/pathname1/pathname2/pathname3?query=true#hash-exists");
callGetters($uri);
callRfc3986Getters($uri);

echo "\n";

$url = Uri\WhatWg\Url::parse("https://username:[email protected]:8080/pathname1/pathname2/pathname3?query=true#hash-exists");
callGetters($url);
callWhatWgGetters($url);

?>
--EXPECT--
Expand All @@ -47,6 +55,8 @@ string(8) "username"
string(8) "username"
string(8) "password"
string(8) "password"
string(17) "username:password"
string(17) "username:password"
string(14) "www.google.com"
string(14) "www.google.com"
int(8080)
Expand All @@ -59,15 +69,10 @@ string(11) "hash-exists"

string(5) "https"
string(8) "username"
string(8) "username"
string(8) "password"
string(8) "password"
string(14) "www.google.com"
string(14) "www.google.com"
int(8080)
string(30) "/pathname1/pathname2/pathname3"
string(30) "/pathname1/pathname2/pathname3"
string(10) "query=true"
string(10) "query=true"
string(11) "hash-exists"
string(11) "hash-exists"
6 changes: 1 addition & 5 deletions ext/uri/tests/024.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Test property mutation - user
Test property mutation - username
--EXTENSIONS--
uri
--FILE--
Expand All @@ -17,17 +17,13 @@ $url6 = $url6->withUsername("user");
var_dump($url2->getRawUsername());
var_dump($url3->getRawUsername());
var_dump($url4->getRawUsername());
var_dump($url4->getUsername());
var_dump($url5->getRawUsername());
var_dump($url5->getUsername());
var_dump($url6->getRawUsername());

?>
--EXPECT--
string(4) "user"
NULL
string(8) "%75s%2Fr"
string(4) "us/r"
string(9) "u%3As%2Fr"
string(5) "u:s/r"
NULL
46 changes: 31 additions & 15 deletions ext/uri/tests/039.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ uri
--FILE--
<?php

function printUri($uri) {
function callRfc3986Getters($uri)
{
var_dump($uri->getScheme());
var_dump($uri->getRawScheme());
var_dump($uri->getUsername());
var_dump($uri->getRawUsername());
var_dump($uri->getPassword());
var_dump($uri->getRawPassword());
if ($uri instanceof Uri\Rfc3986\Uri) {
var_dump($uri->getHost());
var_dump($uri->getRawHost());
} else {
var_dump($uri->getAsciiHost());
var_dump($uri->getUnicodeHost());
}
var_dump($uri->getUserInfo());
var_dump($uri->getRawUserInfo());
var_dump($uri->getHost());
var_dump($uri->getRawHost());
var_dump($uri->getPort());
var_dump($uri->getPath());
var_dump($uri->getRawPath());
var_dump($uri->getQuery());
Expand All @@ -25,35 +26,50 @@ function printUri($uri) {
var_dump($uri->getRawFragment());
}

function callWhatWgGetters($uri)
{
var_dump($uri->getScheme());
var_dump($uri->getRawUsername());
var_dump($uri->getRawPassword());
var_dump($uri->getAsciiHost());
var_dump($uri->getUnicodeHost());
var_dump($uri->getPort());
var_dump($uri->getRawPath());
var_dump($uri->getRawQuery());
var_dump($uri->getRawFragment());
}

$uri = Uri\Rfc3986\Uri::parse("http://%61pple:p%61ss@ex%61mple.com/foob%61r?%61bc=%61bc#%61bc");
printUri($uri);
callRfc3986Getters($uri);

$url = Uri\WhatWg\Url::parse("http://%61pple:p%61ss@ex%61mple.com/foob%61r?%61bc=%61bc#%61bc");
printUri($url);
callWhatWgGetters($url);

?>
--EXPECT--
string(4) "http"
string(4) "http"
string(5) "apple"
string(7) "%61pple"
string(4) "pass"
string(6) "p%61ss"
string(10) "apple:pass"
string(14) "%61pple:p%61ss"
string(11) "example.com"
string(13) "ex%61mple.com"
NULL
string(7) "/foobar"
string(9) "/foob%61r"
string(7) "abc=abc"
string(11) "%61bc=%61bc"
string(3) "abc"
string(5) "%61bc"
string(5) "apple"
string(4) "http"
string(7) "%61pple"
string(4) "pass"
string(6) "p%61ss"
string(11) "example.com"
string(11) "example.com"
string(7) "/foobar"
NULL
string(9) "/foob%61r"
string(7) "abc=abc"
string(11) "%61bc=%61bc"
string(3) "abc"
string(5) "%61bc"
Loading