Skip to content

Commit d8e4aa3

Browse files
committed
introduce strict mode
1 parent 56a8baf commit d8e4aa3

File tree

7 files changed

+59
-43
lines changed

7 files changed

+59
-43
lines changed

ext/standard/basic_functions.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3418,7 +3418,7 @@ function uniqid(string $prefix = "", bool $more_entropy = false): string {}
34183418
* @return int|string|array<string, int|string>|null|false
34193419
* @refcount 1
34203420
*/
3421-
function parse_url(string $url, int $component = -1): int|string|array|null|false {}
3421+
function parse_url(string $url, int $component = -1, bool $strict = true): int|string|array|null|false {}
34223422

34233423
/**
34243424
* @compile-time-eval

ext/standard/basic_functions_arginfo.h

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/ftp_fopen_wrapper.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static php_stream *php_ftp_fopen_connect(php_stream_wrapper *wrapper, const char
133133
char *transport;
134134
int transport_len;
135135

136-
resource = php_url_parse(path);
136+
resource = php_url_parse_ex(path, strlen(path));
137137
if (resource == NULL || resource->path == NULL) {
138138
if (resource && presource) {
139139
*presource = resource;
@@ -949,8 +949,8 @@ static int php_stream_ftp_rename(php_stream_wrapper *wrapper, const char *url_fr
949949
int result;
950950
char tmp_line[512];
951951

952-
resource_from = php_url_parse(url_from);
953-
resource_to = php_url_parse(url_to);
952+
resource_from = php_url_parse_ex(url_from, strlen(url_from));
953+
resource_to = php_url_parse_ex(url_to, strlen(url_to));
954954
/* Must be same scheme (ftp/ftp or ftps/ftps), same host, and same port
955955
(or a 21/0 0/21 combination which is also "same")
956956
Also require paths to/from */

ext/standard/http_fopen_wrapper.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
153153
return NULL;
154154
}
155155

156-
resource = php_url_parse(path);
156+
resource = php_url_parse_ex(path, strlen(path));
157157
if (resource == NULL) {
158158
return NULL;
159159
}
@@ -888,7 +888,7 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
888888

889889
php_url_free(resource);
890890
/* check for invalid redirection URLs */
891-
if ((resource = php_url_parse(new_path)) == NULL) {
891+
if ((resource = php_url_parse_ex(new_path, strlen(new_path))) == NULL) {
892892
php_stream_wrapper_log_error(wrapper, options, "Invalid redirect URL! %s", new_path);
893893
goto out;
894894
}

ext/standard/tests/url/parse_url_unterminated.phpt

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,13 @@ include_once(__DIR__ . '/urls.inc');
1414
foreach ($urls as $url) {
1515
echo "\n--> $url: ";
1616
$str = zend_create_unterminated_string($url);
17-
try {
18-
var_dump(parse_url($str));
19-
} catch (ValueError $e) {
20-
echo $e->getMessage() . "\n";
21-
} finally {
22-
zend_terminate_string($str);
23-
}
17+
var_dump(parse_url($str, strict: false));
18+
zend_terminate_string($str);
2419
}
2520

2621
echo "Done";
2722
?>
28-
--EXPECTF--
23+
--EXPECT--
2924
--> 64.246.30.37: array(1) {
3025
["path"]=>
3126
string(12) "64.246.30.37"
@@ -773,7 +768,7 @@ echo "Done";
773768
string(9) "/blah.com"
774769
}
775770

776-
--> x://::abc/?: Invalid port (abc)
771+
--> x://::abc/?: bool(false)
777772

778773
--> http://::?: array(3) {
779774
["scheme"]=>
@@ -802,9 +797,9 @@ echo "Done";
802797
int(6)
803798
}
804799

805-
--> http://?:/: Invalid host (?:/)
800+
--> http://?:/: bool(false)
806801

807-
--> http://@?:/: Invalid host (?:/)
802+
--> http://@?:/: bool(false)
808803

809804
--> file:///:: array(2) {
810805
["scheme"]=>
@@ -895,32 +890,31 @@ echo "Done";
895890
string(1) "/"
896891
}
897892

898-
--> http:///blah.com: Invalid host (%s)
893+
--> http:///blah.com: bool(false)
899894

900-
--> http://:80: Invalid host (%s)
895+
--> http://:80: bool(false)
901896

902-
--> http://user@:80: Invalid host (%s)
897+
--> http://user@:80: bool(false)
903898

904-
--> http://user:pass@:80: Invalid host (%s)
899+
--> http://user:pass@:80: bool(false)
905900

906-
--> http://:: Invalid host (%s)
901+
--> http://:: bool(false)
907902

908-
--> http://@/: Invalid host (%s)
903+
--> http://@/: bool(false)
909904

910-
--> http://@:/: Invalid host (%s)
905+
--> http://@:/: bool(false)
911906

912-
--> http://:/: Invalid host (%s)
907+
--> http://:/: bool(false)
913908

914-
--> http://?: Invalid host (%s)
909+
--> http://?: bool(false)
915910

