21
21
#include "Zend/zend_exceptions.h"
22
22
23
23
static void uriparser_free_uri (void * uri );
24
- static void throw_invalid_uri_exception (void );
25
24
26
25
static void * uriparser_malloc (UriMemoryManager * memory_manager , size_t size )
27
26
{
@@ -293,21 +292,25 @@ static uriparser_uris_t *uriparser_create_uris(void)
293
292
return uriparser_uris ;
294
293
}
295
294
296
- static void throw_invalid_uri_exception (void )
297
- {
298
- zend_throw_exception (uri_invalid_uri_exception_ce , "The specified URI is malformed" , 0 );
299
- }
300
295
void * uriparser_parse_uri_ex (const zend_string * uri_str , const uriparser_uris_t * uriparser_base_urls , bool silent )
301
296
{
302
297
UriUriA uri = {0 };
303
298
304
299
/* Empty URIs are always invalid. */
305
300
if (ZSTR_LEN (uri_str ) == 0 ) {
301
+ if (!silent ) {
302
+ zend_throw_exception (uri_invalid_uri_exception_ce , "The specified URI must not be empty" , 0 );
303
+ }
304
+
306
305
goto fail ;
307
306
}
308
307
309
308
/* Parse the URI. */
310
309
if (uriParseSingleUriExMmA (& uri , ZSTR_VAL (uri_str ), ZSTR_VAL (uri_str ) + ZSTR_LEN (uri_str ), NULL , mm ) != URI_SUCCESS ) {
310
+ if (!silent ) {
311
+ zend_throw_exception (uri_invalid_uri_exception_ce , "The specified URI is malformed" , 0 );
312
+ }
313
+
311
314
goto fail ;
312
315
}
313
316
@@ -316,7 +319,20 @@ void *uriparser_parse_uri_ex(const zend_string *uri_str, const uriparser_uris_t
316
319
317
320
/* Combine the parsed URI with the base URI and store the result in 'tmp',
318
321
* since the target and source URLs must be distinct. */
319
- if (uriAddBaseUriExMmA (& tmp , & uri , & uriparser_base_urls -> uri , URI_RESOLVE_STRICTLY , mm ) != URI_SUCCESS ) {
322
+ int result = uriAddBaseUriExMmA (& tmp , & uri , & uriparser_base_urls -> uri , URI_RESOLVE_STRICTLY , mm );
323
+ if (result != URI_SUCCESS ) {
324
+ if (!silent ) {
325
+ switch (result ) {
326
+ case URI_ERROR_ADDBASE_REL_BASE :
327
+ zend_throw_exception (uri_invalid_uri_exception_ce , "The specified base URI must be absolute" , 0 );
328
+ break ;
329
+ default :
330
+ /* This should be unreachable in practice. */
331
+ zend_throw_exception (uri_invalid_uri_exception_ce , "Failed to resolve the specified URI against the base URI" , 0 );
332
+ break ;
333
+ }
334
+ }
335
+
320
336
goto fail ;
321
337
}
322
338
@@ -337,10 +353,6 @@ void *uriparser_parse_uri_ex(const zend_string *uri_str, const uriparser_uris_t
337
353
338
354
uriFreeUriMembersMmA (& uri , mm );
339
355
340
- if (!silent ) {
341
- throw_invalid_uri_exception ();
342
- }
343
-
344
356
return NULL ;
345
357
}
346
358
0 commit comments