Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions ext/lexbor/lexbor/url/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,10 +599,6 @@ lxb_url_percent_decode(const lxb_char_t *data, const lxb_char_t *end,
lexbor_str_t *str, lexbor_mraw_t *mraw,
lxb_url_host_opt_t *opt);

static lxb_status_t
lxb_url_percent_decode_plus(const lxb_char_t *data, const lxb_char_t *end,
lexbor_str_t *str, lexbor_mraw_t *mraw);

static const lxb_char_t *
lxb_url_path_part_by_index(const lxb_url_t *url, size_t index,
size_t *out_length);
Expand Down Expand Up @@ -4188,7 +4184,7 @@ lxb_url_percent_decode(const lxb_char_t *data, const lxb_char_t *end,
return LXB_STATUS_OK;
}

static lxb_status_t
lxb_status_t
lxb_url_percent_decode_plus(const lxb_char_t *data, const lxb_char_t *end,
lexbor_str_t *str, lexbor_mraw_t *mraw)
{
Expand Down
4 changes: 4 additions & 0 deletions ext/lexbor/lexbor/url/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,10 @@ lxb_url_get(lxb_url_parser_t *parser)
return parser->url;
}

lxb_status_t
lxb_url_percent_decode_plus(const lxb_char_t *data, const lxb_char_t *end,
lexbor_str_t *str, lexbor_mraw_t *mraw);


#ifdef __cplusplus
} /* extern "C" */
Expand Down
3 changes: 2 additions & 1 deletion ext/uri/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ PHP_INSTALL_HEADERS([ext/uri], m4_normalize([
php_uri.h
php_uri_common.h
php_uri_decl.h
php_uri_query.h
uri_parser_rfc3986.h
uri_parser_whatwg.h
uri_parser_php_parse_url.h
Expand Down Expand Up @@ -39,7 +40,7 @@ else
PHP_EVAL_INCLINE([$LIBURIPARSER_CFLAGS])
fi

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],,[$URI_CFLAGS])
PHP_NEW_EXTENSION(uri, [php_uri.c php_uri_common.c php_uri_query.c uri_parser_rfc3986.c uri_parser_whatwg.c uri_parser_php_parse_url.c $URIPARSER_SOURCES], [no],,[$URI_CFLAGS])
PHP_ADD_EXTENSION_DEP(uri, lexbor)

if test "$PHP_EXTERNAL_URIPARSER" = "no"; then
Expand Down
4 changes: 2 additions & 2 deletions ext/uri/config.w32
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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");
EXTENSION("uri", "php_uri.c php_uri_common.c php_uri_query.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");

AC_DEFINE("URI_ENABLE_ANSI", 1, "Define to 1 for enabling ANSI support of uriparser.")
AC_DEFINE("URI_NO_UNICODE", 1, "Define to 1 for disabling unicode support of uriparser.")
Expand All @@ -9,4 +9,4 @@ ADD_SOURCES("ext/uri/uriparser/src", "UriCommon.c UriCompare.c UriCopy.c UriEsca
UriMemory.c UriNormalize.c UriNormalizeBase.c UriParse.c UriParseBase.c UriQuery.c UriRecompose.c UriResolve.c \
UriSetFragment.c UriSetHostAuto.c UriSetHostCommon.c UriSetHostIp4.c UriSetHostIp6.c UriSetHostIpFuture.c UriSetHostRegName.c \
UriSetPath.c UriSetPort.c UriSetQuery.c UriSetScheme.c UriSetUserInfo.c UriShorten.c UriVersion.c", "uri");
PHP_INSTALL_HEADERS("ext/uri", "php_uri.h php_uri_common.h php_uri_decl.h uri_parser_rfc3986.h uri_parser_whatwg.h uri_parser_php_parse_url.h uriparser/src uriparser/include");
PHP_INSTALL_HEADERS("ext/uri", "php_uri.h php_uri_common.h php_uri_decl.h php_uri_query.h uri_parser_rfc3986.h uri_parser_whatwg.h uri_parser_php_parse_url.h uriparser/src uriparser/include");
146 changes: 133 additions & 13 deletions ext/uri/php_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "ext/standard/info.h"

#include "php_uri.h"
#include "php_uri_query.h"
#include "uri_parser_whatwg.h"
#include "uri_parser_rfc3986.h"
#include "uri_parser_php_parse_url.h"
Expand All @@ -39,13 +40,16 @@ zend_class_entry *php_uri_ce_comparison_mode;
zend_class_entry *php_uri_ce_exception;
zend_class_entry *php_uri_ce_error;
zend_class_entry *php_uri_ce_invalid_uri_exception;
zend_class_entry *php_uri_ce_invalid_query_params_exception;
zend_class_entry *php_uri_ce_query_params;
zend_class_entry *php_uri_ce_whatwg_url_host_type;
zend_class_entry *php_uri_ce_whatwg_invalid_url_exception;
zend_class_entry *php_uri_ce_whatwg_url_validation_error_type;
zend_class_entry *php_uri_ce_whatwg_url_validation_error;

static zend_object_handlers object_handlers_rfc3986_uri;
static zend_object_handlers object_handlers_whatwg_uri;
static zend_object_handlers object_handlers_query_params;

typedef zend_result (*php_uri_component_validator_string)(const zend_string *component);
typedef zend_result (*php_uri_component_validator_long)(zend_long component);
Expand Down Expand Up @@ -704,6 +708,54 @@ PHP_METHOD(Uri_Rfc3986_Uri, getQuery)
php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_QUERY, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII);
}

