@@ -67,7 +67,7 @@ const uri_handler_t lexbor_uri_handler = {
6767 } \
6868} while (0)
6969
70- #define CHECK_WRITE_RESULT (status , lexbor_uri , str , errors ) do { \
70+ #define CHECK_WRITE_RESULT (status , errors ) do { \
7171 if (status != LXB_STATUS_OK) { \
7272 fill_errors(errors); \
7373 return FAILURE; \
@@ -94,19 +94,21 @@ static void lexbor_cleanup_parser(void)
9494 */
9595static void fill_errors (zval * errors )
9696{
97- if (errors == NULL || lexbor_parser -> log == NULL ) {
97+ if (errors == NULL ) {
9898 return ;
9999 }
100100
101- if (lexbor_array_obj_size (& lexbor_parser -> log -> list )) {
102- array_init (errors );
101+ array_init (errors );
102+
103+ if (lexbor_parser -> log == NULL ) {
104+ return ;
103105 }
104106
105107 lexbor_plog_entry_t * lxb_error ;
106108 while ((lxb_error = lexbor_array_obj_pop (& lexbor_parser -> log -> list )) != NULL ) {
107109 zval error ;
108- object_init_ex (& error , whatwg_url_validation_error_ce );
109- zend_update_property_string (whatwg_url_validation_error_ce , Z_OBJ (error ), ZEND_STRL ("context" ), (const char * ) lxb_error -> data );
110+ object_init_ex (& error , uri_whatwg_url_validation_error_ce );
111+ zend_update_property_string (uri_whatwg_url_validation_error_ce , Z_OBJ (error ), ZEND_STRL ("context" ), (const char * ) lxb_error -> data );
110112
111113 zend_string * error_str ;
112114 zval failure ;
@@ -231,17 +233,15 @@ static void fill_errors(zval *errors)
231233 }
232234
233235 zval error_type ;
234- zend_enum_new (& error_type , whatwg_url_validation_error_type_ce , error_str , NULL );
235- zend_update_property (whatwg_url_validation_error_ce , Z_OBJ (error ), ZEND_STRL ("type" ), & error_type );
236+ zend_enum_new (& error_type , uri_whatwg_url_validation_error_type_ce , error_str , NULL );
237+ zend_update_property (uri_whatwg_url_validation_error_ce , Z_OBJ (error ), ZEND_STRL ("type" ), & error_type );
236238 zend_string_release_ex (error_str , false);
237239 zval_ptr_dtor (& error_type );
238240
239- zend_update_property (whatwg_url_validation_error_ce , Z_OBJ (error ), ZEND_STRL ("failure" ), & failure );
241+ zend_update_property (uri_whatwg_url_validation_error_ce , Z_OBJ (error ), ZEND_STRL ("failure" ), & failure );
240242
241243 add_next_index_zval (errors , & error );
242244 }
243-
244- lexbor_array_obj_clean (& lexbor_parser -> log -> list ); // TODO check if needed
245245}
246246
247247static lxb_status_t lexbor_serialize_callback (const lxb_char_t * data , size_t length , void * ctx )
@@ -273,9 +273,13 @@ static zend_result lexbor_write_scheme(uri_internal_t *internal_uri, zval *value
273273
274274 ZVAL_STRING_OR_NULL_TO_LEXBOR_STR (value , str );
275275
276- lxb_status_t status = lxb_url_api_protocol_set (lexbor_uri , lexbor_parser , str .data , str .length );
276+ if (lxb_url_api_protocol_set (lexbor_uri , lexbor_parser , str .data , str .length ) != LXB_STATUS_OK ) {
277+ fill_errors (errors );
278+
279+ return FAILURE ;
280+ }
277281
278- CHECK_WRITE_RESULT ( status , lexbor_uri , str , errors ) ;
282+ return SUCCESS ;
279283}
280284
281285static zend_result lexbor_read_username (const uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
@@ -298,9 +302,13 @@ static zend_result lexbor_write_username(uri_internal_t *internal_uri, zval *val
298302
299303 ZVAL_STRING_OR_NULL_TO_LEXBOR_STR (value , str );
300304
301- lxb_status_t status = lxb_url_api_username_set (lexbor_uri , str .data , str .length );
305+ if (lxb_url_api_username_set (lexbor_uri , str .data , str .length ) != LXB_STATUS_OK ) {
306+ fill_errors (errors );
307+
308+ return FAILURE ;
309+ }
302310
303- CHECK_WRITE_RESULT ( status , lexbor_uri , str , errors ) ;
311+ return SUCCESS ;
304312}
305313
306314static zend_result lexbor_read_password (const uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
@@ -323,9 +331,24 @@ static zend_result lexbor_write_password(uri_internal_t *internal_uri, zval *val
323331
324332 ZVAL_STRING_OR_NULL_TO_LEXBOR_STR (value , str );
325333
326- lxb_status_t status = lxb_url_api_password_set (lexbor_uri , str .data , str .length );
334+ if (lxb_url_api_password_set (lexbor_uri , str .data , str .length ) != LXB_STATUS_OK ) {
335+ fill_errors (errors );
327336
328- CHECK_WRITE_RESULT (status , lexbor_uri , str , errors );
337+ return FAILURE ;
338+ }
339+
340+ return SUCCESS ;
341+ }
342+
343+ static ZEND_RESULT_CODE init_idna (void )
344+ {
345+ if (lexbor_parser -> idna != NULL ) {
346+ return SUCCESS ;
347+ }
348+
349+ lexbor_parser -> idna = lxb_unicode_idna_create ();
350+
351+ return lxb_unicode_idna_init (lexbor_parser -> idna ) == LXB_STATUS_OK ? SUCCESS : FAILURE ;
329352}
330353
331354static zend_result lexbor_read_host (const uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
@@ -337,30 +360,26 @@ static zend_result lexbor_read_host(const uri_internal_t *internal_uri, uri_comp
337360
338361 lxb_url_serialize_host_ipv4 (lexbor_uri -> host .u .ipv4 , lexbor_serialize_callback , & host_str );
339362
340- ZVAL_STR (retval , smart_str_extract (& host_str ));
363+ ZVAL_NEW_STR (retval , smart_str_extract (& host_str ));
341364 } else if (lexbor_uri -> host .type == LXB_URL_HOST_TYPE_IPV6 ) {
342365 smart_str host_str = {0 };
343366
344367 smart_str_appendc (& host_str , '[' );
345368 lxb_url_serialize_host_ipv6 (lexbor_uri -> host .u .ipv6 , lexbor_serialize_callback , & host_str );
346369 smart_str_appendc (& host_str , ']' );
347370
348- ZVAL_STR (retval , smart_str_extract (& host_str ));
371+ ZVAL_NEW_STR (retval , smart_str_extract (& host_str ));
349372 } else if (lexbor_uri -> host .type != LXB_URL_HOST_TYPE_EMPTY && lexbor_uri -> host .type != LXB_URL_HOST_TYPE__UNDEF ) {
350373 switch (read_mode ) {
351374 case URI_COMPONENT_READ_NORMALIZED_UNICODE : {
352375 smart_str host_str = {0 };
353- if (lexbor_parser -> idna == NULL ) {
354- lexbor_parser -> idna = lxb_unicode_idna_create ();
355- lxb_status_t status = lxb_unicode_idna_init (lexbor_parser -> idna );
356- if (status != LXB_STATUS_OK ) {
357- return FAILURE ;
358- }
376+ if (init_idna () == FAILURE ) {
377+ return FAILURE ;
359378 }
360379 lxb_url_serialize_host_unicode (lexbor_parser -> idna , & lexbor_uri -> host , lexbor_serialize_callback , & host_str );
361380 lxb_unicode_idna_clean (lexbor_parser -> idna );
362381
363- ZVAL_STR (retval , smart_str_extract (& host_str ));
382+ ZVAL_NEW_STR (retval , smart_str_extract (& host_str ));
364383 break ;
365384 }
366385 case URI_COMPONENT_READ_NORMALIZED_ASCII :
@@ -384,9 +403,13 @@ static zend_result lexbor_write_host(uri_internal_t *internal_uri, zval *value,
384403
385404 ZVAL_STRING_OR_NULL_TO_LEXBOR_STR (value , str );
386405
387- lxb_status_t status = lxb_url_api_hostname_set (lexbor_uri , lexbor_parser , str .data , str .length );
406+ if (lxb_url_api_hostname_set (lexbor_uri , lexbor_parser , str .data , str .length ) != LXB_STATUS_OK ) {
407+ fill_errors (errors );
408+
409+ return FAILURE ;
410+ }
388411
389- CHECK_WRITE_RESULT ( status , lexbor_uri , str , errors ) ;
412+ return SUCCESS ;
390413}
391414
392415static zend_result lexbor_read_port (const uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
@@ -409,9 +432,13 @@ static zend_result lexbor_write_port(uri_internal_t *internal_uri, zval *value,
409432
410433 ZVAL_LONG_OR_NULL_TO_LEXBOR_STR (value , str );
411434
412- lxb_status_t status = lxb_url_api_port_set (lexbor_uri , lexbor_parser , str .data , str .length );
435+ if (lxb_url_api_port_set (lexbor_uri , lexbor_parser , str .data , str .length ) != LXB_STATUS_OK ) {
436+ fill_errors (errors );
437+
438+ return FAILURE ;
439+ }
413440
414- CHECK_WRITE_RESULT ( status , lexbor_uri , str , errors ) ;
441+ return SUCCESS ;
415442}
416443
417444static zend_result lexbor_read_path (const uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
@@ -434,9 +461,13 @@ static zend_result lexbor_write_path(uri_internal_t *internal_uri, zval *value,
434461
435462 ZVAL_STRING_OR_NULL_TO_LEXBOR_STR (value , str );
436463
437- lxb_status_t status = lxb_url_api_pathname_set (lexbor_uri , lexbor_parser , str .data , str .length );
464+ if (lxb_url_api_pathname_set (lexbor_uri , lexbor_parser , str .data , str .length ) != LXB_STATUS_OK ) {
465+ fill_errors (errors );
438466
439- CHECK_WRITE_RESULT (status , lexbor_uri , str , errors );
467+ return FAILURE ;
468+ }
469+
470+ return SUCCESS ;
440471}
441472
442473static zend_result lexbor_read_query (const uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
@@ -459,9 +490,13 @@ static zend_result lexbor_write_query(uri_internal_t *internal_uri, zval *value,
459490
460491 ZVAL_STRING_OR_NULL_TO_LEXBOR_STR (value , str );
461492
462- lxb_status_t status = lxb_url_api_search_set (lexbor_uri , lexbor_parser , str .data , str .length );
493+ if ( lxb_url_api_search_set (lexbor_uri , lexbor_parser , str .data , str .length ) != LXB_STATUS_OK ) {
494+ fill_errors (errors );
463495
464- CHECK_WRITE_RESULT (status , lexbor_uri , str , errors );
496+ return FAILURE ;
497+ }
498+
499+ return SUCCESS ;
465500}
466501
467502static zend_result lexbor_read_fragment (const uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
@@ -484,9 +519,13 @@ static zend_result lexbor_write_fragment(uri_internal_t *internal_uri, zval *val
484519
485520 ZVAL_STRING_OR_NULL_TO_LEXBOR_STR (value , str );
486521
487- lxb_status_t status = lxb_url_api_hash_set (lexbor_uri , lexbor_parser , str .data , str .length );
522+ if (lxb_url_api_hash_set (lexbor_uri , lexbor_parser , str .data , str .length ) != LXB_STATUS_OK ) {
523+ fill_errors (errors );
524+
525+ return FAILURE ;
526+ }
488527
489- CHECK_WRITE_RESULT ( status , lexbor_uri , str , errors ) ;
528+ return SUCCESS ;
490529}
491530
492531static zend_result lexbor_init_parser (void )
@@ -498,15 +537,15 @@ static zend_result lexbor_init_parser(void)
498537 return FAILURE ;
499538 }
500539
501- lxb_url_parser_t * parser = lxb_url_parser_create ();
502- status = lxb_url_parser_init (parser , mraw );
540+ lexbor_parser = lxb_url_parser_create ();
541+ status = lxb_url_parser_init (lexbor_parser , mraw );
503542 if (status != LXB_STATUS_OK ) {
504- lxb_url_parser_destroy (parser , true);
543+ lxb_url_parser_destroy (lexbor_parser , true);
544+ lexbor_parser = NULL ;
505545 lexbor_mraw_destroy (mraw , true);
506546 return FAILURE ;
507547 }
508548
509- lexbor_parser = parser ;
510549 lexbor_urls = 0 ;
511550
512551 zend_hash_init (& lexbor_property_handlers , 8 , NULL , NULL , true);
@@ -527,35 +566,25 @@ static void *lexbor_parse_uri(const zend_string *uri_str, const void *base_url,
527566{
528567 lexbor_cleanup_parser ();
529568
530- lxb_url_t * url ;
531- const lxb_url_t * lexbor_base_url = NULL ;
532-
533- if (base_url ) {
534- lexbor_base_url = (lxb_url_t * ) base_url ;
535- }
536-
537- url = lxb_url_parse (lexbor_parser , lexbor_base_url , (unsigned char * ) ZSTR_VAL (uri_str ), ZSTR_LEN (uri_str ));
569+ const lxb_url_t * lexbor_base_url = base_url ;
570+ lxb_url_t * url = lxb_url_parse (lexbor_parser , lexbor_base_url , (unsigned char * ) ZSTR_VAL (uri_str ), ZSTR_LEN (uri_str ));
538571 fill_errors (errors );
539572
540573 return url ;
541574}
542575
543576static void lexbor_create_invalid_uri_exception (zval * exception_zv , zval * errors )
544577{
545- object_init_ex (exception_zv , whatwg_invalid_url_exception_ce );
578+ object_init_ex (exception_zv , uri_whatwg_invalid_url_exception_ce );
546579
547- zend_update_property_string (
548- whatwg_invalid_url_exception_ce ,
549- Z_OBJ_P (exception_zv ),
550- ZSTR_VAL (ZSTR_KNOWN (ZEND_STR_MESSAGE )),
551- ZSTR_LEN (ZSTR_KNOWN (ZEND_STR_MESSAGE )),
552- "URL parsing failed"
553- );
580+ zval value ;
581+ ZVAL_STRING (& value , "URL parsing failed" );
582+ zend_update_property_ex (uri_whatwg_invalid_url_exception_ce , Z_OBJ_P (exception_zv ), ZSTR_KNOWN (ZEND_STR_MESSAGE ), & value );
583+ zval_ptr_dtor (& value );
554584
555- zend_update_property (whatwg_invalid_url_exception_ce , Z_OBJ_P (exception_zv ), ZEND_STRL ("errors" ), errors );
585+ zend_update_property (uri_whatwg_invalid_url_exception_ce , Z_OBJ_P (exception_zv ), ZEND_STRL ("errors" ), errors );
556586}
557587
558-
559588static void * lexbor_clone_uri (void * uri )
560589{
561590 lxb_url_t * lexbor_uri = (lxb_url_t * ) uri ;
@@ -572,12 +601,8 @@ static zend_string *lexbor_uri_to_string(void *uri, uri_recomposition_mode_t rec
572601 case URI_RECOMPOSITION_RAW_UNICODE :
573602 ZEND_FALLTHROUGH ;
574603 case URI_RECOMPOSITION_NORMALIZED_UNICODE :
575- if (lexbor_parser -> idna == NULL ) {
576- lexbor_parser -> idna = lxb_unicode_idna_create ();
577- lxb_status_t status = lxb_unicode_idna_init (lexbor_parser -> idna );
578- if (status != LXB_STATUS_OK ) {
579- return NULL ;
580- }
604+ if (init_idna () == FAILURE ) {
605+ return NULL ;
581606 }
582607 lxb_url_serialize_idna (lexbor_parser -> idna , lexbor_uri , lexbor_serialize_callback , & uri_str , exclude_fragment );
583608 lxb_unicode_idna_clean (lexbor_parser -> idna );
0 commit comments