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 normalization support
  • Loading branch information
kocsismate committed May 25, 2025
commit e4de160a5d3e5f56294c47dec9f0088f221e412a
2 changes: 1 addition & 1 deletion ext/soap/soap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2790,7 +2790,7 @@ PHP_METHOD(SoapClient, __doRequest)
if (make_http_soap_request(this_ptr, buf, location, action, version, uri_parser_name, NULL)) {
RETURN_EMPTY_STRING();
}
} else if (make_http_soap_request(this_ptr, buf, location, action, uri_parser_name, version,
} else if (make_http_soap_request(this_ptr, buf, location, action, version, uri_parser_name,
return_value)) {
return;
}
Expand Down
6 changes: 6 additions & 0 deletions ext/standard/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,11 @@ static void *parse_url_parse_uri(const zend_string *uri_str, const zend_string *
return php_url_parse_ex2(ZSTR_VAL(uri_str), ZSTR_LEN(uri_str), &has_port);
}

static zend_result parse_url_normalize_uri(void *uri)
{
ZEND_UNREACHABLE();
}

static zend_string *parse_url_uri_to_string(void *uri, bool exclude_fragment)
{
ZEND_UNREACHABLE();
Expand All @@ -481,6 +486,7 @@ const uri_handler_t parse_url_uri_handler = {
parse_url_parse_uri,
parse_url_get_uri_ce,
parse_url_clone_uri,
parse_url_normalize_uri,
parse_url_uri_to_string,
parse_url_free_uri,
parse_url_destroy_parser,
Expand Down
7 changes: 7 additions & 0 deletions ext/uri/php_lexbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ 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 zend_class_entry *lexbor_get_uri_ce(void);
static void *lexbor_clone_uri(void *uri);
static zend_result lexbor_normalize_uri(void *uri);
static zend_string *lexbor_uri_to_string(void *uri, bool exclude_fragment);
static void lexbor_free_uri(void *uri);
static zend_result lexbor_destroy_parser(void);
Expand All @@ -42,6 +43,7 @@ const uri_handler_t lexbor_uri_handler = {
lexbor_parse_uri,
lexbor_get_uri_ce,
lexbor_clone_uri,
lexbor_normalize_uri,
lexbor_uri_to_string,
lexbor_free_uri,
lexbor_destroy_parser,
Expand Down Expand Up @@ -381,6 +383,11 @@ static void *lexbor_clone_uri(void *uri)
return lxb_url_clone(lexbor_parser->mraw, lexbor_uri);
}

static zend_result lexbor_normalize_uri(void *uri)
{
return SUCCESS;
}

static lxb_status_t lexbor_serialize_callback(const lxb_char_t *data, size_t length, void *ctx)
{
smart_str *uri_str = (smart_str *) ctx;
Expand Down
62 changes: 56 additions & 6 deletions ext/uri/php_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ zend_class_entry *whatwg_uri_ce;
zend_object_handlers whatwg_uri_object_handlers;
zend_class_entry *uri_exception_ce;
zend_class_entry *uninitialized_uri_exception_ce;
zend_class_entry *uri_operation_exception_ce;
zend_class_entry *invalid_uri_exception_ce;
zend_class_entry *whatwg_error_ce;

Expand Down Expand Up @@ -382,11 +383,11 @@ PHP_METHOD(Uri_Rfc3986Uri, equalsTo)
zend_object *that_object;
bool exclude_fragment = true;

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_OBJ_OF_CLASS(that_object, uri_interface_ce)
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_OBJ_OF_CLASS(that_object, uri_interface_ce)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(exclude_fragment)
ZEND_PARSE_PARAMETERS_END();
Z_PARAM_BOOL(exclude_fragment)
ZEND_PARSE_PARAMETERS_END();

zend_object *this_object = Z_OBJ_P(ZEND_THIS);
uri_internal_t *this_internal_uri = uri_internal_from_obj(this_object);
Expand All @@ -411,6 +412,53 @@ PHP_METHOD(Uri_Rfc3986Uri, equalsTo)
zend_string_release(that_str);
}

PHP_METHOD(Uri_Rfc3986Uri, normalize)
{
ZEND_PARSE_PARAMETERS_NONE();

zend_object *this_object = Z_OBJ_P(ZEND_THIS);
uri_internal_t *internal_uri = uri_internal_from_obj(this_object);
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, this_object);

zend_object *new_object = uri_clone_obj_handler(this_object);
if (UNEXPECTED(EG(exception) != NULL)) {
RETURN_THROWS();
}
uri_internal_t *new_internal_uri = uri_internal_from_obj(new_object);
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, this_object);

if (UNEXPECTED(internal_uri->handler->normalize_uri(new_internal_uri->uri) == FAILURE)) {
zend_throw_error(uri_operation_exception_ce, "Failed to normalize %s", ZSTR_VAL(this_object->ce->name));
RETURN_THROWS();
}

ZVAL_OBJ(return_value, new_object);
}

PHP_METHOD(Uri_Rfc3986Uri, toNormalizedString)
{
ZEND_PARSE_PARAMETERS_NONE();

zend_object *object = Z_OBJ_P(ZEND_THIS);
uri_internal_t *internal_uri = uri_internal_from_obj(object);
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, object);

void *new_uri = internal_uri->handler->clone_uri(internal_uri->uri);
if (UNEXPECTED(new_uri == NULL)) {
zend_throw_error(uri_operation_exception_ce, "Failed to normalize %s", ZSTR_VAL(object->ce->name));
RETURN_THROWS();
}

if (UNEXPECTED(internal_uri->handler->normalize_uri(new_uri) == FAILURE)) {
zend_throw_error(uri_operation_exception_ce, "Failed to normalize %s", ZSTR_VAL(object->ce->name));
internal_uri->handler->free_uri(new_uri);
RETURN_THROWS();
}

RETVAL_STR(internal_uri->handler->uri_to_string(new_uri, false));
internal_uri->handler->free_uri(new_uri);
}

