Skip to content

Commit 42e60da

Browse files
committed
Remove the create_invalid_uri_exception URI handler
1 parent 2260bdc commit 42e60da

File tree

5 files changed

+48
-48
lines changed

5 files changed

+48
-48
lines changed

ext/uri/php_lexbor.c

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "php_uri_common.h"
2020
#include "Zend/zend_enum.h"
2121
#include "Zend/zend_smart_str.h"
22+
#include "Zend/zend_exceptions.h"
2223
#ifdef HAVE_ARPA_INET_H
2324
#include <arpa/inet.h>
2425
#endif
@@ -225,6 +226,30 @@ static void fill_errors(zval *errors)
225226
}
226227
}
227228

229+
static void throw_invalid_url_exception(zval *errors)
230+
{
231+
ZEND_ASSERT(errors != NULL && Z_TYPE_P(errors) == IS_ARRAY);
232+
233+
zval exception;
234+
235+
object_init_ex(&exception, uri_whatwg_invalid_url_exception_ce);
236+
237+
zval value;
238+
ZVAL_STRING(&value, "URL parsing failed");
239+
zend_update_property_ex(uri_whatwg_invalid_url_exception_ce, Z_OBJ(exception), ZSTR_KNOWN(ZEND_STR_MESSAGE), &value);
240+
zval_ptr_dtor_str(&value);
241+
242+
zend_update_property(uri_whatwg_invalid_url_exception_ce, Z_OBJ(exception), ZEND_STRL("errors"), errors);
243+
244+
zend_throw_exception_object(&exception);
245+
}
246+
247+
static void throw_invalid_url_exception_during_write(zval *errors)
248+
{
249+
fill_errors(errors);
250+
throw_invalid_url_exception(errors);
251+
}
252+
228253
static lxb_status_t lexbor_serialize_callback(const lxb_char_t *data, size_t length, void *ctx)
229254
{
230255
smart_str *uri_str = ctx;
@@ -255,7 +280,7 @@ static zend_result lexbor_write_scheme(struct uri_internal_t *internal_uri, zval
255280
zval_string_or_null_to_lexbor_str(value, &str);
256281

257282
if (lxb_url_api_protocol_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
258-
fill_errors(errors);
283+
throw_invalid_url_exception_during_write(errors);
259284

260285
return FAILURE;
261286
}
@@ -284,7 +309,7 @@ static zend_result lexbor_write_username(uri_internal_t *internal_uri, zval *val
284309
zval_string_or_null_to_lexbor_str(value, &str);
285310

286311
if (lxb_url_api_username_set(lexbor_uri, str.data, str.length) != LXB_STATUS_OK) {
287-
fill_errors(errors);
312+
throw_invalid_url_exception_during_write(errors);
288313

289314
return FAILURE;
290315
}
@@ -313,7 +338,7 @@ static zend_result lexbor_write_password(struct uri_internal_t *internal_uri, zv
313338
zval_string_or_null_to_lexbor_str(value, &str);
314339

315340
if (lxb_url_api_password_set(lexbor_uri, str.data, str.length) != LXB_STATUS_OK) {
316-
fill_errors(errors);
341+
throw_invalid_url_exception_during_write(errors);
317342

318343
return FAILURE;
319344
}
@@ -385,7 +410,7 @@ static zend_result lexbor_write_host(struct uri_internal_t *internal_uri, zval *
385410
zval_string_or_null_to_lexbor_str(value, &str);
386411

387412
if (lxb_url_api_hostname_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
388-
fill_errors(errors);
413+
throw_invalid_url_exception_during_write(errors);
389414

390415
return FAILURE;
391416
}
@@ -414,7 +439,7 @@ static zend_result lexbor_write_port(struct uri_internal_t *internal_uri, zval *
414439
zval_long_or_null_to_lexbor_str(value, &str);
415440

416441
if (lxb_url_api_port_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
417-
fill_errors(errors);
442+
throw_invalid_url_exception_during_write(errors);
418443

419444
return FAILURE;
420445
}
@@ -443,7 +468,7 @@ static zend_result lexbor_write_path(struct uri_internal_t *internal_uri, zval *
443468
zval_string_or_null_to_lexbor_str(value, &str);
444469

445470
if (lxb_url_api_pathname_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
446-
fill_errors(errors);
471+
throw_invalid_url_exception_during_write(errors);
447472

448473
return FAILURE;
449474
}
@@ -472,7 +497,7 @@ static zend_result lexbor_write_query(struct uri_internal_t *internal_uri, zval
472497
zval_string_or_null_to_lexbor_str(value, &str);
473498

474499
if (lxb_url_api_search_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
475-
fill_errors(errors);
500+
throw_invalid_url_exception_during_write(errors);
476501

477502
return FAILURE;
478503
}
@@ -501,7 +526,7 @@ static zend_result lexbor_write_fragment(struct uri_internal_t *internal_uri, zv
501526
zval_string_or_null_to_lexbor_str(value, &str);
502527

503528
if (lxb_url_api_hash_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) {
504-
fill_errors(errors);
529+
throw_invalid_url_exception_during_write(errors);
505530

506531
return FAILURE;
507532
}
@@ -538,29 +563,23 @@ void lexbor_request_shutdown(void)
538563
lexbor_urls = 0;
539564
}
540565

541-
static void *lexbor_parse_uri(const zend_string *uri_str, const void *base_url, zval *errors)
566+
lxb_url_t *lexbor_parse_uri_ex(const zend_string *uri_str, const lxb_url_t *lexbor_base_url, zval *errors, bool silent)
542567
{
543568
lexbor_cleanup_parser();
544569

545-
const lxb_url_t *lexbor_base_url = base_url;
546570
lxb_url_t *url = lxb_url_parse(&lexbor_parser, lexbor_base_url, (unsigned char *) ZSTR_VAL(uri_str), ZSTR_LEN(uri_str));
547571
fill_errors(errors);
548572

573+
if (url == NULL && !silent) {
574+
throw_invalid_url_exception(errors);
575+
}
576+
549577
return url;
550578
}
551579

552-
static void lexbor_create_invalid_uri_exception(zval *exception_zv, zval *errors)
580+
static void *lexbor_parse_uri(const zend_string *uri_str, const void *base_url, zval *errors, bool silent)
553581
{
554-
ZEND_ASSERT(Z_TYPE_P(errors) == IS_ARRAY && "Lexbor always returns an array of errors when URI parsing fails");
555-
556-
object_init_ex(exception_zv, uri_whatwg_invalid_url_exception_ce);
557-
558-
zval value;
559-
ZVAL_STRING(&value, "URL parsing failed");
560-
zend_update_property_ex(uri_whatwg_invalid_url_exception_ce, Z_OBJ_P(exception_zv), ZSTR_KNOWN(ZEND_STR_MESSAGE), &value);
561-
zval_ptr_dtor_str(&value);
562-
563-
zend_update_property(uri_whatwg_invalid_url_exception_ce, Z_OBJ_P(exception_zv), ZEND_STRL("errors"), errors);
582+
return lexbor_parse_uri_ex(uri_str, base_url, errors, silent);
564583
}
565584

566585
static void *lexbor_clone_uri(void *uri)
@@ -603,7 +622,6 @@ static void lexbor_free_uri(void *uri)
603622
const uri_handler_t lexbor_uri_handler = {
604623
.name = URI_PARSER_WHATWG,
605624
.parse_uri = lexbor_parse_uri,
606-
.create_invalid_uri_exception = lexbor_create_invalid_uri_exception,
607625
.clone_uri = lexbor_clone_uri,
608626
.uri_to_string = lexbor_uri_to_string,
609627
.free_uri = lexbor_free_uri,

ext/uri/php_lexbor.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
extern const uri_handler_t lexbor_uri_handler;
2424

25-
void lexbor_module_init(void);
26-
void lexbor_module_shutdown(void);
25+
lxb_url_t *lexbor_parse_uri_ex(const zend_string *uri_str, const lxb_url_t *lexbor_base_url, zval *errors, bool silent);
2726

2827
zend_result lexbor_request_init(void);
2928
void lexbor_request_shutdown(void);

ext/uri/php_uri.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,9 @@ PHPAPI void php_uri_instantiate_uri(
209209
base_url = internal_base_url->uri;
210210
}
211211

212-
void *uri = handler->parse_uri(uri_str, base_url, should_throw || errors_zv != NULL ? &errors : NULL);
212+
void *uri = handler->parse_uri(uri_str, base_url, should_throw || errors_zv != NULL ? &errors : NULL, !should_throw);
213213
if (UNEXPECTED(uri == NULL)) {
214214
if (should_throw) {
215-
throw_invalid_uri_exception(handler, &errors);
216-
zval_ptr_dtor(&errors);
217215
RETURN_THROWS();
218216
} else {
219217
if (pass_errors_by_ref_and_free(errors_zv, &errors) == FAILURE) {
@@ -352,7 +350,7 @@ static void uri_unserialize(INTERNAL_FUNCTION_PARAMETERS, const char *handler_na
352350
if (internal_uri->uri != NULL) {
353351
internal_uri->handler->free_uri(internal_uri->uri);
354352
}
355-
internal_uri->uri = internal_uri->handler->parse_uri(Z_STR_P(uri_zv), NULL, NULL);
353+
internal_uri->uri = internal_uri->handler->parse_uri(Z_STR_P(uri_zv), NULL, NULL, true);
356354
if (internal_uri->uri == NULL) {
357355
zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(object->ce->name));
358356
RETURN_THROWS();
@@ -617,7 +615,6 @@ zend_result uri_handler_register(const uri_handler_t *uri_handler)
617615

618616
ZEND_ASSERT(uri_handler->name != NULL);
619617
ZEND_ASSERT(uri_handler->parse_uri != NULL);
620-
ZEND_ASSERT(uri_handler->create_invalid_uri_exception != NULL);
621618
ZEND_ASSERT(uri_handler->clone_uri != NULL);
622619
ZEND_ASSERT(uri_handler->uri_to_string != NULL);
623620
ZEND_ASSERT(uri_handler->free_uri != NULL);

ext/uri/php_uri_common.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,6 @@ static zend_string *get_known_string_by_property_name(uri_property_name_t proper
6565
}
6666
}
6767

68-
void throw_invalid_uri_exception(const uri_handler_t *uri_handler, zval *errors)
69-
{
70-
zval exception_zv;
71-
72-
uri_handler->create_invalid_uri_exception(&exception_zv, errors);
73-
74-
zend_throw_exception_object(&exception_zv);
75-
}
76-
7768
void uri_read_component(INTERNAL_FUNCTION_PARAMETERS, uri_property_name_t property_name, uri_component_read_mode_t component_read_mode)
7869
{
7970
ZEND_PARSE_PARAMETERS_NONE();
@@ -117,7 +108,6 @@ static void uri_write_component_ex(INTERNAL_FUNCTION_PARAMETERS, uri_property_na
117108
zval errors;
118109
ZVAL_UNDEF(&errors);
119110
if (property_handler->write_func(new_internal_uri, property_zv, &errors) == FAILURE) {
120-
throw_invalid_uri_exception(new_internal_uri->handler, &errors);
121111
zval_ptr_dtor(&errors);
122112
zend_object_release(new_object);
123113
RETURN_THROWS();

ext/uri/php_uri_common.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,16 @@ typedef struct uri_handler_t {
8383
* returned.
8484
*
8585
* The errors by-ref parameter can contain errors that occurred during parsing.
86-
* If the input value is NULL, or there were no errors, errors should not be changed.
86+
* If the input value is NULL, or there were no errors, the errors parameter should
87+
* not be modified.
8788
*
8889
* If the URI string is valid and the base_url URI is not NULL, the URI object
8990
* is resolved against the base_url.
90-
*/
91-
void *(*parse_uri)(const zend_string *uri_str, const void *base_url, zval *errors);
92-
/**
93-
* Create a Uri\InvalidUriException instance based on the errors parameter.
94-
* The errors parameter is either an array or an UNDEF zval.
9591
*
96-
* The exception object is passed by ref to the exception_zv parameter.
92+
* If the silent parameter is true, a Uri\InvalidUriException instance must be thrown.
93+
* If the parameter is false, the possible errors should be handled by the caller.
9794
*/
98-
void (*create_invalid_uri_exception)(zval *exception_zv, zval *errors);
95+
void *(*parse_uri)(const zend_string *uri_str, const void *base_url, zval *errors, bool silent);
9996
void *(*clone_uri)(void *uri);
10097
zend_string *(*uri_to_string)(void *uri, uri_recomposition_mode_t recomposition_mode, bool exclude_fragment);
10198
void (*free_uri)(void *uri);
@@ -129,7 +126,6 @@ static inline uri_internal_t *uri_internal_from_obj(const zend_object *object) {
129126

130127
zend_result uri_handler_register(const uri_handler_t *uri_handler);
131128
const uri_property_handler_t *uri_property_handler_from_internal_uri(const uri_internal_t *internal_uri, uri_property_name_t property_name);
132-
void throw_invalid_uri_exception(const uri_handler_t *uri_handler, zval *errors);
133129
void uri_read_component(INTERNAL_FUNCTION_PARAMETERS, uri_property_name_t property_name, uri_component_read_mode_t component_read_mode);
134130
void uri_write_component_str(INTERNAL_FUNCTION_PARAMETERS, uri_property_name_t property_name);
135131
void uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAMETERS, uri_property_name_t property_name);

0 commit comments

Comments
 (0)