add clear

This commit is contained in:
2020-04-03 18:27:06 +08:00
parent db6d6a0611
commit f8103f93b9
+56 -10
View File
@@ -282,8 +282,11 @@ class HttpClient
{
$data = $this->paramEncode($data);
if ($this->use_swoole) {
[$url, $host] = $this->parseUrlHost($url);
return $this->coroutine($host, $url, $data);
$url = $this->matchHost($url);
if (!$this->checkIsIp($this->host)) {
$this->host = System::gethostbyname($this->host);
}
return $this->coroutine($this->host, $url, $data);
} else {
return $this->useCurl($url, $data);
}
@@ -297,6 +300,53 @@ class HttpClient
return function_exists('getIsCli') && getIsCli();
}
/**
* @param string $string
* @return bool|mixed
*/
private function matchHost($string = '')
{
if (empty($string)) {
return false;
}
$string = str_replace('http://', '', $string);
if ($this->isHttps($string)) {
$string = str_replace('https://', '', $string);
$this->isSSL = true;
$this->port = 443;
}
$hostAndUrls = explode('/', $string);
if (empty($hostAndUrls)) {
return false;
}
$this->host = array_shift($hostAndUrls);
$hostAndUrls = implode('/', $hostAndUrls);
if (strpos($this->host, ':') !== false) {
[$this->host, $this->port] = explode(':', $this->host);
if ($this->checkIsIp($this->host)) {
return '/' . $hostAndUrls;
}
}
if (!$this->isDomainName($this->host)) {
return false;
}
return '/' . $hostAndUrls;
}
/**
* @param $name
* @return bool|mixed
*/
private function isDomainName($name)
{
if (!preg_match('/^[a-zA-Z\-0-9]+(\.[a-zA-Z\-0-9]+)+[^\/]?/', $name, $out)) {
return false;
}
return $out[0];
}
/**
* @param $url
* @param $data
@@ -320,18 +370,14 @@ class HttpClient
*/
private function parseUrlHost($url)
{
if (!empty($this->host) && strpos($this->host, $url) === false) {
$url = rtrim($this->host, '/') . '/' . ltrim($url, '/');
}
if (!$this->isHttp($url) && !$this->isHttps($url)) {
$url = ($this->isSSL ? 'https://' : 'http://') . $url;
}
[$host, $url] = $this->cutRequestUrl($url);
if ($this->checkIsIp($host)) {
return [$url, $host];
$url = $this->matchHost($url);
if ($this->checkIsIp($this->host)) {
return [$url, $this->host];
}
$this->host = $host;
return [$url, System::gethostbyname($host)];
return [$url, System::gethostbyname($this->host)];
}
/**