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
Changes based on discussion
  • Loading branch information
kocsismate committed May 25, 2025
commit b7011b839b5818e0595590aea763d0950f035fb4
2 changes: 1 addition & 1 deletion ext/dom/lexbor/lexbor/url/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extern "C" {


typedef enum {
LXB_URL_ERROR_TYPE_DOMAIN_TO_ASCII = 0x00,
LXB_URL_ERROR_TYPE_DOMAIN_TO_ASCII = 0x00,
LXB_URL_ERROR_TYPE_DOMAIN_TO_UNICODE,
LXB_URL_ERROR_TYPE_DOMAIN_INVALID_CODE_POINT,
LXB_URL_ERROR_TYPE_HOST_INVALID_CODE_POINT,
Expand Down
2 changes: 1 addition & 1 deletion ext/filter/logical_filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ void php_filter_validate_url(http://github.com/PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
zval *option_val;
zend_string *parser_name;
int parser_name_set;
FETCH_STR_OPTION(parser_name, "parser");
FETCH_STR_OPTION(parser_name, "uri_parser_class");

uri_handler_t *uri_handler = php_uri_get_handler(parser_name_set ? parser_name : NULL);
if (uri_handler == NULL) {
Expand Down
24 changes: 12 additions & 12 deletions ext/filter/tests/062.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,25 @@ function validateUrls(string $parserName)
];

foreach ($values as $value) {
var_dump(filter_var($value, FILTER_VALIDATE_URL, ["parser" => $parserName]));
var_dump(filter_var($value, FILTER_VALIDATE_URL, ["uri_parser_class" => $parserName]));
}

var_dump(filter_var("qwe", FILTER_VALIDATE_URL, ["parser" => $parserName]));
var_dump(filter_var("http://qwe", FILTER_VALIDATE_URL, ["parser" => $parserName]));
var_dump(filter_var("http://", FILTER_VALIDATE_URL, ["parser" => $parserName]));
var_dump(filter_var("/tmp/test", FILTER_VALIDATE_URL, ["parser" => $parserName]));
var_dump(filter_var("http://www.example.com", FILTER_VALIDATE_URL, ["parser" => $parserName]));
var_dump(filter_var("http://www.example.com", FILTER_VALIDATE_URL, ["parser" => $parserName, "flags" => FILTER_FLAG_PATH_REQUIRED]));
var_dump(filter_var("http://www.example.com/path/at/the/server/", FILTER_VALIDATE_URL, ["parser" => $parserName, "flags" => FILTER_FLAG_PATH_REQUIRED]));
var_dump(filter_var("http://www.example.com/index.html", FILTER_VALIDATE_URL, ["parser" => $parserName, "flags" => FILTER_FLAG_QUERY_REQUIRED]));
var_dump(filter_var("http://www.example.com/index.php?a=b&c=d", FILTER_VALIDATE_URL, ["parser" => $parserName, "flags" => FILTER_FLAG_QUERY_REQUIRED]));
var_dump(filter_var("qwe", FILTER_VALIDATE_URL, ["uri_parser_class" => $parserName]));
var_dump(filter_var("http://qwe", FILTER_VALIDATE_URL, ["uri_parser_class" => $parserName]));
var_dump(filter_var("http://", FILTER_VALIDATE_URL, ["uri_parser_class" => $parserName]));
var_dump(filter_var("/tmp/test", FILTER_VALIDATE_URL, ["uri_parser_class" => $parserName]));
var_dump(filter_var("http://www.example.com", FILTER_VALIDATE_URL, ["uri_parser_class" => $parserName]));
var_dump(filter_var("http://www.example.com", FILTER_VALIDATE_URL, ["uri_parser_class" => $parserName, "flags" => FILTER_FLAG_PATH_REQUIRED]));
var_dump(filter_var("http://www.example.com/path/at/the/server/", FILTER_VALIDATE_URL, ["uri_parser_class" => $parserName, "flags" => FILTER_FLAG_PATH_REQUIRED]));
var_dump(filter_var("http://www.example.com/index.html", FILTER_VALIDATE_URL, ["uri_parser_class" => $parserName, "flags" => FILTER_FLAG_QUERY_REQUIRED]));
var_dump(filter_var("http://www.example.com/index.php?a=b&c=d", FILTER_VALIDATE_URL, ["uri_parser_class" => $parserName, "flags" => FILTER_FLAG_QUERY_REQUIRED]));
}

echo "RFC3986:\n";
validateUrls(Uri\URI_PARSER_RFC3986);
validateUrls(Uri\Rfc3986Uri::class);

echo "\nWHATWG:\n";
validateUrls(Uri\URI_PARSER_WHATWG);
validateUrls(Uri\WhatWgUri::class);

echo "Done\n";
?>
Expand Down
6 changes: 3 additions & 3 deletions ext/soap/php_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ int make_http_soap_request(zval *this_ptr,
zend_string *location,
char *soapaction,
int soap_version,
const zend_string *uri_parser_name,
const zend_string *uri_parser_class,
zval *return_value)
{
zend_string *request;
Expand Down Expand Up @@ -436,7 +436,7 @@ int make_http_soap_request(zval *this_ptr,
}

if (location != NULL && ZSTR_VAL(location)[0] != '\000') {
uri_handler_t *uri_handler = php_uri_get_handler(uri_parser_name);
uri_handler_t *uri_handler = php_uri_get_handler(uri_parser_class);
if (uri_handler == NULL) {
zend_argument_value_error(6, "must be a valid URI parser name");
return FALSE;
Expand Down Expand Up @@ -1160,7 +1160,7 @@ int make_http_soap_request(zval *this_ptr,
char *loc;

if ((loc = get_http_header_value(ZSTR_VAL(http_headers), "Location:")) != NULL) {
uri_handler_t *uri_handler = php_uri_get_handler(uri_parser_name);
uri_handler_t *uri_handler = php_uri_get_handler(uri_parser_class);
if (uri_handler == NULL) {
zend_argument_value_error(6, "must be a valid URI parser name");
return FALSE;
Expand Down
2 changes: 1 addition & 1 deletion ext/soap/php_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int make_http_soap_request(zval *this_ptr,
zend_string *location,
char *soapaction,
int soap_version,
const zend_string *uri_parser_name,
const zend_string *uri_parser_class,
zval *response);

int proxy_authentication(zval* this_ptr, smart_str* soap_headers);
Expand Down
8 changes: 4 additions & 4 deletions ext/soap/soap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2769,7 +2769,7 @@ PHP_METHOD(SoapClient, __getLastResponseHeaders)
/* {{{ SoapClient::__doRequest() */
PHP_METHOD(SoapClient, __doRequest)
{
zend_string *buf, *location, *uri_parser_name = NULL;
zend_string *buf, *location, *uri_parser_class = NULL;
char *action;
size_t action_size;
zend_long version;
Expand All @@ -2780,17 +2780,17 @@ PHP_METHOD(SoapClient, __doRequest)
&buf,
&location,
&action, &action_size,
&version, &one_way, &uri_parser_name) == FAILURE) {
&version, &one_way, &uri_parser_class) == FAILURE) {
RETURN_THROWS();
}
if (SOAP_GLOBAL(features) & SOAP_WAIT_ONE_WAY_CALLS) {
one_way = 0;
}
if (one_way) {
if (make_http_soap_request(this_ptr, buf, location, action, version, uri_parser_name, NULL)) {
if (make_http_soap_request(this_ptr, buf, location, action, version, uri_parser_class, NULL)) {
RETURN_EMPTY_STRING();
}
} else if (make_http_soap_request(this_ptr, buf, location, action, version, uri_parser_name,
} else if (make_http_soap_request(this_ptr, buf, location, action, version, uri_parser_class,
return_value)) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion ext/soap/soap.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ public function __getLastRequestHeaders(): ?string {}
public function __getLastResponseHeaders(): ?string {}

/** @tentative-return-type */
public function __doRequest(string $request, string $location, string $action, int $version, bool $oneWay = false, ?string $uriParserName = null): ?string {}
public function __doRequest(string $request, string $location, string $action, int $version, bool $oneWay = false, ?string $uriParserClass = null): ?string {}
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 number of tests that you changed, is this a breaking change? If so, it needs to be noted in the RFC.

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, that's a good idea


/** @tentative-return-type */
public function __setCookie(string $name, ?string $value = null): void {}
Expand Down
4 changes: 2 additions & 2 deletions ext/soap/soap_arginfo.h

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

8 changes: 6 additions & 2 deletions ext/uri/php_lexbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ static void fill_errors(zval *errors)
while ((lxb_error = lexbor_array_obj_pop(&lexbor_parser->log->list)) != NULL) {
zval error;
object_init_ex(&error, whatwg_error_ce);
zend_update_property_string(whatwg_error_ce, Z_OBJ(error), "position", sizeof("position") - 1, (const char *) lxb_error->data);
zend_update_property_long(whatwg_error_ce, Z_OBJ(error), "errorCode", sizeof("errorCode") - 1, lxb_error->id);
zend_update_property_string(whatwg_error_ce, Z_OBJ(error), "context", sizeof("context") - 1, (const char *) lxb_error->data);

zval error_type;
zend_string *error_str = zend_string_init((const char *) lxb_error->data,strlen((const char *) lxb_error->data), false);
zend_enum_new(&error_type, whatwg_error_type_ce, error_str, NULL); // todo case name
zend_update_property(whatwg_error_ce, Z_OBJ(error), "type", sizeof("type") - 1, &error_type);

add_next_index_zval(errors, &error);
}
Expand Down
72 changes: 50 additions & 22 deletions ext/uri/php_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "php.h"
#include "Zend/zend_interfaces.h"
#include "Zend/zend_exceptions.h"
#include "Zend/zend_enum.h"
#include "main/php_ini.h"

#include "php_uri.h"
Expand All @@ -38,6 +39,7 @@ 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_type_ce;
zend_class_entry *whatwg_error_ce;

static zend_array uri_handlers;
Expand Down Expand Up @@ -181,25 +183,23 @@ PHPAPI void php_uri_free(uri_internal_t *internal_uri)
efree(internal_uri);
}

PHP_METHOD(Uri_WhatWgError, __construct)
PHP_METHOD(Uri_WhatWg_WhatWgError, __construct)
{
zend_string *uri, *position;
zend_long error;
zend_string *context;
zval *type;

ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_STR(uri)
Z_PARAM_STR(position)
Z_PARAM_LONG(error)
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR(context)
Z_PARAM_OBJECT_OF_CLASS(type, whatwg_error_type_ce)
ZEND_PARSE_PARAMETERS_END();

zend_update_property_str(whatwg_error_ce, Z_OBJ_P(ZEND_THIS), "uri", sizeof("uri") - 1, uri);
zend_update_property_str(whatwg_error_ce, Z_OBJ_P(ZEND_THIS), "position", sizeof("position") - 1, position);
zend_update_property_long(whatwg_error_ce, Z_OBJ_P(ZEND_THIS), "error", sizeof("error") - 1, error);
zend_update_property_str(whatwg_error_ce, Z_OBJ_P(ZEND_THIS), "context", sizeof("context") - 1, context);
zend_update_property(whatwg_error_ce, Z_OBJ_P(ZEND_THIS), "type", sizeof("type") - 1, type);
}

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,
bool is_constructor, bool return_errors
bool is_constructor, zval *errors_zv
) {
zval errors;
ZVAL_UNDEF(&errors);
Expand All @@ -211,8 +211,16 @@ PHPAPI void php_uri_instantiate_uri(
zval_ptr_dtor(&errors);
RETURN_THROWS();
} else {
if (return_errors && Z_TYPE(errors) == IS_ARRAY) {
RETURN_ZVAL(&errors, false, false);
if (errors_zv != NULL && Z_TYPE(errors) == IS_ARRAY) {
errors_zv = zend_try_array_init(errors_zv);
if (!errors_zv) {
RETURN_THROWS();
}

zval *error;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(errors_zv), error) {
zend_hash_next_index_insert(Z_ARRVAL_P(errors_zv), error);
} ZEND_HASH_FOREACH_END();
}

zval_ptr_dtor(&errors);
Expand All @@ -235,11 +243,13 @@ 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;
zval *errors;

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_ZVAL(errors)
ZEND_PARSE_PARAMETERS_END();

if (ZSTR_LEN(uri_str) == 0) {
Expand All @@ -252,7 +262,7 @@ static void create_rfc3986_uri(INTERNAL_FUNCTION_PARAMETERS, bool is_constructor
RETURN_THROWS();
}

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

PHP_METHOD(Uri_Rfc3986Uri, parse)
Expand All @@ -268,11 +278,13 @@ PHP_METHOD(Uri_Rfc3986Uri, __construct)
static void create_whatwg_uri(INTERNAL_FUNCTION_PARAMETERS, bool is_constructor)
{
zend_string *uri_str, *base_url_str = NULL;
zval *errors = NULL;

ZEND_PARSE_PARAMETERS_START(1, 2)
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_ZVAL(errors)
ZEND_PARSE_PARAMETERS_END();

if (ZSTR_LEN(uri_str) == 0) {
Expand All @@ -285,7 +297,7 @@ static void create_whatwg_uri(INTERNAL_FUNCTION_PARAMETERS, bool is_constructor)
RETURN_THROWS();
}

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

PHP_METHOD(Uri_WhatWgUri, parse)
Expand Down Expand Up @@ -378,7 +390,7 @@ PHP_METHOD(Uri_Rfc3986Uri, withFragment)
URI_WITHER_STR(ZSTR_KNOWN(ZEND_STR_FRAGMENT));
}

PHP_METHOD(Uri_Rfc3986Uri, equalsTo)
PHP_METHOD(Uri_Rfc3986Uri, equals)
{
zend_object *that_object;
bool exclude_fragment = true;
Expand Down Expand Up @@ -514,7 +526,7 @@ PHP_METHOD(Uri_Rfc3986Uri, toNormalizedString)
internal_uri->handler->free_uri(new_uri);
}

PHP_METHOD(Uri_Rfc3986Uri, __toString)
PHP_METHOD(Uri_Rfc3986Uri, toString)
{
ZEND_PARSE_PARAMETERS_NONE();

Expand All @@ -525,6 +537,23 @@ PHP_METHOD(Uri_Rfc3986Uri, __toString)
RETURN_STR(internal_uri->handler->uri_to_string(internal_uri->uri, false));
}

PHP_METHOD(Uri_Rfc3986Uri, resolve)
{
zend_string *uri_str;

ZEND_PARSE_PARAMETERS_START(1, 1) \
Z_PARAM_PATH_STR(uri_str) \
ZEND_PARSE_PARAMETERS_END(); \

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_string *base_uri_str = internal_uri->handler->uri_to_string(internal_uri->uri, false); // TODO optimize by not reparsing the base URI

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

PHP_METHOD(Uri_Rfc3986Uri, __serialize)
{
ZEND_PARSE_PARAMETERS_NONE();
Expand Down Expand Up @@ -839,9 +868,7 @@ zend_result uri_handler_register(const uri_handler_t *uri_handler)

static PHP_MINIT_FUNCTION(uri)
{
register_php_uri_symbols(module_number);

uri_interface_ce = register_class_Uri_UriInterface(zend_ce_stringable);
uri_interface_ce = register_class_Uri_Uri();

rfc3986_uri_ce = register_class_Uri_Rfc3986Uri(uri_interface_ce);
php_uri_implementation_set_object_handlers(rfc3986_uri_ce, &rfc3986_uri_object_handlers);
Expand All @@ -853,7 +880,8 @@ static PHP_MINIT_FUNCTION(uri)
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();
whatwg_error_ce = register_class_Uri_WhatWg_WhatWgError();
whatwg_error_type_ce = register_class_Uri_WhatWg_WhatWgErrorType();

zend_hash_init(&uri_handlers, 4, NULL, ZVAL_PTR_DTOR, true);

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 @@ -40,7 +40,7 @@ PHPAPI void php_uri_free(uri_internal_t *internal_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,
bool is_constructor, bool return_errors
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
Loading