25
25
#endif
26
26
27
27
ZEND_TLS lxb_url_parser_t lexbor_parser ;
28
- ZEND_TLS unsigned short int lexbor_urls ;
28
+ ZEND_TLS unsigned short int parsed_urls ;
29
29
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 ;
32
32
33
33
static zend_always_inline void zval_string_or_null_to_lexbor_str (zval * value , lexbor_str_t * lexbor_str )
34
34
{
@@ -55,16 +55,6 @@ static zend_always_inline void zval_long_or_null_to_lexbor_str(zval *value, lexb
55
55
}
56
56
}
57
57
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
-
68
58
/**
69
59
* Creates a Uri\WhatWg\UrlValidationError class by mapping error codes listed in
70
60
* 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
247
237
zend_update_property (exception -> ce , exception , ZEND_STRL ("errors" ), errors );
248
238
}
249
239
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 )
251
241
{
252
242
smart_str * uri_str = ctx ;
253
243
@@ -258,7 +248,7 @@ static lxb_status_t lexbor_serialize_callback(const lxb_char_t *data, size_t len
258
248
return LXB_STATUS_OK ;
259
249
}
260
250
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 )
262
252
{
263
253
lxb_url_t * lexbor_uri = internal_uri -> uri ;
264
254
@@ -269,7 +259,7 @@ static zend_result lexbor_read_scheme(const struct uri_internal_t *internal_uri,
269
259
return SUCCESS ;
270
260
}
271
261
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 )
273
263
{
274
264
lxb_url_t * lexbor_uri = internal_uri -> uri ;
275
265
lexbor_str_t str = {0 };
@@ -285,7 +275,7 @@ static zend_result lexbor_write_scheme(struct uri_internal_t *internal_uri, zval
285
275
return SUCCESS ;
286
276
}
287
277
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 )
289
279
{
290
280
lxb_url_t * lexbor_uri = internal_uri -> uri ;
291
281
@@ -298,7 +288,7 @@ static zend_result lexbor_read_username(const struct uri_internal_t *internal_ur
298
288
return SUCCESS ;
299
289
}
300
290
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 )
302
292
{
303
293
lxb_url_t * lexbor_uri = internal_uri -> uri ;
304
294
lexbor_str_t str = {0 };
@@ -314,7 +304,7 @@ static zend_result lexbor_write_username(uri_internal_t *internal_uri, zval *val
314
304
return SUCCESS ;
315
305
}
316
306
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 )
318
308
{
319
309
lxb_url_t * lexbor_uri = internal_uri -> uri ;
320
310
@@ -327,7 +317,7 @@ static zend_result lexbor_read_password(const struct uri_internal_t *internal_ur
327
317
return SUCCESS ;
328
318
}
329
319
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 )
331
321
{
332
322
lxb_url_t * lexbor_uri = internal_uri -> uri ;
333
323
lexbor_str_t str = {0 };
@@ -354,21 +344,21 @@ static zend_result init_idna(void)
354
344
return lxb_unicode_idna_init (lexbor_parser .idna ) == LXB_STATUS_OK ? SUCCESS : FAILURE ;
355
345
}
356
346
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 )
358
348
{
359
349
lxb_url_t * lexbor_uri = internal_uri -> uri ;
360
350
361
351
if (lexbor_uri -> host .type == LXB_URL_HOST_TYPE_IPV4 ) {
362
352
smart_str host_str = {0 };
363
353
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 );
365
355
366
356
ZVAL_NEW_STR (retval , smart_str_extract (& host_str ));
367
357
} else if (lexbor_uri -> host .type == LXB_URL_HOST_TYPE_IPV6 ) {
368
358
smart_str host_str = {0 };
369
359
370
360
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 );
372
362
smart_str_appendc (& host_str , ']' );
373
363
374
364
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
381
371
if (init_idna () == FAILURE ) {
382
372
return FAILURE ;
383
373
}
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 );
385
375
lxb_unicode_idna_clean (lexbor_parser .idna );
386
376
387
377
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
401
391
return SUCCESS ;
402
392
}
403
393
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 )
405
395
{
406
396
lxb_url_t * lexbor_uri = internal_uri -> uri ;
407
397
lexbor_str_t str = {0 };
@@ -417,7 +407,7 @@ static zend_result lexbor_write_host(struct uri_internal_t *internal_uri, zval *
417
407
return SUCCESS ;
418
408
}
419
409
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 )
421
411
{
422
412
lxb_url_t * lexbor_uri = internal_uri -> uri ;
423
413
@@ -430,7 +420,7 @@ static zend_result lexbor_read_port(const struct uri_internal_t *internal_uri, u
430
420
return SUCCESS ;
431
421
}
432
422
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 )
434
424
{
435
425
lxb_url_t * lexbor_uri = internal_uri -> uri ;
436
426
lexbor_str_t str = {0 };
@@ -446,7 +436,7 @@ static zend_result lexbor_write_port(struct uri_internal_t *internal_uri, zval *
446
436
return SUCCESS ;
447
437
}
448
438
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 )
450
440
{
451
441
lxb_url_t * lexbor_uri = internal_uri -> uri ;
452
442
@@ -459,7 +449,7 @@ static zend_result lexbor_read_path(const struct uri_internal_t *internal_uri, u
459
449
return SUCCESS ;
460
450
}
461
451
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 )
463
453
{
464
454
lxb_url_t * lexbor_uri = internal_uri -> uri ;
465
455
lexbor_str_t str = {0 };
@@ -475,7 +465,7 @@ static zend_result lexbor_write_path(struct uri_internal_t *internal_uri, zval *
475
465
return SUCCESS ;
476
466
}
477
467
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 )
479
469
{
480
470
lxb_url_t * lexbor_uri = internal_uri -> uri ;
481
471
@@ -488,7 +478,7 @@ static zend_result lexbor_read_query(const struct uri_internal_t *internal_uri,
488
478
return SUCCESS ;
489
479
}
490
480
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 )
492
482
{
493
483
lxb_url_t * lexbor_uri = internal_uri -> uri ;
494
484
lexbor_str_t str = {0 };
@@ -504,7 +494,7 @@ static zend_result lexbor_write_query(struct uri_internal_t *internal_uri, zval
504
494
return SUCCESS ;
505
495
}
506
496
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 )
508
498
{
509
499
lxb_url_t * lexbor_uri = internal_uri -> uri ;
510
500
@@ -517,7 +507,7 @@ static zend_result lexbor_read_fragment(const struct uri_internal_t *internal_ur
517
507
return SUCCESS ;
518
508
}
519
509
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 )
521
511
{
522
512
lxb_url_t * lexbor_uri = internal_uri -> uri ;
523
513
lexbor_str_t str = {0 };
@@ -536,7 +526,7 @@ static zend_result lexbor_write_fragment(struct uri_internal_t *internal_uri, zv
536
526
PHP_RINIT_FUNCTION (uri_parser_whatwg )
537
527
{
538
528
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 );
540
530
if (status != LXB_STATUS_OK ) {
541
531
lexbor_mraw_destroy (mraw , true);
542
532
return FAILURE ;
@@ -549,7 +539,7 @@ PHP_RINIT_FUNCTION(uri_parser_whatwg)
549
539
return FAILURE ;
550
540
}
551
541
552
- lexbor_urls = 0 ;
542
+ parsed_urls = 0 ;
553
543
554
544
return SUCCESS ;
555
545
}
@@ -559,14 +549,24 @@ PHP_RSHUTDOWN_FUNCTION(uri_parser_whatwg)
559
549
lxb_url_parser_memory_destroy (& lexbor_parser );
560
550
lxb_url_parser_destroy (& lexbor_parser , false);
561
551
562
- lexbor_urls = 0 ;
552
+ parsed_urls = 0 ;
563
553
564
554
return SUCCESS ;
565
555
}
566
556
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 )
568
568
{
569
- lexbor_cleanup_parser ();
569
+ reset_parser_state ();
570
570
571
571
lxb_url_t * url = lxb_url_parse (& lexbor_parser , lexbor_base_url , (unsigned char * ) uri_str , uri_str_len );
572
572
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
579
579
return url ;
580
580
}
581
581
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 )
583
583
{
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 );
585
585
}
586
586
587
- static void * lexbor_clone_uri (void * uri )
587
+ static void * php_uri_parser_whatwg_clone (void * uri )
588
588
{
589
589
lxb_url_t * lexbor_uri = (lxb_url_t * ) uri ;
590
590
591
591
return lxb_url_clone (lexbor_parser .mraw , lexbor_uri );
592
592
}
593
593
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 )
595
595
{
596
596
lxb_url_t * lexbor_uri = (lxb_url_t * ) uri ;
597
597
smart_str uri_str = {0 };
@@ -603,38 +603,38 @@ static zend_string *lexbor_uri_to_string(void *uri, uri_recomposition_mode_t rec
603
603
if (init_idna () == FAILURE ) {
604
604
return NULL ;
605
605
}
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 );
607
607
lxb_unicode_idna_clean (lexbor_parser .idna );
608
608
break ;
609
609
case URI_RECOMPOSITION_RAW_ASCII :
610
610
ZEND_FALLTHROUGH ;
611
611
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 );
613
613
break ;
614
614
EMPTY_SWITCH_DEFAULT_CASE ()
615
615
}
616
616
617
617
return smart_str_extract (& uri_str );
618
618
}
619
619
620
- static void lexbor_free_uri (void * uri )
620
+ static void php_uri_parser_whatwg_free (void * uri )
621
621
{
622
622
}
623
623
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 ,
630
630
{
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 },
639
639
}
640
640
};
0 commit comments