2121#include "Zend/zend_exceptions.h"
2222
2323static void uriparser_free_uri (void * uri );
24- static void throw_invalid_uri_exception (void );
2524
2625static void * uriparser_malloc (UriMemoryManager * memory_manager , size_t size )
2726{
@@ -293,21 +292,25 @@ static uriparser_uris_t *uriparser_create_uris(void)
293292 return uriparser_uris ;
294293}
295294
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- }
300295void * uriparser_parse_uri_ex (const zend_string * uri_str , const uriparser_uris_t * uriparser_base_urls , bool silent )
301296{
302297 UriUriA uri = {0 };
303298
304299 /* Empty URIs are always invalid. */
305300 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+
306305 goto fail ;
307306 }
308307
309308 /* Parse the URI. */
310309 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+
311314 goto fail ;
312315 }
313316
@@ -316,7 +319,20 @@ void *uriparser_parse_uri_ex(const zend_string *uri_str, const uriparser_uris_t
316319
317320 /* Combine the parsed URI with the base URI and store the result in 'tmp',
318321 * 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+
320336 goto fail ;
321337 }
322338
@@ -337,10 +353,6 @@ void *uriparser_parse_uri_ex(const zend_string *uri_str, const uriparser_uris_t
337353
338354 uriFreeUriMembersMmA (& uri , mm );
339355
340- if (!silent ) {
341- throw_invalid_uri_exception ();
342- }
343-
344356 return NULL ;
345357}
346358
0 commit comments