Skip to content

Commit

Permalink
Some cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Nov 18, 2024
1 parent 2ac633f commit 8e21e67
Show file tree
Hide file tree
Showing 32 changed files with 415 additions and 168 deletions.
28 changes: 13 additions & 15 deletions ext/filter/tests/062.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ filter
--FILE--
<?php

function validateUrls()
function validateUrls(string $parserName)
{
$values = [
'http://example.com/index.html',
Expand Down Expand Up @@ -53,27 +53,25 @@ function validateUrls()
];

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

var_dump(filter_var("qwe", FILTER_VALIDATE_URL));
var_dump(filter_var("http://qwe", FILTER_VALIDATE_URL));
var_dump(filter_var("http://", FILTER_VALIDATE_URL));
var_dump(filter_var("/tmp/test", FILTER_VALIDATE_URL));
var_dump(filter_var("http://www.example.com", FILTER_VALIDATE_URL));
var_dump(filter_var("http://www.example.com", FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED));
var_dump(filter_var("http://www.example.com/path/at/the/server/", FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED));
var_dump(filter_var("http://www.example.com/index.html", FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED));
var_dump(filter_var("http://www.example.com/index.php?a=b&c=d", FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED));
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]));
}

echo "RFC3986:\n";
ini_set("uri.default_handler", "rfc3986");
validateUrls();
validateUrls(Uri\URI_PARSER_RFC3986);

echo "\nWHATWG:\n";
ini_set("uri.default_handler", "whatwg");
validateUrls();
validateUrls(Uri\URI_PARSER_WHATWG);

echo "Done\n";
?>
Expand Down
8 changes: 1 addition & 7 deletions ext/uri/php_lexbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ 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 @@ -43,7 +42,7 @@ const uri_handler_t lexbor_uri_handler = {
lexbor_parse_uri,
lexbor_get_uri_ce,
lexbor_clone_uri,
lexbor_normalize_uri,
NULL,
lexbor_uri_to_string,
lexbor_free_uri,
lexbor_destroy_parser,
Expand Down Expand Up @@ -383,11 +382,6 @@ 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
71 changes: 62 additions & 9 deletions ext/uri/php_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ PHPAPI void php_uri_instantiate_uri(
ZEND_ASSERT(Z_TYPE(errors) == IS_UNDEF);

if (!is_constructor) {
object_init_ex(return_value, handler->get_uri_ce());
object_init_ex(return_value, Z_CE_P(ZEND_THIS));
}

uri_object_t *uri_object = Z_URI_OBJECT_P(is_constructor ? ZEND_THIS : return_value);
Expand Down Expand Up @@ -255,7 +255,7 @@ static void create_rfc3986_uri(INTERNAL_FUNCTION_PARAMETERS, bool is_constructor
php_uri_instantiate_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, &uriparser_uri_handler, uri_str, base_url_str, is_constructor, false);
}

PHP_METHOD(Uri_Rfc3986Uri, create)
PHP_METHOD(Uri_Rfc3986Uri, parse)
{
create_rfc3986_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
}
Expand Down Expand Up @@ -288,7 +288,7 @@ static void create_whatwg_uri(INTERNAL_FUNCTION_PARAMETERS, bool is_constructor)
php_uri_instantiate_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, &lexbor_uri_handler, uri_str, base_url_str, is_constructor, true);
}

PHP_METHOD(Uri_WhatWgUri, create)
PHP_METHOD(Uri_WhatWgUri, parse)
{
create_whatwg_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
}
Expand Down Expand Up @@ -403,13 +403,57 @@ PHP_METHOD(Uri_Rfc3986Uri, equalsTo)
RETURN_FALSE;
}

zend_string *this_str = this_internal_uri->handler->uri_to_string(this_internal_uri->uri, exclude_fragment);
void *this_uri, *that_uri;

if (this_internal_uri->handler->normalize_uri != NULL) {
this_uri = this_internal_uri->handler->clone_uri(this_internal_uri->uri);
if (UNEXPECTED(this_uri == NULL)) {
zend_throw_error(uri_operation_exception_ce, "Failed to normalize %s", ZSTR_VAL(this_object->ce->name));
RETURN_THROWS();
}

zend_result result = this_internal_uri->handler->normalize_uri(this_uri);
if (UNEXPECTED(result == FAILURE)) {
zend_throw_error(uri_operation_exception_ce, "Failed to normalize %s", ZSTR_VAL(this_object->ce->name));
this_internal_uri->handler->free_uri(this_uri);
RETURN_THROWS();
}
} else {
this_uri = this_internal_uri->uri;
}

