From f8103f93b94f60eef99794df189d7faf7f0933af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 3 Apr 2020 18:27:06 +0800 Subject: [PATCH] add clear --- wchat/common/HttpClient.php | 66 +++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/wchat/common/HttpClient.php b/wchat/common/HttpClient.php index bb1237b..3e4ef0f 100644 --- a/wchat/common/HttpClient.php +++ b/wchat/common/HttpClient.php @@ -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)]; } /**