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
A lot of fixes and API changes
  • Loading branch information
kocsismate committed May 25, 2025
commit c88e92bf61a7ad8facc060d16c5567c5aa108474
102 changes: 88 additions & 14 deletions ext/dom/lexbor/lexbor/url/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ typedef enum {
}
lxb_url_map_type_t;

typedef enum {
LXB_URL_HOST_OPT_UNDEF = 0 << 0,
LXB_URL_HOST_OPT_NOT_SPECIAL = 1 << 0,
LXB_URL_HOST_OPT_DECODE = 1 << 1,
LXB_URL_HOST_OPT_IDNA = 1 << 2
}
lxb_url_host_opt_t;

typedef struct {
lexbor_mraw_t *mraw;
lexbor_str_t *str;
Expand Down Expand Up @@ -599,11 +591,6 @@ lxb_url_opaque_host_parse(lxb_url_parser_t *parser, const lxb_char_t *data,
const lxb_char_t *end, lxb_url_host_t *host,
lexbor_mraw_t *mraw);

static lxb_status_t
lxb_url_percent_decode(const lxb_char_t *data, const lxb_char_t *end,
lexbor_str_t *str, lexbor_mraw_t *mraw,
lxb_url_host_opt_t *opt);

static const lxb_char_t *
lxb_url_path_part_by_index(const lxb_url_t *url, size_t index,
size_t *out_length);
Expand Down Expand Up @@ -3961,7 +3948,7 @@ lxb_url_opaque_host_parse(lxb_url_parser_t *parser, const lxb_char_t *data,
LXB_URL_MAP_C0, false);
}

static lxb_status_t
lxb_status_t
lxb_url_percent_decode(const lxb_char_t *data, const lxb_char_t *end,
lexbor_str_t *str, lexbor_mraw_t *mraw,
lxb_url_host_opt_t *opt)
Expand Down Expand Up @@ -4523,6 +4510,93 @@ lxb_url_serialize(const lxb_url_t *url, lexbor_serialize_cb_f cb, void *ctx,
return LXB_STATUS_OK;
}

lxb_status_t
lxb_url_serialize_unicode(lxb_unicode_idna_t *idna, const lxb_url_t *url, lexbor_serialize_cb_f cb, void *ctx,
bool exclude_fragment)
{
lxb_status_t status;
const lexbor_str_t *str;
lxb_char_t *p;
lxb_char_t buf[LXB_URL_BUFFER_NUM_SIZE];

static const lexbor_str_t colon_str = lexbor_str(":");
static const lexbor_str_t dsol_str = lexbor_str("//");
static const lexbor_str_t at_str = lexbor_str("@");
static const lexbor_str_t dt_str = lexbor_str("/.");
static const lexbor_str_t qm_str = lexbor_str("?");
static const lexbor_str_t hs_str = lexbor_str("#");

/* Scheme. */

str = &url->scheme.name;

lexbor_serialize_write(cb, str->data, str->length, ctx, status);
lexbor_serialize_write(cb, colon_str.data, colon_str.length, ctx, status);

/* Host. */

if (url->host.type != LXB_URL_HOST_TYPE__UNDEF) {
lexbor_serialize_write(cb, dsol_str.data, dsol_str.length, ctx, status);

if (lxb_url_includes_credentials(url)) {
lexbor_serialize_write(cb, url->username.data, url->username.length,
ctx, status);

if (url->password.length != 0) {
lexbor_serialize_write(cb, colon_str.data, colon_str.length,
ctx, status);
lexbor_serialize_write(cb, url->password.data,
url->password.length, ctx, status);
}

lexbor_serialize_write(cb, at_str.data, at_str.length, ctx, status);
}

status = lxb_url_serialize_host_unicode(idna, &url->host, cb, ctx);
if (status != LXB_STATUS_OK) {
return status;
}

if (url->has_port) {
lexbor_serialize_write(cb, colon_str.data, colon_str.length,
ctx, status);

p = buf + lexbor_conv_int64_to_data((int64_t) url->port,
buf, LXB_URL_BUFFER_NUM_SIZE);

lexbor_serialize_write(cb, buf, p - buf, ctx, status);
}
}
else if (!url->path.opaque && url->path.str.length > 1) {
str = &url->path.str;

if (str->data[0] == '/' && str->data[1] == '/') {
lexbor_serialize_write(cb, dt_str.data, dt_str.length, ctx, status);
}
}

status = lxb_url_serialize_path(&url->path, cb, ctx);
if (status != LXB_STATUS_OK) {
return status;
}

if (url->query.data != NULL) {
lexbor_serialize_write(cb, qm_str.data, qm_str.length,
ctx, status);
lexbor_serialize_write(cb, url->query.data, url->query.length,
ctx, status);
}

if (!exclude_fragment && url->fragment.data != NULL) {
lexbor_serialize_write(cb, hs_str.data, hs_str.length,
ctx, status);
lexbor_serialize_write(cb, url->fragment.data, url->fragment.length,
ctx, status);
}

return LXB_STATUS_OK;
}

