paulgoughbooks_old
This repository has no backups
This repository's network speed is throttled to 100KB/sec
Upgrade your account to fix these warnings, or use backups.vc for automated backups
@ 2
paulgoughbooks_old / trunk / system / HTTP / URI.php
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 |
<?php /** * CodeIgniter * * An open source application development framework for PHP * * This content is released under the MIT License (MIT) * * Copyright (c) 2014-2019 British Columbia Institute of Technology * Copyright (c) 2019-2020 CodeIgniter Foundation * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * * @package CodeIgniter * @author CodeIgniter Dev Team * @copyright 2019-2020 CodeIgniter Foundation * @license https://opensource.org/licenses/MIT MIT License * @link https://codeigniter.com * @since Version 4.0.0 * @filesource */ namespace CodeIgniter\HTTP; use CodeIgniter\HTTP\Exceptions\HTTPException; /** * Abstraction for a uniform resource identifier (URI). */ class URI { /** * Sub-delimiters used in query strings and fragments. * * @const string */ const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;='; /** * Unreserved characters used in paths, query strings, and fragments. * * @const string */ const CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~'; /** * Current URI string * * @var string */ protected $uriString; /** * List of URI segments. * * Starts at 1 instead of 0 * * @var array */ protected $segments = []; /** * The URI Scheme. * * @var string */ protected $scheme = 'http'; /** * URI User Info * * @var string */ protected $user; /** * URI User Password * * @var string */ protected $password; /** * URI Host * * @var string */ protected $host; /** * URI Port * * @var integer */ protected $port; /** * URI path. * * @var string */ protected $path; /** * The name of any fragment. * * @var string */ protected $fragment = ''; /** * The query string. * * @var array */ protected $query = []; /** * Default schemes/ports. * * @var array */ 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 boolean */ protected $showPassword = false; /** * If true, will continue instead of throwing exceptions. * * @var boolean */ protected $silent = false; //-------------------------------------------------------------------- /** * Constructor. * * @param string $uri * * @throws \InvalidArgumentException */ public function __construct(string $uri = null) { if (! is_null($uri)) { $this->setURI($uri); } } //-------------------------------------------------------------------- /** * If $silent == true, then will not throw exceptions and will * attempt to continue gracefully. * * @param boolean $silent * * @return URI */ public function setSilent(bool $silent = true) { $this->silent = $silent; return $this; } //-------------------------------------------------------------------- /** * Sets and overwrites any current URI information. * * @param string|null $uri * * @return URI */ public function setURI(string $uri = null) { if (! is_null($uri)) { $parts = parse_url($uri); if ($parts === false) { if ($this->silent) { return $this; } throw HTTPException::forUnableToParseURI($uri); } $this->applyParts($parts); } return $this; } //-------------------------------------------------------------------- /** * 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 * * @param boolean $ignorePort * * @return string The URI authority, in "[user-info@]host[:port]" format. */ public function getAuthority(bool $ignorePort = false): string { if (empty($this->host)) { return ''; } $authority = $this->host; if (! empty($this->getUserInfo())) { $authority = $this->getUserInfo() . '@' . $authority; } if (! empty($this->port) && ! $ignorePort) { // Don't add port if it's a standard port for // this scheme if ($this->port !== $this->defaultPorts[$this->scheme]) { $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 && ! empty($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(). * * @param boolean $val * * @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 null|integer 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 (is_null($this->path)) ? '' : $this->path; } //-------------------------------------------------------------------- /** * Retrieve the query string * * @param array $options * * @return string */ 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 empty($vars) ? '' : http_build_query($vars); } //-------------------------------------------------------------------- /** * Retrieve a URI fragment * * @return string */ public function getFragment(): string { return is_null($this->fragment) ? '' : $this->fragment; } //-------------------------------------------------------------------- /** * Returns the segments of the path as an array. * * @return array */ public function getSegments(): array { return $this->segments; } //-------------------------------------------------------------------- /** * Returns the value of a specific segment of the URI path. * * @param integer $number Segment number * @param string $default Default value * * @return string The value of the segment. If no segment is found, * throws InvalidArgumentError */ public function getSegment(int $number, string $default = ''): string { // The segment should treat the array as 1-based for the user // but we still have to deal with a zero-based array. $number -= 1; if ($number > count($this->segments) && ! $this->silent) { throw HTTPException::forURISegmentOutOfRange($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. * * @param integer $number * @param mixed $value (string or int) * * @return $this */ public function setSegment(int $number, $value) { // The segment should treat the array as 1-based for the user // but we still have to deal with a zero-based array. $number -= 1; if ($number > count($this->segments) + 1) { if ($this->silent) { return $this; } throw HTTPException::forURISegmentOutOfRange($number); } $this->segments[$number] = $value; $this->refreshPath(); return $this; } //-------------------------------------------------------------------- /** * Returns the total number of segments. * * @return integer */ public function getTotalSegments(): int { return count($this->segments); } //-------------------------------------------------------------------- /** * Allow the URI to be output as a string by simply casting it to a string * or echoing out. */ public function __toString(): string { return static::createURIString( $this->getScheme(), $this->getAuthority(), $this->getPath(), // Absolute URIs should use a "/" for an empty path $this->getQuery(), $this->getFragment() ); } //-------------------------------------------------------------------- /** * Builds a representation of the string from the component parts. * * @param string $scheme * @param string $authority * @param string $path * @param string $query * @param string $fragment * * @return string */ public static function createURIString(string $scheme = null, string $authority = null, string $path = null, string $query = null, string $fragment = null): string { $uri = ''; if (! empty($scheme)) { $uri .= $scheme . '://'; } if (! empty($authority)) { $uri .= $authority; } if ($path !== '') { $uri .= substr($uri, -1, 1) !== '/' ? '/' . ltrim($path, '/') : ltrim($path, '/'); } if ($query) { $uri .= '?' . $query; } if ($fragment) { $uri .= '#' . $fragment; } return $uri; } //-------------------------------------------------------------------- /** * Parses the given string an saves the appropriate authority pieces. * * @param string $str * * @return $this */ public function setAuthority(string $str) { $parts = parse_url($str); if (! isset($parts['path'])) { $parts['path'] = $this->getPath(); } if (empty($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 * * @param $str * * @return $this */ public function setScheme(string $str) { $str = strtolower($str); $str = preg_replace('#:(//)?$#', '', $str); $this->scheme = $str; return $this; } //-------------------------------------------------------------------- /** * Sets the userInfo/Authority portion of the URI. * * @param string $user The user's username * @param string $pass The user's password * * @return $this */ public function setUserInfo(string $user, string $pass) { $this->user = trim($user); $this->password = trim($pass); return $this; } //-------------------------------------------------------------------- /** * Sets the host name to use. * * @param string $str * * @return $this */ public function setHost(string $str) { $this->host = trim($str); return $this; } //-------------------------------------------------------------------- /** * Sets the port portion of the URI. * * @param integer $port * * @return $this */ public function setPort(int $port = null) { if (is_null($port)) { return $this; } if ($port <= 0 || $port > 65535) { if ($this->silent) { return $this; } throw HTTPException::forInvalidPort($port); } $this->port = $port; return $this; } //-------------------------------------------------------------------- /** * Sets the path portion of the URI. * * @param string $path * * @return $this */ public function setPath(string $path) { $this->path = $this->filterPath($path); $tempPath = trim($this->path, '/'); $this->segments = ($tempPath === '') ? [] : explode('/', $tempPath); return $this; } /** * Sets the path portion of the URI based on segments. * * @param string $path * * @return $this */ 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. * * @param string $query * * @return $this */ public function setQuery(string $query) { if (strpos($query, '#') !== false) { if ($this->silent) { return $this; } throw HTTPException::forMalformedQueryString(); } // Can't have leading ? if (! empty($query) && strpos($query, '?') === 0) { $query = substr($query, 1); } parse_str($query, $this->query); return $this; } //-------------------------------------------------------------------- /** * A convenience method to pass an array of items in as the Query * portion of the URI. * * @param array $query * * @return \CodeIgniter\HTTP\URI */ public function setQueryArray(array $query) { $query = http_build_query($query); return $this->setQuery($query); } //-------------------------------------------------------------------- /** * Adds a single new element to the query vars. * * @param string $key * @param mixed $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. * * @param array ...$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. * * @param array ...$params * * @return $this */ public function keepQuery(...$params) { $temp = []; foreach ($this->query as $key => $value) { if (! in_array($key, $params)) { 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 * * @param string $string * * @return $this */ 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. * * @param $path * * @return string */ 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 = $this->removeDotSegments($path); // Fix up some leading slash edge cases... if (strpos($orig, './') === 0) { $path = '/' . $path; } if (strpos($orig, '../') === 0) { $path = '/' . $path; } // Encode characters $path = preg_replace_callback( '/(?:[^' . static::CHAR_UNRESERVED . ':@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/', function (array $matches) { return rawurlencode($matches[0]); }, $path ); return $path; } //-------------------------------------------------------------------- /** * Saves our parts from a parse_url call. * * @param array $parts */ protected function applyParts(array $parts) { if (! empty($parts['host'])) { $this->host = $parts['host']; } if (! empty($parts['user'])) { $this->user = $parts['user']; } if (isset($parts['path']) && $parts['path'] !== '') { $this->path = $this->filterPath($parts['path']); } if (! empty($parts['query'])) { $this->setQuery($parts['query']); } if (! empty($parts['fragment'])) { $this->fragment = $parts['fragment']; } // Scheme if (isset($parts['scheme'])) { $this->setScheme(rtrim($parts['scheme'], ':/')); } else { $this->setScheme('http'); } // Port if (isset($parts['port'])) { if (! is_null($parts['port'])) { // Valid port numbers are enforced by earlier parse_url or setPort() $port = $parts['port']; $this->port = $port; } } if (isset($parts['pass'])) { $this->password = $parts['pass']; } // Populate our segments array 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 * * @param string $uri * * @return \CodeIgniter\HTTP\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 URI(); $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 (! empty($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 (strpos($relative->getPath(), '/') === 0) { $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 * * @param URI $base * @param URI $reference * * @return string */ protected function mergePaths(URI $base, URI $reference): string { if (! empty($base->getAuthority()) && $base->getPath() === '') { return '/' . ltrim($reference->getPath(), '/ '); } $path = explode('/', $base->getPath()); if ($path[0] === '') { unset($path[0]); } array_pop($path); array_push($path, $reference->getPath()); return implode('/', $path); } //-------------------------------------------------------------------- /** * 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 * * @param string $path * * @return string * @internal param \CodeIgniter\HTTP\URI $uri */ public 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); } else if ($segment !== '.' && $segment !== '') { array_push($output, $segment); } } $output = implode('/', $output); $output = ltrim($output, '/ '); if ($output !== '/') { // Add leading slash if necessary if (strpos($path, '/') === 0) { $output = '/' . $output; } // Add trailing slash if necessary if (substr($path, -1, 1) === '/') { $output .= '/'; } } return $output; } //-------------------------------------------------------------------- } |
Commits for paulgoughbooks_old/trunk/system/HTTP/URI.php
Revision | Author | Commited | Message |
---|---|---|---|
2 | tporter | Tue 03 Nov, 2020 08:50:21 +0000 | Migration of Paul Goughs Books site to Codeignitor 4 |