Skip to content

Commit d8a5aec

Browse files
authored
Implement "Followup improvements for ext/uri" RFC - Host type detection (#22100)
RFC: https://wiki.php.net/rfc/uri_followup#host_type_detection
1 parent 1bf64ce commit d8a5aec

22 files changed

Lines changed: 296 additions & 5 deletions

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ PHP NEWS
244244
- URI:
245245
. Added Uri\Rfc3986\Uri:getUriType() and Uri\WhatWg\Url:isSpecialScheme().
246246
(kocsismate)
247+
. Added Uri\Rfc3986\Uri:getHostType() and Uri\WhatWg\Url:getHostType().
248+
(kocsismate)
247249

248250
- Zip:
249251
. Fixed ZipArchive callback being called after executor has shut down.

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ PHP 8.6 UPGRADE NOTES
214214
- URI:
215215
. Added Uri\Rfc3986\Uri:getUriType() and Uri\WhatWg\Url:isSpecialScheme().
216216
RFC: https://wiki.php.net/rfc/uri_followup#uri_type_detection
217+
. Added Uri\Rfc3986\Uri:getHostType() and Uri\WhatWg\Url:getHostType().
218+
RFC: https://wiki.php.net/rfc/uri_followup#host_type_detection
217219

218220
========================================
219221
3. Changes in SAPI modules

ext/uri/php_uri.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@
3232

3333
zend_class_entry *php_uri_ce_rfc3986_uri;
3434
zend_class_entry *php_uri_ce_rfc3986_uri_type;
35+
zend_class_entry *php_uri_ce_rfc3986_uri_host_type;
3536
zend_class_entry *php_uri_ce_whatwg_url;
3637
zend_class_entry *php_uri_ce_comparison_mode;
3738
zend_class_entry *php_uri_ce_exception;
3839
zend_class_entry *php_uri_ce_error;
3940
zend_class_entry *php_uri_ce_invalid_uri_exception;
41+
zend_class_entry *php_uri_ce_whatwg_url_host_type;
4042
zend_class_entry *php_uri_ce_whatwg_invalid_url_exception;
4143
zend_class_entry *php_uri_ce_whatwg_url_validation_error_type;
4244
zend_class_entry *php_uri_ce_whatwg_url_validation_error;
@@ -622,6 +624,16 @@ PHP_METHOD(Uri_Rfc3986_Uri, getRawHost)
622624
php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_HOST, PHP_URI_COMPONENT_READ_MODE_RAW);
623625
}
624626

627+
PHP_METHOD(Uri_Rfc3986_Uri, getHostType)
628+
{
629+
ZEND_PARSE_PARAMETERS_NONE();
630+
631+
php_uri_object *uri_object = Z_URI_OBJECT_P(ZEND_THIS);
632+
ZEND_ASSERT(uri_object->uri != NULL);
633+
634+
php_uri_parser_rfc3986_host_type_read(uri_object->uri, return_value);
635+
}
636+
625637
PHP_METHOD(Uri_Rfc3986_Uri, withHost)
626638
{
627639
php_uri_property_write_str_or_null_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_HOST);
@@ -924,6 +936,16 @@ PHP_METHOD(Uri_WhatWg_Url, getUnicodeHost)
924936
php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_HOST, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_UNICODE);
925937
}
926938

939+
PHP_METHOD(Uri_WhatWg_Url, getHostType)
940+
{
941+
ZEND_PARSE_PARAMETERS_NONE();
942+
943+
php_uri_object *uri_object = Z_URI_OBJECT_P(ZEND_THIS);
944+
ZEND_ASSERT(uri_object->uri != NULL);
945+
946+
php_uri_parser_whatwg_host_type_read(uri_object->uri, return_value);
947+
}
948+
927949
PHP_METHOD(Uri_WhatWg_Url, getFragment)
928950
{
929951
php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_FRAGMENT, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_UNICODE);
@@ -1100,6 +1122,7 @@ static PHP_MINIT_FUNCTION(uri)
11001122
object_handlers_rfc3986_uri.clone_obj = php_uri_object_handler_clone;
11011123

11021124
php_uri_ce_rfc3986_uri_type = register_class_Uri_Rfc3986_UriType();
1125+
php_uri_ce_rfc3986_uri_host_type = register_class_Uri_Rfc3986_UriHostType();
11031126

