Skip to content

Commit 2850224

Browse files
committed
Review fixes
1 parent 0dc9669 commit 2850224

10 files changed

Lines changed: 173 additions & 11 deletions

ext/lexbor/lexbor/url/url.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,15 @@ lxb_url_port_set(lxb_url_t *url, uint16_t port)
11831183
url->has_port = true;
11841184
}
11851185

1186-
static void
1186+
void
1187+
lxb_url_query_set_null(lxb_url_t *url)
1188+
{
1189+
if (url->query.data != NULL) {
1190+
(void) lexbor_str_destroy(&url->query, url->mraw, false);
1191+
}
1192+
}
1193+
1194+
void
11871195
lxb_url_fragment_set_null(lxb_url_t *url)
11881196
{
11891197
if (url->fragment.data != NULL) {

ext/lexbor/lexbor/url/url.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,12 @@ lxb_url_path_set_null(lxb_url_t *url);
900900
LXB_API void
901901
lxb_url_host_set_empty(lxb_url_host_t *host, lexbor_mraw_t *mraw);
902902

903+
LXB_API void
904+
lxb_url_query_set_null(lxb_url_t *url);
905+
906+
LXB_API void
907+
lxb_url_fragment_set_null(lxb_url_t *url);
908+
903909
/*
904910
* Inline functions.
905911
*/

ext/uri/tests/whatwg/builder/basic_error_with_opaque_base.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ $builder->setPath("/foo/bar/baz");
99
try {
1010
$builder->build(new Uri\WhatWg\Url("scheme:opaque-path"));
1111
} catch (Throwable $e) {
12-
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
12+
echo $e::class, ': ', $e->getMessage(), "\n";
1313
}
1414

1515
?>

ext/uri/tests/whatwg/builder/basic_success_with_scheme_relative_url.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));
1515

1616
?>
1717
--EXPECTF--
18-
string(45) "https://user:pass@example.net:124/foo/bar/baz"
18+
string(35) "https://example.net:124/foo/bar/baz"
1919
object(Uri\WhatWg\Url)#%d (%d) {
2020
["scheme"]=>
2121
string(5) "https"
2222
["username"]=>
23-
string(4) "user"
23+
NULL
2424
["password"]=>
25-
string(4) "pass"
25+
NULL
2626
["host"]=>
2727
string(11) "example.net"
2828
["port"]=>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
Test Uri\WhatWg\UrlBuilder::setFragment() - success - with base URL
3+
--FILE--
4+
<?php
5+
6+
$builder = new Uri\WhatWg\UrlBuilder();
7+
$builder->setFragment("foo");
8+
$url = $builder->build(new Uri\WhatWg\Url("https://example.com/#bar"));
9+
10+
var_dump($url->toAsciiString());
11+
var_dump($url);
12+
var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));
13+
14+
?>
15+
--EXPECTF--
16+
string(24) "https://example.com/#foo"
17+
object(Uri\WhatWg\Url)#%d (%d) {
18+
["scheme"]=>
19+
string(5) "https"
20+
["username"]=>
21+
NULL
22+
["password"]=>
23+
NULL
24+
["host"]=>
25+
string(11) "example.com"
26+
["port"]=>
27+
NULL
28+
["path"]=>
29+
string(1) "/"
30+
["query"]=>
31+
NULL
32+
["fragment"]=>
33+
string(3) "foo"
34+
}
35+
bool(true)
36+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test Uri\WhatWg\UrlBuilder::setPassword() - error - missing opaque host with base URL
3+
--FILE--
4+
<?php
5+
6+
$builder = new Uri\WhatWg\UrlBuilder();
7+
$builder->setPassword("password");
8+
9+
try {
10+
$builder->build(new Uri\WhatWg\Url("https://example.com"));
11+
} catch (Throwable $e) {
12+
echo $e::class, ': ', $e->getMessage(), "\n";
13+
}
14+
15+
?>
16+
--EXPECT--
17+
Uri\WhatWg\InvalidUrlException: The specified URL cannot have password
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test Uri\WhatWg\UrlBuilder::setPort() - error - missing opaque host with base URL
3+
--FILE--
4+
<?php
5+
6+
$builder = new Uri\WhatWg\UrlBuilder();
7+
$builder->setPort(123);
8+
9+
try {
10+
$builder->build(new Uri\WhatWg\Url("https://example.com"));
11+
} catch (Throwable $e) {
12+
echo $e::class, ': ', $e->getMessage(), "\n";
13+
}
14+
15+
?>
16+
--EXPECT--
17+
Uri\WhatWg\InvalidUrlException: The specified URL cannot have port
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Test Uri\WhatWg\UrlBuilder::setQuery() - success - with base URL
3+
--FILE--
4+
<?php
5+
6+
$builder = new Uri\WhatWg\UrlBuilder();
7+
$builder->setQuery("foo=bar");
8+
$url = $builder->build(new Uri\WhatWg\Url("https://example.com/?baz"));
9+
10+
var_dump($url->toAsciiString());
11+
var_dump($url);
12+
var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));
13+
14+
?>
15+
--EXPECTF--
16+
string(28) "https://example.com/?foo=bar"
17+
object(Uri\WhatWg\Url)#%d (%d) {
18+
["scheme"]=>
19+
string(5) "https"
20+
["username"]=>
21+
NULL
22+
["password"]=>
23+
NULL
24+
["host"]=>
25+
string(11) "example.com"
26+
["port"]=>
27+
NULL
28+
["path"]=>
29+
string(1) "/"
30+
["query"]=>
31+
string(7) "foo=bar"
32+
["fragment"]=>
33+
NULL
34+
}
35+
bool(true)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test Uri\WhatWg\UrlBuilder::setUsername() - error - missing opaque host with base URL
3+
--FILE--
4+
<?php
5+
6+
$builder = new Uri\WhatWg\UrlBuilder();
7+
$builder->setUsername("username");
8+
9+
try {
10+
$builder->build(new Uri\WhatWg\Url("https://example.com"));
11+
} catch (Throwable $e) {
12+
echo $e::class, ': ', $e->getMessage(), "\n";
13+
}
14+
15+
?>
16+
--EXPECT--
17+
Uri\WhatWg\InvalidUrlException: The specified URL cannot have username

