@@ -73,20 +73,21 @@ static void lexbor_cleanup_parser(void)
73
73
* When errors is NULL, the caller is not interested in the additional error information,
74
74
* so the function does nothing.
75
75
*/
76
- static void fill_errors (zval * errors )
76
+ static zend_string * fill_errors (zval * errors )
77
77
{
78
78
if (errors == NULL ) {
79
- return ;
79
+ return NULL ;
80
80
}
81
81
82
82
ZEND_ASSERT (Z_ISUNDEF_P (errors ));
83
83
84
84
array_init (errors );
85
85
86
86
if (lexbor_parser .log == NULL ) {
87
- return ;
87
+ return NULL ;
88
88
}
89
89
90
+ zend_string * result = NULL ;
90
91
lexbor_plog_entry_t * lxb_error ;
91
92
while ((lxb_error = lexbor_array_obj_pop (& lexbor_parser .log -> list )) != NULL ) {
92
93
zval error ;
@@ -223,32 +224,29 @@ static void fill_errors(zval *errors)
223
224
224
225
zend_update_property (uri_whatwg_url_validation_error_ce , Z_OBJ (error ), ZEND_STRL ("failure" ), & failure );
225
226
227
+ if (Z_TYPE (failure ) == IS_TRUE ) {
228
+ result = error_str ;
229
+ }
230
+
226
231
add_next_index_zval (errors , & error );
227
232
}
228
- }
229
-
230
- static void throw_invalid_url_exception (zval * errors )
231
- {
232
- ZEND_ASSERT (errors != NULL && Z_TYPE_P (errors ) == IS_ARRAY );
233
-
234
- zval exception ;
235
-
236
- object_init_ex (& exception , uri_whatwg_invalid_url_exception_ce );
237
-
238
- zval value ;
239
- ZVAL_STRING (& value , "URL parsing failed" );
240
- zend_update_property_ex (uri_whatwg_invalid_url_exception_ce , Z_OBJ (exception ), ZSTR_KNOWN (ZEND_STR_MESSAGE ), & value );
241
- zval_ptr_dtor_str (& value );
242
-
243
- zend_update_property (uri_whatwg_invalid_url_exception_ce , Z_OBJ (exception ), ZEND_STRL ("errors" ), errors );
244
233
245
- zend_throw_exception_object ( & exception ) ;
234
+ return result ;
246
235
}
247
236
248
- static void throw_invalid_url_exception_during_write (zval * errors )
237
+ static void throw_invalid_url_exception_during_write (zval * errors , const char * component )
249
238
{
250
- fill_errors (errors );
251
- throw_invalid_url_exception (errors );
239
+ zend_string * reason = fill_errors (errors );
240
+ zend_object * exception = zend_throw_exception_ex (
241
+ uri_whatwg_invalid_url_exception_ce ,
242
+ 0 ,
243
+ "The specified %s is malformed%s%s%s" ,
244
+ component ,
245
+ reason ? " (" : "" ,
246
+ reason ? ZSTR_VAL (reason ) : "" ,
247
+ reason ? ")" : ""
248
+ );
249
+ zend_update_property (exception -> ce , exception , ZEND_STRL ("errors" ), errors );
252
250
}
253
251
254
252
static lxb_status_t lexbor_serialize_callback (const lxb_char_t * data , size_t length , void * ctx )
@@ -281,7 +279,7 @@ static zend_result lexbor_write_scheme(struct uri_internal_t *internal_uri, zval
281
279
zval_string_or_null_to_lexbor_str (value , & str );
282
280
283
281
if (lxb_url_api_protocol_set (lexbor_uri , & lexbor_parser , str .data , str .length ) != LXB_STATUS_OK ) {
284
- throw_invalid_url_exception_during_write (errors );
282
+ throw_invalid_url_exception_during_write (errors , "scheme" );
285
283
286
284
return FAILURE ;
287
285
}
@@ -310,7 +308,7 @@ static zend_result lexbor_write_username(uri_internal_t *internal_uri, zval *val
310
308
zval_string_or_null_to_lexbor_str (value , & str );
311
309
312
310
if (lxb_url_api_username_set (lexbor_uri , str .data , str .length ) != LXB_STATUS_OK ) {
313
- throw_invalid_url_exception_during_write (errors );
311
+ throw_invalid_url_exception_during_write (errors , "username" );
314
312
315
313
return FAILURE ;
316
314
}
@@ -339,7 +337,7 @@ static zend_result lexbor_write_password(struct uri_internal_t *internal_uri, zv
339
337
zval_string_or_null_to_lexbor_str (value , & str );
340
338
341
339
if (lxb_url_api_password_set (lexbor_uri , str .data , str .length ) != LXB_STATUS_OK ) {
342
- throw_invalid_url_exception_during_write (errors );
340
+ throw_invalid_url_exception_during_write (errors , "password" );
343
341
344
342
return FAILURE ;
345
343
}
@@ -411,7 +409,7 @@ static zend_result lexbor_write_host(struct uri_internal_t *internal_uri, zval *
411
409
zval_string_or_null_to_lexbor_str (value , & str );
412
410
413
411
if (lxb_url_api_hostname_set (lexbor_uri , & lexbor_parser , str .data , str .length ) != LXB_STATUS_OK ) {
414
- throw_invalid_url_exception_during_write (errors );
412
+ throw_invalid_url_exception_during_write (errors , "host" );
415
413
416
414
return FAILURE ;
417
415
}
@@ -440,7 +438,7 @@ static zend_result lexbor_write_port(struct uri_internal_t *internal_uri, zval *
440
438
zval_long_or_null_to_lexbor_str (value , & str );
441
439
442
440
if (lxb_url_api_port_set (lexbor_uri , & lexbor_parser , str .data , str .length ) != LXB_STATUS_OK ) {
443
- throw_invalid_url_exception_during_write (errors );
441
+ throw_invalid_url_exception_during_write (errors , "port" );
444
442
445
443
return FAILURE ;
446
444
}
@@ -469,7 +467,7 @@ static zend_result lexbor_write_path(struct uri_internal_t *internal_uri, zval *
469
467
zval_string_or_null_to_lexbor_str (value , & str );
470
468
471
469
if (lxb_url_api_pathname_set (lexbor_uri , & lexbor_parser , str .data , str .length ) != LXB_STATUS_OK ) {
472
- throw_invalid_url_exception_during_write (errors );
470
+ throw_invalid_url_exception_during_write (errors , "path" );
473
471
474
472
return FAILURE ;
475
473
}
@@ -498,7 +496,7 @@ static zend_result lexbor_write_query(struct uri_internal_t *internal_uri, zval
498
496
zval_string_or_null_to_lexbor_str (value , & str );
499
497
500
498
if (lxb_url_api_search_set (lexbor_uri , & lexbor_parser , str .data , str .length ) != LXB_STATUS_OK ) {
501
- throw_invalid_url_exception_during_write (errors );
499
+ throw_invalid_url_exception_during_write (errors , "query string" );
502
500
503
501
return FAILURE ;
504
502
}
@@ -527,7 +525,7 @@ static zend_result lexbor_write_fragment(struct uri_internal_t *internal_uri, zv
527
525
zval_string_or_null_to_lexbor_str (value , & str );
528
526
529
527
if (lxb_url_api_hash_set (lexbor_uri , & lexbor_parser , str .data , str .length ) != LXB_STATUS_OK ) {
530
- throw_invalid_url_exception_during_write (errors );
528
+ throw_invalid_url_exception_during_write (errors , "fragment" );
531
529
532
530
return FAILURE ;
533
531
}
@@ -569,10 +567,11 @@ lxb_url_t *lexbor_parse_uri_ex(const zend_string *uri_str, const lxb_url_t *lexb
569
567
lexbor_cleanup_parser ();
570
568
571
569
lxb_url_t * url = lxb_url_parse (& lexbor_parser , lexbor_base_url , (unsigned char * ) ZSTR_VAL (uri_str ), ZSTR_LEN (uri_str ));
572
- fill_errors (errors );
570
+ zend_string * reason = fill_errors (errors );
573
571
574
572
if (url == NULL && !silent ) {
575
- throw_invalid_url_exception (errors );
573
+ zend_object * exception = zend_throw_exception_ex (uri_whatwg_invalid_url_exception_ce , 0 , "The specified URI is malformed%s%s%s" , reason ? " (" : "" , reason ? ZSTR_VAL (reason ) : "" , reason ? ")" : "" );
574
+ zend_update_property (exception -> ce , exception , ZEND_STRL ("errors" ), errors );
576
575
}
577
576
578
577
return url ;
0 commit comments