Skip to content

Commit 8e21e67

Browse files
committed
Some cleanups
1 parent 2ac633f commit 8e21e67

32 files changed

+415
-168
lines changed

Diff for: ext/filter/tests/062.phpt

+13-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ filter
55
--FILE--
66
<?php
77

8-
function validateUrls()
8+
function validateUrls(string $parserName)
99
{
1010
$values = [
1111
'http://example.com/index.html',
@@ -53,27 +53,25 @@ function validateUrls()
5353
];
5454

5555
foreach ($values as $value) {
56-
var_dump(filter_var($value, FILTER_VALIDATE_URL));
56+
var_dump(filter_var($value, FILTER_VALIDATE_URL, ["parser" => $parserName]));
5757
}
5858

59-
var_dump(filter_var("qwe", FILTER_VALIDATE_URL));
60-
var_dump(filter_var("http://qwe", FILTER_VALIDATE_URL));
61-
var_dump(filter_var("http://", FILTER_VALIDATE_URL));
62-
var_dump(filter_var("/tmp/test", FILTER_VALIDATE_URL));
63-
var_dump(filter_var("http://www.example.com", FILTER_VALIDATE_URL));
64-
var_dump(filter_var("http://www.example.com", FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED));
65-
var_dump(filter_var("http://www.example.com/path/at/the/server/", FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED));
66-
var_dump(filter_var("http://www.example.com/index.html", FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED));
67-
var_dump(filter_var("http://www.example.com/index.php?a=b&c=d", FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED));
59+
var_dump(filter_var("qwe", FILTER_VALIDATE_URL, ["parser" => $parserName]));
60+
var_dump(filter_var("http://qwe", FILTER_VALIDATE_URL, ["parser" => $parserName]));
61+
var_dump(filter_var("http://", FILTER_VALIDATE_URL, ["parser" => $parserName]));
62+
var_dump(filter_var("/tmp/test", FILTER_VALIDATE_URL, ["parser" => $parserName]));
63+
var_dump(filter_var("http://www.example.com", FILTER_VALIDATE_URL, ["parser" => $parserName]));
64+
var_dump(filter_var("http://www.example.com", FILTER_VALIDATE_URL, ["parser" => $parserName, "flags" => FILTER_FLAG_PATH_REQUIRED]));
65+
var_dump(filter_var("http://www.example.com/path/at/the/server/", FILTER_VALIDATE_URL, ["parser" => $parserName, "flags" => FILTER_FLAG_PATH_REQUIRED]));
66+
var_dump(filter_var("http://www.example.com/index.html", FILTER_VALIDATE_URL, ["parser" => $parserName, "flags" => FILTER_FLAG_QUERY_REQUIRED]));
67+
var_dump(filter_var("http://www.example.com/index.php?a=b&c=d", FILTER_VALIDATE_URL, ["parser" => $parserName, "flags" => FILTER_FLAG_QUERY_REQUIRED]));
6868
}
6969

7070
echo "RFC3986:\n";
71-
ini_set("uri.default_handler", "rfc3986");
72-
validateUrls();
71+
validateUrls(Uri\URI_PARSER_RFC3986);
7372

7473
echo "\nWHATWG:\n";
75-
ini_set("uri.default_handler", "whatwg");
76-
validateUrls();
74+
validateUrls(Uri\URI_PARSER_WHATWG);
7775

7876
echo "Done\n";
7977
?>

Diff for: ext/uri/php_lexbor.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ static zend_result lexbor_init_parser(void);
2727
static void *lexbor_parse_uri(const zend_string *url_str, const zend_string *base_url_str, zval *errors);
2828
static zend_class_entry *lexbor_get_uri_ce(void);
2929
static void *lexbor_clone_uri(void *uri);
30-
static zend_result lexbor_normalize_uri(void *uri);
3130
static zend_string *lexbor_uri_to_string(void *uri, bool exclude_fragment);
3231
static void lexbor_free_uri(void *uri);
3332
static zend_result lexbor_destroy_parser(void);
@@ -43,7 +42,7 @@ const uri_handler_t lexbor_uri_handler = {
4342
lexbor_parse_uri,
4443
lexbor_get_uri_ce,
4544
lexbor_clone_uri,
46-
lexbor_normalize_uri,
45+
NULL,
4746
lexbor_uri_to_string,
4847
lexbor_free_uri,
4948
lexbor_destroy_parser,
@@ -383,11 +382,6 @@ static void *lexbor_clone_uri(void *uri)
383382
return lxb_url_clone(lexbor_parser->mraw, lexbor_uri);
384383
}
385384

