Skip to content

Commit

Permalink
🎨 Http2
Browse files Browse the repository at this point in the history
  • Loading branch information
何平 committed Mar 18, 2024
1 parent ce8c87d commit 74cbae3
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app/Task/FooTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
final class FooTask
{
#[Inject]
private StdoutLoggerInterface $logger;
private readonly StdoutLoggerInterface $logger;

public function execute()
{
Expand Down
8 changes: 4 additions & 4 deletions bin/hyperf.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
* @document https://wiki.cloud-admin.jayjay.cn
* @license https://github.com/swow-cloud/swow-admin/blob/master/LICENSE
*/
use SwowCloud\SDB\Config\ServerConfig;
use SwowCloud\SDB\Config\SslConfig;
use SwowCloud\SDB\WebSocketDebugger;
use Hyperf\Contract\ApplicationInterface;
use Hyperf\Di\ClassLoader;
use Hyperf\Di\ScanHandler\ProcScanHandler;
use Psr\Container\ContainerInterface;
use Swow\Coroutine;
use Swow\Debug\Debugger\Debugger;
use SwowCloud\SDB\Config\ServerConfig;
use SwowCloud\SDB\Config\SslConfig;
use SwowCloud\SDB\WebSocketDebugger;
use Symfony\Component\Console\Application;

function initialize(): void
Expand Down Expand Up @@ -53,7 +53,7 @@ function initialize(): void
} else {
[$serverOptions, $sslOptions] = \array_values($debuggerOptions['options']);

$serverConfig = new \SwowCloud\SDB\Config\ServerConfig(
$serverConfig = new ServerConfig(
host: $serverOptions['host'],
port: $serverOptions['port']
);
Expand Down
4 changes: 2 additions & 2 deletions cloud-admin/Http2/HPack/HPack.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ public function decode(string $input, int $maxSize): ?array /* : ?array */

if ($dynamic) {
array_unshift($this->headers, $header);
$this->size += 32 + strlen($header[0]) + strlen($header[1]);
$this->size += 32 + strlen((string) $header[0]) + strlen($header[1]);
if ($this->currentMaxSize < $this->size) {
$this->resizeTable();
}
Expand All @@ -522,7 +522,7 @@ public function decode(string $input, int $maxSize): ?array /* : ?array */
continue;
}

$size += strlen($name) + strlen($value);
$size += strlen((string) $name) + strlen($value);

if ($size > $maxSize) {
return null;
Expand Down
7 changes: 4 additions & 3 deletions cloud-admin/Http2/Parser/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CloudAdmin\Http2\Parser;

use JsonException;
use function explode;
use function file_put_contents;
use function is_array;
Expand Down Expand Up @@ -80,7 +81,7 @@ public function post($name = null, $default = null): mixed
if (!isset($this->data['post'])) {
try {
$this->parsePost();
} catch (\JsonException $e) {
} catch (JsonException) {
}
}
if ($name === null) {
Expand Down Expand Up @@ -168,9 +169,9 @@ protected function parsePost(): void
return;
}
if (preg_match('/\bjson\b/i', $content_type)) {
$this->data['post'] = (array)json_decode($this->rawBody, true, 512, JSON_THROW_ON_ERROR);
$this->data['post'] = (array)json_decode((string) $this->rawBody, true, 512, JSON_THROW_ON_ERROR);
} else {
parse_str($this->rawBody, $this->data['post']);
parse_str((string) $this->rawBody, $this->data['post']);
}
}

Expand Down
12 changes: 6 additions & 6 deletions cloud-admin/Http2/Server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Hyperf\Engine\Http;

use function Hyperf\Config\config;
use CloudAdmin\Http2\Config\Ssl;
use CloudAdmin\Http2\Parser\Http2Connection;
use CloudAdmin\Http2\Parser\Http2Parser;
Expand Down Expand Up @@ -72,7 +73,7 @@ public function __construct(protected LoggerInterface $logger)
parent::__construct();

/** @var array{certificate:string,certificate_key:string,verify_peer:bool,verify_peer_name:bool,allow_self_signed:bool} $config */
$config = \Hyperf\Config\config('ssl');
$config = config('ssl');

if ($config['enable'] ?? false) {
if (! Extension::isBuiltWith('ssl')) {
Expand Down Expand Up @@ -110,12 +111,11 @@ public function start(): void
if ($this->ssl) {
$options = $this->sslConfig->toArray();
}
$this->onRequest = function (Request $request, Http2Connection $connection) {
$this->onRequest = fn(Request $request, Http2Connection $connection) =>
//http2太难了 ,等作者实现吧
// $handler = $this->handler;
// $handler($request, $connection->connection);
return new Response(200, ['A' => 'hello world'], "<h1>hello h2!<h1>");
};
// $handler = $this->handler;
// $handler($request, $connection->connection);
new Response(200, ['A' => 'hello world'], "<h1>hello h23333!<h1>");
while (ProcessManager::isRunning()) {
try {
$connection = $this->acceptConnection();
Expand Down
1 change: 0 additions & 1 deletion cloud-admin/Validation/Request/FormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function rules(): array
}

/**
* @return array
* @phpstan-return array<array-key, mixed>
*/
public function getRules(): array
Expand Down
4 changes: 2 additions & 2 deletions config/autoload/annotations.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
ArrayShape::class,
],
'class_map' => [
Server::class => BASE_PATH . '/cloud-admin//Http2/Server/Server.php',
// Server::class => BASE_PATH . '/cloud-admin/Server/Server.php',
Server::class => BASE_PATH . '/cloud-admin//Http2/Server/Server.php',
// Server::class => BASE_PATH . '/cloud-admin/Server/Server.php',
],
],
];
6 changes: 3 additions & 3 deletions config/autoload/debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
* @license https://github.com/swow-cloud/swow-admin/blob/master/LICENSE
*/
use Swow\Debug\Debugger\Debugger;

use SwowCloud\SDB\WebSocketDebugger;

use function Hyperf\Support\env;

return [
'handler' => Debugger::class,
// 'handler' => WebSocketDebugger::class,
'handler' => Debugger::class,
// 'handler' => WebSocketDebugger::class,
'options' => [
'server' => [
'host' => '127.0.0.1',
Expand Down
2 changes: 1 addition & 1 deletion config/autoload/profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
return [
'enable' => env('ENABLE_PROFILER', false),
'options' => [
'flags' => XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY,
// 'flags' => XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY,
],
];
42 changes: 42 additions & 0 deletions http2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);
/**
* This file is part of Cloud-Admin project.
*
* @link https://www.cloud-admin.jayjay.cn
* @document https://wiki.cloud-admin.jayjay.cn
* @license https://github.com/swow-cloud/swow-admin/blob/master/LICENSE
*/
$ch = \curl_init();

// 设置 URL 及其他适当的选项
\curl_setopt($ch, CURLOPT_URL, 'https://localhost:9501');
\curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($ch, CURLOPT_HTTPHEADER, ['Connection: keep-alive']);

// 获取响应内容
$response = \curl_exec($ch);

// 检查是否有错误发生
if (\curl_errno($ch)) {
// 如果 curl_exec() 返回 false,就表示发生了错误
echo 'cURL 错误: ' . \curl_error($ch);
} else {
// 正常处理响应内容
echo '响应内容: ' . $response;
}

// 检查是否发送了 HTTP/2 请求
if (\curl_getinfo($ch, CURLINFO_HTTP_VERSION) == CURL_HTTP_VERSION_2_0) {
echo "HTTP/2 request sent\n";
} else {
echo "HTTP/2 request not sent\n";
}

// 关闭 cURL 资源,并释放系统资源
\curl_close($ch);

// 输出响应结果
echo $response;
2 changes: 1 addition & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
]);

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81,
LevelSetList::UP_TO_PHP_83,
]);
// register a single rule
// $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
Expand Down

0 comments on commit 74cbae3

Please sign in to comment.