static void uri_get_query_params(INTERNAL_FUNCTION_PARAMETERS, const php_uri_query_params_parsing_method parsing_method)
{
zend_long max_param_count = PHP_URI_QUERY_PARAMS_DEFAULT_MAX_PARAM_COUNT;

ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(max_param_count)
ZEND_PARSE_PARAMETERS_END();

if (max_param_count < 0) {
zend_argument_value_error(1, "must be greater than or equal to 0");
RETURN_THROWS();
}

const php_uri_object *uri_object = Z_URI_OBJECT_P(ZEND_THIS);
ZEND_ASSERT(uri_object->uri != NULL);

zval query;
ZVAL_UNDEF(&query);

if (UNEXPECTED(uri_object->parser->property_handler.query.read(uri_object->uri, PHP_URI_COMPONENT_READ_MODE_RAW, &query) == FAILURE)) {
zval_ptr_dtor(&query);
zend_throw_exception_ex(php_uri_ce_error, 0, "The query component cannot be retrieved");
RETURN_THROWS();
}

if (Z_TYPE(query) == IS_NULL) {
RETURN_NULL();
}

object_init_ex(return_value, php_uri_ce_query_params);

const zend_result result = php_uri_query_params_parse(
Z_STRVAL(query), Z_STRLEN(query), Z_URI_QUERY_PARAMS_OBJECT_P(return_value),
parsing_method, false, max_param_count
);
zval_ptr_dtor(&query);

if (UNEXPECTED(result == FAILURE)) {
RETURN_THROWS();
}
}

PHP_METHOD(Uri_Rfc3986_Uri, getQueryParams)
{
uri_get_query_params(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_QUERY_PARAMS_PARSING_METHOD_RFC3986);
}

PHP_METHOD(Uri_Rfc3986_Uri, getRawQuery)
{
php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_QUERY, PHP_URI_COMPONENT_READ_MODE_RAW);
Expand All @@ -714,6 +766,32 @@ PHP_METHOD(Uri_Rfc3986_Uri, withQuery)
php_uri_property_write_str_or_null_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_QUERY);
}

PHP_METHOD(Uri_Rfc3986_Uri, withQueryParams)
{
zend_object *query_params = NULL;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJ_OF_CLASS_OR_NULL(query_params, php_uri_ce_query_params)
ZEND_PARSE_PARAMETERS_END();

zval zv;

if (query_params == NULL) {
ZVAL_NULL(&zv);
} else {
zend_string *query = php_uri_query_params_to_rfc3986_string(php_uri_query_params_object_from_obj(query_params));
/* This should be unreachable in practice, as query is null only due to memory errors. */
if (UNEXPECTED(query == NULL)) {
zend_throw_exception_ex(php_uri_ce_error, 0, "Cannot recompose %pS to a string", query_params->ce->name);
RETURN_THROWS();
}
ZVAL_STR(&zv, query);
}

php_uri_property_write_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_QUERY, &zv);
zval_ptr_dtor(&zv);
}