if (that_internal_uri->handler->normalize_uri != NULL) {
that_uri = that_internal_uri->handler->clone_uri(that_internal_uri->uri);
if (UNEXPECTED(that_uri == NULL)) {
zend_throw_error(uri_operation_exception_ce, "Failed to normalize %s", ZSTR_VAL(that_object->ce->name));
RETURN_THROWS();
}

zend_result result = that_internal_uri->handler->normalize_uri(that_uri);
if (UNEXPECTED(result == FAILURE)) {
zend_throw_error(uri_operation_exception_ce, "Failed to normalize %s", ZSTR_VAL(that_object->ce->name));
that_internal_uri->handler->free_uri(that_uri);
RETURN_THROWS();
}
} else {
that_uri = that_internal_uri->uri;
}

zend_string *this_str = this_internal_uri->handler->uri_to_string(this_internal_uri->uri, exclude_fragment); // TODO what happens if the __clone() method is overridden?
zend_string *that_str = that_internal_uri->handler->uri_to_string(that_internal_uri->uri, exclude_fragment);

RETVAL_BOOL(zend_string_equals(this_str, that_str));

zend_string_release(this_str);
zend_string_release(that_str);

if (this_internal_uri->handler->normalize_uri != NULL) {
this_internal_uri->handler->free_uri(this_uri);
}

if (that_internal_uri->handler->normalize_uri != NULL) {
that_internal_uri->handler->free_uri(that_uri);
}
}

PHP_METHOD(Uri_Rfc3986Uri, normalize)
Expand All @@ -420,6 +464,10 @@ PHP_METHOD(Uri_Rfc3986Uri, normalize)
uri_internal_t *internal_uri = uri_internal_from_obj(this_object);
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, this_object);

if (internal_uri->handler->normalize_uri == NULL) {
RETURN_COPY(ZEND_THIS);
}

zend_object *new_object = uri_clone_obj_handler(this_object);
if (UNEXPECTED(EG(exception) != NULL)) {
zend_object_release(new_object);
Expand All @@ -446,6 +494,10 @@ PHP_METHOD(Uri_Rfc3986Uri, toNormalizedString)
uri_internal_t *internal_uri = uri_internal_from_obj(object);
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, object);

if (internal_uri->handler->normalize_uri == NULL) {
RETURN_STR(internal_uri->handler->uri_to_string(internal_uri->uri, false));
}

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));
Expand Down Expand Up @@ -513,12 +565,13 @@ PHP_METHOD(Uri_Rfc3986Uri, __unserialize)
zend_object *object = Z_OBJ_P(ZEND_THIS);
uri_internal_t *internal_uri = uri_internal_from_obj(object);

zend_string *str = zend_string_init("https://example.com", sizeof("https://example.com") - 1, false);
zend_string *str = zend_string_init("https://example.com", sizeof("https://example.com") - 1, false); // TODO set URI components from ht

zval errors;
ZVAL_UNDEF(&errors);

