2525#endif
2626
2727ZEND_TLS lxb_url_parser_t lexbor_parser ;
28- ZEND_TLS unsigned short int lexbor_urls ;
28+ ZEND_TLS unsigned short int parsed_urls ;
2929
30- static const unsigned short int LEXBOR_MAX_URL_COUNT = 500 ;
31- static const size_t LEXBOR_MRAW_BYTE_SIZE = 8192 ;
30+ static const unsigned short int maximum_parses_before_cleanup = 500 ;
31+ static const size_t lexbor_mraw_byte_size = 8192 ;
3232
3333static zend_always_inline void zval_string_or_null_to_lexbor_str (zval * value , lexbor_str_t * lexbor_str )
3434{
@@ -55,16 +55,6 @@ static zend_always_inline void zval_long_or_null_to_lexbor_str(zval *value, lexb
5555 }
5656}
5757
58- static void lexbor_cleanup_parser (void )
59- {
60- if (++ lexbor_urls % LEXBOR_MAX_URL_COUNT == 0 ) {
61- lexbor_mraw_clean (lexbor_parser .mraw );
62- lexbor_urls = 0 ;
63- }
64-
65- lxb_url_parser_clean (& lexbor_parser );
66- }
67-
6858/**
6959 * Creates a Uri\WhatWg\UrlValidationError class by mapping error codes listed in
7060 * https://url.spec.whatwg.org/#writing to a Uri\WhatWg\UrlValidationErrorType enum.
@@ -247,7 +237,7 @@ static void throw_invalid_url_exception_during_write(zval *errors, const char *c
247237 zend_update_property (exception -> ce , exception , ZEND_STRL ("errors" ), errors );
248238}
249239
250- static lxb_status_t lexbor_serialize_callback (const lxb_char_t * data , size_t length , void * ctx )
240+ static lxb_status_t serialize_to_smart_str_callback (const lxb_char_t * data , size_t length , void * ctx )
251241{
252242 smart_str * uri_str = ctx ;
253243
@@ -258,7 +248,7 @@ static lxb_status_t lexbor_serialize_callback(const lxb_char_t *data, size_t len
258248 return LXB_STATUS_OK ;
259249}
260250
261- static zend_result lexbor_read_scheme (const struct uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
251+ static zend_result php_uri_parser_whatwg_scheme_read (const struct uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
262252{
263253 lxb_url_t * lexbor_uri = internal_uri -> uri ;
264254
@@ -269,7 +259,7 @@ static zend_result lexbor_read_scheme(const struct uri_internal_t *internal_uri,
269259 return SUCCESS ;
270260}
271261
272- static zend_result lexbor_write_scheme (struct uri_internal_t * internal_uri , zval * value , zval * errors )
262+ static zend_result php_uri_parser_whatwg_scheme_write (struct uri_internal_t * internal_uri , zval * value , zval * errors )
273263{
274264 lxb_url_t * lexbor_uri = internal_uri -> uri ;
275265 lexbor_str_t str = {0 };
@@ -285,7 +275,7 @@ static zend_result lexbor_write_scheme(struct uri_internal_t *internal_uri, zval
285275 return SUCCESS ;
286276}
287277
288- static zend_result lexbor_read_username (const struct uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
278+ static zend_result php_uri_parser_whatwg_username_read (const struct uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
289279{
290280 lxb_url_t * lexbor_uri = internal_uri -> uri ;
291281
@@ -298,7 +288,7 @@ static zend_result lexbor_read_username(const struct uri_internal_t *internal_ur
298288 return SUCCESS ;
299289}
300290
301- static zend_result lexbor_write_username (uri_internal_t * internal_uri , zval * value , zval * errors )
291+ static zend_result php_uri_parser_whatwg_username_write (uri_internal_t * internal_uri , zval * value , zval * errors )
302292{
303293 lxb_url_t * lexbor_uri = internal_uri -> uri ;
304294 lexbor_str_t str = {0 };
@@ -314,7 +304,7 @@ static zend_result lexbor_write_username(uri_internal_t *internal_uri, zval *val
314304 return SUCCESS ;
315305}
316306
317- static zend_result lexbor_read_password (const struct uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
307+ static zend_result php_uri_parser_whatwg_password_read (const struct uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
318308{
319309 lxb_url_t * lexbor_uri = internal_uri -> uri ;
320310
@@ -327,7 +317,7 @@ static zend_result lexbor_read_password(const struct uri_internal_t *internal_ur
327317 return SUCCESS ;
328318}
329319
330- static zend_result lexbor_write_password (struct uri_internal_t * internal_uri , zval * value , zval * errors )
320+ static zend_result php_uri_parser_whatwg_password_write (struct uri_internal_t * internal_uri , zval * value , zval * errors )
331321{
332322 lxb_url_t * lexbor_uri = internal_uri -> uri ;
333323 lexbor_str_t str = {0 };
@@ -354,21 +344,21 @@ static zend_result init_idna(void)
354344 return lxb_unicode_idna_init (lexbor_parser .idna ) == LXB_STATUS_OK ? SUCCESS : FAILURE ;
355345}
356346
357- static zend_result lexbor_read_host (const struct uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
347+ static zend_result php_uri_parser_whatwg_host_read (const struct uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
358348{
359349 lxb_url_t * lexbor_uri = internal_uri -> uri ;
360350
361351 if (lexbor_uri -> host .type == LXB_URL_HOST_TYPE_IPV4 ) {
362352 smart_str host_str = {0 };
363353
364- lxb_url_serialize_host_ipv4 (lexbor_uri -> host .u .ipv4 , lexbor_serialize_callback , & host_str );
354+ lxb_url_serialize_host_ipv4 (lexbor_uri -> host .u .ipv4 , serialize_to_smart_str_callback , & host_str );
365355
366356 ZVAL_NEW_STR (retval , smart_str_extract (& host_str ));
367357 } else if (lexbor_uri -> host .type == LXB_URL_HOST_TYPE_IPV6 ) {
368358 smart_str host_str = {0 };
369359
370360 smart_str_appendc (& host_str , '[' );
371- lxb_url_serialize_host_ipv6 (lexbor_uri -> host .u .ipv6 , lexbor_serialize_callback , & host_str );
361+ lxb_url_serialize_host_ipv6 (lexbor_uri -> host .u .ipv6 , serialize_to_smart_str_callback , & host_str );
372362 smart_str_appendc (& host_str , ']' );
373363
374364 ZVAL_NEW_STR (retval , smart_str_extract (& host_str ));
@@ -381,7 +371,7 @@ static zend_result lexbor_read_host(const struct uri_internal_t *internal_uri, u
381371 if (init_idna () == FAILURE ) {
382372 return FAILURE ;
383373 }
384- lxb_url_serialize_host_unicode (lexbor_parser .idna , & lexbor_uri -> host , lexbor_serialize_callback , & host_str );
374+ lxb_url_serialize_host_unicode (lexbor_parser .idna , & lexbor_uri -> host , serialize_to_smart_str_callback , & host_str );
385375 lxb_unicode_idna_clean (lexbor_parser .idna );
386376
387377 ZVAL_NEW_STR (retval , smart_str_extract (& host_str ));
@@ -401,7 +391,7 @@ static zend_result lexbor_read_host(const struct uri_internal_t *internal_uri, u
401391 return SUCCESS ;
402392}
403393
404- static zend_result lexbor_write_host (struct uri_internal_t * internal_uri , zval * value , zval * errors )
394+ static zend_result php_uri_parser_whatwg_host_write (struct uri_internal_t * internal_uri , zval * value , zval * errors )
405395{
406396 lxb_url_t * lexbor_uri = internal_uri -> uri ;
407397 lexbor_str_t str = {0 };
@@ -417,7 +407,7 @@ static zend_result lexbor_write_host(struct uri_internal_t *internal_uri, zval *
417407 return SUCCESS ;
418408}
419409
420- static zend_result lexbor_read_port (const struct uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
410+ static zend_result php_uri_parser_whatwg_port_read (const struct uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
421411{
422412 lxb_url_t * lexbor_uri = internal_uri -> uri ;
423413
@@ -430,7 +420,7 @@ static zend_result lexbor_read_port(const struct uri_internal_t *internal_uri, u
430420 return SUCCESS ;
431421}
432422
433- static zend_result lexbor_write_port (struct uri_internal_t * internal_uri , zval * value , zval * errors )
423+ static zend_result php_uri_parser_whatwg_port_write (struct uri_internal_t * internal_uri , zval * value , zval * errors )
434424{
435425 lxb_url_t * lexbor_uri = internal_uri -> uri ;
436426 lexbor_str_t str = {0 };
@@ -446,7 +436,7 @@ static zend_result lexbor_write_port(struct uri_internal_t *internal_uri, zval *
446436 return SUCCESS ;
447437}
448438
449- static zend_result lexbor_read_path (const struct uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
439+ static zend_result php_uri_parser_whatwg_path_read (const struct uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
450440{
451441 lxb_url_t * lexbor_uri = internal_uri -> uri ;
452442
@@ -459,7 +449,7 @@ static zend_result lexbor_read_path(const struct uri_internal_t *internal_uri, u
459449 return SUCCESS ;
460450}
461451
462- static zend_result lexbor_write_path (struct uri_internal_t * internal_uri , zval * value , zval * errors )
452+ static zend_result php_uri_parser_whatwg_path_write (struct uri_internal_t * internal_uri , zval * value , zval * errors )
463453{
464454 lxb_url_t * lexbor_uri = internal_uri -> uri ;
465455 lexbor_str_t str = {0 };
@@ -475,7 +465,7 @@ static zend_result lexbor_write_path(struct uri_internal_t *internal_uri, zval *
475465 return SUCCESS ;
476466}
477467
478- static zend_result lexbor_read_query (const struct uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
468+ static zend_result php_uri_parser_whatwg_query_read (const struct uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
479469{
480470 lxb_url_t * lexbor_uri = internal_uri -> uri ;
481471
@@ -488,7 +478,7 @@ static zend_result lexbor_read_query(const struct uri_internal_t *internal_uri,
488478 return SUCCESS ;
489479}
490480
491- static zend_result lexbor_write_query (struct uri_internal_t * internal_uri , zval * value , zval * errors )
481+ static zend_result php_uri_parser_whatwg_query_write (struct uri_internal_t * internal_uri , zval * value , zval * errors )
492482{
493483 lxb_url_t * lexbor_uri = internal_uri -> uri ;
494484 lexbor_str_t str = {0 };
@@ -504,7 +494,7 @@ static zend_result lexbor_write_query(struct uri_internal_t *internal_uri, zval
504494 return SUCCESS ;
505495}
506496
507- static zend_result lexbor_read_fragment (const struct uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
497+ static zend_result php_uri_parser_whatwg_fragment_read (const struct uri_internal_t * internal_uri , uri_component_read_mode_t read_mode , zval * retval )
508498{
509499 lxb_url_t * lexbor_uri = internal_uri -> uri ;
510500
@@ -517,7 +507,7 @@ static zend_result lexbor_read_fragment(const struct uri_internal_t *internal_ur
517507 return SUCCESS ;
518508}
519509
520- static zend_result lexbor_write_fragment (struct uri_internal_t * internal_uri , zval * value , zval * errors )
510+ static zend_result php_uri_parser_whatwg_fragment_write (struct uri_internal_t * internal_uri , zval * value , zval * errors )
521511{
522512 lxb_url_t * lexbor_uri = internal_uri -> uri ;
523513 lexbor_str_t str = {0 };
@@ -536,7 +526,7 @@ static zend_result lexbor_write_fragment(struct uri_internal_t *internal_uri, zv
536526PHP_RINIT_FUNCTION (uri_parser_whatwg )
537527{
538528 lexbor_mraw_t * mraw = lexbor_mraw_create ();
539- lxb_status_t status = lexbor_mraw_init (mraw , LEXBOR_MRAW_BYTE_SIZE );
529+ lxb_status_t status = lexbor_mraw_init (mraw , lexbor_mraw_byte_size );
540530 if (status != LXB_STATUS_OK ) {
541531 lexbor_mraw_destroy (mraw , true);
542532 return FAILURE ;
@@ -549,7 +539,7 @@ PHP_RINIT_FUNCTION(uri_parser_whatwg)
549539 return FAILURE ;
550540 }
551541
552- lexbor_urls = 0 ;
542+ parsed_urls = 0 ;
553543
554544 return SUCCESS ;
555545}
@@ -559,14 +549,24 @@ PHP_RSHUTDOWN_FUNCTION(uri_parser_whatwg)
559549 lxb_url_parser_memory_destroy (& lexbor_parser );
560550 lxb_url_parser_destroy (& lexbor_parser , false);
561551
562- lexbor_urls = 0 ;
552+ parsed_urls = 0 ;
563553
564554 return SUCCESS ;
565555}
566556
567- lxb_url_t * lexbor_parse_uri_ex (const char * uri_str , size_t uri_str_len , const lxb_url_t * lexbor_base_url , zval * errors , bool silent )
557+ static void reset_parser_state (void )
558+ {
559+ if (++ parsed_urls % maximum_parses_before_cleanup == 0 ) {
560+ lexbor_mraw_clean (lexbor_parser .mraw );
561+ parsed_urls = 0 ;
562+ }
563+
564+ lxb_url_parser_clean (& lexbor_parser );
565+ }
566+
567+ lxb_url_t * php_uri_parser_whatwg_parse_ex (const char * uri_str , size_t uri_str_len , const lxb_url_t * lexbor_base_url , zval * errors , bool silent )
568568{
569- lexbor_cleanup_parser ();
569+ reset_parser_state ();
570570
571571 lxb_url_t * url = lxb_url_parse (& lexbor_parser , lexbor_base_url , (unsigned char * ) uri_str , uri_str_len );
572572 const char * reason = fill_errors (errors );
@@ -579,19 +579,19 @@ lxb_url_t *lexbor_parse_uri_ex(const char *uri_str, size_t uri_str_len, const lx
579579 return url ;
580580}
581581
582- static void * lexbor_parse_uri (const char * uri_str , size_t uri_str_len , const void * base_url , zval * errors , bool silent )
582+ static void * php_uri_parser_whatwg_parse (const char * uri_str , size_t uri_str_len , const void * base_url , zval * errors , bool silent )
583583{
584- return lexbor_parse_uri_ex (uri_str , uri_str_len , base_url , errors , silent );
584+ return php_uri_parser_whatwg_parse_ex (uri_str , uri_str_len , base_url , errors , silent );
585585}
586586
587- static void * lexbor_clone_uri (void * uri )
587+ static void * php_uri_parser_whatwg_clone (void * uri )
588588{
589589 lxb_url_t * lexbor_uri = (lxb_url_t * ) uri ;
590590
591591 return lxb_url_clone (lexbor_parser .mraw , lexbor_uri );
592592}
593593
594- static zend_string * lexbor_uri_to_string (void * uri , uri_recomposition_mode_t recomposition_mode , bool exclude_fragment )
594+ static zend_string * php_uri_parser_whatwg_to_string (void * uri , uri_recomposition_mode_t recomposition_mode , bool exclude_fragment )
595595{
596596 lxb_url_t * lexbor_uri = (lxb_url_t * ) uri ;
597597 smart_str uri_str = {0 };
@@ -603,38 +603,38 @@ static zend_string *lexbor_uri_to_string(void *uri, uri_recomposition_mode_t rec
603603 if (init_idna () == FAILURE ) {
604604 return NULL ;
605605 }
606- lxb_url_serialize_idna (lexbor_parser .idna , lexbor_uri , lexbor_serialize_callback , & uri_str , exclude_fragment );
606+ lxb_url_serialize_idna (lexbor_parser .idna , lexbor_uri , serialize_to_smart_str_callback , & uri_str , exclude_fragment );
607607 lxb_unicode_idna_clean (lexbor_parser .idna );
608608 break ;
609609 case URI_RECOMPOSITION_RAW_ASCII :
610610 ZEND_FALLTHROUGH ;
611611 case URI_RECOMPOSITION_NORMALIZED_ASCII :
612- lxb_url_serialize (lexbor_uri , lexbor_serialize_callback , & uri_str , exclude_fragment );
612+ lxb_url_serialize (lexbor_uri , serialize_to_smart_str_callback , & uri_str , exclude_fragment );
613613 break ;
614614 EMPTY_SWITCH_DEFAULT_CASE ()
615615 }
616616
617617 return smart_str_extract (& uri_str );
618618}
619619
620- static void lexbor_free_uri (void * uri )
620+ static void php_uri_parser_whatwg_free (void * uri )
621621{
622622}
623623
624- const uri_parser_t lexbor_uri_parser = {
625- .name = URI_PARSER_WHATWG ,
626- .parse_uri = lexbor_parse_uri ,
627- .clone_uri = lexbor_clone_uri ,
628- .uri_to_string = lexbor_uri_to_string ,
629- .free_uri = lexbor_free_uri ,
624+ const uri_parser_t php_uri_parser_whatwg = {
625+ .name = PHP_URI_PARSER_WHATWG ,
626+ .parse_uri = php_uri_parser_whatwg_parse ,
627+ .clone_uri = php_uri_parser_whatwg_clone ,
628+ .uri_to_string = php_uri_parser_whatwg_to_string ,
629+ .free_uri = php_uri_parser_whatwg_free ,
630630 {
631- .scheme = {.read_func = lexbor_read_scheme , .write_func = lexbor_write_scheme },
632- .username = {.read_func = lexbor_read_username , .write_func = lexbor_write_username },
633- .password = {.read_func = lexbor_read_password , .write_func = lexbor_write_password },
634- .host = {.read_func = lexbor_read_host , .write_func = lexbor_write_host },
635- .port = {.read_func = lexbor_read_port , .write_func = lexbor_write_port },
636- .path = {.read_func = lexbor_read_path , .write_func = lexbor_write_path },
637- .query = {.read_func = lexbor_read_query , .write_func = lexbor_write_query },
638- .fragment = {.read_func = lexbor_read_fragment , .write_func = lexbor_write_fragment },
631+ .scheme = {.read_func = php_uri_parser_whatwg_scheme_read , .write_func = php_uri_parser_whatwg_scheme_write },
632+ .username = {.read_func = php_uri_parser_whatwg_username_read , .write_func = php_uri_parser_whatwg_username_write },
633+ .password = {.read_func = php_uri_parser_whatwg_password_read , .write_func = php_uri_parser_whatwg_password_write },
634+ .host = {.read_func = php_uri_parser_whatwg_host_read , .write_func = php_uri_parser_whatwg_host_write },
635+ .port = {.read_func = php_uri_parser_whatwg_port_read , .write_func = php_uri_parser_whatwg_port_write },
636+ .path = {.read_func = php_uri_parser_whatwg_path_read , .write_func = php_uri_parser_whatwg_path_write },
637+ .query = {.read_func = php_uri_parser_whatwg_query_read , .write_func = php_uri_parser_whatwg_query_write },
638+ .fragment = {.read_func = php_uri_parser_whatwg_fragment_read , .write_func = php_uri_parser_whatwg_fragment_write },
639639 }
640640};
0 commit comments