PHP_METHOD(Uri_Rfc3986_Uri, getFragment)
{
php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_FRAGMENT, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII);
Expand Down Expand Up @@ -989,6 +1067,37 @@ PHP_METHOD(Uri_WhatWg_Url, getHostType)
php_uri_parser_whatwg_host_type_read(uri_object->uri, return_value);
}

PHP_METHOD(Uri_WhatWg_Url, getQueryParams)
{
uri_get_query_params(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_QUERY_PARAMS_PARSING_METHOD_WHATWG_URL);
}

PHP_METHOD(Uri_WhatWg_Url, withQueryParams)
{
zend_object *query_params = NULL;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJ_OF_CLASS_OR_NULL(query_params, php_uri_ce_query_params)
ZEND_PARSE_PARAMETERS_END();

zval zv;

if (query_params == NULL) {
ZVAL_NULL(&zv);
} else {
zend_string *query = php_uri_query_params_to_whatwg_string(php_uri_query_params_object_from_obj(query_params));
/* This should be unreachable in practice, as query is null only due to memory errors. */
if (UNEXPECTED(query == NULL)) {
zend_throw_exception_ex(php_uri_ce_error, 0, "Cannot recompose %pS to a string", query_params->ce->name);
RETURN_THROWS();
}
ZVAL_STR(&zv, query);
}

php_uri_property_write_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_QUERY, &zv);
zval_ptr_dtor(&zv);
}

PHP_METHOD(Uri_WhatWg_Url, equals)
{
zend_object *that_object;
Expand Down Expand Up @@ -1537,6 +1646,19 @@ PHPAPI zend_result php_uri_parser_register(const php_uri_parser *uri_parser)
return result;
}

static PHP_MINFO_FUNCTION(uri)
{
php_info_print_table_start();
php_info_print_table_row(2, "URI support", "active");
#ifdef URI_STATIC_BUILD
php_info_print_table_row(2, "uriparser bundled version", URI_VER_ANSI);
#else
php_info_print_table_row(2, "uriparser compiled version", URI_VER_ANSI);
php_info_print_table_row(2, "uriparser loaded version", uriBaseRuntimeVersionA());
#endif
php_info_print_table_end();
}