ext/uri/uri_parser_whatwg.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,14 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 2, 3, 4, 5, 6, 7, 8, 9) lxb_url_t *php_uri_parser
10011001
if (status != LXB_STATUS_OK) {
10021002
goto failure;
10031003
}
1004+
} else if (lexbor_base_url->username.data != NULL) {
1005+
zval zv;
1006+
ZVAL_NULL(&zv);
1007+
const zend_result result = php_uri_parser_whatwg_username_write(lexbor_url, &zv, NULL);
1008+
php_uri_parser_whatwg_build_errors(&errors);
1009+
if (result == FAILURE) {
1010+
goto failure;
1011+
}
10041012
}
10051013

10061014
if (Z_TYPE_P(password) == IS_STRING) {
@@ -1012,6 +1020,14 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 2, 3, 4, 5, 6, 7, 8, 9) lxb_url_t *php_uri_parser
10121020
if (status != LXB_STATUS_OK) {
10131021
goto failure;
10141022
}
1023+
} else if (lexbor_base_url->password.data != NULL) {
1024+
zval zv;
1025+
ZVAL_NULL(&zv);
1026+
const zend_result result = php_uri_parser_whatwg_password_write(lexbor_url, &zv, NULL);
1027+
php_uri_parser_whatwg_build_errors(&errors);
1028+
if (result == FAILURE) {
1029+
goto failure;
1030+
}
10151031
}
10161032

10171033
if (Z_TYPE_P(host) == IS_STRING) {
@@ -1037,6 +1053,14 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 2, 3, 4, 5, 6, 7, 8, 9) lxb_url_t *php_uri_parser
10371053
if (status != LXB_STATUS_OK) {
10381054
goto failure;
10391055
}
1056+
} else if (lexbor_base_url->has_port) {
1057+
zval zv;
1058+
ZVAL_NULL(&zv);
1059+
const zend_result result = php_uri_parser_whatwg_port_write(lexbor_url, &zv, NULL);
1060+
php_uri_parser_whatwg_build_errors(&errors);
1061+
if (result == FAILURE) {
1062+
goto failure;
1063+
}
10401064
}
10411065

10421066
if (Z_TYPE_P(path) == IS_STRING && Z_STRLEN_P(path) > 0) {
@@ -1060,6 +1084,7 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 2, 3, 4, 5, 6, 7, 8, 9) lxb_url_t *php_uri_parser
10601084
}
10611085

10621086
if (Z_TYPE_P(query) == IS_STRING) {
1087+
lxb_url_query_set_null(lexbor_url);
10631088
status = lxb_url_parse_basic(&lexbor_parser, lexbor_url, lexbor_base_url,
10641089
(lxb_char_t *) Z_STRVAL_P(query), Z_STRLEN_P(query),
10651090
LXB_URL_STATE_QUERY_STATE, LXB_ENCODING_AUTO
@@ -1079,6 +1104,7 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 2, 3, 4, 5, 6, 7, 8, 9) lxb_url_t *php_uri_parser
10791104
}
10801105

10811106
if (Z_TYPE_P(fragment) == IS_STRING) {
1107+
lxb_url_fragment_set_null(lexbor_url);
10821108
status = lxb_url_parse_basic(&lexbor_parser, lexbor_url, lexbor_base_url,
10831109
(lxb_char_t *) Z_STRVAL_P(fragment), Z_STRLEN_P(fragment),
10841110
LXB_URL_STATE_FRAGMENT_STATE, LXB_ENCODING_AUTO
@@ -1116,12 +1142,6 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(2, 3, 4, 5, 6, 7, 8, 9) lxb_url_t *php_uri_parser_wh
11161142
) {
11171143
lxb_url_parser_clean(&lexbor_parser);
11181144

1119-
if (lexbor_base_url != NULL && Z_TYPE_P(scheme) == IS_STRING && Z_STRLEN_P(scheme) == 0) {
1120-
return php_uri_parser_whatwg_resolve_reference_from_zval(
1121-
lexbor_base_url, scheme, username, password, host, port, path, query, fragment, errors_zv
1122-
);
1123-
}
1124-
11251145
if (Z_TYPE_P(host) == IS_NULL ||
11261146
Z_STRLEN_P(host) == 0 ||
11271147
php_uri_parser_whatwg_get_special_scheme(Z_STR_P(scheme)) == LXB_URL_SCHEMEL_TYPE_FILE
@@ -1142,6 +1162,12 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(2, 3, 4, 5, 6, 7, 8, 9) lxb_url_t *php_uri_parser_wh
11421162
}
11431163
}
11441164

1165+
if (lexbor_base_url != NULL && Z_TYPE_P(scheme) == IS_STRING && Z_STRLEN_P(scheme) == 0) {
1166+
return php_uri_parser_whatwg_resolve_reference_from_zval(
1167+
lexbor_base_url, scheme, username, password, host, port, path, query, fragment, errors_zv
1168+
);
1169+
}
1170+
11451171
lxb_url_t *lexbor_url = lexbor_mraw_calloc(lexbor_parser.mraw, sizeof(*lexbor_url));
11461172
if (lexbor_url == NULL) {
11471173
zend_throw_exception(php_uri_ce_whatwg_invalid_url_exception, "Memory allocation error", 0);

0 commit comments

Comments
 (0)