Skip to content

Commit 7e2ba9d

Browse files
committed
uri: Improve exceptions for Uri\Rfc3986\Uri
1 parent c38309b commit 7e2ba9d

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

ext/uri/php_uriparser.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "Zend/zend_exceptions.h"
2222

2323
static void uriparser_free_uri(void *uri);
24-
static void throw_invalid_uri_exception(void);
2524

2625
static 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-
}
300295
void *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

ext/uri/tests/004.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var_dump(Uri\WhatWg\Url::parse("http://RuPaul's Drag Race All Stars 7 Winners Ca
2929

3030
?>
3131
--EXPECTF--
32-
The specified URI is malformed
32+
The specified URI must not be empty
3333
NULL
3434
The specified URI is malformed (MissingSchemeNonRelativeUrl)
3535
NULL

ext/uri/tests/055.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ try {
1212
}
1313
?>
1414
--EXPECT--
15-
The specified URI is malformed
15+
The specified base URI must be absolute

0 commit comments

Comments
 (0)