3 * @see https://github.com/zendframework/zend-uri for the canonical source repository
4 * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
5 * @license https://github.com/zendframework/zend-uri/blob/master/LICENSE.md New BSD License
13 class Http
extends Uri
16 * @see Uri::$validSchemes
18 protected static $validSchemes = [
24 * @see Uri::$defaultPorts
26 protected static $defaultPorts = [
32 * @see Uri::$validHostTypes
34 protected $validHostTypes = self
::HOST_DNS_OR_IPV4_OR_IPV6_OR_REGNAME
;
37 * User name as provided in authority of URI
43 * Password as provided in authority of URI
49 * Get the username part (before the ':') of the userInfo URI part
53 public function getUser()
59 * Get the password part (after the ':') of the userInfo URI part
63 public function getPassword()
65 return $this->password
;
69 * Get the User-info (usually user:password) part
73 public function getUserInfo()
75 return $this->userInfo
;
79 * Set the username part (before the ':') of the userInfo URI part
81 * @param string|null $user
85 public function setUser($user)
87 $this->user
= null === $user ?
null : (string) $user;
89 $this->buildUserInfo();
95 * Set the password part (after the ':') of the userInfo URI part
97 * @param string $password
101 public function setPassword($password)
103 $this->password
= null === $password ?
null : (string) $password;
105 $this->buildUserInfo();
111 * Set the URI User-info part (usually user:password)
113 * @param string|null $userInfo
117 * @throws Exception\InvalidUriPartException If the schema definition does not have this part
119 public function setUserInfo($userInfo)
121 $this->userInfo
= null === $userInfo ?
null : (string) $userInfo;
123 $this->parseUserInfo();
129 * Validate the host part of an HTTP URI
131 * This overrides the common URI validation method with a DNS or IP only
132 * default. Users may still enforce allowing other host types.
134 * @param string $host
135 * @param int $allowed
138 public static function validateHost($host, $allowed = self
::HOST_DNS_OR_IPV4_OR_IPV6
)
140 return parent
::validateHost($host, $allowed);
144 * Parse the user info into username and password segments
146 * Parses the user information into username and password segments, and
147 * then sets the appropriate values.
151 protected function parseUserInfo()
153 // No user information? we're done
154 if (null === $this->userInfo
) {
155 $this->setUser(null);
156 $this->setPassword(null);
161 // If no ':' separator, we only have a username
162 if (false === strpos($this->userInfo
, ':')) {
163 $this->setUser($this->userInfo
);
164 $this->setPassword(null);
168 // Split on the ':', and set both user and password
169 list($this->user
, $this->password
) = explode(':', $this->userInfo
, 2);
173 * Build the user info based on user and password
175 * Builds the user info based on the given user and password values
179 protected function buildUserInfo()
181 if (null !== $this->password
) {
182 $this->userInfo
= $this->user
. ':' . $this->password
;
184 $this->userInfo
= $this->user
;
189 * Return the URI port
191 * If no port is set, will return the default port according to the scheme
194 * @see Zend\Uri\Uri::getPort()
196 public function getPort()
198 if (empty($this->port
)) {
199 if (array_key_exists($this->scheme
, static::$defaultPorts)) {
200 return static::$defaultPorts[$this->scheme
];
212 public function parse($uri)
216 if (empty($this->path
)) {