@@ -67,7 +67,7 @@ const uri_handler_t lexbor_uri_handler = {
67
67
} \
68
68
} while (0)
69
69
70
- #define CHECK_WRITE_RESULT (status , lexbor_uri , str , errors ) do { \
70
+ #define CHECK_WRITE_RESULT (status , errors ) do { \
71
71
if (status != LXB_STATUS_OK) { \
72
72
fill_errors(errors); \
73
73
return FAILURE; \
@@ -94,19 +94,21 @@ static void lexbor_cleanup_parser(void)
94
94
*/
95
95
static void fill_errors (zval * errors )
96
96
{
97
- if (errors == NULL || lexbor_parser -> log == NULL ) {
97
+ if (errors == NULL ) {
98
98
return ;
99
99
}
100
100
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 ;
103
105
}
104
106
105
107
lexbor_plog_entry_t * lxb_error ;
106
108
while ((lxb_error = lexbor_array_obj_pop (& lexbor_parser -> log -> list )) != NULL ) {
107
109
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 );
110
112
111
113
zend_string * error_str ;
112
114
zval failure ;
@@ -231,17 +233,15 @@ static void fill_errors(zval *errors)
231
233
}
232
234
233
235
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 );
236
238
zend_string_release_ex (error_str , false);
237
239
zval_ptr_dtor (& error_type );
238
240
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 );
240
242
241
243
add_next_index_zval (errors , & error );
242
244
}
243
-
244
- lexbor_array_obj_clean (& lexbor_parser -> log -> list ); // TODO check if needed
245
245
}
246
246
247
247
static 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
273
273
274
274
ZVAL_STRING_OR_NULL_TO_LEXBOR_STR (value , str );
275
275
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
+ }
277
281
278
- CHECK_WRITE_RESULT ( status , lexbor_uri , str , errors ) ;
282
+ return SUCCESS ;
279
283
}
280
284
281
285
static 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
298
302
299
303
ZVAL_STRING_OR_NULL_TO_LEXBOR_STR (value , str );
300
304
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
+ }
302
310
303
- CHECK_WRITE_RESULT ( status , lexbor_uri , str , errors ) ;
311
+ return SUCCESS ;
304
312
}
305
313
306
314
static 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
323
331
324
332
ZVAL_STRING_OR_NULL_TO_LEXBOR_STR (value , str );
325
333
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 );
327
336
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 ;
329
352
}
330
353
331
354
static 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
337
360
338
361
lxb_url_serialize_host_ipv4 (lexbor_uri -> host .u .ipv4 , lexbor_serialize_callback , & host_str );
339
362
340
- ZVAL_STR (retval , smart_str_extract (& host_str ));
363
+ ZVAL_NEW_STR (retval , smart_str_extract (& host_str ));
341
364
} else if (lexbor_uri -> host .type == LXB_URL_HOST_TYPE_IPV6 ) {
342
365
smart_str host_str = {0 };
343
366
344
367
smart_str_appendc (& host_str , '[' );
345
368
lxb_url_serialize_host_ipv6 (lexbor_uri -> host .u .ipv6 , lexbor_serialize_callback , & host_str );
346
369
smart_str_appendc (& host_str , ']' );
347
370
348
- ZVAL_STR (retval , smart_str_extract (& host_str ));
371
+ ZVAL_NEW_STR (retval , smart_str_extract (& host_str ));
349
372
} else if (lexbor_uri -> host .type != LXB_URL_HOST_TYPE_EMPTY && lexbor_uri -> host .type != LXB_URL_HOST_TYPE__UNDEF ) {
350
373
switch (read_mode ) {
351
374
case URI_COMPONENT_READ_NORMALIZED_UNICODE : {
352
375
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 ;
359
378
}
360
379
lxb_url_serialize_host_unicode (lexbor_parser -> idna , & lexbor_uri -> host , lexbor_serialize_callback , & host_str );
361
380
lxb_unicode_idna_clean (lexbor_parser -> idna );
362
381
363
- ZVAL_STR (retval , smart_str_extract (& host_str ));
382
+ ZVAL_NEW_STR (retval , smart_str_extract (& host_str ));
364
383
break ;
365
384
}
366
385
case URI_COMPONENT_READ_NORMALIZED_ASCII :
@@ -384,9 +403,13 @@ static zend_result lexbor_write_host(uri_internal_t *internal_uri, zval *value,
384
403
385
404
ZVAL_STRING_OR_NULL_TO_LEXBOR_STR (value , str );
386
405
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
+ }
388
411
389
- CHECK_WRITE_RESULT ( status , lexbor_uri , str , errors ) ;
412
+ return SUCCESS ;
390
413
}
391
414
392
415
static 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,
409
432
410
433
ZVAL_LONG_OR_NULL_TO_LEXBOR_STR (value , str );
411
434
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
+ }
413
440
414
- CHECK_WRITE_RESULT ( status , lexbor_uri , str , errors ) ;
441
+ return SUCCESS ;
415
442
}
416
443
417
444
static 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,
434
461
435
462
ZVAL_STRING_OR_NULL_TO_LEXBOR_STR (value , str );
436
463
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 );
438
466
439
- CHECK_WRITE_RESULT (status , lexbor_uri , str , errors );
467
+ return FAILURE ;
468
+ }
469
+
470
+ return SUCCESS ;
440
471
}
441
472
442
473
static 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,
459
490
460
491
ZVAL_STRING_OR_NULL_TO_LEXBOR_STR (value , str );
461
492
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 );
463
495
464
- CHECK_WRITE_RESULT (status , lexbor_uri , str , errors );
496
+ return FAILURE ;
497
+ }
498
+
499
+ return SUCCESS ;
465
500
}
466
501
467
502
static 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
484
519
485
520
ZVAL_STRING_OR_NULL_TO_LEXBOR_STR (value , str );
486
521
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
+ }
488
527
489
- CHECK_WRITE_RESULT ( status , lexbor_uri , str , errors ) ;
528
+ return SUCCESS ;
490
529
}
491
530
492
531
static zend_result lexbor_init_parser (void )
@@ -498,15 +537,15 @@ static zend_result lexbor_init_parser(void)
498
537
return FAILURE ;
499
538
}
500
539
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 );
503
542
if (status != LXB_STATUS_OK ) {
504
- lxb_url_parser_destroy (parser , true);
543
+ lxb_url_parser_destroy (lexbor_parser , true);
544
+ lexbor_parser = NULL ;
505
545
lexbor_mraw_destroy (mraw , true);
506
546
return FAILURE ;
507
547
}
508
548
509
- lexbor_parser = parser ;
510
549
lexbor_urls = 0 ;
511
550
512
551
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,
527
566
{
528
567
lexbor_cleanup_parser ();
529
568
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 ));
538
571
fill_errors (errors );
539
572
540
573
return url ;
541
574
}
542
575
543
576
static void lexbor_create_invalid_uri_exception (zval * exception_zv , zval * errors )
544
577
{
545
- object_init_ex (exception_zv , whatwg_invalid_url_exception_ce );
578
+ object_init_ex (exception_zv , uri_whatwg_invalid_url_exception_ce );
546
579
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 );
554
584
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 );
556
586
}
557
587
558
-
559
588
static void * lexbor_clone_uri (void * uri )
560
589
{
561
590
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
572
601
case URI_RECOMPOSITION_RAW_UNICODE :
573
602
ZEND_FALLTHROUGH ;
574
603
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 ;
581
606
}
582
607
lxb_url_serialize_idna (lexbor_parser -> idna , lexbor_uri , lexbor_serialize_callback , & uri_str , exclude_fragment );
583
608
lxb_unicode_idna_clean (lexbor_parser -> idna );
0 commit comments