Skip to content

Commit 1bf64ce

Browse files
kocsismateTimWolla
andauthored
Implement "Followup improvements for ext/uri" RFC - URI type detection (#22075)
RFC: https://wiki.php.net/rfc/uri_followup#uri_type_detection --------- Co-authored-by: Tim Düsterhus <timwolla@googlemail.com>
1 parent f58b95e commit 1bf64ce

19 files changed

Lines changed: 225 additions & 5 deletions

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ PHP NEWS
241241
. Fixed bug #49874 (ftell() and fseek() inconsistency when using stream
242242
filters). (Jakub Zelenka)
243243

244+
- URI:
245+
. Added Uri\Rfc3986\Uri:getUriType() and Uri\WhatWg\Url:isSpecialScheme().
246+
(kocsismate)
247+
244248
- Zip:
245249
. Fixed ZipArchive callback being called after executor has shut down.
246250
(ilutov)

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ PHP 8.6 UPGRADE NOTES
211211
options.
212212
. Allowed casting casting filtered streams as file descriptor for select.
213213

214+
- URI:
215+
. Added Uri\Rfc3986\Uri:getUriType() and Uri\WhatWg\Url:isSpecialScheme().
216+
RFC: https://wiki.php.net/rfc/uri_followup#uri_type_detection
217+
214218
========================================
215219
3. Changes in SAPI modules
216220
========================================

ext/uri/php_uri.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "uriparser/Uri.h"
3232

3333
zend_class_entry *php_uri_ce_rfc3986_uri;
34+
zend_class_entry *php_uri_ce_rfc3986_uri_type;
3435
zend_class_entry *php_uri_ce_whatwg_url;
3536
zend_class_entry *php_uri_ce_comparison_mode;
3637
zend_class_entry *php_uri_ce_exception;
@@ -508,6 +509,16 @@ PHP_METHOD(Uri_WhatWg_Url, __construct)
508509
create_whatwg_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
509510
}
510511

512+
PHP_METHOD(Uri_Rfc3986_Uri, getUriType)
513+
{
514+
ZEND_PARSE_PARAMETERS_NONE();
515+
516+
php_uri_object *uri_object = Z_URI_OBJECT_P(ZEND_THIS);
517+
ZEND_ASSERT(uri_object->uri != NULL);
518+
519+
php_uri_parser_rfc3986_uri_type_read(uri_object->uri, return_value);
520+
}
521+
511522
PHP_METHOD(Uri_Rfc3986_Uri, getScheme)
512523
{
513524
php_uri_property_read_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_SCHEME, PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII);
@@ -883,6 +894,16 @@ PHP_METHOD(Uri_WhatWg_Url, withScheme)
883894
php_uri_property_write_str_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_SCHEME);
884895
}
885896

897+
PHP_METHOD(Uri_WhatWg_Url, isSpecialScheme)
898+
{
899+
ZEND_PARSE_PARAMETERS_NONE();
900+
901+
php_uri_object *uri_object = Z_URI_OBJECT_P(ZEND_THIS);
902+
ZEND_ASSERT(uri_object->uri != NULL);
903+
904+
RETVAL_BOOL(php_uri_parser_whatwg_is_special(uri_object->uri));
905+
}
906+
886907
PHP_METHOD(Uri_WhatWg_Url, withUsername)
887908
{
888909
php_uri_property_write_str_or_null_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_URI_PROPERTY_NAME_USERNAME);
@@ -1078,6 +1099,8 @@ static PHP_MINIT_FUNCTION(uri)
10781099
object_handlers_rfc3986_uri.free_obj = php_uri_object_handler_free;
10791100
object_handlers_rfc3986_uri.clone_obj = php_uri_object_handler_clone;
10801101

1102+
php_uri_ce_rfc3986_uri_type = register_class_Uri_Rfc3986_UriType();
1103+
10811104
php_uri_ce_whatwg_url = register_class_Uri_WhatWg_Url();
10821105
php_uri_ce_whatwg_url->create_object = php_uri_object_create_whatwg;
10831106
php_uri_ce_whatwg_url->default_object_handlers = &object_handlers_whatwg_uri;

ext/uri/php_uri.stub.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,23 @@ enum UriComparisonMode
2929
}
3030

3131
namespace Uri\Rfc3986 {
32+
enum UriType
33+
{
34+
case AbsolutePathReference;
35+
case RelativePathReference;
36+
case NetworkPathReference;
37+
case Uri;
38+
}
39+
3240
/** @strict-properties */
3341
final readonly class Uri
3442
{
3543
public static function parse(string $uri, ?\Uri\Rfc3986\Uri $baseUrl = null): ?static {}
3644

3745
public function __construct(string $uri, ?\Uri\Rfc3986\Uri $baseUrl = null) {}
3846

47+
public function getUriType(): ?\Uri\Rfc3986\UriType {}
48+
3949
public function getScheme(): ?string {}
4050

4151
public function getRawScheme(): ?string {}
@@ -165,6 +175,8 @@ public function getScheme(): string {}
165175

166176
public function withScheme(string $scheme): static {}
167177

178+
public function isSpecialScheme(): bool {}
179+
168180
/** @implementation-alias Uri\Rfc3986\Uri::getUsername */
169181
public function getUsername(): ?string {}
170182

ext/uri/php_uri_arginfo.h

Lines changed: 26 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "php_uri_decl.h"
1919

2020
extern zend_class_entry *php_uri_ce_rfc3986_uri;
21+
extern zend_class_entry *php_uri_ce_rfc3986_uri_type;
2122
extern zend_class_entry *php_uri_ce_whatwg_url;
2223
extern zend_class_entry *php_uri_ce_comparison_mode;
2324
extern zend_class_entry *php_uri_ce_exception;

ext/uri/php_uri_decl.h

Lines changed: 11 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 getter - uri type - Absolute path reference
3+
--FILE--
4+
<?php
5+
6+
$uri = Uri\Rfc3986\Uri::parse("/foo/bar");
7+
8+
var_dump($uri->getUriType());
9+
10+
?>
11+
--EXPECT--
12+
enum(Uri\Rfc3986\UriType::AbsolutePathReference)
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 getter - uri type - Network path reference
3+
--FILE--
4+
<?php
5+
6+
$uri = Uri\Rfc3986\Uri::parse("//example.com/foo/bar");
7+
8+
var_dump($uri->getUriType());
9+
10+
?>
11+
--EXPECT--
12+
enum(Uri\Rfc3986\UriType::NetworkPathReference)
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 getter - uri type - Relative path reference
3+
--FILE--
4+
<?php
5+
6+
$uri = Uri\Rfc3986\Uri::parse("foo/bar");
7+
8+
var_dump($uri->getUriType());
9+
10+
?>
11+
--EXPECT--
12+
enum(Uri\Rfc3986\UriType::RelativePathReference)

0 commit comments

Comments
 (0)