This commit is contained in:
2021-04-28 19:05:15 +08:00
parent 1c39bf1f2b
commit 81fdf7e929
3 changed files with 16 additions and 18 deletions
+6 -6
View File
@@ -516,7 +516,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @param $url * @param $url
* @return bool * @return bool
*/ */
protected function isHttp($url): bool #[Pure] protected function isHttp($url): bool
{ {
return str_starts_with($url, 'http://'); return str_starts_with($url, 'http://');
} }
@@ -525,7 +525,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @param $url * @param $url
* @return bool * @return bool
*/ */
protected function isHttps($url): bool #[Pure] protected function isHttps($url): bool
{ {
return str_starts_with($url, 'https://'); return str_starts_with($url, 'https://');
} }
@@ -669,7 +669,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @return bool * @return bool
* check isPost Request * check isPost Request
*/ */
public function isPost(): bool #[Pure] public function isPost(): bool
{ {
return strtolower($this->method) === self::POST; return strtolower($this->method) === self::POST;
} }
@@ -680,7 +680,7 @@ abstract class ClientAbstracts extends Component implements IClient
* *
* check isGet Request * check isGet Request
*/ */
public function isGet(): bool #[Pure] public function isGet(): bool
{ {
return strtolower($this->method) === self::GET; return strtolower($this->method) === self::GET;
} }
@@ -691,7 +691,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @return array|string * @return array|string
* 将请求参数进行编码 * 将请求参数进行编码
*/ */
protected function paramEncode($arr): array|string #[Pure] protected function paramEncode($arr): array|string
{ {
if (!is_array($arr)) { if (!is_array($arr)) {
return $arr; return $arr;
@@ -757,7 +757,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @param $params * @param $params
* @return string * @return string
*/ */
protected function joinGetParams($path, $params): string #[Pure] protected function joinGetParams($path, $params): string
{ {
if (empty($params)) { if (empty($params)) {
return $path; return $path;
+2 -1
View File
@@ -5,6 +5,7 @@ namespace HttpServer\Client;
use Exception; use Exception;
use JetBrains\PhpStorm\Pure;
/** /**
@@ -212,7 +213,7 @@ class Curl extends ClientAbstracts
/** /**
* @return array * @return array
*/ */
private function parseHeaderMat(): array #[Pure] private function parseHeaderMat(): array
{ {
$headers = []; $headers = [];
foreach ($this->getHeader() as $key => $val) { foreach ($this->getHeader() as $key => $val) {
+8 -11
View File
@@ -10,8 +10,8 @@ use Snowflake\Abstracts\Component;
use Snowflake\Core\Json; use Snowflake\Core\Json;
use Snowflake\Core\Xml; use Snowflake\Core\Xml;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Http2\Request;
use Swoole\Coroutine\Http2\Client as H2Client; use Swoole\Coroutine\Http2\Client as H2Client;
use Swoole\Http2\Request;
use Swoole\Http2\Response; use Swoole\Http2\Response;
@@ -141,7 +141,10 @@ class Http2 extends Component
} }
$client = $this->getClient($domain, $ssl, $timeout); $client = $this->getClient($domain, $ssl, $timeout);
$request = $this->getRequest($domain, $path, $method, $params, $isUpload); $request = $this->getRequest($path, $method, $params, $isUpload);
$request->headers = array_merge($request->headers, [
'Host' => $domain
]);
$client->send($request); $client->send($request);
defer(function () use ($domain, $path, $client, $request, $method) { defer(function () use ($domain, $path, $client, $request, $method) {
@@ -200,7 +203,6 @@ class Http2 extends Component
/** /**
* @param $domain
* @param $path * @param $path
* @param $method * @param $method
* @param $params * @param $params
@@ -208,7 +210,7 @@ class Http2 extends Component
* @return Request * @return Request
* @throws Exception * @throws Exception
*/ */
public function getRequest($domain, $path, $method, $params, $isUpload = false): Request public function getRequest($path, $method, $params, $isUpload = false): Request
{ {
if (!str_starts_with($path, '/')) { if (!str_starts_with($path, '/')) {
$path = '/' . $path; $path = '/' . $path;
@@ -245,16 +247,11 @@ class Http2 extends Component
$domain = rtrim($domain, '/'); $domain = rtrim($domain, '/');
if (str_contains($domain, ':')) { if (str_contains($domain, ':')) {
[$domain, $port] = explode(':', $domain); [$domain, $port] = explode(':', $domain);
} else if ($isSsl === true) {
$port = 443;
} else { } else {
$port = 80; $port = $isSsl === true ? 443 : 80;
} }
$client = new H2Client($domain, (int)$port, $isSsl); $client = new H2Client($domain, (int)$port, $isSsl);
$client->set([ $client->set(['timeout' => $timeout, 'ssl_host_name' => $domain]);
'timeout' => $timeout,
'ssl_host_name' => $domain
]);
return $client; return $client;
}); });
if ($client->connected && $client->ping()) { if ($client->connected && $client->ping()) {