386-
static zend_result lexbor_normalize_uri(void *uri)
387-
{
388-
return SUCCESS;
389-
}
390-
391385
static lxb_status_t lexbor_serialize_callback(const lxb_char_t *data, size_t length, void *ctx)
392386
{
393387
smart_str *uri_str = (smart_str *) ctx;

Diff for: ext/uri/php_uri.c

+62-9
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ PHPAPI void php_uri_instantiate_uri(
224224
ZEND_ASSERT(Z_TYPE(errors) == IS_UNDEF);
225225

226226
if (!is_constructor) {
227-
object_init_ex(return_value, handler->get_uri_ce());
227+
object_init_ex(return_value, Z_CE_P(ZEND_THIS));
228228
}
229229

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

258-
PHP_METHOD(Uri_Rfc3986Uri, create)
258+
PHP_METHOD(Uri_Rfc3986Uri, parse)
259259
{
260260
create_rfc3986_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
261261
}
@@ -288,7 +288,7 @@ static void create_whatwg_uri(INTERNAL_FUNCTION_PARAMETERS, bool is_constructor)
288288
php_uri_instantiate_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, &lexbor_uri_handler, uri_str, base_url_str, is_constructor, true);
289289
}
290290

291-
PHP_METHOD(Uri_WhatWgUri, create)
291+
PHP_METHOD(Uri_WhatWgUri, parse)
292292
{
293293
create_whatwg_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
294294
}
@@ -403,13 +403,57 @@ PHP_METHOD(Uri_Rfc3986Uri, equalsTo)
403403
RETURN_FALSE;
404404
}
405405

406-
zend_string *this_str = this_internal_uri->handler->uri_to_string(this_internal_uri->uri, exclude_fragment);
406+
void *this_uri, *that_uri;
407+
408+
if (this_internal_uri->handler->normalize_uri != NULL) {
409+
this_uri = this_internal_uri->handler->clone_uri(this_internal_uri->uri);
410+
if (UNEXPECTED(this_uri == NULL)) {
411+
zend_throw_error(uri_operation_exception_ce, "Failed to normalize %s", ZSTR_VAL(this_object->ce->name));
412+
RETURN_THROWS();
413+
}
414+
415+
zend_result result = this_internal_uri->handler->normalize_uri(this_uri);
416+
if (UNEXPECTED(result == FAILURE)) {
417+
zend_throw_error(uri_operation_exception_ce, "Failed to normalize %s", ZSTR_VAL(this_object->ce->name));
418+
this_internal_uri->handler->free_uri(this_uri);
419+
RETURN_THROWS();
420+
}
421+
} else {
422+
this_uri = this_internal_uri->uri;
423+
}
424+
425+
if (that_internal_uri->handler->normalize_uri != NULL) {
426+
that_uri = that_internal_uri->handler->clone_uri(that_internal_uri->uri);
427+
if (UNEXPECTED(that_uri == NULL)) {
428+
zend_throw_error(uri_operation_exception_ce, "Failed to normalize %s", ZSTR_VAL(that_object->ce->name));
429+
RETURN_THROWS();
430+
}
431+
432+
zend_result result = that_internal_uri->handler->normalize_uri(that_uri);
433+
if (UNEXPECTED(result == FAILURE)) {
434+
zend_throw_error(uri_operation_exception_ce, "Failed to normalize %s", ZSTR_VAL(that_object->ce->name));
435+
that_internal_uri->handler->free_uri(that_uri);
436+
RETURN_THROWS();
437+
}
438+
} else {
439+
that_uri = that_internal_uri->uri;
440+
}
441+
442+
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?
407443
zend_string *that_str = that_internal_uri->handler->uri_to_string(that_internal_uri->uri, exclude_fragment);
408444

409445
RETVAL_BOOL(zend_string_equals(this_str, that_str));
410446

411447
zend_string_release(this_str);
412448
zend_string_release(that_str);
449+
450+
if (this_internal_uri->handler->normalize_uri != NULL) {
451+
this_internal_uri->handler->free_uri(this_uri);
452+
}
453+
454+
if (that_internal_uri->handler->normalize_uri != NULL) {
455+
that_internal_uri->handler->free_uri(that_uri);
456+
}
413457
}
414458

