Skip to content

Commit 120ff96

Browse files
committed
ext/uri: fixed url component delimiter handling
1 parent 1f1d223 commit 120ff96

3 files changed

Lines changed: 96 additions & 14 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
Test Uri\WhatWg\UrlBuilder::setFragment() - success - hashmark only with base URL
3+
--FILE--
4+
<?php
5+
6+
$base = new Uri\WhatWg\Url('https://example.com/base/path?oldQuery#oldFragment');
7+
8+
$url = new Uri\WhatWg\UrlBuilder()
9+
->setFragment('#')
10+
->build($base);
11+
12+
var_dump($url->toAsciiString());
13+
var_dump($url);
14+
var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));
15+
var_dump($url->equals(new Uri\WhatWg\Url('#', $base), Uri\UriComparisonMode::IncludeFragment));
16+
17+
?>
18+
--EXPECTF--
19+
string(39) "https://example.com/base/path?oldQuery#"
20+
object(Uri\WhatWg\Url)#%d (%d) {
21+
["scheme"]=>
22+
string(5) "https"
23+
["username"]=>
24+
NULL
25+
["password"]=>
26+
NULL
27+
["host"]=>
28+
string(11) "example.com"
29+
["port"]=>
30+
NULL
31+
["path"]=>
32+
string(10) "/base/path"
33+
["query"]=>
34+
string(8) "oldQuery"
35+
["fragment"]=>
36+
string(0) ""
37+
}
38+
bool(true)
39+
bool(true)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
Test Uri\WhatWg\UrlBuilder::setQuery() - success - question mark only with base URL
3+
--FILE--
4+
<?php
5+
6+
$base = new Uri\WhatWg\Url('https://example.com/base/path?oldQuery#oldFragment');
7+
8+
$url = new Uri\WhatWg\UrlBuilder()
9+
->setQuery('?')
10+
->build($base);
11+
12+
var_dump($url->toAsciiString());
13+
var_dump($url);
14+
var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));
15+
var_dump($url->equals(new Uri\WhatWg\Url('?', $base), Uri\UriComparisonMode::IncludeFragment));
16+
17+
?>
18+
--EXPECTF--
19+
string(30) "https://example.com/base/path?"
20+
object(Uri\WhatWg\Url)#%d (%d) {
21+
["scheme"]=>
22+
string(5) "https"
23+
["username"]=>
24+
NULL
25+
["password"]=>
26+
NULL
27+
["host"]=>
28+
string(11) "example.com"
29+
["port"]=>
30+
NULL
31+
["path"]=>
32+
string(10) "/base/path"
33+
["query"]=>
34+
string(0) ""
35+
["fragment"]=>
36+
NULL
37+
}
38+
bool(true)
39+
bool(true)

‎ext/uri/uri_parser_whatwg.c‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,26 +1166,30 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 2, 3, 4, 5, 6, 7, 8, 9) lxb_url_t *php_uri_parser
11661166

11671167
if (Z_TYPE_P(query) == IS_STRING) {
11681168
php_uri_parser_whatwg_query_set_null(lexbor_url);
1169-
lxb_url_parser_clean(&lexbor_parser);
1170-
status = lxb_url_parse_basic(&lexbor_parser, lexbor_url, lexbor_base_url,
1171-
(lxb_char_t *) Z_STRVAL_P(query), Z_STRLEN_P(query),
1172-
LXB_URL_STATE_QUERY_STATE, LXB_ENCODING_AUTO
1173-
);
1174-
php_uri_parser_whatwg_build_errors_and_throw(status, "query", &errors);
1175-
if (status != LXB_STATUS_OK) {
1169+
zend_result result;
1170+
if (Z_STRLEN_P(query) == 0) {
1171+
lexbor_str_init(&lexbor_url->query, lexbor_url->mraw, 1);
1172+
result = SUCCESS;
1173+
} else {
1174+
result = php_uri_parser_whatwg_query_write(lexbor_url, query, NULL);
1175+
}
1176+
php_uri_parser_whatwg_build_errors(&errors);
1177+
if (result == FAILURE) {
11761178
goto failure;
11771179
}
11781180
}
11791181

11801182
if (Z_TYPE_P(fragment) == IS_STRING) {
11811183
php_uri_parser_whatwg_fragment_set_null(lexbor_url);
1182-
lxb_url_parser_clean(&lexbor_parser);
1183-
status = lxb_url_parse_basic(&lexbor_parser, lexbor_url, lexbor_base_url,
1184-
(lxb_char_t *) Z_STRVAL_P(fragment), Z_STRLEN_P(fragment),
1185-
LXB_URL_STATE_FRAGMENT_STATE, LXB_ENCODING_AUTO
1186-
);
1187-
php_uri_parser_whatwg_build_errors_and_throw(status, "fragment", &errors);
1188-
if (status != LXB_STATUS_OK) {
1184+
zend_result result;
1185+
if (Z_STRLEN_P(fragment) == 0) {
1186+
lexbor_str_init(&lexbor_url->fragment, lexbor_url->mraw, 1);
1187+
result = SUCCESS;
1188+
} else {
1189+
result = php_uri_parser_whatwg_fragment_write(lexbor_url, fragment, NULL);
1190+
}
1191+
php_uri_parser_whatwg_build_errors(&errors);
1192+
if (result == FAILURE) {
11891193
goto failure;
11901194
}
11911195
}

0 commit comments

Comments
 (0)