Skip to content

Commit ae1438b

Browse files
authored
uri/standard: Move the parse_url() URI parser into ext/uri/ (#19587)
* uri/standard: Move the `parse_url()` URI parser into ext/uri/ Making ext/standard depend on ext/uri/ is a bit iffy (given that it is called *standard*) and having the parser implementation in ext/uri/ makes it easier to find and keep in sync. * uri: Mark local pointers as `const` in uri_parser_php_parse_url.c * uri: Remove useless explicit cast from void in uri_parser_php_parse_url.c * uri: Properly prefix symbols in uri_parser_php_parse_url.[ch] * uri: Remove useless `throw_invalid_uri_exception()` helper in uri_parser_php_parse_url.c
1 parent 3888fa0 commit ae1438b

File tree

10 files changed

+225
-186
lines changed

10 files changed

+225
-186
lines changed

ext/filter/logical_filters.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ zend_result php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
634634

635635
if (
636636
/* Skipping these checks is possible because the new URI implementations perform comprehensive validations. */
637-
strcmp(uri_parser->name, URI_PARSER_PHP) == 0 &&
637+
strcmp(uri_parser->name, PHP_URI_PARSER_PHP_PARSE_URL) == 0 &&
638638
/* An IPv6 enclosed by square brackets is a valid hostname.*/
639639
!php_filter_is_valid_ipv6_hostname(uri->host) &&
640640
/* Validate domain.
@@ -655,7 +655,7 @@ zend_result php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
655655
RETURN_VALIDATION_FAILED
656656
}
657657

658-
if (strcmp(uri_parser->name, URI_PARSER_PHP) == 0 &&
658+
if (strcmp(uri_parser->name, PHP_URI_PARSER_PHP_PARSE_URL) == 0 &&
659659
(
660660
(uri->user != NULL && !is_userinfo_valid(uri->user)) ||
661661
(uri->password != NULL && !is_userinfo_valid(uri->password))

ext/standard/basic_functions.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ PHP_MINIT_FUNCTION(basic) /* {{{ */
305305
BASIC_MINIT_SUBMODULE(user_filters)
306306
BASIC_MINIT_SUBMODULE(password)
307307
BASIC_MINIT_SUBMODULE(image)
308-
BASIC_MINIT_SUBMODULE(url)
309308

310309
#ifdef ZTS
311310
BASIC_MINIT_SUBMODULE(localeconv)

ext/standard/url.c

Lines changed: 1 addition & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
#include "file.h"
2626
#include "zend_simd.h"
2727
#include "Zend/zend_smart_str.h"
28-
#include "Zend/zend_exceptions.h"
29-
#include "ext/uri/php_uri.h"
3028

3129
/* {{{ free_url */
3230
PHPAPI void php_url_free(php_url *theurl)
@@ -49,13 +47,6 @@ PHPAPI void php_url_free(php_url *theurl)
4947
}
5048
/* }}} */
5149

52-
static void parse_url_free_uri(void *uri)
53-
{
54-
php_url *parse_url_uri = (php_url *) uri;
55-
56-
php_url_free(parse_url_uri);
57-
}
58-
5950
static void php_replace_controlchars(char *str, size_t len)
6051
{
6152
unsigned char *s = (unsigned char *)str;
@@ -320,166 +311,7 @@ PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port
320311

321312
return ret;
322313
}
323-
324-
static void parse_url_decode_component(zval *zv, uri_component_read_mode_t read_mode)
325-
{
326-
if (Z_TYPE_P(zv) != IS_STRING) {
327-
return;
328-
}
329-
330-
if (read_mode == URI_COMPONENT_READ_RAW) {
331-
return;
332-
}
333-
334-
php_raw_url_decode(Z_STRVAL_P(zv), Z_STRLEN_P(zv));
335-
}
336-
337-
static zend_result parse_url_read_scheme(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
338-
{
339-
php_url *parse_url_uri = internal_uri->uri;
340-
341-
if (parse_url_uri->scheme) {
342-
ZVAL_STR_COPY(retval, parse_url_uri->scheme);
343-
parse_url_decode_component(retval, read_mode);
344-
} else {
345-
ZVAL_NULL(retval);
346-
}
347-
348-
return SUCCESS;
349-
}
350-
351-
static zend_result parse_url_read_username(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
352-
{
353-
php_url *parse_url_uri = internal_uri->uri;
354-
355-
if (parse_url_uri->user) {
356-
ZVAL_STR_COPY(retval, parse_url_uri->user);
357-
parse_url_decode_component(retval, read_mode);
358-
} else {
359-
ZVAL_NULL(retval);
360-
}
361-
362-
return SUCCESS;
363-
}
364-
365-
static zend_result parse_url_read_password(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
366-
{
367-
php_url *parse_url_uri = internal_uri->uri;
368-
369-
if (parse_url_uri->pass) {
370-
ZVAL_STR_COPY(retval, parse_url_uri->pass);
371-
parse_url_decode_component(retval, read_mode);
372-
} else {
373-
ZVAL_NULL(retval);
374-
}
375-
376-
return SUCCESS;
377-
}
378-
379-
static zend_result parse_url_read_host(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
380-
{
381-
php_url *parse_url_uri = internal_uri->uri;
382-
383-
if (parse_url_uri->host) {
384-
ZVAL_STR_COPY(retval, parse_url_uri->host);
385-
parse_url_decode_component(retval, read_mode);
386-
} else {
387-
ZVAL_NULL(retval);
388-
}
389-
390-
return SUCCESS;
391-
}
392-
393-
static zend_result parse_url_read_port(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
394-
{
395-
php_url *parse_url_uri = internal_uri->uri;
396-
397-
if (parse_url_uri->port) {
398-
ZVAL_LONG(retval, parse_url_uri->port);
399-
parse_url_decode_component(retval, read_mode);
400-
} else {
401-
ZVAL_NULL(retval);
402-
}
403-
404-
return SUCCESS;
405-
}
406-
407-
static zend_result parse_url_read_path(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
408-
{
409-
php_url *parse_url_uri = internal_uri->uri;
410-
411-
if (parse_url_uri->path) {
412-
ZVAL_STR_COPY(retval, parse_url_uri->path);
413-
parse_url_decode_component(retval, read_mode);
414-
} else {
415-
ZVAL_NULL(retval);
416-
}
417-
418-
return SUCCESS;
419-
}
420-
421-
static zend_result parse_url_read_query(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
422-
{
423-
php_url *parse_url_uri = internal_uri->uri;
424-
425-
if (parse_url_uri->query) {
426-
ZVAL_STR_COPY(retval, parse_url_uri->query);
427-
parse_url_decode_component(retval, read_mode);
428-
} else {
429-
ZVAL_NULL(retval);
430-
}
431-
432-
return SUCCESS;
433-
}
434-
435-
static zend_result parse_url_read_fragment(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
436-
{
437-
php_url *parse_url_uri = internal_uri->uri;
438-
439-
if (parse_url_uri->fragment) {
440-
ZVAL_STR_COPY(retval, parse_url_uri->fragment);
441-
parse_url_decode_component(retval, read_mode);
442-
} else {
443-
ZVAL_NULL(retval);
444-
}
445-
446-
return SUCCESS;
447-
}
448-
449-
static void throw_invalid_uri_exception(void)
450-
{
451-
zend_throw_exception(uri_invalid_uri_exception_ce, "The specified URI is malformed", 0);
452-
}
453-
454-
static void *parse_url_parse_uri(const char *uri_str, size_t uri_str_len, const void *base_url, zval *errors, bool silent)
455-
{
456-
bool has_port;
457-
458-
php_url *url = php_url_parse_ex2(uri_str, uri_str_len, &has_port);
459-
if (url == NULL && !silent) {
460-
throw_invalid_uri_exception();
461-
}
462-
463-
return url;
464-
}
465-
466-
const uri_parser_t parse_url_uri_parser = {
467-
.name = URI_PARSER_PHP,
468-
.parse_uri = parse_url_parse_uri,
469-
.clone_uri = NULL,
470-
.uri_to_string = NULL,
471-
.free_uri = parse_url_free_uri,
472-
{
473-
.scheme = {.read_func = parse_url_read_scheme, .write_func = NULL},
474-
.username = {.read_func = parse_url_read_username, .write_func = NULL},
475-
.password = {.read_func = parse_url_read_password, .write_func = NULL},
476-
.host = {.read_func = parse_url_read_host, .write_func = NULL},
477-
.port = {.read_func = parse_url_read_port, .write_func = NULL},
478-
.path = {.read_func = parse_url_read_path, .write_func = NULL},
479-
.query = {.read_func = parse_url_read_query, .write_func = NULL},
480-
.fragment = {.read_func = parse_url_read_fragment, .write_func = NULL},
481-
}
482-
};
314+
/* }}} */
483315

484316
/* {{{ Parse a URL and return its components */
485317
PHP_FUNCTION(parse_url)
@@ -921,8 +753,3 @@ PHP_FUNCTION(get_headers)
921753
php_stream_close(stream);
922754
}
923755
/* }}} */
924-
925-
PHP_MINIT_FUNCTION(url)
926-
{
927-
return php_uri_parser_register(&parse_url_uri_parser);
928-
}

ext/standard/url.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#ifndef URL_H
1818
#define URL_H
1919

20-
PHP_MINIT_FUNCTION(url);
21-
2220
typedef struct php_url {
2321
zend_string *scheme;
2422
zend_string *user;

ext/uri/config.m4

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ PHP_INSTALL_HEADERS([ext/uri], m4_normalize([
66
php_uri_common.h
77
uri_parser_rfc3986.h
88
uri_parser_whatwg.h
9+
uri_parser_php_parse_url.h
910
]))
1011

1112
AC_DEFINE([URI_ENABLE_ANSI], [1], [Define to 1 for enabling ANSI support of uriparser.])
@@ -18,6 +19,6 @@ $URIPARSER_DIR/src/UriMemory.c $URIPARSER_DIR/src/UriNormalize.c $URIPARSER_DIR/
1819
$URIPARSER_DIR/src/UriParse.c $URIPARSER_DIR/src/UriParseBase.c $URIPARSER_DIR/src/UriQuery.c \
1920
$URIPARSER_DIR/src/UriRecompose.c $URIPARSER_DIR/src/UriResolve.c $URIPARSER_DIR/src/UriShorten.c"
2021

21-
PHP_NEW_EXTENSION(uri, [php_uri.c php_uri_common.c uri_parser_rfc3986.c uri_parser_whatwg.c $URIPARSER_SOURCES], [no],,[-I$ext_srcdir/$URIPARSER_DIR/include -DURI_STATIC_BUILD -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1])
22+
PHP_NEW_EXTENSION(uri, [php_uri.c php_uri_common.c uri_parser_rfc3986.c uri_parser_whatwg.c uri_parser_php_parse_url.c $URIPARSER_SOURCES], [no],,[-I$ext_srcdir/$URIPARSER_DIR/include -DURI_STATIC_BUILD -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1])
2223
PHP_ADD_EXTENSION_DEP(uri, lexbor)
2324
PHP_ADD_BUILD_DIR($ext_builddir/$URIPARSER_DIR/src $ext_builddir/$URIPARSER_DIR/include)

ext/uri/config.w32

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
EXTENSION("uri", "php_uri.c php_uri_common.c uri_parser_rfc3986.c uri_parser_whatwg.c ", false /* never shared */, "/I ext/lexbor /I ext/uri/uriparser/include /DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
1+
EXTENSION("uri", "php_uri.c php_uri_common.c uri_parser_rfc3986.c uri_parser_whatwg.c uri_parser_php_parse_url.c", false /* never shared */, "/I ext/lexbor /I ext/uri/uriparser/include /DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
22

33
AC_DEFINE("URI_ENABLE_ANSI", 1, "Define to 1 for enabling ANSI support of uriparser.")
44
AC_DEFINE("URI_NO_UNICODE", 1, "Define to 1 for disabling unicode support of uriparser.")
55
ADD_FLAG("CFLAGS_URI", "/D URI_STATIC_BUILD");
66

77
ADD_EXTENSION_DEP('uri', 'lexbor');
88
ADD_SOURCES("ext/uri/uriparser/src", "UriCommon.c UriCompare.c UriCopy.c UriEscape.c UriFile.c UriIp4.c UriIp4Base.c UriMemory.c UriNormalize.c UriNormalizeBase.c UriParse.c UriParseBase.c UriQuery.c UriRecompose.c UriResolve.c UriShorten.c", "uri");
9-
PHP_INSTALL_HEADERS("ext/uri", "php_uri.h php_uri_common.h uri_parser_rfc3986.h uri_parser_whatwg.h uriparser/src uriparser/include");
9+
PHP_INSTALL_HEADERS("ext/uri", "php_uri.h php_uri_common.h uri_parser_rfc3986.h uri_parser_whatwg.h uri_parser_php_parse_url.h uriparser/src uriparser/include");

ext/uri/php_uri.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "php_uri.h"
2929
#include "uri_parser_whatwg.h"
3030
#include "uri_parser_rfc3986.h"
31+
#include "uri_parser_php_parse_url.h"
3132
#include "php_uri_arginfo.h"
3233
#include "uriparser/src/UriConfig.h"
3334

@@ -109,7 +110,7 @@ static HashTable *uri_get_debug_properties(zend_object *object)
109110
PHPAPI uri_parser_t *php_uri_get_parser(const zend_string *uri_parser_name)
110111
{
111112
if (uri_parser_name == NULL) {
112-
return uri_parser_by_name(URI_PARSER_PHP, sizeof(URI_PARSER_PHP) - 1);
113+
return uri_parser_by_name(PHP_URI_PARSER_PHP_PARSE_URL, sizeof(PHP_URI_PARSER_PHP_PARSE_URL) - 1);
113114
}
114115

115116
return uri_parser_by_name(ZSTR_VAL(uri_parser_name), ZSTR_LEN(uri_parser_name));
@@ -1021,8 +1022,8 @@ PHPAPI zend_result php_uri_parser_register(const uri_parser_t *uri_parser)
10211022

10221023
ZEND_ASSERT(uri_parser->name != NULL);
10231024
ZEND_ASSERT(uri_parser->parse_uri != NULL);
1024-
ZEND_ASSERT(uri_parser->clone_uri != NULL || strcmp(uri_parser->name, URI_PARSER_PHP) == 0);
1025-
ZEND_ASSERT(uri_parser->uri_to_string != NULL || strcmp(uri_parser->name, URI_PARSER_PHP) == 0);
1025+
ZEND_ASSERT(uri_parser->clone_uri != NULL || strcmp(uri_parser->name, PHP_URI_PARSER_PHP_PARSE_URL) == 0);
1026+
ZEND_ASSERT(uri_parser->uri_to_string != NULL || strcmp(uri_parser->name, PHP_URI_PARSER_PHP_PARSE_URL) == 0);
10261027
ZEND_ASSERT(uri_parser->free_uri != NULL);
10271028

10281029
zend_result result = zend_hash_add_ptr(&uri_parsers, key, (void *) uri_parser) != NULL ? SUCCESS : FAILURE;
@@ -1057,6 +1058,10 @@ static PHP_MINIT_FUNCTION(uri)
10571058
return FAILURE;
10581059
}
10591060

1061+
if (php_uri_parser_register(&php_uri_parser_php_parse_url) == FAILURE) {
1062+
return FAILURE;
1063+
}
1064+
10601065
return SUCCESS;
10611066
}
10621067

ext/uri/php_uri_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static inline uri_internal_t *uri_internal_from_obj(const zend_object *object) {
164164

165165
#define PHP_URI_PARSER_RFC3986 "Uri\\Rfc3986\\Uri"
166166
#define PHP_URI_PARSER_WHATWG "Uri\\WhatWg\\Url"
167-
#define URI_PARSER_PHP "parse_url"
167+
#define PHP_URI_PARSER_PHP_PARSE_URL "parse_url"
168168
#define URI_SERIALIZED_PROPERTY_NAME "uri"
169169

170170
const uri_property_handler_t *uri_property_handler_from_internal_uri(const uri_internal_t *internal_uri, uri_property_name_t property_name);

0 commit comments

Comments
 (0)