415459
PHP_METHOD(Uri_Rfc3986Uri, normalize)
@@ -420,6 +464,10 @@ PHP_METHOD(Uri_Rfc3986Uri, normalize)
420464
uri_internal_t *internal_uri = uri_internal_from_obj(this_object);
421465
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, this_object);
422466

467+
if (internal_uri->handler->normalize_uri == NULL) {
468+
RETURN_COPY(ZEND_THIS);
469+
}
470+
423471
zend_object *new_object = uri_clone_obj_handler(this_object);
424472
if (UNEXPECTED(EG(exception) != NULL)) {
425473
zend_object_release(new_object);
@@ -446,6 +494,10 @@ PHP_METHOD(Uri_Rfc3986Uri, toNormalizedString)
446494
uri_internal_t *internal_uri = uri_internal_from_obj(object);
447495
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, object);
448496

497+
if (internal_uri->handler->normalize_uri == NULL) {
498+
RETURN_STR(internal_uri->handler->uri_to_string(internal_uri->uri, false));
499+
}
500+
449501
void *new_uri = internal_uri->handler->clone_uri(internal_uri->uri);
450502
if (UNEXPECTED(new_uri == NULL)) {
451503
zend_throw_error(uri_operation_exception_ce, "Failed to normalize %s", ZSTR_VAL(object->ce->name));
@@ -513,12 +565,13 @@ PHP_METHOD(Uri_Rfc3986Uri, __unserialize)
513565
zend_object *object = Z_OBJ_P(ZEND_THIS);
514566
uri_internal_t *internal_uri = uri_internal_from_obj(object);
515567

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

518570
zval errors;
519571
ZVAL_UNDEF(&errors);
520572

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

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

548601
zval errors;
549602
ZVAL_UNDEF(&errors);
550603

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

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

711-
void *uri = internal_uri->handler->clone_uri(internal_uri->uri);
765+
void *uri = internal_uri->handler->clone_uri(internal_uri->uri); // TODO what happens if the __clone() method is overridden?
712766
if (UNEXPECTED(uri == NULL)) {
713767
zend_throw_error(uri_operation_exception_ce, "Failed to clone %s", ZSTR_VAL(object->ce->name));
714768
return &new_uri_object->std;
@@ -773,9 +827,8 @@ zend_result uri_handler_register(const uri_handler_t *uri_handler)
773827
ZEND_ASSERT(uri_handler->name != NULL);
774828
ZEND_ASSERT(uri_handler->init_parser != NULL);
775829
ZEND_ASSERT(uri_handler->parse_uri != NULL);
776-
ZEND_ASSERT(uri_handler->get_uri_ce != NULL);
830+
ZEND_ASSERT(uri_handler->get_uri_ce != NULL); // TODO unused handler, maybe remove?
777831
ZEND_ASSERT(uri_handler->clone_uri != NULL);
778-
ZEND_ASSERT(uri_handler->normalize_uri != NULL);
779832
ZEND_ASSERT(uri_handler->uri_to_string != NULL);
780833
ZEND_ASSERT(uri_handler->free_uri != NULL);
781834
ZEND_ASSERT(uri_handler->destroy_parser != NULL);

Diff for: ext/uri/php_uri.stub.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class InvalidUriException extends \Uri\UriException
3838
}
3939

4040
/** @strict-properties */
41-
final readonly class WhatWgError
41+
readonly class WhatWgError
4242
{
4343
/** @cvalue LXB_URL_ERROR_TYPE_DOMAIN_TO_ASCII */
4444
public const int ERROR_TYPE_DOMAIN_TO_ASCII = UNKNOWN;
@@ -168,7 +168,7 @@ public function __toString(): string {}
168168
/** @virtual */
169169
private ?string $fragment;
170170

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

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

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

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

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

Diff for: ext/uri/php_uri_arginfo.h

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ext/uri/php_uriparser.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ static zend_result uriparser_write_port(uri_internal_t *internal_uri, zval *valu
341341
uriparser_append_authority(internal_uri, &uri_str);
342342
uriparser_append_host(internal_uri, &uri_str);
343343

344-
if (Z_TYPE_P(value) == IS_LONG && Z_LVAL_P(value) != 0) {
344+
if (Z_TYPE_P(value) == IS_LONG) {
345345
smart_str_appendc(&uri_str, ':');
346346
smart_str_append_long(&uri_str, Z_LVAL_P(value));
347347
}

Diff for: ext/uri/tests/001.phpt

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ uri
77

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

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

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

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

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

3636
echo "PHP parser: https://example.com\n";

0 commit comments

Comments
 (0)