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
Add UriComparisonMode
  • Loading branch information
kocsismate committed May 25, 2025
commit d1df694cfebd5d59eb83e6b9a410d477ce84168c
2 changes: 1 addition & 1 deletion ext/standard/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ static void *parse_url_clone_uri(void *uri)
return NULL;
}

static void *parse_url_parse_uri(const zend_string *uri_str, const zend_string *base_uri_str, zval *errors)
static void *parse_url_parse_uri(const zend_string *uri_str, const void *base_url, zval *errors)
{
bool has_port;

Expand Down
15 changes: 6 additions & 9 deletions ext/uri/php_lexbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#endif

static zend_result lexbor_init_parser(void);
static void *lexbor_parse_uri(const zend_string *url_str, const zend_string *base_url_str, zval *errors);
static void *lexbor_parse_uri(const zend_string *uri_str, const void *base_url, zval *errors);
static void lexbor_create_invalid_uri_exception(zval *exception_zv, zval *errors);
static void *lexbor_clone_uri(void *uri);
static zend_string *lexbor_uri_to_string(void *uri, uri_recomposition_mode_t recomposition_mode, bool exclude_fragment);
Expand Down Expand Up @@ -521,20 +521,17 @@ static zend_result lexbor_init_parser(void)
return SUCCESS;
}

static void *lexbor_parse_uri(const zend_string *url_str, const zend_string *base_url_str, zval *errors)
static void *lexbor_parse_uri(const zend_string *uri_str, const void *base_url, zval *errors)
{
lexbor_cleanup_parser();

lxb_url_t *url, *base_url = NULL;
lxb_url_t *url, *lexbor_base_url = NULL;

if (base_url_str) {
if ((base_url = lxb_url_parse(lexbor_parser, NULL, (unsigned char *) ZSTR_VAL(base_url_str), ZSTR_LEN(base_url_str))) == NULL) {
fill_errors(errors);
return NULL;
}
if (base_url) {
lexbor_base_url = (lxb_url_t *) base_url;
}

url = lxb_url_parse(lexbor_parser, base_url, (unsigned char *) ZSTR_VAL(url_str), ZSTR_LEN(url_str));
url = lxb_url_parse(lexbor_parser, lexbor_base_url, (unsigned char *) ZSTR_VAL(uri_str), ZSTR_LEN(uri_str));
fill_errors(errors);

return url;
Expand Down
78 changes: 35 additions & 43 deletions ext/uri/php_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ zend_class_entry *rfc3986_uri_ce;
zend_object_handlers rfc3986_uri_object_handlers;
zend_class_entry *whatwg_url_ce;
zend_object_handlers whatwg_uri_object_handlers;
zend_class_entry *uri_comparison_mode_ce;
zend_class_entry *uri_exception_ce;
zend_class_entry *invalid_uri_exception_ce;
zend_class_entry *whatwg_invalid_url_exception_ce;
Expand Down Expand Up @@ -353,13 +354,20 @@ static zend_result pass_errors_by_ref(zval *errors_zv, zval *errors)
}

PHPAPI void php_uri_instantiate_uri(
INTERNAL_FUNCTION_PARAMETERS, const uri_handler_t *handler, const zend_string *uri_str, const zend_string *base_url_str,
INTERNAL_FUNCTION_PARAMETERS, const uri_handler_t *handler, const zend_string *uri_str, const zend_object *base_url_object,
bool is_constructor, zval *errors_zv
) {
zval errors;
ZVAL_UNDEF(&errors);

void *uri = handler->parse_uri(uri_str, base_url_str, is_constructor || errors_zv != NULL ? &errors : NULL);
void *base_url = NULL;
if (base_url_object != NULL) {
uri_internal_t *internal_base_url = uri_internal_from_obj(base_url_object);
URI_CHECK_INITIALIZATION(internal_base_url);
base_url = internal_base_url->uri;
}

void *uri = handler->parse_uri(uri_str, base_url, is_constructor || errors_zv != NULL ? &errors : NULL);
if (UNEXPECTED(uri == NULL)) {
if (is_constructor) {
throw_invalid_uri_exception(handler, &errors);
Expand Down Expand Up @@ -389,25 +397,16 @@ PHPAPI void php_uri_instantiate_uri(

static void create_rfc3986_uri(INTERNAL_FUNCTION_PARAMETERS, bool is_constructor)
{
zend_string *uri_str, *base_url_str = NULL;
zend_string *uri_str;
zend_object *base_url_object = NULL;

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_PATH_STR(uri_str)
Z_PARAM_OPTIONAL
Z_PARAM_PATH_STR_OR_NULL(base_url_str)
Z_PARAM_OBJ_OF_CLASS_OR_NULL(base_url_object, rfc3986_uri_ce)
ZEND_PARSE_PARAMETERS_END();

if (ZSTR_LEN(uri_str) == 0) {
zend_argument_value_error(1, "cannot be empty");
RETURN_THROWS();
}

if (base_url_str && ZSTR_LEN(base_url_str) == 0) {
zend_argument_value_error(2, "cannot be empty");
RETURN_THROWS();
}

php_uri_instantiate_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, &uriparser_uri_handler, uri_str, base_url_str, is_constructor, NULL);
php_uri_instantiate_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, &uriparser_uri_handler, uri_str, base_url_object, is_constructor, NULL);
}

PHP_METHOD(Uri_Rfc3986_Uri, parse)
Expand All @@ -422,27 +421,18 @@ PHP_METHOD(Uri_Rfc3986_Uri, __construct)

static void create_whatwg_uri(INTERNAL_FUNCTION_PARAMETERS, bool is_constructor)
{
zend_string *uri_str, *base_url_str = NULL;
zend_string *uri_str;
zend_object *base_url_object = NULL;
zval *errors = NULL;

ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_PATH_STR(uri_str)
Z_PARAM_OPTIONAL
Z_PARAM_PATH_STR_OR_NULL(base_url_str)
Z_PARAM_OBJ_OF_CLASS_OR_NULL(base_url_object, whatwg_url_ce)
Z_PARAM_ZVAL(errors)
ZEND_PARSE_PARAMETERS_END();

if (ZSTR_LEN(uri_str) == 0) {
zend_argument_value_error(1, "cannot be empty");
RETURN_THROWS();
}

if (base_url_str && ZSTR_LEN(base_url_str) == 0) {
zend_argument_value_error(2, "cannot be empty");
RETURN_THROWS();
}

php_uri_instantiate_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, &lexbor_uri_handler, uri_str, base_url_str, is_constructor, errors);
php_uri_instantiate_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, &lexbor_uri_handler, uri_str, base_url_object, is_constructor, errors);
}

PHP_METHOD(Uri_WhatWg_Url, parse)
Expand Down Expand Up @@ -575,7 +565,7 @@ PHP_METHOD(Uri_Rfc3986_Uri, withFragment)
URI_WITHER_STR_OR_NULL(ZSTR_KNOWN(ZEND_STR_FRAGMENT));
}

