Skip to content

Commit 54fd0bb

Browse files
committed
Add normalization support
1 parent 4a982a2 commit 54fd0bb

File tree

11 files changed

+230
-11
lines changed

11 files changed

+230
-11
lines changed

ext/soap/soap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2792,7 +2792,7 @@ PHP_METHOD(SoapClient, __doRequest)
27922792
if (make_http_soap_request(this_ptr, buf, location, action, version, uri_parser_name, NULL)) {
27932793
RETURN_EMPTY_STRING();
27942794
}
2795-
} else if (make_http_soap_request(this_ptr, buf, location, action, uri_parser_name, version,
2795+
} else if (make_http_soap_request(this_ptr, buf, location, action, version, uri_parser_name,
27962796
return_value)) {
27972797
return;
27982798
}

ext/standard/url.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,11 @@ static void *parse_url_parse_uri(const zend_string *uri_str, const zend_string *
465465
return php_url_parse_ex2(ZSTR_VAL(uri_str), ZSTR_LEN(uri_str), &has_port);
466466
}
467467

468+
static zend_result parse_url_normalize_uri(void *uri)
469+
{
470+
ZEND_UNREACHABLE();
471+
}
472+
468473
static zend_string *parse_url_uri_to_string(void *uri, bool exclude_fragment)
469474
{
470475
ZEND_UNREACHABLE();
@@ -483,6 +488,7 @@ const uri_handler_t parse_url_uri_handler = {
483488
parse_url_parse_uri,
484489
parse_url_get_uri_ce,
485490
parse_url_clone_uri,
491+
parse_url_normalize_uri,
486492
parse_url_uri_to_string,
487493
parse_url_free_uri,
488494
parse_url_destroy_parser,

ext/uri/php_lexbor.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ 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);
3031
static zend_string *lexbor_uri_to_string(void *uri, bool exclude_fragment);
3132
static void lexbor_free_uri(void *uri);
3233
static zend_result lexbor_destroy_parser(void);
@@ -42,6 +43,7 @@ const uri_handler_t lexbor_uri_handler = {
4243
lexbor_parse_uri,
4344
lexbor_get_uri_ce,
4445
lexbor_clone_uri,
46+
lexbor_normalize_uri,
4547
lexbor_uri_to_string,
4648
lexbor_free_uri,
4749
lexbor_destroy_parser,
@@ -381,6 +383,11 @@ static void *lexbor_clone_uri(void *uri)
381383
return lxb_url_clone(lexbor_parser->mraw, lexbor_uri);
382384
}
383385

386+
static zend_result lexbor_normalize_uri(void *uri)
387+
{
388+
return SUCCESS;
389+
}
390+
384391
static lxb_status_t lexbor_serialize_callback(const lxb_char_t *data, size_t length, void *ctx)
385392
{
386393
smart_str *uri_str = (smart_str *) ctx;

ext/uri/php_uri.c

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ zend_class_entry *whatwg_uri_ce;
3636
zend_object_handlers whatwg_uri_object_handlers;
3737
zend_class_entry *uri_exception_ce;
3838
zend_class_entry *uninitialized_uri_exception_ce;
39+
zend_class_entry *uri_operation_exception_ce;
3940
zend_class_entry *invalid_uri_exception_ce;
4041
zend_class_entry *whatwg_error_ce;
4142

@@ -382,11 +383,11 @@ PHP_METHOD(Uri_Rfc3986Uri, equalsTo)
382383
zend_object *that_object;
383384
bool exclude_fragment = true;
384385

385-
ZEND_PARSE_PARAMETERS_START(1, 2)
386-
Z_PARAM_OBJ_OF_CLASS(that_object, uri_interface_ce)
386+
ZEND_PARSE_PARAMETERS_START(1, 2)
387+
Z_PARAM_OBJ_OF_CLASS(that_object, uri_interface_ce)
387388
Z_PARAM_OPTIONAL
388-
Z_PARAM_BOOL(exclude_fragment)
389-
ZEND_PARSE_PARAMETERS_END();
389+
Z_PARAM_BOOL(exclude_fragment)
390+
ZEND_PARSE_PARAMETERS_END();
390391

391392
zend_object *this_object = Z_OBJ_P(ZEND_THIS);
392393
uri_internal_t *this_internal_uri = uri_internal_from_obj(this_object);
@@ -411,6 +412,53 @@ PHP_METHOD(Uri_Rfc3986Uri, equalsTo)
411412
zend_string_release(that_str);
412413
}
413414

415+
PHP_METHOD(Uri_Rfc3986Uri, normalize)
416+
{
417+
ZEND_PARSE_PARAMETERS_NONE();
418+
419+
zend_object *this_object = Z_OBJ_P(ZEND_THIS);
420+
uri_internal_t *internal_uri = uri_internal_from_obj(this_object);
421+
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, this_object);
422+
423+
zend_object *new_object = uri_clone_obj_handler(this_object);
424+
if (UNEXPECTED(EG(exception) != NULL)) {
425+
RETURN_THROWS();
426+
}
427+
uri_internal_t *new_internal_uri = uri_internal_from_obj(new_object);
428+
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, this_object);
429+
430+
if (UNEXPECTED(internal_uri->handler->normalize_uri(new_internal_uri->uri) == FAILURE)) {
431+
zend_throw_error(uri_operation_exception_ce, "Failed to normalize %s", ZSTR_VAL(this_object->ce->name));
432+
RETURN_THROWS();
433+
}
434+
435+
ZVAL_OBJ(return_value, new_object);
436+
}
437+
438+
PHP_METHOD(Uri_Rfc3986Uri, toNormalizedString)
439+
{
440+
ZEND_PARSE_PARAMETERS_NONE();
441+
442+
zend_object *object = Z_OBJ_P(ZEND_THIS);
443+
uri_internal_t *internal_uri = uri_internal_from_obj(object);
444+
URI_CHECK_INITIALIZATION_RETURN_THROWS(internal_uri, object);
445+
446+
void *new_uri = internal_uri->handler->clone_uri(internal_uri->uri);
447+
if (UNEXPECTED(new_uri == NULL)) {
448+
zend_throw_error(uri_operation_exception_ce, "Failed to normalize %s", ZSTR_VAL(object->ce->name));
449+
RETURN_THROWS();
450+
}
451+
452+
if (UNEXPECTED(internal_uri->handler->normalize_uri(new_uri) == FAILURE)) {
453+
zend_throw_error(uri_operation_exception_ce, "Failed to normalize %s", ZSTR_VAL(object->ce->name));
454+
internal_uri->handler->free_uri(new_uri);
455+
RETURN_THROWS();
456+
}
457+
458+
RETVAL_STR(internal_uri->handler->uri_to_string(new_uri, false));
459+
internal_uri->handler->free_uri(new_uri);
460+
}
461+
414462
PHP_METHOD(Uri_Rfc3986Uri, __toString)
415463
{
416464
ZEND_PARSE_PARAMETERS_NONE();
@@ -445,7 +493,7 @@ static void uri_restore_custom_properties(zend_object *object, uri_internal_t *i
445493
}
446494

447495
zend_update_property_ex(object->ce, object, prop_name, prop_val);
448-
if (EG(exception)) {
496+
if (UNEXPECTED(EG(exception) != NULL)) {
449497
break;
450498
}
451499
} ZEND_HASH_FOREACH_END();
@@ -659,7 +707,7 @@ static zend_object *uri_clone_obj_handler(zend_object *object)
659707

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

@@ -724,6 +772,7 @@ zend_result uri_handler_register(const uri_handler_t *uri_handler)
724772
ZEND_ASSERT(uri_handler->parse_uri != NULL);
725773
ZEND_ASSERT(uri_handler->get_uri_ce != NULL);
726774
ZEND_ASSERT(uri_handler->clone_uri != NULL);
775+
ZEND_ASSERT(uri_handler->normalize_uri != NULL);
727776
ZEND_ASSERT(uri_handler->uri_to_string != NULL);
728777
ZEND_ASSERT(uri_handler->free_uri != NULL);
729778
ZEND_ASSERT(uri_handler->destroy_parser != NULL);
@@ -746,6 +795,7 @@ static PHP_MINIT_FUNCTION(uri)
746795

747796
uri_exception_ce = register_class_Uri_UriException(zend_ce_exception);
748797
uninitialized_uri_exception_ce = register_class_Uri_UninitializedUriException(uri_exception_ce);
798+
uri_operation_exception_ce = register_class_Uri_UriOperationException(uri_exception_ce);
749799
invalid_uri_exception_ce = register_class_Uri_InvalidUriException(uri_exception_ce);
750800
whatwg_error_ce = register_class_Uri_WhatWgError();
751801

ext/uri/php_uri.stub.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class UninitializedUriException extends \Uri\UriException
2626
{
2727
}
2828

29+
/** @strict-properties */
30+
class UriOperationException extends \Uri\UriException
31+
{
32+
}
33+
2934
/** @strict-properties */
3035
class InvalidUriException extends \Uri\UriException
3136
{
@@ -136,6 +141,10 @@ public function withFragment(?string $fragment): static {}
136141

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

144+
public function normalize(): static {}
145+
146+
public function toNormalizedString(): string {}
147+
139148
public function __toString(): string {}
140149
}
141150

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

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

209+
public function normalize(): static {}
210+
211+
public function toNormalizedString(): string {}
212+
200213
public function __toString(): string {}
201214

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

295+
/** @implementation-alias Uri\Rfc3986Uri::normalize */
296+
public function normalize(): static {}
297+
298+
/** @implementation-alias Uri\Rfc3986Uri::toNormalizedString */
299+
public function toNormalizedString(): string {}
300+
282301
/** @implementation-alias Uri\Rfc3986Uri::__toString */
283302
public function __toString(): string {}
284303

ext/uri/php_uri_arginfo.h

Lines changed: 35 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/uri/php_uri_common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern zend_class_entry *whatwg_uri_ce;
2424
extern zend_object_handlers whatwg_uri_object_handlers;
2525
extern zend_class_entry *uri_exception_ce;
2626
extern zend_class_entry *uninitialized_uri_exception_ce;
27+
extern zend_class_entry *uri_operation_exception_ce;
2728
extern zend_class_entry *invalid_uri_exception_ce;
2829
extern zend_class_entry *whatwg_error_ce;
2930

@@ -34,6 +35,7 @@ typedef struct uri_handler_t {
3435
void *(*parse_uri)(const zend_string *uri_str, const zend_string *base_url_str, zval *errors);
3536
zend_class_entry *(*get_uri_ce)(void);
3637
void *(*clone_uri)(void *uri);
38+
zend_result (*normalize_uri)(void *uri);
3739
zend_string *(*uri_to_string)(void *uri, bool exclude_fragment);
3840
void (*free_uri)(void *uri);
3941
zend_result (*destroy_parser)(void);
@@ -130,6 +132,9 @@ void throw_invalid_uri_exception(zval *errors);
130132
const uri_property_handler_t *property_handler = uri_property_handler_from_internal_uri(internal_uri, property_name); \
131133
ZEND_ASSERT(property_handler != NULL); \
132134
zend_object *new_object = uri_clone_obj_handler(Z_OBJ_P(ZEND_THIS)); \
135+
if (UNEXPECTED(EG(exception) != NULL)) { \
136+
RETURN_THROWS(); \
137+
} \
133138
uri_internal_t *new_internal_uri = uri_internal_from_obj(new_object); \
134139
URI_CHECK_INITIALIZATION_RETURN_THROWS(new_internal_uri, Z_OBJ_P(ZEND_THIS)); \
135140
if (property_handler->write_func == NULL) { \

ext/uri/php_uriparser.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ static zend_result uriparser_init_parser(void);
2323
static void *uriparser_parse_uri(const zend_string *uri_str, const zend_string *base_uri_str, zval *errors);
2424
static zend_class_entry *uriparser_get_uri_ce(void);
2525
static void *uriparser_clone_uri(void *uri);
26+
static zend_result uriparser_normalize_uri(void *uri);
2627
static zend_string *uriparser_uri_to_string(void *uri, bool exclude_fragment);
2728
static void uriparser_free_uri(void *uri);
2829
static zend_result uriparser_destroy_parser(void);
@@ -35,6 +36,7 @@ const uri_handler_t uriparser_uri_handler = {
3536
uriparser_parse_uri,
3637
uriparser_get_uri_ce,
3738
uriparser_clone_uri,
39+
uriparser_normalize_uri,
3840
uriparser_uri_to_string,
3941
uriparser_free_uri,
4042
uriparser_destroy_parser,
@@ -559,6 +561,17 @@ static void *uriparser_clone_uri(void *uri)
559561
return new_uriparser_uri;
560562
}
561563

564+
static zend_result uriparser_normalize_uri(void *uri)
565+
{
566+
UriUriA *uriparser_uri = (UriUriA *) uri;
567+
568+
if (uriNormalizeSyntaxA(uriparser_uri) != URI_SUCCESS) {
569+
return FAILURE;
570+
}
571+
572+
return SUCCESS;
573+
}
574+
562575
static zend_string *uriparser_uri_to_string(void *uri, bool exclude_fragment)
563576
{
564577
UriUriA *uriparser_uri = (UriUriA *) uri;

0 commit comments

Comments
 (0)