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
<?php
/**
* @see https://github.com/zendframework/zend-uri for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-uri/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Uri;
/**
* HTTP URI handler
*/
class Http extends Uri
{
/**
* @see Uri::$validSchemes
*/
protected static $validSchemes = [
'http',
'https'
];
/**
* @see Uri::$defaultPorts
*/
protected static $defaultPorts = [
'http' => 80,
'https' => 443,
];
/**
* @see Uri::$validHostTypes
*/
protected $validHostTypes = self::HOST_DNS_OR_IPV4_OR_IPV6_OR_REGNAME;
/**
* User name as provided in authority of URI
* @var null|string
*/
protected $user;
/**
* Password as provided in authority of URI
* @var null|string
*/
protected $password;
/**
* Get the username part (before the ':') of the userInfo URI part
*
* @return string|null
*/
public function getUser()
{
return $this->user;
}
/**
* Get the password part (after the ':') of the userInfo URI part
*
* @return string|null
*/
public function getPassword()
{
return $this->password;
}
/**
* Get the User-info (usually user:password) part
*
* @return string|null
*/
public function getUserInfo()
{
return $this->userInfo;
}
/**
* Set the username part (before the ':') of the userInfo URI part
*
* @param string|null $user
*
* @return self
*/
public function setUser($user)
{
$this->user = null === $user ? null : (string) $user;
$this->buildUserInfo();
return $this;
}
/**
* Set the password part (after the ':') of the userInfo URI part
*
* @param string $password
*
* @return self
*/
public function setPassword($password)
{
$this->password = null === $password ? null : (string) $password;
$this->buildUserInfo();
return $this;
}
/**
* Set the URI User-info part (usually user:password)
*
* @param string|null $userInfo
*
* @return self
*
* @throws Exception\InvalidUriPartException If the schema definition does not have this part
*/
public function setUserInfo($userInfo)
{
$this->userInfo = null === $userInfo ? null : (string) $userInfo;
$this->parseUserInfo();
return $this;
}
/**
* Validate the host part of an HTTP URI
*
* This overrides the common URI validation method with a DNS or IP only
* default. Users may still enforce allowing other host types.
*
* @param string $host
* @param int $allowed
* @return bool
*/
public static function validateHost($host, $allowed = self::HOST_DNS_OR_IPV4_OR_IPV6)
{
return parent::validateHost($host, $allowed);
}
/**
* Parse the user info into username and password segments
*
* Parses the user information into username and password segments, and
* then sets the appropriate values.
*
* @return void
*/
protected function parseUserInfo()
{
// No user information? we're done
if (null === $this->userInfo) {
$this->setUser(null);
$this->setPassword(null);
return;
}
// If no ':' separator, we only have a username
if (false === strpos($this->userInfo, ':')) {
$this->setUser($this->userInfo);
$this->setPassword(null);
return;
}
// Split on the ':', and set both user and password
list($this->user, $this->password) = explode(':', $this->userInfo, 2);
}
/**
* Build the user info based on user and password
*
* Builds the user info based on the given user and password values
*
* @return void
*/
protected function buildUserInfo()
{
if (null !== $this->password) {
$this->userInfo = $this->user . ':' . $this->password;
} else {
$this->userInfo = $this->user;
}
}
/**
* Return the URI port
*
* If no port is set, will return the default port according to the scheme
*
* @return int
* @see Zend\Uri\Uri::getPort()
*/
public function getPort()
{
if (empty($this->port)) {
if (array_key_exists($this->scheme, static::$defaultPorts)) {
return static::$defaultPorts[$this->scheme];
}
}
return $this->port;
}
/**
* Parse a URI string
*
* @param string $uri
* @return Http
*/
public function parse($uri)
{
parent::parse($uri);
if (empty($this->path)) {
$this->path = '/';
}
return $this;
}
}