@@ -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
415459PHP_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 );
0 commit comments