916-
--> http://#: Invalid host (%s)
911+
--> http://#: bool(false)
917912

918-
--> http://?:: Invalid host (%s)
913+
--> http://?:: bool(false)
919914

920-
--> http://:?: Invalid host (%s)
915+
--> http://:?: bool(false)
921916

922-
--> http://blah.com:123456: Invalid port (%s)
917+
--> http://blah.com:123456: bool(false)
923918

924-
--> http://blah.com:abcdef: Invalid port (%s
925-
)
919+
--> http://blah.com:abcdef: bool(false)
926920
Done

ext/standard/url.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ PHPAPI php_url *php_url_parse_ex(char const *str, size_t length)
102102
/* {{{ php_url_parse_ex2
103103
*/
104104
PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port)
105+
{
106+
return php_url_parse_ex3(str, length, false, has_port);
107+
}
108+
109+
PHPAPI php_url *php_url_parse_ex3(char const *str, size_t length, bool strict, bool *has_port)
105110
{
106111
char port_buf[6];
107112
php_url *ret = ecalloc(1, sizeof(php_url));
@@ -203,12 +208,16 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port
203208
s += 2;
204209
}
205210
} else {
206-
zend_value_error("Invalid port (%s)", port_buf);
211+
if (strict) {
212+
zend_value_error("Invalid port (%s)", port_buf);
213+
}
207214
php_url_free(ret);
208215
return NULL;
209216
}
210217
} else if (p == pp && pp == ue) {
211-
zend_value_error("Invalid path (%s)", str);
218+
if (strict) {
219+
zend_value_error("Invalid path (%s)", str);
220+
}
212221
php_url_free(ret);
213222
return NULL;
214223
} else if (s + 1 < ue && *s == '/' && *(s + 1) == '/') { /* relative-scheme URL */
@@ -256,7 +265,9 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port
256265
if (!ret->port) {
257266
p++;
258267
if (e-p > 5) { /* port cannot be longer then 5 characters */
259-
zend_value_error("Invalid port (%s)", p);
268+
if (strict) {
269+
zend_value_error("Invalid port (%s)", p);
270+
}
260271
php_url_free(ret);
261272
return NULL;
262273
} else if (e - p > 0) {
@@ -269,7 +280,9 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port
269280
*has_port = 1;
270281
ret->port = (unsigned short)port;
271282
} else {
272-
zend_value_error("Invalid port (%s)", port_buf);
283+
if (strict) {
284+
zend_value_error("Invalid port (%s)", port_buf);
285+
}
273286
php_url_free(ret);
274287
return NULL;
275288
}
@@ -282,7 +295,9 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port
282295

283296
/* check if we have a valid host, if we don't reject the string as url */
284297
if ((p-s) < 1) {
285-
zend_value_error("Invalid host (%s)", s);
298+
if (strict) {
299+
zend_value_error("Invalid host (%s)", s);
300+
}
286301
php_url_free(ret);
287302
return NULL;
288303
}
@@ -340,18 +355,23 @@ PHP_FUNCTION(parse_url)
340355
php_url *resource;
341356
zend_long key = -1;
342357
zval tmp;
343-
bool has_port;
358+
bool has_port, strict = true;
344359

345-
ZEND_PARSE_PARAMETERS_START(1, 2)
360+
ZEND_PARSE_PARAMETERS_START(1, 3)
346361
Z_PARAM_STRING(str, str_len)
347362
Z_PARAM_OPTIONAL
348363
Z_PARAM_LONG(key)
364+
Z_PARAM_BOOL(strict)
349365
ZEND_PARSE_PARAMETERS_END();
350366

351-
resource = php_url_parse_ex2(str, str_len, &has_port);
367+
resource = php_url_parse_ex3(str, str_len, strict, &has_port);
352368
if (resource == NULL) {
353369
/* @todo Find a method to determine why php_url_parse_ex() failed */
354-
RETURN_FALSE;
370+
if (strict) {
371+
RETURN_THROWS();
372+
} else {
373+
RETURN_FALSE;
374+
}
355375
}
356376

357377
if (key > -1) {

ext/standard/url.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ typedef struct php_url {
2929
} php_url;
3030

3131
PHPAPI void php_url_free(php_url *theurl);
32-
PHPAPI php_url *php_url_parse(char const *str);
32+
PHPAPI ZEND_ATTRIBUTE_DEPRECATED php_url *php_url_parse(char const *str);
3333
PHPAPI php_url *php_url_parse_ex(char const *str, size_t length);
3434
PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port);
35+
PHPAPI php_url *php_url_parse_ex3(char const *str, size_t length, bool strict, bool *has_port);
3536
PHPAPI size_t php_url_decode(char *str, size_t len); /* return value: length of decoded string */
3637
PHPAPI size_t php_raw_url_decode(char *str, size_t len); /* return value: length of decoded string */
3738
PHPAPI zend_string *php_url_encode(char const *s, size_t len);

0 commit comments

Comments
 (0)