static void uri_equals(INTERNAL_FUNCTION_PARAMETERS, zend_object *that_object, bool exclude_fragment)
static void uri_equals(INTERNAL_FUNCTION_PARAMETERS, zend_object *that_object, zend_object *comparison_mode)
{
zend_object *this_object = Z_OBJ_P(ZEND_THIS);
uri_internal_t *this_internal_uri = uri_internal_from_obj(this_object);
Expand All @@ -591,6 +581,14 @@ static void uri_equals(INTERNAL_FUNCTION_PARAMETERS, zend_object *that_object, b
RETURN_FALSE;
}

bool exclude_fragment = true;
if (comparison_mode) {
zval *case_name = zend_enum_fetch_case_name(comparison_mode);
zend_string *comparison_mode_name = Z_STR_P(case_name);

exclude_fragment = ZSTR_VAL(comparison_mode_name)[0] + ZSTR_LEN(comparison_mode_name) == 'E' + sizeof("ExcludeFragment") - 1;
}

zend_string *this_str = this_internal_uri->handler->uri_to_string(
this_internal_uri->uri, URI_RECOMPOSITION_NORMALIZED_ASCII, exclude_fragment);
if (this_str == NULL) {
Expand All @@ -614,15 +612,15 @@ static void uri_equals(INTERNAL_FUNCTION_PARAMETERS, zend_object *that_object, b
PHP_METHOD(Uri_Rfc3986_Uri, equals)
{
zend_object *that_object;
bool exclude_fragment = true;
zend_object *comparison_mode = NULL;

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_OBJ_OF_CLASS(that_object, rfc3986_uri_ce)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(exclude_fragment)
Z_PARAM_OBJ_OF_CLASS(comparison_mode, uri_comparison_mode_ce);
ZEND_PARSE_PARAMETERS_END();

uri_equals(INTERNAL_FUNCTION_PARAM_PASSTHRU, that_object, exclude_fragment);
uri_equals(INTERNAL_FUNCTION_PARAM_PASSTHRU, that_object, comparison_mode);
}

PHP_METHOD(Uri_Rfc3986_Uri, toRawString)
Expand Down Expand Up @@ -671,14 +669,7 @@ PHP_METHOD(Uri_Rfc3986_Uri, resolve)
uri_internal_t *internal_uri = uri_internal_from_obj(this_object);
URI_CHECK_INITIALIZATION(internal_uri);

zend_string *base_uri_str = internal_uri->handler->uri_to_string(
internal_uri->uri, URI_RECOMPOSITION_RAW_ASCII, false); // TODO optimize by not reparsing the base URI
if (base_uri_str == NULL) {
zend_throw_exception_ex(NULL, 0, "Cannot recompose %s to string", ZSTR_VAL(this_object->ce->name));
RETURN_THROWS();
}

php_uri_instantiate_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, internal_uri->handler, uri_str, base_uri_str, true, NULL);
php_uri_instantiate_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, internal_uri->handler, uri_str, this_object, true, NULL);
}

