-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractUri.php
More file actions
160 lines (147 loc) · 3.95 KB
/
Copy pathAbstractUri.php
File metadata and controls
160 lines (147 loc) · 3.95 KB
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
<?php
/**
* This file is part of HttpMessage
*
* @package bdk/http-message
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2024 Brad Kent
* @version v1.0
*/
namespace bdk\HttpMessage;
use bdk\HttpMessage\AssertionTrait;
use InvalidArgumentException;
/**
* Extended by Uri
*
* All the non-public Uri bits
*
* @psalm-consistent-constructor
*/
abstract class AbstractUri
{
use AssertionTrait;
const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;=';
const CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~';
/** @var array<string,int> */
private static $schemes = array(
'ftp' => 21,
'http' => 80,
'https' => 443,
);
/**
* Create path component of Uri
*
* @param string $authority Authority [user-info@]host[:port]
* @param string $path Path
*
* @return string
*/
protected static function createUriPath($authority, $path)
{
if ($path === '') {
return $path;
}
if ($path[0] !== '/' && $authority !== '') {
// If the path is rootless and an authority is present,
// the path MUST be prefixed by "/"
return '/' . $path;
}
if (\substr($path, 0, 2) === '//' && $authority === '') {
// If the path is starting with more than one "/" and no authority is present,
// starting slashes MUST be reduced to one.
return '/' . \ltrim($path, '/');
}
return $path;
}
/**
* Filter/validate path
*
* @param string $path URI path
*
* @return string
* @throws InvalidArgumentException
*/
protected function filterPath($path)
{
$this->assertString($path, 'path');
$specPattern = '%:@\/';
$encodePattern = '%(?![A-Fa-f0-9]{2})';
$regex = '/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . $specPattern . ']+|' . $encodePattern . ')/';
return $this->regexEncode($regex, $path);
}
/**
* Filter/validate port
*
* @param null|int|string $port Port
*
* @return null|int
* @throws InvalidArgumentException
*/
protected function filterPort($port)
{
if ($port === null) {
return null;
}
if (\is_string($port) && \preg_match('/^\d+$/', $port)) {
$port = (int) $port;
}
$this->assertPort($port);
return $port;
}
/**
* Filter/validate query and fragment
*
* @param string $str query or fragment
*
* @return string
*/
protected function filterQueryAndFragment($str)
{
$specPattern = '%:@\/\?';
$encodePattern = '%(?![A-Fa-f0-9]{2})';
$regex = '/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . $specPattern . ']+|' . $encodePattern . ')/';
return $this->regexEncode($regex, $str);
}
/**
* Is a given port standard for the given scheme?
*
* @param string $scheme Scheme
* @param int|null $port Port
*
* @return bool
*/
protected static function isStandardPort($scheme, $port)
{
return isset(self::$schemes[$scheme]) && $port === self::$schemes[$scheme];
}
/**
* Perform Locale-independent lowercasing
*
* @param string $str String to lowercase
*
* @return string
*/
protected static function lowercase($str)
{
return \strtr(
$str,
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz'
);
}
/**
* Call rawurlencode on on match
*
* @param non-empty-string $regex Regular expression
* @param string $str string
*
* @return string
*/
private static function regexEncode($regex, $str)
{
return \preg_replace_callback($regex, static function ($matches) {
return \rawurlencode($matches[0]);
}, $str);
}
}