PHP_METHOD(Uri_Rfc3986Uri, __toString)
{
ZEND_PARSE_PARAMETERS_NONE();
Expand Down Expand Up @@ -445,7 +493,7 @@ static void uri_restore_custom_properties(zend_object *object, uri_internal_t *i
}

zend_update_property_ex(object->ce, object, prop_name, prop_val);
if (EG(exception)) {
if (UNEXPECTED(EG(exception) != NULL)) {
break;
}
} ZEND_HASH_FOREACH_END();
Expand Down Expand Up @@ -659,7 +707,7 @@ static zend_object *uri_clone_obj_handler(zend_object *object)

void *uri = internal_uri->handler->clone_uri(internal_uri->uri);
if (UNEXPECTED(uri == NULL)) {
zend_throw_error(NULL, "Failed to clone %s", ZSTR_VAL(object->ce->name));
zend_throw_error(uri_operation_exception_ce, "Failed to clone %s", ZSTR_VAL(object->ce->name));
return &new_uri_object->std;
}

Expand Down Expand Up @@ -724,6 +772,7 @@ zend_result uri_handler_register(const uri_handler_t *uri_handler)
ZEND_ASSERT(uri_handler->parse_uri != NULL);
ZEND_ASSERT(uri_handler->get_uri_ce != NULL);
ZEND_ASSERT(uri_handler->clone_uri != NULL);
ZEND_ASSERT(uri_handler->normalize_uri != NULL);
ZEND_ASSERT(uri_handler->uri_to_string != NULL);
ZEND_ASSERT(uri_handler->free_uri != NULL);
ZEND_ASSERT(uri_handler->destroy_parser != NULL);
Expand All @@ -746,6 +795,7 @@ static PHP_MINIT_FUNCTION(uri)

uri_exception_ce = register_class_Uri_UriException(zend_ce_exception);
uninitialized_uri_exception_ce = register_class_Uri_UninitializedUriException(uri_exception_ce);
uri_operation_exception_ce = register_class_Uri_UriOperationException(uri_exception_ce);
invalid_uri_exception_ce = register_class_Uri_InvalidUriException(uri_exception_ce);
whatwg_error_ce = register_class_Uri_WhatWgError();

Expand Down
19 changes: 19 additions & 0 deletions ext/uri/php_uri.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class UninitializedUriException extends \Uri\UriException
{
}

/** @strict-properties */
class UriOperationException extends \Uri\UriException
{
}

/** @strict-properties */
class InvalidUriException extends \Uri\UriException
{
Expand Down Expand Up @@ -136,6 +141,10 @@ public function withFragment(?string $fragment): static {}

public function equalsTo(\Uri\UriInterface $uri, bool $excludeFragment = true): bool {}

public function normalize(): static {}

public function toNormalizedString(): string {}

public function __toString(): string {}
}

Expand Down Expand Up @@ -197,6 +206,10 @@ public function withFragment(?string $fragment): static {}

public function equalsTo(\Uri\UriInterface $uri, bool $excludeFragment = true): bool {}

public function normalize(): static {}

public function toNormalizedString(): string {}

public function __toString(): string {}

public function __serialize(): array;
Expand Down Expand Up @@ -279,6 +292,12 @@ public function withFragment(?string $fragment): static {}
/** @implementation-alias Uri\Rfc3986Uri::equalsTo */
public function equalsTo(\Uri\UriInterface $uri, bool $excludeFragment = true): bool {}

/** @implementation-alias Uri\Rfc3986Uri::normalize */
public function normalize(): static {}

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

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

Expand Down
39 changes: 35 additions & 4 deletions ext/uri/php_uri_arginfo.h

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

5 changes: 5 additions & 0 deletions ext/uri/php_uri_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern zend_class_entry *whatwg_uri_ce;
extern zend_object_handlers whatwg_uri_object_handlers;
extern zend_class_entry *uri_exception_ce;
extern zend_class_entry *uninitialized_uri_exception_ce;
extern zend_class_entry *uri_operation_exception_ce;
extern zend_class_entry *invalid_uri_exception_ce;
extern zend_class_entry *whatwg_error_ce;

Expand All @@ -34,6 +35,7 @@ typedef struct uri_handler_t {
void *(*parse_uri)(const zend_string *uri_str, const zend_string *base_url_str, zval *errors);
zend_class_entry *(*get_uri_ce)(void);
void *(*clone_uri)(void *uri);
zend_result (*normalize_uri)(void *uri);
zend_string *(*uri_to_string)(void *uri, bool exclude_fragment);
void (*free_uri)(void *uri);
zend_result (*destroy_parser)(void);
Expand Down Expand Up @@ -130,6 +132,9 @@ void throw_invalid_uri_exception(zval *errors);
const uri_property_handler_t *property_handler = uri_property_handler_from_internal_uri(internal_uri, property_name); \
ZEND_ASSERT(property_handler != NULL); \
zend_object *new_object = uri_clone_obj_handler(Z_OBJ_P(ZEND_THIS)); \
if (UNEXPECTED(EG(exception) != NULL)) { \
RETURN_THROWS(); \
} \
uri_internal_t *new_internal_uri = uri_internal_from_obj(new_object); \
URI_CHECK_INITIALIZATION_RETURN_THROWS(new_internal_uri, Z_OBJ_P(ZEND_THIS)); \
if (property_handler->write_func == NULL) { \
Expand Down
13 changes: 13 additions & 0 deletions ext/uri/php_uriparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static zend_result uriparser_init_parser(void);
static void *uriparser_parse_uri(const zend_string *uri_str, const zend_string *base_uri_str, zval *errors);
static zend_class_entry *uriparser_get_uri_ce(void);
static void *uriparser_clone_uri(void *uri);
static zend_result uriparser_normalize_uri(void *uri);
static zend_string *uriparser_uri_to_string(void *uri, bool exclude_fragment);
static void uriparser_free_uri(void *uri);
static zend_result uriparser_destroy_parser(void);
Expand All @@ -35,6 +36,7 @@ const uri_handler_t uriparser_uri_handler = {
uriparser_parse_uri,
uriparser_get_uri_ce,
uriparser_clone_uri,
uriparser_normalize_uri,
uriparser_uri_to_string,
uriparser_free_uri,
uriparser_destroy_parser,
Expand Down Expand Up @@ -559,6 +561,17 @@ static void *uriparser_clone_uri(void *uri)
return new_uriparser_uri;
}

static zend_result uriparser_normalize_uri(void *uri)
{
UriUriA *uriparser_uri = (UriUriA *) uri;

if (uriNormalizeSyntaxA(uriparser_uri) != URI_SUCCESS) {
return FAILURE;
}

return SUCCESS;
}

static zend_string *uriparser_uri_to_string(void *uri, bool exclude_fragment)
{
UriUriA *uriparser_uri = (UriUriA *) uri;
Expand Down
Loading