PHP_METHOD(Uri_Rfc3986_Uri, __serialize)
Expand Down Expand Up @@ -832,15 +823,15 @@ PHP_METHOD(Uri_WhatWg_Url, withHost)
PHP_METHOD(Uri_WhatWg_Url, equals)
{
zend_object *that_object;
bool exclude_fragment = true;
zend_object *comparison_mode = NULL;

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_OBJ_OF_CLASS(that_object, whatwg_url_ce)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(exclude_fragment)
Z_PARAM_OBJ_OF_CLASS(comparison_mode, uri_comparison_mode_ce);
ZEND_PARSE_PARAMETERS_END();

uri_equals(INTERNAL_FUNCTION_PARAM_PASSTHRU, that_object, exclude_fragment);
uri_equals(INTERNAL_FUNCTION_PARAM_PASSTHRU, that_object, comparison_mode);
}

PHP_METHOD(Uri_WhatWg_Url, toUnicodeString)
Expand Down Expand Up @@ -955,6 +946,7 @@ static PHP_MINIT_FUNCTION(uri)
whatwg_url_ce = register_class_Uri_WhatWg_Url();
php_uri_implementation_set_object_handlers(whatwg_url_ce, &whatwg_uri_object_handlers);

uri_comparison_mode_ce = register_class_Uri_UriComparisonMode();
uri_exception_ce = register_class_Uri_UriException(zend_ce_exception);
invalid_uri_exception_ce = register_class_Uri_InvalidUriException(uri_exception_ce);
whatwg_invalid_url_exception_ce = register_class_Uri_WhatWg_InvalidUrlException(invalid_uri_exception_ce);
Expand Down
2 changes: 1 addition & 1 deletion ext/uri/php_uri.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ PHPAPI php_uri *php_uri_parse_to_struct(const uri_handler_t *uri_handler, zend_s
PHPAPI void php_uri_struct_free(php_uri *uri);

PHPAPI void php_uri_instantiate_uri(
INTERNAL_FUNCTION_PARAMETERS, const uri_handler_t *handler, const zend_string *uri_str, const zend_string *base_url_str,
INTERNAL_FUNCTION_PARAMETERS, const uri_handler_t *handler, const zend_string *uri_str, const zend_object *base_url_object,
bool is_constructor, zval *errors_zv
);
PHPAPI void php_uri_implementation_set_object_handlers(zend_class_entry *ce, zend_object_handlers *object_handlers);
Expand Down
18 changes: 12 additions & 6 deletions ext/uri/php_uri.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ class UriException extends \Exception
class InvalidUriException extends \Uri\UriException
{
}

enum UriComparisonMode
{
case IncludeFragment;
case ExcludeFragment;
}
}

