开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
develop
分支 (7)
标签 (81)
develop
gh-pages
4.8
master
feature/mailer
refactor/email
feature/queue
v4.7.3
v4.7.2
v4.7.1
v4.7.0
v4.6.5
v4.6.4
v4.6.3
v4.6.2
v4.6.1
v4.6.0
v4.5.8
v4.5.7
v4.5.6
v4.5.5
v4.5.4
v4.5.3
v4.5.2
v4.5.1
v4.5.0
v4.4.8
develop
分支 (7)
标签 (81)
develop
gh-pages
4.8
master
feature/mailer
refactor/email
feature/queue
v4.7.3
v4.7.2
v4.7.1
v4.7.0
v4.6.5
v4.6.4
v4.6.3
v4.6.2
v4.6.1
v4.6.0
v4.5.8
v4.5.7
v4.5.6
v4.5.5
v4.5.4
v4.5.3
v4.5.2
v4.5.1
v4.5.0
v4.4.8
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
develop
分支 (7)
标签 (81)
develop
gh-pages
4.8
master
feature/mailer
refactor/email
feature/queue
v4.7.3
v4.7.2
v4.7.1
v4.7.0
v4.6.5
v4.6.4
v4.6.3
v4.6.2
v4.6.1
v4.6.0
v4.5.8
v4.5.7
v4.5.6
v4.5.5
v4.5.4
v4.5.3
v4.5.2
v4.5.1
v4.5.0
v4.4.8
URI.php 30.42 KB
一键复制 编辑 原始数据 按行查看 历史
A. V. 提交于 2026年03月22日 02:27 +08:00 . docs: fix typos (#10056)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\HTTP;
use CodeIgniter\Exceptions\BadMethodCallException;
use CodeIgniter\Exceptions\InvalidArgumentException;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use Config\App;
use SensitiveParameter;
use Stringable;
/**
* Abstraction for a uniform resource identifier (URI).
*
* @see \CodeIgniter\HTTP\URITest
*/
class URI implements Stringable
{
/**
* Sub-delimiters used in query strings and fragments.
*/
public const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;=';
/**
* Unreserved characters used in paths, query strings, and fragments.
*/
public const CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~';
/**
* Current URI string
*
* @var string
*
* @deprecated 4.4.0 Not used.
*/
protected $uriString;
/**
* The Current baseURL.
*
* @deprecated 4.4.0 Use SiteURI instead.
*/
private ?string $baseURL = null;
/**
* List of URI segments.
*
* Starts at 1 instead of 0
*
* @var array<int, string>
*/
protected $segments = [];
/**
* The URI Scheme.
*
* @var string
*/
protected $scheme = 'http';
/**
* URI User Info
*
* @var string|null
*/
protected $user;
/**
* URI User Password
*
* @var string|null
*/
protected $password;
/**
* URI Host
*
* @var string|null
*/
protected $host;
/**
* URI Port
*
* @var int|null
*/
protected $port;
/**
* URI path.
*
* @var string|null
*/
protected $path;
/**
* The name of any fragment.
*
* @var string
*/
protected $fragment = '';
/**
* The query string.
*
* @var array<string, string>
*/
protected $query = [];
/**
* Default schemes/ports.
*
* @var array{
* http: int,
* https: int,
* ftp: int,
* sftp: int,
* }
*/
protected $defaultPorts = [
'http' => 80,
'https' => 443,
'ftp' => 21,
'sftp' => 22,
];
/**
* Whether passwords should be shown in userInfo/authority calls.
* Default to false because URIs often show up in logs
*
* @var bool
*/
protected $showPassword = false;
/**
* If true, will continue instead of throwing exceptions.
*
* @var bool
*/
protected $silent = false;
/**
* If true, will use raw query string.
*
* @var bool
*/
protected $rawQueryString = false;
/**
* Builds a representation of the string from the component parts.
*
* @param string|null $scheme URI scheme. E.g., http, ftp
*
* @return string URI string with only passed parts. Maybe incomplete as a URI.
*/
public static function createURIString(
?string $scheme = null,
?string $authority = null,
?string $path = null,
?string $query = null,
?string $fragment = null,
): string {
$uri = '';
if ((string) $scheme !== '') {
$uri .= $scheme . '://';
}
if ((string) $authority !== '') {
$uri .= $authority;
}
if ((string) $path !== '') {
$uri .= str_ends_with($uri, '/')
? ltrim($path, '/')
: '/' . ltrim($path, '/');
}
if ((string) $query !== '') {
$uri .= '?' . $query;
}
if ((string) $fragment !== '') {
$uri .= '#' . $fragment;
}
return $uri;
}
/**
* Used when resolving and merging paths to correctly interpret and
* remove single and double dot segments from the path per
* RFC 3986 Section 5.2.4
*
* @see http://tools.ietf.org/html/rfc3986#section-5.2.4
*
* @internal
*/
public static function removeDotSegments(string $path): string
{
if ($path === '' || $path === '/') {
return $path;
}
$output = [];
$input = explode('/', $path);
if ($input[0] === '') {
unset($input[0]);
$input = array_values($input);
}
// This is not a perfect representation of the
// RFC, but matches most cases and is pretty
// much what Guzzle uses. Should be good enough
// for almost every real use case.
foreach ($input as $segment) {
if ($segment === '..') {
array_pop($output);
} elseif ($segment !== '.' && $segment !== '') {
$output[] = $segment;
}
}
$output = implode('/', $output);
$output = trim($output, '/ ');
// Add leading slash if necessary
if (str_starts_with($path, '/')) {
$output = '/' . $output;
}
// Add trailing slash if necessary
if ($output !== '/' && str_ends_with($path, '/')) {
$output .= '/';
}
return $output;
}
/**
* Constructor.
*
* @param string|null $uri The URI to parse.
*
* @throws HTTPException
*
* @TODO null for param $uri should be removed.
* See https://www.php-fig.org/psr/psr-17/#26-urifactoryinterface
*/
public function __construct(?string $uri = null)
{
$this->setURI($uri);
}
/**
* If $silent == true, then will not throw exceptions and will
* attempt to continue gracefully.
*
* @deprecated 4.4.0 Method not in PSR-7
*
* @return URI
*/
public function setSilent(bool $silent = true)
{
$this->silent = $silent;
return $this;
}
/**
* If $raw == true, then will use parseStr() method
* instead of native parse_str() function.
*
* Note: Method not in PSR-7
*
* @return URI
*/
public function useRawQueryString(bool $raw = true)
{
$this->rawQueryString = $raw;
return $this;
}
/**
* Sets and overwrites any current URI information.
*
* @return URI
*
* @throws HTTPException
*
* @deprecated 4.4.0 This method will be private.
*/
public function setURI(?string $uri = null)
{
if ($uri === null) {
return $this;
}
$parts = parse_url($uri);
if (is_array($parts)) {
$this->applyParts($parts);
return $this;
}
if ($this->silent) {
return $this;
}
throw HTTPException::forUnableToParseURI($uri);
}
/**
* Retrieve the scheme component of the URI.
*
* If no scheme is present, this method MUST return an empty string.
*
* The value returned MUST be normalized to lowercase, per RFC 3986
* Section 3.1.
*
* The trailing ":" character is not part of the scheme and MUST NOT be
* added.
*
* @see https://tools.ietf.org/html/rfc3986#section-3.1
*
* @return string The URI scheme.
*/
public function getScheme(): string
{
return $this->scheme;
}
/**
* Retrieve the authority component of the URI.
*
* If no authority information is present, this method MUST return an empty
* string.
*
* The authority syntax of the URI is:
*
* <pre>
* [user-info@]host[:port]
* </pre>
*
* If the port component is not set or is the standard port for the current
* scheme, it SHOULD NOT be included.
*
* @see https://tools.ietf.org/html/rfc3986#section-3.2
*
* @return string The URI authority, in "[user-info@]host[:port]" format.
*/
public function getAuthority(bool $ignorePort = false): string
{
if ((string) $this->host === '') {
return '';
}
$authority = $this->host;
if ((string) $this->getUserInfo() !== '') {
$authority = $this->getUserInfo() . '@' . $authority;
}
// Don't add port if it's a standard port for this scheme
if ((int) $this->port !== 0 && ! $ignorePort && $this->port !== ($this->defaultPorts[$this->scheme] ?? null)) {
$authority .= ':' . $this->port;
}
$this->showPassword = false;
return $authority;
}
/**
* Retrieve the user information component of the URI.
*
* If no user information is present, this method MUST return an empty
* string.
*
* If a user is present in the URI, this will return that value;
* additionally, if the password is also present, it will be appended to the
* user value, with a colon (":") separating the values.
*
* NOTE that be default, the password, if available, will NOT be shown
* as a security measure as discussed in RFC 3986, Section 7.5. If you know
* the password is not a security issue, you can force it to be shown
* with $this->showPassword();
*
* The trailing "@" character is not part of the user information and MUST
* NOT be added.
*
* @return string|null The URI user information, in "username[:password]" format.
*/
public function getUserInfo()
{
$userInfo = $this->user;
if ($this->showPassword === true && (string) $this->password !== '') {
$userInfo .= ':' . $this->password;
}
return $userInfo;
}
/**
* Temporarily sets the URI to show a password in userInfo. Will
* reset itself after the first call to authority().
*
* Note: Method not in PSR-7
*
* @return URI
*/
public function showPassword(bool $val = true)
{
$this->showPassword = $val;
return $this;
}
/**
* Retrieve the host component of the URI.
*
* If no host is present, this method MUST return an empty string.
*
* The value returned MUST be normalized to lowercase, per RFC 3986
* Section 3.2.2.
*
* @see http://tools.ietf.org/html/rfc3986#section-3.2.2
*
* @return string The URI host.
*/
public function getHost(): string
{
return $this->host ?? '';
}
/**
* Retrieve the port component of the URI.
*
* If a port is present, and it is non-standard for the current scheme,
* this method MUST return it as an integer. If the port is the standard port
* used with the current scheme, this method SHOULD return null.
*
* If no port is present, and no scheme is present, this method MUST return
* a null value.
*
* If no port is present, but a scheme is present, this method MAY return
* the standard port for that scheme, but SHOULD return null.
*
* @return int|null The URI port.
*/
public function getPort()
{
return $this->port;
}
/**
* Retrieve the path component of the URI.
*
* The path can either be empty or absolute (starting with a slash) or
* rootless (not starting with a slash). Implementations MUST support all
* three syntaxes.
*
* Normally, the empty path "" and absolute path "/" are considered equal as
* defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically
* do this normalization because in contexts with a trimmed base path, e.g.
* the front controller, this difference becomes significant. It's the task
* of the user to handle both "" and "/".
*
* The value returned MUST be percent-encoded, but MUST NOT double-encode
* any characters. To determine what characters to encode, please refer to
* RFC 3986, Sections 2 and 3.3.
*
* As an example, if the value should include a slash ("/") not intended as
* delimiter between path segments, that value MUST be passed in encoded
* form (e.g., "%2F") to the instance.
*
* @see https://tools.ietf.org/html/rfc3986#section-2
* @see https://tools.ietf.org/html/rfc3986#section-3.3
*
* @return string The URI path.
*/
public function getPath(): string
{
return $this->path ?? '';
}
/**
* Retrieve the query string
*
* @param array{except?: list<string>|string, only?: list<string>|string} $options
*/
public function getQuery(array $options = []): string
{
$vars = $this->query;
if (array_key_exists('except', $options)) {
if (! is_array($options['except'])) {
$options['except'] = [$options['except']];
}
foreach ($options['except'] as $var) {
unset($vars[$var]);
}
} elseif (array_key_exists('only', $options)) {
$temp = [];
if (! is_array($options['only'])) {
$options['only'] = [$options['only']];
}
foreach ($options['only'] as $var) {
if (array_key_exists($var, $vars)) {
$temp[$var] = $vars[$var];
}
}
$vars = $temp;
}
return $vars === [] ? '' : http_build_query($vars);
}
/**
* Retrieve a URI fragment
*/
public function getFragment(): string
{
return $this->fragment ?? '';
}
/**
* Returns the segments of the path as an array.
*
* @return array<int, string>
*/
public function getSegments(): array
{
return $this->segments;
}
/**
* Returns the value of a specific segment of the URI path.
* Allows to get only existing segments or the next one.
*
* @param int $number Segment number starting at 1
* @param string $default Default value
*
* @return string The value of the segment. If you specify the last +1
* segment, the $default value. If you specify the last +2
* or more throws HTTPException.
*/
public function getSegment(int $number, string $default = ''): string
{
if ($number < 1) {
throw HTTPException::forURISegmentOutOfRange($number);
}
if ($number > count($this->segments) + 1 && ! $this->silent) {
throw HTTPException::forURISegmentOutOfRange($number);
}
// The segment should treat the array as 1-based for the user
// but we still have to deal with a zero-based array.
$number--;
return $this->segments[$number] ?? $default;
}
/**
* Set the value of a specific segment of the URI path.
* Allows to set only existing segments or add new one.
*
* Note: Method not in PSR-7
*
* @param int $number Segment number starting at 1
* @param int|string $value
*
* @return $this
*/
public function setSegment(int $number, $value)
{
if ($number < 1) {
throw HTTPException::forURISegmentOutOfRange($number);
}
if ($number > count($this->segments) + 1) {
if ($this->silent) {
return $this;
}
throw HTTPException::forURISegmentOutOfRange($number);
}
// The segment should treat the array as 1-based for the user
// but we still have to deal with a zero-based array.
$number--;
$this->segments[$number] = $value;
return $this->refreshPath();
}
/**
* Returns the total number of segments.
*
* Note: Method not in PSR-7
*/
public function getTotalSegments(): int
{
return count($this->segments);
}
/**
* Formats the URI as a string.
*
* Warning: For backwards-compatibility this method
* assumes URIs with the same host as baseURL should
* be relative to the project's configuration.
* This aspect of __toString() is deprecated and should be avoided.
*/
public function __toString(): string
{
$path = $this->getPath();
$scheme = $this->getScheme();
// If the hosts matches then assume this should be relative to baseURL
[$scheme, $path] = $this->changeSchemeAndPath($scheme, $path);
return static::createURIString(
$scheme,
$this->getAuthority(),
$path, // Absolute URIs should use a "/" for an empty path
$this->getQuery(),
$this->getFragment(),
);
}
/**
* Change the path (and scheme) assuming URIs with the same host as baseURL
* should be relative to the project's configuration.
*
* @return array{string, string}
*
* @deprecated This method will be deleted.
*/
private function changeSchemeAndPath(string $scheme, string $path): array
{
// Check if this is an internal URI
$config = config(App::class);
$baseUri = new self($config->baseURL);
if (
str_starts_with($this->getScheme(), 'http')
&& $this->getHost() === $baseUri->getHost()
) {
// Check for additional segments
$basePath = trim($baseUri->getPath(), '/') . '/';
$trimPath = ltrim($path, '/');
if ($basePath !== '/' && ! str_starts_with($trimPath, $basePath)) {
$path = $basePath . $trimPath;
}
// Check for forced HTTPS
if ($config->forceGlobalSecureRequests) {
$scheme = 'https';
}
}
return [$scheme, $path];
}
/**
* Parses the given string and saves the appropriate authority pieces.
*
* Note: Method not in PSR-7
*
* @return $this
*/
public function setAuthority(string $str)
{
$parts = parse_url($str);
if (! isset($parts['path'])) {
$parts['path'] = $this->getPath();
}
if (! isset($parts['host']) && $parts['path'] !== '') {
$parts['host'] = $parts['path'];
unset($parts['path']);
}
$this->applyParts($parts);
return $this;
}
/**
* Sets the scheme for this URI.
*
* Because of the large number of valid schemes we cannot limit this
* to only http or https.
*
* @see https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
*
* @return $this
*
* @deprecated 4.4.0 Use `withScheme()` instead.
*/
public function setScheme(string $str)
{
$str = strtolower($str);
$this->scheme = preg_replace('#:(//)?$#', '', $str);
return $this;
}
/**
* Return an instance with the specified scheme.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified scheme.
*
* Implementations MUST support the schemes "http" and "https" case
* insensitively, and MAY accommodate other schemes if required.
*
* An empty scheme is equivalent to removing the scheme.
*
* @param string $scheme The scheme to use with the new instance.
*
* @return static A new instance with the specified scheme.
*
* @throws InvalidArgumentException for invalid or unsupported schemes.
*/
public function withScheme(string $scheme)
{
$uri = clone $this;
$scheme = strtolower($scheme);
$uri->scheme = preg_replace('#:(//)?$#', '', $scheme);
return $uri;
}
/**
* Sets the userInfo/Authority portion of the URI.
*
* @param string $user The user's username
* @param string $pass The user's password
*
* @return $this
*
* @TODO PSR-7: Should be `withUserInfo($user, $password = null)`.
*/
public function setUserInfo(string $user, #[SensitiveParameter] string $pass)
{
$this->user = trim($user);
$this->password = trim($pass);
return $this;
}
/**
* Sets the host name to use.
*
* @return $this
*
* @TODO PSR-7: Should be `withHost($host)`.
*/
public function setHost(string $str)
{
$this->host = trim($str);
return $this;
}
/**
* Sets the port portion of the URI.
*
* @return $this
*
* @TODO PSR-7: Should be `withPort($port)`.
*/
public function setPort(?int $port = null)
{
if ($port === null) {
return $this;
}
if ($port > 0 && $port <= 65535) {
$this->port = $port;
return $this;
}
if ($this->silent) {
return $this;
}
throw HTTPException::forInvalidPort($port);
}
/**
* Sets the path portion of the URI.
*
* @return $this
*
* @TODO PSR-7: Should be `withPath($port)`.
*/
public function setPath(string $path)
{
$this->path = $this->filterPath($path);
$tempPath = trim($this->path, '/');
$this->segments = ($tempPath === '') ? [] : explode('/', $tempPath);
return $this;
}
/**
* Sets the current baseURL.
*
* @interal
*
* @deprecated Use SiteURI instead.
*/
public function setBaseURL(string $baseURL): void
{
$this->baseURL = $baseURL;
}
/**
* Returns the current baseURL.
*
* @interal
*
* @deprecated Use SiteURI instead.
*/
public function getBaseURL(): string
{
if ($this->baseURL === null) {
throw new BadMethodCallException('The $baseURL is not set.');
}
return $this->baseURL;
}
/**
* Sets the path portion of the URI based on segments.
*
* @return $this
*
* @deprecated This method will be private.
*/
public function refreshPath()
{
$this->path = $this->filterPath(implode('/', $this->segments));
$tempPath = trim($this->path, '/');
$this->segments = $tempPath === '' ? [] : explode('/', $tempPath);
return $this;
}
/**
* Sets the query portion of the URI, while attempting
* to clean the various parts of the query keys and values.
*
* @return $this
*
* @TODO PSR-7: Should be `withQuery($query)`.
*/
public function setQuery(string $query)
{
if (str_contains($query, '#')) {
if ($this->silent) {
return $this;
}
throw HTTPException::forMalformedQueryString();
}
// Can't have leading ?
if ($query !== '' && str_starts_with($query, '?')) {
$query = substr($query, 1);
}
if ($this->rawQueryString) {
$this->query = $this->parseStr($query);
} else {
parse_str($query, $this->query);
}
return $this;
}
/**
* A convenience method to pass an array of items in as the Query
* portion of the URI.
*
* @return URI
*
* @TODO: PSR-7: Should be `withQueryParams(array $query)`
*/
public function setQueryArray(array $query)
{
$query = http_build_query($query);
return $this->setQuery($query);
}
/**
* Adds a single new element to the query vars.
*
* Note: Method not in PSR-7
*
* @param int|string|null $value
*
* @return $this
*/
public function addQuery(string $key, $value = null)
{
$this->query[$key] = $value;
return $this;
}
/**
* Removes one or more query vars from the URI.
*
* Note: Method not in PSR-7
*
* @param string ...$params
*
* @return $this
*/
public function stripQuery(...$params)
{
foreach ($params as $param) {
unset($this->query[$param]);
}
return $this;
}
/**
* Filters the query variables so that only the keys passed in
* are kept. The rest are removed from the object.
*
* Note: Method not in PSR-7
*
* @param string ...$params
*
* @return $this
*/
public function keepQuery(...$params)
{
$temp = [];
foreach ($this->query as $key => $value) {
if (! in_array($key, $params, true)) {
continue;
}
$temp[$key] = $value;
}
$this->query = $temp;
return $this;
}
/**
* Sets the fragment portion of the URI.
*
* @see https://tools.ietf.org/html/rfc3986#section-3.5
*
* @return $this
*
* @TODO PSR-7: Should be `withFragment($fragment)`.
*/
public function setFragment(string $string)
{
$this->fragment = trim($string, '# ');
return $this;
}
/**
* Encodes any dangerous characters, and removes dot segments.
* While dot segments have valid uses according to the spec,
* this URI class does not allow them.
*/
protected function filterPath(?string $path = null): string
{
$orig = $path;
// Decode/normalize percent-encoded chars so
// we can always have matching for Routes, etc.
$path = urldecode($path);
// Remove dot segments
$path = self::removeDotSegments($path);
// Fix up some leading slash edge cases...
if (str_starts_with($orig, './')) {
$path = '/' . $path;
}
if (str_starts_with($orig, '../')) {
$path = '/' . $path;
}
// Encode characters
$path = preg_replace_callback(
'/(?:[^' . static::CHAR_UNRESERVED . ':@&=\+\,ドル\/;%]+|%(?![A-Fa-f0-9]{2}))/',
static fn (array $matches): string => rawurlencode($matches[0]),
$path,
);
return $path;
}
/**
* Saves our parts from a parse_url call.
*
* @param array{
* host?: string,
* user?: string,
* path?: string,
* query?: string,
* fragment?: string,
* scheme?: string,
* port?: int,
* pass?: string,
* } $parts
*
* @return void
*/
protected function applyParts(array $parts)
{
if (isset($parts['host']) && $parts['host'] !== '') {
$this->host = $parts['host'];
}
if (isset($parts['user']) && $parts['user'] !== '') {
$this->user = $parts['user'];
}
if (isset($parts['path']) && $parts['path'] !== '') {
$this->path = $this->filterPath($parts['path']);
}
if (isset($parts['query']) && $parts['query'] !== '') {
$this->setQuery($parts['query']);
}
if (isset($parts['fragment']) && $parts['fragment'] !== '') {
$this->fragment = $parts['fragment'];
}
if (isset($parts['scheme'])) {
$this->setScheme(rtrim($parts['scheme'], ':/'));
} else {
$this->setScheme('http');
}
if (isset($parts['port'])) {
// Valid port numbers are enforced by earlier parse_url or setPort()
$this->port = $parts['port'];
}
if (isset($parts['pass'])) {
$this->password = $parts['pass'];
}
if (isset($parts['path']) && $parts['path'] !== '') {
$tempPath = trim($parts['path'], '/');
$this->segments = $tempPath === '' ? [] : explode('/', $tempPath);
}
}
/**
* Combines one URI string with this one based on the rules set out in
* RFC 3986 Section 2
*
* @see http://tools.ietf.org/html/rfc3986#section-5.2
*
* @return URI
*/
public function resolveRelativeURI(string $uri)
{
/*
* NOTE: We don't use removeDotSegments in this
* algorithm since it's already done by this line!
*/
$relative = new self();
$relative->setURI($uri);
if ($relative->getScheme() === $this->getScheme()) {
$relative->setScheme('');
}
$transformed = clone $relative;
// 5.2.2 Transform References in a non-strict method (no scheme)
if ($relative->getAuthority() !== '') {
$transformed
->setAuthority($relative->getAuthority())
->setPath($relative->getPath())
->setQuery($relative->getQuery());
} else {
if ($relative->getPath() === '') {
$transformed->setPath($this->getPath());
if ($relative->getQuery() !== '') {
$transformed->setQuery($relative->getQuery());
} else {
$transformed->setQuery($this->getQuery());
}
} else {
if (str_starts_with($relative->getPath(), '/')) {
$transformed->setPath($relative->getPath());
} else {
$transformed->setPath($this->mergePaths($this, $relative));
}
$transformed->setQuery($relative->getQuery());
}
$transformed->setAuthority($this->getAuthority());
}
$transformed->setScheme($this->getScheme());
$transformed->setFragment($relative->getFragment());
return $transformed;
}
/**
* Given 2 paths, will merge them according to rules set out in RFC 2986,
* Section 5.2
*
* @see http://tools.ietf.org/html/rfc3986#section-5.2.3
*/
protected function mergePaths(self $base, self $reference): string
{
if ($base->getAuthority() !== '' && $base->getPath() === '') {
return '/' . ltrim($reference->getPath(), '/ ');
}
$path = explode('/', $base->getPath());
if ($path[0] === '') {
unset($path[0]);
}
array_pop($path);
$path[] = $reference->getPath();
return implode('/', $path);
}
/**
* This is equivalent to the native PHP parse_str() function.
* This version allows the dot to be used as a key of the query string.
*
* @return array<string, string>
*/
protected function parseStr(string $query): array
{
$return = [];
$query = explode('&', $query);
$params = array_map(static fn (string $chunk): ?string => preg_replace_callback(
'/^(?<key>[^&=]+?)(?:\[[^&=]*\])?=(?<value>[^&=]+)/',
static fn (array $match): string => str_replace($match['key'], bin2hex($match['key']), $match[0]),
urldecode($chunk),
), $query);
$params = implode('&', $params);
parse_str($params, $result);
foreach ($result as $key => $value) {
// Array key might be int
$return[hex2bin((string) $key)] = $value;
}
return $return;
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wfdaj/CodeIgniter4.git
git@gitee.com:wfdaj/CodeIgniter4.git
wfdaj
CodeIgniter4
CodeIgniter4
develop
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /