Skip to content
Open
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: 3 additions & 3 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,10 @@ PHP 8.6 UPGRADE NOTES
RFC: https://wiki.php.net/rfc/uri_followup#uri_type_detection
. Added Uri\Rfc3986\Uri::getHostType() and Uri\WhatWg\Url::getHostType().
RFC: https://wiki.php.net/rfc/uri_followup#host_type_detection
. Added Uri\Rfc3986\UriBuilder.
RFC: https://wiki.php.net/rfc/uri_followup#uri_building
. Added Uri\WhatWg\UrlBuilder.
. Added Uri\Rfc3986\UriBuilder and Uri\WhatWg\UrlBuilder.
RFC: https://wiki.php.net/rfc/uri_followup#uri_building
. Added Uri\url_percent_encode().
RFC: https://wiki.php.net/rfc/uri_followup#percent-encoding_support

========================================
3. Changes in SAPI modules
Expand Down
69 changes: 63 additions & 6 deletions ext/uri/php_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ zend_class_entry *php_uri_ce_rfc3986_uri_type;
zend_class_entry *php_uri_ce_rfc3986_uri_host_type;
zend_class_entry *php_uri_ce_whatwg_url_builder;
zend_class_entry *php_uri_ce_whatwg_url;
zend_class_entry *php_uri_ce_whatwg_url_percent_encoding_mode;
zend_class_entry *php_uri_ce_comparison_mode;
zend_class_entry *php_uri_ce_exception;
zend_class_entry *php_uri_ce_error;
Expand Down Expand Up @@ -1073,6 +1074,60 @@ PHP_METHOD(Uri_WhatWg_Url, __debugInfo)
RETURN_ARR(uri_get_debug_properties(uri_object));
}

PHP_FUNCTION(Uri_WhatWg_url_percent_encode)
{
zend_string *input;
zend_enum_Uri_WhatWg_UrlPercentEncodingMode mode;

ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR(input)
Z_PARAM_ENUM(mode, php_uri_ce_whatwg_url_percent_encoding_mode)
ZEND_PARSE_PARAMETERS_END();

zend_string *str;

switch (mode) {
case ZEND_ENUM_Uri_WhatWg_UrlPercentEncodingMode_Username:
ZEND_FALLTHROUGH;
case ZEND_ENUM_Uri_WhatWg_UrlPercentEncodingMode_Password:
str = php_uri_parser_whatwg_percent_encode_userinfo_component(ZSTR_VAL(input), ZSTR_LEN(input));
break;
case ZEND_ENUM_Uri_WhatWg_UrlPercentEncodingMode_OpaqueHost:
str = php_uri_parser_whatwg_percent_encode_opaque_host_component(ZSTR_VAL(input), ZSTR_LEN(input));
break;
case ZEND_ENUM_Uri_WhatWg_UrlPercentEncodingMode_Path:
str = php_uri_parser_whatwg_percent_encode_path_component(ZSTR_VAL(input), ZSTR_LEN(input));
break;
case ZEND_ENUM_Uri_WhatWg_UrlPercentEncodingMode_OpaquePath:
str = php_uri_parser_whatwg_percent_encode_opaque_path_component(ZSTR_VAL(input), ZSTR_LEN(input));
break;
case ZEND_ENUM_Uri_WhatWg_UrlPercentEncodingMode_PathSegment:
str = php_uri_parser_whatwg_percent_encode_path_segment_component(ZSTR_VAL(input), ZSTR_LEN(input));
break;
case ZEND_ENUM_Uri_WhatWg_UrlPercentEncodingMode_Query:
str = php_uri_parser_whatwg_percent_encode_query_component(ZSTR_VAL(input), ZSTR_LEN(input));
break;
case ZEND_ENUM_Uri_WhatWg_UrlPercentEncodingMode_SpecialQuery:
str = php_uri_parser_whatwg_percent_encode_special_query_component(ZSTR_VAL(input), ZSTR_LEN(input));
break;
case ZEND_ENUM_Uri_WhatWg_UrlPercentEncodingMode_FormQuery:
str = php_uri_parser_whatwg_percent_encode_form_query_component(ZSTR_VAL(input), ZSTR_LEN(input));
break;
case ZEND_ENUM_Uri_WhatWg_UrlPercentEncodingMode_Fragment:
str = php_uri_parser_whatwg_percent_encode_fragment_component(ZSTR_VAL(input), ZSTR_LEN(input));
break;
default: ZEND_UNREACHABLE();
}

/* This should be unreachable in practice, as str is null only due to memory errors. */
if (str == NULL) {
Comment thread
kocsismate marked this conversation as resolved.
zend_throw_exception(php_uri_ce_error, "Cannot percent-encode input", 0);
RETURN_THROWS();
}

RETURN_NEW_STR(str);
}