namespace Uri\Rfc3986 {
/** @strict-properties */
final readonly class Uri
{
public static function parse(string $uri, ?string $baseUrl = null): ?static {}
public static function parse(string $uri, ?\Uri\Rfc3986\Uri $baseUrl = null): ?static {}

public function __construct(string $uri, ?string $baseUrl = null) {}
public function __construct(string $uri, ?\Uri\Rfc3986\Uri $baseUrl = null) {}

public function getScheme(): ?string {}

Expand Down Expand Up @@ -70,7 +76,7 @@ public function getRawFragment(): ?string {}

public function withFragment(?string $fragment): static {}

public function equals(\Uri\Rfc3986\Uri $uri, bool $excludeFragment = true): bool {}
public function equals(\Uri\Rfc3986\Uri $uri, \Uri\UriComparisonMode $comparisonMode = \Uri\UriComparisonMode::ExcludeFragment): bool {}

public function toString(): string {}

Expand Down Expand Up @@ -140,10 +146,10 @@ public function __construct(string $context, \Uri\WhatWg\UrlValidationErrorType
final readonly class Url
{
/** @param array $errors */
public static function parse(string $uri, ?string $baseUrl = null, &$errors = null): ?static {}
public static function parse(string $uri, ?\Uri\WhatWg\Url $baseUrl = null, &$errors = null): ?static {}

/** @param array $softErrors */
public function __construct(string $uri, ?string $baseUrl = null, &$softErrors = null) {}
public function __construct(string $uri, ?\Uri\WhatWg\Url $baseUrl = null, &$softErrors = null) {}

public function getScheme(): string {}

Expand Down Expand Up @@ -189,7 +195,7 @@ public function getFragment(): ?string {}
/** @implementation-alias Uri\Rfc3986\Uri::withFragment */
public function withFragment(?string $fragment): static {}

public function equals(\Uri\WhatWg\Url $url, bool $excludeFragment = true): bool {}
public function equals(\Uri\WhatWg\Url $url, \Uri\UriComparisonMode $comparisonMode = \Uri\UriComparisonMode::ExcludeFragment): bool {}

public function toAsciiString(): string {}

Expand Down
25 changes: 18 additions & 7 deletions ext/uri/php_uri_arginfo.h

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

7 changes: 4 additions & 3 deletions ext/uri/php_uri_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern zend_class_entry *rfc3986_uri_ce;
extern zend_object_handlers rfc3986_uri_object_handlers;
extern zend_class_entry *whatwg_url_ce;
extern zend_object_handlers whatwg_uri_object_handlers;
extern zend_class_entry *uri_comparison_mode_ce;
extern zend_class_entry *uri_exception_ce;
extern zend_class_entry *invalid_uri_exception_ce;
extern zend_class_entry *whatwg_invalid_url_exception_ce;
Expand All @@ -44,7 +45,7 @@ typedef struct uri_handler_t {
const char *name;

zend_result (*init_parser)(void);
void *(*parse_uri)(const zend_string *uri_str, const zend_string *base_url_str, zval *errors);
void *(*parse_uri)(const zend_string *uri_str, const void *base_url, zval *errors);
void (*create_invalid_uri_exception)(zval *exception_zv, zval *errors);
void *(*clone_uri)(void *uri);
zend_string *(*uri_to_string)(void *uri, uri_recomposition_mode_t recomposition_mode, bool exclude_fragment);
Expand All @@ -63,11 +64,11 @@ typedef struct uri_object_t {
zend_object std;
} uri_object_t;

static inline uri_object_t *uri_object_from_obj(zend_object *object) {
static inline uri_object_t *uri_object_from_obj(const zend_object *object) {
return (uri_object_t*)((char*)(object) - XtOffsetOf(uri_object_t, std));
}

static inline uri_internal_t *uri_internal_from_obj(zend_object *object) {
static inline uri_internal_t *uri_internal_from_obj(const zend_object *object) {
return &(uri_object_from_obj(object)->internal);
}

Expand Down
Loading