Skip to content

Commit 60911e5

Browse files
committed
ext/uri: fixed url component delimiter handling
1 parent 1228a4c commit 60911e5

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
@@ -1143,26 +1143,30 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 2, 3, 4, 5, 6, 7, 8, 9) lxb_url_t *php_uri_parser
11431143

11441144
if (Z_TYPE_P(query) == IS_STRING) {
11451145
lxb_url_query_set_null(lexbor_url);
1146-
lxb_url_parser_clean(&lexbor_parser);
1147-
status = lxb_url_parse_basic(&lexbor_parser, lexbor_url, lexbor_base_url,
1148-
(lxb_char_t *) Z_STRVAL_P(query), Z_STRLEN_P(query),
1149-
LXB_URL_STATE_QUERY_STATE, LXB_ENCODING_AUTO
1150-
);
1151-
php_uri_parser_whatwg_build_errors_and_throw(status, "query", &errors);
1152-
if (status != LXB_STATUS_OK) {
1146+
zend_result result;
1147+
if (Z_STRLEN_P(query) == 0) {
1148+
lexbor_str_init(&lexbor_url->query, lexbor_url->mraw, 1);
1149+
result = SUCCESS;
1150+
} else {
1151+
result = php_uri_parser_whatwg_query_write(lexbor_url, query, NULL);
1152+
}
1153+
php_uri_parser_whatwg_build_errors(&errors);
1154+
if (result == FAILURE) {
11531155
goto failure;
11541156
}
11551157
}
11561158

11571159
if (Z_TYPE_P(fragment) == IS_STRING) {
11581160
lxb_url_fragment_set_null(lexbor_url);
1159-
lxb_url_parser_clean(&lexbor_parser);
1160-
status = lxb_url_parse_basic(&lexbor_parser, lexbor_url, lexbor_base_url,
1161-
(lxb_char_t *) Z_STRVAL_P(fragment), Z_STRLEN_P(fragment),
1162-
LXB_URL_STATE_FRAGMENT_STATE, LXB_ENCODING_AUTO
1163-
);
1164-
php_uri_parser_whatwg_build_errors_and_throw(status, "fragment", &errors);
1165-
if (status != LXB_STATUS_OK) {
1161+
zend_result result;
1162+
if (Z_STRLEN_P(fragment) == 0) {
1163+
lexbor_str_init(&lexbor_url->fragment, lexbor_url->mraw, 1);
1164+
result = SUCCESS;
1165+
} else {
1166+
result = php_uri_parser_whatwg_fragment_write(lexbor_url, fragment, NULL);
1167+
}
1168+
php_uri_parser_whatwg_build_errors(&errors);
1169+
if (result == FAILURE) {
11661170
goto failure;
11671171
}
11681172
}

0 commit comments

Comments
 (0)