static PHP_MINIT_FUNCTION(uri)
{
php_uri_ce_rfc3986_uri_builder = register_class_Uri_Rfc3986_UriBuilder();
Expand Down Expand Up @@ -1568,6 +1690,17 @@ static PHP_MINIT_FUNCTION(uri)
php_uri_ce_exception = register_class_Uri_UriException(zend_ce_exception);
php_uri_ce_error = register_class_Uri_UriError(zend_ce_error);
php_uri_ce_invalid_uri_exception = register_class_Uri_InvalidUriException(php_uri_ce_exception);
php_uri_ce_invalid_query_params_exception = register_class_Uri_InvalidQueryParamsException(php_uri_ce_exception);

php_uri_ce_query_params = register_class_Uri_QueryParams(zend_ce_aggregate, zend_ce_countable);
php_uri_ce_query_params->create_object = php_uri_object_query_params_create_object;
php_uri_ce_query_params->get_iterator = php_uri_object_query_params_get_iterator;
php_uri_ce_query_params->default_object_handlers = &object_handlers_query_params;
memcpy(&object_handlers_query_params, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
object_handlers_query_params.offset = offsetof(php_uri_query_params_object, std);
object_handlers_query_params.free_obj = php_uri_query_params_object_handler_free;
object_handlers_query_params.clone_obj = php_uri_query_params_object_handler_clone;

php_uri_ce_whatwg_invalid_url_exception = register_class_Uri_WhatWg_InvalidUrlException(php_uri_ce_invalid_uri_exception);
php_uri_ce_whatwg_url_host_type = register_class_Uri_WhatWg_UrlHostType();
php_uri_ce_whatwg_url_validation_error = register_class_Uri_WhatWg_UrlValidationError();
Expand All @@ -1590,19 +1723,6 @@ static PHP_MINIT_FUNCTION(uri)
return SUCCESS;
}

static PHP_MINFO_FUNCTION(uri)
{
php_info_print_table_start();
php_info_print_table_row(2, "URI support", "active");
#ifdef URI_STATIC_BUILD
php_info_print_table_row(2, "uriparser bundled version", URI_VER_ANSI);
#else
php_info_print_table_row(2, "uriparser compiled version", URI_VER_ANSI);
php_info_print_table_row(2, "uriparser loaded version", uriBaseRuntimeVersionA());
#endif
php_info_print_table_end();
}

static PHP_MSHUTDOWN_FUNCTION(uri)
{
zend_hash_destroy(&uri_parsers);
Expand Down
79 changes: 79 additions & 0 deletions ext/uri/php_uri.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,82 @@ class InvalidUriException extends \Uri\UriException
{
}

/** @strict-properties */
class InvalidQueryParamsException extends \Uri\UriException
{
}

enum UriComparisonMode
{
case IncludeFragment;
case ExcludeFragment;
}

/** @strict-properties */
final readonly class QueryParams implements \IteratorAggregate, \Countable
{
public static function parseRfc1866(string $queryString, int $maxParamCount = 1000): \Uri\QueryParams {}

public static function parseRfc3986(string $queryString, int $maxParamCount = 1000): \Uri\QueryParams {}

public static function parseWhatWgUrlSearchParams(string $queryString, int $maxParamCount = 1000): \Uri\QueryParams {}

public static function fromArray(array $queryParams): \Uri\QueryParams {}

public static function fromList(array $queryParams): \Uri\QueryParams {}

public function __construct() {}

public function has(string $name): bool {}

public function hasValue(string $name, string|int|float|\BackedEnum|null $value): bool {}

public function getFirst(string $name): ?string {}

public function getLast(string $name): ?string {}

public function getAll(string $name): array {}

public function getIterator(): \Traversable {}

public function count(): int {}

#[\NoDiscard]
public function append(string $name, string|int|float|\BackedEnum|null $value): static {}

#[\NoDiscard]
public function set(string $name, string|int|float|\BackedEnum|null $value): static {}

#[\NoDiscard]
public function delete(string $name): static {}

#[\NoDiscard]
public function deleteValue(string $name, string|int|float|\BackedEnum|null $value): static {}

#[\NoDiscard]
public function sortByBytes(): static {}

#[\NoDiscard]
public function sortByUtf16(): static {}

public function withArray(array $queryParams): static {}

public function toList(): array {}

public function toArray(int $maxNestingLevel = 64): array {}

public function toRfc1866String(): string {}

public function toRfc3986String(): string {}

public function toWhatWgUrlString(): string {}

public function __serialize(): array {}

public function __unserialize(array $data): void {}

public function __debugInfo(): array {}
}
}

namespace Uri\Rfc3986 {
Expand Down Expand Up @@ -126,8 +197,12 @@ public function getQuery(): ?string {}

public function getRawQuery(): ?string {}

public function getQueryParams(int $maxParamCount = 1000): ?\Uri\QueryParams {}

public function withQuery(?string $query): static {}

public function withQueryParams(?\Uri\QueryParams $queryParams): static {}

public function getFragment(): ?string {}

public function getRawFragment(): ?string {}
Expand Down Expand Up @@ -293,9 +368,13 @@ public function withPath(string $path): static {}
/** @implementation-alias Uri\Rfc3986\Uri::getQuery */
public function getQuery(): ?string {}

public function getQueryParams(int $maxParamCount = 1000): ?\Uri\QueryParams {}

/** @implementation-alias Uri\Rfc3986\Uri::withQuery */
public function withQuery(?string $query): static {}

public function withQueryParams(?\Uri\QueryParams $queryParams): static {}

/** @implementation-alias Uri\Rfc3986\Uri::getFragment */
public function getFragment(): ?string {}

Expand Down
Loading
Loading