@@ -76,9 +76,9 @@ PHPAPI char *php_replace_controlchars(char *str)
76
76
return php_replace_controlchars_ex (str , strlen (str ));
77
77
}
78
78
79
- PHPAPI php_url * php_url_parse (char const * str )
79
+ PHPAPI php_url * php_url_parse (char const * str , bool strict )
80
80
{
81
- return php_url_parse_ex (str , strlen (str ));
81
+ return php_url_parse_ex (str , strlen (str ), strict );
82
82
}
83
83
84
84
static 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
93
93
}
94
94
95
95
/* {{{ 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 )
97
97
{
98
98
bool has_port ;
99
- return php_url_parse_ex2 (str , length , & has_port );
99
+ return php_url_parse_ex2 (str , length , strict , & has_port );
100
100
}
101
101
102
102
/* {{{ php_url_parse_ex2
103
103
*/
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 )
105
105
{
106
106
char port_buf [6 ];
107
107
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
203
203
s += 2 ;
204
204
}
205
205
} else {
206
- zend_value_error ("Invalid port (%s)" , port_buf );
206
+ if (strict ) {
207
+ zend_value_error ("Invalid port (%s)" , port_buf );
208
+ }
207
209
php_url_free (ret );
208
210
return NULL ;
209
211
}
210
212
} 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
+ }
212
216
php_url_free (ret );
213
217
return NULL ;
214
218
} 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
256
260
if (!ret -> port ) {
257
261
p ++ ;
258
262
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
+ }
260
266
php_url_free (ret );
261
267
return NULL ;
262
268
} 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
269
275
* has_port = 1 ;
270
276
ret -> port = (unsigned short )port ;
271
277
} else {
272
- zend_value_error ("Invalid port (%s)" , port_buf );
278
+ if (strict ) {
279
+ zend_value_error ("Invalid port (%s)" , port_buf );
280
+ }
273
281
php_url_free (ret );
274
282
return NULL ;
275
283
}
@@ -282,7 +290,9 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port
282
290
283
291
/* check if we have a valid host, if we don't reject the string as url */
284
292
if ((p - s ) < 1 ) {
285
- zend_value_error ("Invalid host (%s)" , s );
293
+ if (strict ) {
294
+ zend_value_error ("Invalid host (%s)" , s );
295
+ }
286
296
php_url_free (ret );
287
297
return NULL ;
288
298
}
@@ -340,18 +350,23 @@ PHP_FUNCTION(parse_url)
340
350
php_url * resource ;
341
351
zend_long key = -1 ;
342
352
zval tmp ;
343
- bool has_port ;
353
+ bool has_port , strict = true ;
344
354
345
- ZEND_PARSE_PARAMETERS_START (1 , 2 )
355
+ ZEND_PARSE_PARAMETERS_START (1 , 3 )
346
356
Z_PARAM_STRING (str , str_len )
347
357
Z_PARAM_OPTIONAL
348
358
Z_PARAM_LONG (key )
359
+ Z_PARAM_BOOL (strict )
349
360
ZEND_PARSE_PARAMETERS_END ();
350
361
351
- resource = php_url_parse_ex2 (str , str_len , & has_port );
362
+ resource = php_url_parse_ex2 (str , str_len , strict , & has_port );
352
363
if (resource == NULL) {
353
364
/* @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
+ }
355
370
}
356
371
357
372
if (key > -1 ) {
0 commit comments