lxb_status_t
lxb_url_serialize_scheme(const lxb_url_t *url,
lexbor_serialize_cb_f cb, void *ctx)
Expand Down
21 changes: 21 additions & 0 deletions ext/dom/lexbor/lexbor/url/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@ LXB_API lxb_status_t
lxb_url_serialize(const lxb_url_t *url, lexbor_serialize_cb_f cb, void *ctx,
bool exclude_fragment);

LXB_API lxb_status_t
lxb_url_serialize_unicode(lxb_unicode_idna_t *idna, const lxb_url_t *url, lexbor_serialize_cb_f cb, void *ctx,
bool exclude_fragment);

LXB_API lxb_status_t
lxb_url_serialize_scheme(const lxb_url_t *url,
lexbor_serialize_cb_f cb, void *ctx);
Expand Down Expand Up @@ -462,6 +466,23 @@ lxb_url_serialize_fragment(const lxb_url_t *url,
LXB_API lxb_url_t *
lxb_url_clone(lexbor_mraw_t *mraw, lxb_url_t *url);

/*
* Below are auxiliary functions.
*/

typedef enum {
LXB_URL_HOST_OPT_UNDEF = 0 << 0,
LXB_URL_HOST_OPT_NOT_SPECIAL = 1 << 0,
LXB_URL_HOST_OPT_DECODE = 1 << 1,
LXB_URL_HOST_OPT_IDNA = 1 << 2
}
lxb_url_host_opt_t;

LXB_API lxb_status_t
lxb_url_percent_decode(const lxb_char_t *data, const lxb_char_t *end,
lexbor_str_t *str, lexbor_mraw_t *mraw,
lxb_url_host_opt_t *opt);

/*
* Inline functions.
*/
Expand Down
12 changes: 6 additions & 6 deletions ext/filter/logical_filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,14 +620,14 @@ void php_filter_validate_url(http://github.com/PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
}

zval scheme;
result = php_uri_get_scheme(internal_uri, &scheme);
result = php_uri_get_scheme(internal_uri, URI_COMPONENT_READ_RAW, &scheme);
if (result == FAILURE) {
php_uri_free(internal_uri);
RETURN_VALIDATION_FAILED
}

zval host;
result = php_uri_get_host(internal_uri, &host);
result = php_uri_get_host(internal_uri, URI_COMPONENT_READ_RAW, &host);
if (result == FAILURE) {
zval_ptr_dtor(&scheme);
php_uri_free(internal_uri);
Expand Down Expand Up @@ -669,7 +669,7 @@ void php_filter_validate_url(http://github.com/PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
}

zval path;
result = php_uri_get_path(internal_uri, &path);
result = php_uri_get_path(internal_uri, URI_COMPONENT_READ_RAW, &path);
if (result == FAILURE) {
php_uri_free(internal_uri);
zval_ptr_dtor(&scheme);
Expand All @@ -678,7 +678,7 @@ void php_filter_validate_url(http://github.com/PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
}

zval query;
result = php_uri_get_query(internal_uri, &query);
result = php_uri_get_query(internal_uri, URI_COMPONENT_READ_RAW, &query);
if (result == FAILURE) {
php_uri_free(internal_uri);
zval_ptr_dtor(&scheme);
Expand Down Expand Up @@ -707,14 +707,14 @@ void php_filter_validate_url(http://github.com/PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
zval_ptr_dtor(&query);

zval user;
result = php_uri_get_user(internal_uri, &user);
result = php_uri_get_user(internal_uri, URI_COMPONENT_READ_RAW, &user);
if (result == FAILURE) {
php_uri_free(internal_uri);
RETURN_VALIDATION_FAILED
}

zval password;
result = php_uri_get_password(internal_uri, &password);
result = php_uri_get_password(internal_uri, URI_COMPONENT_READ_RAW, &password);
if (result == FAILURE) {
php_uri_free(internal_uri);
zval_ptr_dtor(&user);
Expand Down
2 changes: 1 addition & 1 deletion ext/openssl/xp_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2627,7 +2627,7 @@ static char *php_openssl_get_url_name(const char *resourcename,
}

zval host_zv;
zend_result result = php_uri_get_host(internal_uri, &host_zv);
zend_result result = php_uri_get_host(internal_uri, URI_COMPONENT_READ_RAW, &host_zv);
if (result == SUCCESS && Z_TYPE(host_zv) == IS_STRING) {
const char * host = Z_STRVAL(host_zv);
char * url_name = NULL;
Expand Down
Loading