PHP_METHOD(Uri_Rfc3986_UriBuilder, reset)
{
ZEND_PARSE_PARAMETERS_NONE();
Expand Down Expand Up @@ -1481,6 +1536,8 @@ static PHP_MINIT_FUNCTION(uri)
object_handlers_whatwg_uri.free_obj = php_uri_object_handler_free;
object_handlers_whatwg_uri.clone_obj = php_uri_object_handler_clone;

php_uri_ce_whatwg_url_percent_encoding_mode = register_class_Uri_WhatWg_UrlPercentEncodingMode();

php_uri_ce_comparison_mode = register_class_Uri_UriComparisonMode();
php_uri_ce_exception = register_class_Uri_UriException(zend_ce_exception);
php_uri_ce_error = register_class_Uri_UriError(zend_ce_error);
Expand Down Expand Up @@ -1548,14 +1605,14 @@ ZEND_MODULE_POST_ZEND_DEACTIVATE_D(uri)
zend_module_entry uri_module_entry = {
STANDARD_MODULE_HEADER_EX, NULL,
uri_deps,
"uri", /* Extension name */
NULL, /* zend_function_entry */
"uri", /* Extension name */
ext_functions, /* zend_function_entry */
PHP_MINIT(uri), /* PHP_MINIT - Module initialization */
PHP_MSHUTDOWN(uri), /* PHP_MSHUTDOWN - Module shutdown */
PHP_MSHUTDOWN(uri), /* PHP_MSHUTDOWN - Module shutdown */
PHP_RINIT(uri), /* PHP_RINIT - Request initialization */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(uri), /* PHP_MINFO - Module info */
PHP_VERSION, /* Version */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(uri), /* PHP_MINFO - Module info */
PHP_VERSION, /* Version */
NO_MODULE_GLOBALS,
ZEND_MODULE_POST_ZEND_DEACTIVATE_N(uri),
STANDARD_MODULE_PROPERTIES_EX
Expand Down
16 changes: 16 additions & 0 deletions ext/uri/php_uri.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,20 @@ public function __unserialize(array $data): void {}

public function __debugInfo(): array {}
}

enum UrlPercentEncodingMode
{
case Username;
case Password;
case OpaqueHost;
case Path;
case OpaquePath;
case PathSegment;
case Query;
case SpecialQuery;
case FormQuery;
case Fragment;
}

function url_percent_encode(string $input, \Uri\WhatWg\UrlPercentEncodingMode $mode): string {}
}
40 changes: 39 additions & 1 deletion ext/uri/php_uri_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ext/uri/php_uri_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extern zend_class_entry *php_uri_ce_rfc3986_uri_type;
extern zend_class_entry *php_uri_ce_rfc3986_uri_host_type;
extern zend_class_entry *php_uri_ce_whatwg_url_builder;
extern zend_class_entry *php_uri_ce_whatwg_url;
extern zend_class_entry *php_uri_ce_whatwg_url_percent_encoding_mode;
extern zend_class_entry *php_uri_ce_comparison_mode;
extern zend_class_entry *php_uri_ce_exception;
extern zend_class_entry *php_uri_ce_error;
Expand Down
21 changes: 17 additions & 4 deletions ext/uri/php_uri_decl.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Test Uri\WhatWg\url_percent_encode() - form query - control code points
--FILE--
<?php

var_dump(Uri\WhatWg\url_percent_encode("\x11", Uri\WhatWg\UrlPercentEncodingMode::FormQuery));

?>
--EXPECT--
string(3) "%11"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Test Uri\WhatWg\url_percent_encode() - form query - percent sign code point
--FILE--
<?php

