@@ -76,9 +76,9 @@ PHPAPI char *php_replace_controlchars(char *str)
7676 return php_replace_controlchars_ex (str , strlen (str ));
7777}
7878
79- PHPAPI php_url * php_url_parse (char const * str )
79+ PHPAPI php_url * php_url_parse (char const * str , bool strict )
8080{
81- return php_url_parse_ex (str , strlen (str ));
81+ return php_url_parse_ex (str , strlen (str ), strict );
8282}
8383
8484static const char * binary_strcspn (const char * s , const char * e , const char * chars ) {
@@ -93,15 +93,15 @@ static const char *binary_strcspn(const char *s, const char *e, const char *char
9393}
9494
9595/* {{{ php_url_parse */
96- PHPAPI php_url * php_url_parse_ex (char const * str , size_t length )
96+ PHPAPI php_url * php_url_parse_ex (char const * str , size_t length , bool strict )
9797{
9898 bool has_port ;
99- return php_url_parse_ex2 (str , length , & has_port );
99+ return php_url_parse_ex2 (str , length , strict , & has_port );
100100}
101101
102102/* {{{ php_url_parse_ex2
103103 */
104- PHPAPI php_url * php_url_parse_ex2 (char const * str , size_t length , bool * has_port )
104+ PHPAPI php_url * php_url_parse_ex2 (char const * str , size_t length , bool strict , bool * has_port )
105105{
106106 char port_buf [6 ];
107107 php_url * ret = ecalloc (1 , sizeof (php_url ));
@@ -203,12 +203,16 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port
203203 s += 2 ;
204204 }
205205 } else {
206- zend_value_error ("Invalid port (%s)" , port_buf );
206+ if (strict ) {
207+ zend_value_error ("Invalid port (%s)" , port_buf );
208+ }
207209 php_url_free (ret );
208210 return NULL ;
209211 }
210212 } else if (p == pp && pp == ue ) {
211- zend_value_error ("Invalid path (%s)" , str );
213+ if (strict ) {
214+ zend_value_error ("Invalid path (%s)" , str );
215+ }
212216 php_url_free (ret );
213217 return NULL ;
214218 } else if (s + 1 < ue && * s == '/' && * (s + 1 ) == '/' ) { /* relative-scheme URL */
@@ -256,7 +260,9 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port
256260 if (!ret -> port ) {
257261 p ++ ;
258262 if (e - p > 5 ) { /* port cannot be longer then 5 characters */
259- zend_value_error ("Invalid port (%s)" , p );
263+ if (strict ) {
264+ zend_value_error ("Invalid port (%s)" , p );
265+ }
260266 php_url_free (ret );
261267 return NULL ;
262268 } else if (e - p > 0 ) {
@@ -269,7 +275,9 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port
269275 * has_port = 1 ;
270276 ret -> port = (unsigned short )port ;
271277 } else {
272- zend_value_error ("Invalid port (%s)" , port_buf );
278+ if (strict ) {
279+ zend_value_error ("Invalid port (%s)" , port_buf );
280+ }
273281 php_url_free (ret );
274282 return NULL ;
275283 }
@@ -282,7 +290,9 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port
282290
283291 /* check if we have a valid host, if we don't reject the string as url */
284292 if ((p - s ) < 1 ) {
285- zend_value_error ("Invalid host (%s)" , s );
293+ if (strict ) {
294+ zend_value_error ("Invalid host (%s)" , s );
295+ }
286296 php_url_free (ret );
287297 return NULL ;
288298 }
@@ -340,18 +350,23 @@ PHP_FUNCTION(parse_url)
340350 php_url * resource ;
341351 zend_long key = -1 ;
342352 zval tmp ;
343- bool has_port ;
353+ bool has_port , strict = true ;
344354
345- ZEND_PARSE_PARAMETERS_START (1 , 2 )
355+ ZEND_PARSE_PARAMETERS_START (1 , 3 )
346356 Z_PARAM_STRING (str , str_len )
347357 Z_PARAM_OPTIONAL
348358 Z_PARAM_LONG (key )
359+ Z_PARAM_BOOL (strict )
349360 ZEND_PARSE_PARAMETERS_END ();
350361
351- resource = php_url_parse_ex2 (str , str_len , & has_port );
362+ resource = php_url_parse_ex2 (str , str_len , strict , & has_port );
352363 if (resource == NULL) {
353364 /* @todo Find a method to determine why php_url_parse_ex() failed */
354- RETURN_FALSE ;
365+ if (strict ) {
366+ RETURN_THROWS ();
367+ } else {
368+ RETURN_FALSE ;
369+ }
355370 }
356371
357372 if (key > -1 ) {
0 commit comments