internal_uri->handler = uri_handler_by_name(URI_PARSER_RFC3986, sizeof(URI_PARSER_RFC3986) - 1);
// TODO free current URI if any
internal_uri->uri = internal_uri->handler->parse_uri(str, NULL, &errors);
if (internal_uri->uri == NULL) {
throw_invalid_uri_exception(&errors);
Expand All @@ -543,12 +596,13 @@ PHP_METHOD(Uri_WhatWgUri, __unserialize)
zend_object *object = Z_OBJ_P(ZEND_THIS);
uri_internal_t *internal_uri = uri_internal_from_obj(object);

zend_string *str = zend_string_init("https://example.com", sizeof("https://example.com") - 1, false);
zend_string *str = zend_string_init("https://example.com", sizeof("https://example.com") - 1, false); // TODO set URI components from ht

zval errors;
ZVAL_UNDEF(&errors);

internal_uri->handler = uri_handler_by_name(URI_PARSER_WHATWG, sizeof(URI_PARSER_WHATWG) - 1);
// TODO free current URI if any
internal_uri->uri = internal_uri->handler->parse_uri(str, NULL, &errors);
if (internal_uri->uri == NULL) {
throw_invalid_uri_exception(&errors);
Expand Down Expand Up @@ -708,7 +762,7 @@ static zend_object *uri_clone_obj_handler(zend_object *object)

new_uri_object->internal.handler = internal_uri->handler;

void *uri = internal_uri->handler->clone_uri(internal_uri->uri);
void *uri = internal_uri->handler->clone_uri(internal_uri->uri); // TODO what happens if the __clone() method is overridden?
if (UNEXPECTED(uri == NULL)) {
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 @@ -773,9 +827,8 @@ zend_result uri_handler_register(const uri_handler_t *uri_handler)
ZEND_ASSERT(uri_handler->name != NULL);
ZEND_ASSERT(uri_handler->init_parser != NULL);
ZEND_ASSERT(uri_handler->parse_uri != NULL);
ZEND_ASSERT(uri_handler->get_uri_ce != NULL);
ZEND_ASSERT(uri_handler->get_uri_ce != NULL); // TODO unused handler, maybe remove?
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 Down
6 changes: 3 additions & 3 deletions ext/uri/php_uri.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class InvalidUriException extends \Uri\UriException
}

/** @strict-properties */
final readonly class WhatWgError
readonly class WhatWgError
{
/** @cvalue LXB_URL_ERROR_TYPE_DOMAIN_TO_ASCII */
public const int ERROR_TYPE_DOMAIN_TO_ASCII = UNKNOWN;
Expand Down Expand Up @@ -168,7 +168,7 @@ public function __toString(): string {}
/** @virtual */
private ?string $fragment;

public static function create(string $uri, ?string $baseUrl = null): ?static {}
public static function parse(string $uri, ?string $baseUrl = null): ?static {}

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

Expand Down Expand Up @@ -237,7 +237,7 @@ public function __unserialize(array $data): void;
/** @virtual */
private ?string $fragment;

public static function create(string $uri, ?string $baseUrl = null): static|array {}
public static function parse(string $uri, ?string $baseUrl = null): static|array {}

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

Expand Down
16 changes: 8 additions & 8 deletions ext/uri/php_uri_arginfo.h

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

2 changes: 1 addition & 1 deletion ext/uri/php_uriparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ static zend_result uriparser_write_port(uri_internal_t *internal_uri, zval *valu
uriparser_append_authority(internal_uri, &uri_str);
uriparser_append_host(internal_uri, &uri_str);

if (Z_TYPE_P(value) == IS_LONG && Z_LVAL_P(value) != 0) {
if (Z_TYPE_P(value) == IS_LONG) {
smart_str_appendc(&uri_str, ':');
smart_str_append_long(&uri_str, Z_LVAL_P(value));
}
Expand Down
8 changes: 4 additions & 4 deletions ext/uri/tests/001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ uri

$t1 = hrtime(true);
for ($i = 0; $i < 1000; $i++) {
Uri\Rfc3986Uri::create("https://example.com");
Uri\Rfc3986Uri::parse("https://example.com");
}
$t2 = hrtime(true);

$t3 = hrtime(true);
for ($i = 0; $i < 1000; $i++) {
Uri\WhatWgUri::create("https://example.com/");
Uri\WhatWgUri::parse("https://example.com/");
}
$t4 = hrtime(true);

Expand All @@ -25,12 +25,12 @@ $t6 = hrtime(true);

echo "RFC 3986 parser: https://example.com\n";
var_dump(($t2 - $t1) / 1_000_000_000);
var_dump(Uri\Rfc3986Uri::create("https://example.com"));
var_dump(Uri\Rfc3986Uri::parse("https://example.com"));
echo "------------------------\n";

echo "WHATWG parser: https://example.com\n";
var_dump(($t4 - $t3) / 1_000_000_000);
var_dump(Uri\WhatWgUri::create("https://example.com"));
var_dump(Uri\WhatWgUri::parse("https://example.com"));
echo "------------------------\n";

echo "PHP parser: https://example.com\n";
Expand Down
Loading

0 comments on commit 8e21e67

Please sign in to comment.