diff --git a/HttpServer/Client/Client.php b/HttpServer/Client/Client.php index f4d81ef5..1c321d21 100644 --- a/HttpServer/Client/Client.php +++ b/HttpServer/Client/Client.php @@ -11,6 +11,8 @@ namespace HttpServer\Client; use Snowflake\Core\Help; use Exception; +use Snowflake\Exception\ComponentException; +use Snowflake\Snowflake; use Swoole\Coroutine; use Swoole\Coroutine\Http\Client as SClient; use Swoole\Coroutine\System; @@ -18,6 +20,7 @@ use Swoole\Coroutine\System; /** * Class Client * @package Snowflake\Snowflake\Http + * @property Http2 $http2 */ class Client { @@ -110,6 +113,17 @@ class Client { } + + /** + * @return Http2 + * @throws ComponentException + */ + public function getHttp2() + { + return Snowflake::app()->get('http2'); + } + + /** * @param $data */ diff --git a/HttpServer/Client/Http2.php b/HttpServer/Client/Http2.php new file mode 100644 index 00000000..0a42a3ee --- /dev/null +++ b/HttpServer/Client/Http2.php @@ -0,0 +1,117 @@ +getClient($domain, $path, $timeout); + $client->send($this->getRequest($domain, $path, 'GET', $params)); + return $client->recv(); + } + + + /** + * @param $domain + * @param $path + * @param array $params + * @param int $timeout + * @return mixed + * @throws Exception + */ + public function push($domain, $path, $params = [], $timeout = -1) + { + $client = $this->getClient($domain, $path, $timeout); + $client->send($this->getRequest($domain, $path, 'POST', $params)); + return $client->recv(); + } + + + /** + * @param $domain + * @param $path + * @param $method + * @param $params + * @return Request + * @throws Exception + */ + public function getRequest($domain, $path, $method, $params) + { + if (isset($this->_requests[$domain . $path])) { + $req = $this->_requests[$domain . $path]; + } else { + $req = new Request(); + $this->_requests[$domain . $path] = $req; + } + $req->method = $method; + $req->path = $path; + $req->headers = [ + 'host' => $domain, + 'user-agent' => 'Chrome/49.0.2587.3', + 'accept' => 'text/html,application/json', + 'accept-encoding' => 'gzip' + ]; + if (!is_string($params)) { + $params = JSON::encode($params); + } + $req->data = $params; + return $req; + } + + + /** + * @param $domain + * @param $path + * @param int $timeout + * @return H2Client + * @throws Exception + */ + private function getClient($domain, $path, $timeout = -1) + { + if (isset($this->_clients[$path])) { + return $this->_clients[$path]; + } + $client = new H2Client($domain, 443, true); + $client->set([ + 'timeout' => $timeout, + 'ssl_host_name' => $domain + ]); + if (!$client->connect()) { + throw new Exception('Connected fail.'); + } + return $this->_clients[$domain . $path] = $client; + } + + +} diff --git a/System/Abstracts/BaseApplication.php b/System/Abstracts/BaseApplication.php index 0902d989..e0faa60a 100644 --- a/System/Abstracts/BaseApplication.php +++ b/System/Abstracts/BaseApplication.php @@ -10,6 +10,8 @@ namespace Snowflake\Abstracts; use Exception; +use HttpServer\Client\Client; +use HttpServer\Client\Http2; use HttpServer\Http\Request; use HttpServer\Http\Response; use HttpServer\Route\Router; @@ -46,6 +48,7 @@ use Database\DatabasesProviders; * @property Logger $logger * @property Jwt $jwt * @property BaseGoto $goto + * @property Client $client */ abstract class BaseApplication extends Service { @@ -104,7 +107,7 @@ abstract class BaseApplication extends Service * * @return array */ - protected function readLinesFromFile($filePath) + protected function readLinesFromFile(string $filePath) { // Read file into an array of lines with auto-detected line endings $autodetect = ini_get('auto_detect_line_endings'); @@ -122,7 +125,7 @@ abstract class BaseApplication extends Service * * @return bool */ - protected function isComment($line) + protected function isComment(string $line) { $line = ltrim($line); @@ -136,7 +139,7 @@ abstract class BaseApplication extends Service * * @return bool */ - protected function looksLikeSetter($line) + protected function looksLikeSetter(string $line) { return strpos($line, '=') !== false; } @@ -363,6 +366,8 @@ abstract class BaseApplication extends Service 'error' => ['class' => ErrorHandler::class], 'event' => ['class' => Event::class], 'annotation' => ['class' => Annotation::class], + 'client' => ['class' => Client::class], + 'http2' => ['class' => Http2::class], 'connections' => ['class' => Connection::class], 'redis_connections' => ['class' => SRedis::class], 'pool' => ['class' => SPool::class],