Skip to content

Commit 9673773

Browse files
committed
Fix parsing URIs with empty hosts
Both RFC 3986 and WHATWG support empty hosts
1 parent 889f381 commit 9673773

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

ext/uri/php_lexbor.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,9 @@ static zend_result lexbor_read_host(const struct uri_internal_t *internal_uri, u
372372
smart_str_appendc(&host_str, ']');
373373

374374
ZVAL_NEW_STR(retval, smart_str_extract(&host_str));
375-
} else if (lexbor_uri->host.type != LXB_URL_HOST_TYPE_EMPTY && lexbor_uri->host.type != LXB_URL_HOST_TYPE__UNDEF) {
375+
} else if (lexbor_uri->host.type == LXB_URL_HOST_TYPE_EMPTY) {
376+
ZVAL_EMPTY_STRING(retval);
377+
} else if (lexbor_uri->host.type != LXB_URL_HOST_TYPE__UNDEF) {
376378
switch (read_mode) {
377379
case URI_COMPONENT_READ_NORMALIZED_UNICODE: {
378380
smart_str host_str = {0};

ext/uri/php_uriparser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ ZEND_ATTRIBUTE_NONNULL static zend_result uriparser_read_host(const uri_internal
174174
UriUriA *uriparser_uri = uriparser_read_uri(internal_uri->uri, read_mode);
175175
ZEND_ASSERT(uriparser_uri != NULL);
176176

177-
if (uriparser_uri->hostText.first != NULL && uriparser_uri->hostText.afterLast != NULL && get_text_range_length(&uriparser_uri->hostText) > 0) {
177+
if (uriparser_uri->hostText.first != NULL && uriparser_uri->hostText.afterLast != NULL) {
178178
if (uriparser_uri->hostData.ip6 != NULL || uriparser_uri->hostData.ipFuture.first != NULL) {
179179
/* the textual representation of the host is always accessible in the .hostText field no matter what the host is */
180180
smart_str host_str = {0};

ext/uri/tests/003.phpt

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Parse URL exotic URLs
2+
Parse special URIs
33
--EXTENSIONS--
44
uri
55
--FILE--
@@ -8,6 +8,8 @@ uri
88
var_dump(Uri\Rfc3986\Uri::parse("http://username:password@héééostname:9090/gah/../path?arg=vaéue#anchor"));
99
var_dump(Uri\WhatWg\Url::parse("http://username:password@héééostname:9090/gah/../path?arg=vaéue#anchor"));
1010

11+
var_dump(Uri\Rfc3986\Uri::parse("//host123/"));
12+
var_dump(Uri\Rfc3986\Uri::parse("///foo/"));
1113
var_dump(Uri\Rfc3986\Uri::parse("/page:1"));
1214
var_dump(Uri\WhatWg\Url::parse("/page:1"));
1315

@@ -32,6 +34,42 @@ object(Uri\WhatWg\Url)#%d (%d) {
3234
["fragment"]=>
3335
string(6) "anchor"
3436
}
37+
object(Uri\Rfc3986\Uri)#%d (%d) {
38+
["scheme"]=>
39+
NULL
40+
["username"]=>
41+
NULL
42+
["password"]=>
43+
NULL
44+
["host"]=>
45+
string(7) "host123"
46+
["port"]=>
47+
NULL
48+
["path"]=>
49+
string(1) "/"
50+
["query"]=>
51+
NULL
52+
["fragment"]=>
53+
NULL
54+
}
55+
object(Uri\Rfc3986\Uri)#%d (%d) {
56+
["scheme"]=>
57+
NULL
58+
["username"]=>
59+
NULL
60+
["password"]=>
61+
NULL
62+
["host"]=>
63+
string(0) ""
64+
["port"]=>
65+
NULL
66+
["path"]=>
67+
string(5) "/foo/"
68+
["query"]=>
69+
NULL
70+
["fragment"]=>
71+
NULL
72+
}
3573
object(Uri\Rfc3986\Uri)#%d (%d) {
3674
["scheme"]=>
3775
NULL

ext/uri/tests/012.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object(Uri\Rfc3986\Uri)#%d (%d) {
5757
["password"]=>
5858
NULL
5959
["host"]=>
60-
NULL
60+
string(0) ""
6161
["port"]=>
6262
NULL
6363
["path"]=>
@@ -75,7 +75,7 @@ object(Uri\WhatWg\Url)#%d (%d) {
7575
["password"]=>
7676
NULL
7777
["host"]=>
78-
NULL
78+
string(0) ""
7979
["port"]=>
8080
NULL
8181
["path"]=>

0 commit comments

Comments
 (0)