var_dump(Uri\WhatWg\url_percent_encode("WHATWG%20url", Uri\WhatWg\UrlPercentEncodingMode::FormQuery));

?>
--EXPECT--
string(14) "WHATWG%2520url"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Test Uri\WhatWg\url_percent_encode() - form query - space code point
--FILE--
<?php

var_dump(Uri\WhatWg\url_percent_encode("WHATWG url", Uri\WhatWg\UrlPercentEncodingMode::FormQuery));

?>
--EXPECT--
string(10) "WHATWG+url"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Test Uri\WhatWg\url_percent_encode() - form query - special code points in the percent-encode set
--FILE--
<?php

var_dump(Uri\WhatWg\url_percent_encode('"#<>&+,', Uri\WhatWg\UrlPercentEncodingMode::FormQuery));

?>
--EXPECT--
string(21) "%22%23%3C%3E%26%2B%2C"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Test Uri\WhatWg\url_percent_encode() - form query - special code points not in the percent-encode set
--FILE--
<?php

var_dump(Uri\WhatWg\url_percent_encode("*-._", Uri\WhatWg\UrlPercentEncodingMode::FormQuery));

?>
--EXPECT--
string(4) "*-._"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Test Uri\WhatWg\url_percent_encode() - form query - Unicode code points
--FILE--
<?php

var_dump(Uri\WhatWg\url_percent_encode("föő", Uri\WhatWg\UrlPercentEncodingMode::FormQuery));

?>
--EXPECT--
string(13) "f%C3%B6%C5%91"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Test Uri\WhatWg\url_percent_encode() - fragment - control code points
--FILE--
<?php

var_dump(Uri\WhatWg\url_percent_encode("\x11", Uri\WhatWg\UrlPercentEncodingMode::Fragment));

?>
--EXPECT--
string(3) "%11"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Test Uri\WhatWg\url_percent_encode() - fragment - percent sign code point
--FILE--
<?php

var_dump(Uri\WhatWg\url_percent_encode("WHATWG%20url", Uri\WhatWg\UrlPercentEncodingMode::Username));

?>
--EXPECT--
string(14) "WHATWG%2520url"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Test Uri\WhatWg\url_percent_encode() - fragment - special code points in the percent-encode set
--FILE--
<?php

var_dump(Uri\WhatWg\url_percent_encode(' "<>`', Uri\WhatWg\UrlPercentEncodingMode::Fragment));

?>
--EXPECT--
string(15) "%20%22%3C%3E%60"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Test Uri\WhatWg\url_percent_encode() - fragment - special code points not in the percent-encode set
--FILE--
<?php

var_dump(Uri\WhatWg\url_percent_encode("('$+)", Uri\WhatWg\UrlPercentEncodingMode::Fragment));

?>
--EXPECT--
string(5) "('$+)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Test Uri\WhatWg\url_percent_encode() - fragment - Unicode code points
--FILE--
<?php

var_dump(Uri\WhatWg\url_percent_encode("föő", Uri\WhatWg\UrlPercentEncodingMode::Fragment));

?>
--EXPECT--
string(13) "f%C3%B6%C5%91"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Test Uri\WhatWg\url_percent_encode() - opaque host - control code points
--FILE--
<?php

var_dump(Uri\WhatWg\url_percent_encode("\x11", Uri\WhatWg\UrlPercentEncodingMode::OpaqueHost));

?>
--EXPECT--
string(3) "%11"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Test Uri\WhatWg\url_percent_encode() - opaque host - percent sign code point
--FILE--
<?php

var_dump(Uri\WhatWg\url_percent_encode("WHATWG%20url", Uri\WhatWg\UrlPercentEncodingMode::OpaqueHost));

?>
--EXPECT--
string(14) "WHATWG%2520url"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Test Uri\WhatWg\url_percent_encode() - opaque host - special code points not in the percent-encode set
--FILE--
<?php

var_dump(Uri\WhatWg\url_percent_encode(" \"#<>?^`{}@'$+,", Uri\WhatWg\UrlPercentEncodingMode::OpaqueHost));

?>
--EXPECT--
string(15) " "#<>?^`{}@'$+,"
Loading
Loading