11041127
php_uri_ce_whatwg_url = register_class_Uri_WhatWg_Url();
11051128
php_uri_ce_whatwg_url->create_object = php_uri_object_create_whatwg;
@@ -1114,6 +1137,7 @@ static PHP_MINIT_FUNCTION(uri)
11141137
php_uri_ce_error = register_class_Uri_UriError(zend_ce_error);
11151138
php_uri_ce_invalid_uri_exception = register_class_Uri_InvalidUriException(php_uri_ce_exception);
11161139
php_uri_ce_whatwg_invalid_url_exception = register_class_Uri_WhatWg_InvalidUrlException(php_uri_ce_invalid_uri_exception);
1140+
php_uri_ce_whatwg_url_host_type = register_class_Uri_WhatWg_UrlHostType();
11171141
php_uri_ce_whatwg_url_validation_error = register_class_Uri_WhatWg_UrlValidationError();
11181142
php_uri_ce_whatwg_url_validation_error_type = register_class_Uri_WhatWg_UrlValidationErrorType();
11191143

ext/uri/php_uri.stub.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ enum UriType
3737
case Uri;
3838
}
3939

40+
enum UriHostType
41+
{
42+
case IPv4;
43+
case IPv6;
44+
case IPvFuture;
45+
case RegisteredName;
46+
}
47+
4048
/** @strict-properties */
4149
final readonly class Uri
4250
{
@@ -70,6 +78,8 @@ public function getHost(): ?string {}
7078

7179
public function getRawHost(): ?string {}
7280

81+
public function getHostType(): ?\Uri\Rfc3986\UriHostType {}
82+
7383
public function withHost(?string $host): static {}
7484

7585
public function getPort(): ?int {}
@@ -162,6 +172,15 @@ enum UrlValidationErrorType
162172
public function __construct(string $context, \Uri\WhatWg\UrlValidationErrorType $type, bool $failure) {}
163173
}
164174

175+
enum UrlHostType
176+
{
177+
case IPv4;
178+
case IPv6;
179+
case Domain;
180+
case Opaque;
181+
case Empty;
182+
}
183+
165184
/** @strict-properties */
166185
final readonly class Url
167186
{
@@ -191,6 +210,8 @@ public function getAsciiHost(): ?string {}
191210

192211
public function getUnicodeHost(): ?string {}
193212

213+
public function getHostType(): ?\Uri\WhatWg\UrlHostType {}
214+
194215
/** @implementation-alias Uri\Rfc3986\Uri::withHost */
195216
public function withHost(?string $host): static {}
196217

ext/uri/php_uri_arginfo.h

Lines changed: 43 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/uri/php_uri_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
extern zend_class_entry *php_uri_ce_rfc3986_uri;
2121
extern zend_class_entry *php_uri_ce_rfc3986_uri_type;
22+
extern zend_class_entry *php_uri_ce_rfc3986_uri_host_type;
2223
extern zend_class_entry *php_uri_ce_whatwg_url;
2324
extern zend_class_entry *php_uri_ce_comparison_mode;
2425
extern zend_class_entry *php_uri_ce_exception;
@@ -27,6 +28,7 @@ extern zend_class_entry *php_uri_ce_invalid_uri_exception;
2728
extern zend_class_entry *php_uri_ce_whatwg_invalid_url_exception;
2829
extern zend_class_entry *php_uri_ce_whatwg_url_validation_error_type;
2930
extern zend_class_entry *php_uri_ce_whatwg_url_validation_error;
31+
extern zend_class_entry *php_uri_ce_whatwg_url_host_type;
3032

3133
typedef enum php_uri_recomposition_mode {
3234
PHP_URI_RECOMPOSITION_MODE_RAW_ASCII,

ext/uri/php_uri_decl.h

Lines changed: 19 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Test Uri\Rfc3986\Uri component retrieval - host type - IP future
3+
--FILE--
4+
<?php
5+
6+
$uri = Uri\Rfc3986\Uri::parse("https://[vF.addr]");
7+
8+
var_dump($uri->getHostType());
9+
10+
?>
11+
--EXPECT--
12+
enum(Uri\Rfc3986\UriHostType::IPvFuture)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Test Uri\Rfc3986\Uri component retrieval - host type - IPv4
3+
--FILE--
4+
<?php
5+
6+
$uri = Uri\Rfc3986\Uri::parse("https://192.168.0.1");
7+
8+
var_dump($uri->getHostType());
9+
10+
?>
11+
--EXPECT--
12+
enum(Uri\Rfc3986\UriHostType::IPv4)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Test Uri\Rfc3986\Uri component retrieval - host type - IPv6
3+
--FILE--
4+
<?php
5+
6+
$uri = Uri\Rfc3986\Uri::parse("https://[2001:0db8:3333:4444:5555:6666:7777:8888]");
7+
8+
var_dump($uri->getHostType());
9+
10+
?>
11+
--EXPECT--
12+
enum(Uri\Rfc3986\UriHostType::IPv6)

0 commit comments

Comments
 (0)