Skip to content

Commit d6a8e48

Browse files
sy-recordslimingxinleoPandaLIU-1111
authored
Added HTTP chunk support for hyperf/http-message (#4290)
Co-authored-by: 李铭昕 <[email protected]> Co-authored-by: pandaLIU <[email protected]>
1 parent d2771b8 commit d6a8e48

File tree

7 files changed

+132
-1
lines changed

7 files changed

+132
-1
lines changed

src/Server/Chunk/Chunkable.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\HttpMessage\Server\Chunk;
13+
14+
interface Chunkable
15+
{
16+
public function write(string $data): bool;
17+
}

src/Server/Chunk/HasChunk.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\HttpMessage\Server\Chunk;
13+
14+
trait HasChunk
15+
{
16+
public function write(string $content): bool
17+
{
18+
if (isset($this->connection) && $this->connection instanceof Chunkable) {
19+
return $this->connection->write($content);
20+
}
21+
22+
return false;
23+
}
24+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\HttpMessage\Server\Connection;
13+
14+
use Hyperf\HttpMessage\Server\Chunk\Chunkable;
15+
use Hyperf\HttpMessage\Server\ConnectionInterface;
16+
use Swoole\Http\Response;
17+
18+
class SwooleConnection implements ConnectionInterface, Chunkable
19+
{
20+
/**
21+
* @var Response
22+
*/
23+
protected $response;
24+
25+
public function __construct(Response $response)
26+
{
27+
$this->response = $response;
28+
}
29+
30+
public function write(string $data): bool
31+
{
32+
return $this->response->write($data);
33+
}
34+
}

src/Server/ConnectionInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\HttpMessage\Server;
13+
14+
interface ConnectionInterface
15+
{
16+
}

src/Server/Response.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
namespace Hyperf\HttpMessage\Server;
1313

1414
use Hyperf\HttpMessage\Cookie\Cookie;
15+
use Hyperf\HttpMessage\Server\Chunk\Chunkable;
16+
use Hyperf\HttpMessage\Server\Chunk\HasChunk;
1517
use Hyperf\HttpMessage\Stream\SwooleStream;
1618

17-
class Response extends \Hyperf\HttpMessage\Base\Response
19+
class Response extends \Hyperf\HttpMessage\Base\Response implements Chunkable
1820
{
21+
use HasChunk;
22+
1923
/**
2024
* @var array
2125
*/
@@ -26,6 +30,11 @@ class Response extends \Hyperf\HttpMessage\Base\Response
2630
*/
2731
protected $trailers = [];
2832

33+
/**
34+
* @var null|ConnectionInterface
35+
*/
36+
protected $connection;
37+
2938
/**
3039
* Returns an instance with body content.
3140
*/
@@ -80,4 +89,15 @@ public function getTrailers(): array
8089
{
8190
return $this->trailers;
8291
}
92+
93+
public function setConnection(ConnectionInterface $connection)
94+
{
95+
$this->connection = $connection;
96+
return $this;
97+
}
98+
99+
public function getConnection(): ?ConnectionInterface
100+
{
101+
return $this->connection;
102+
}
83103
}

tests/ResponseProxyTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public function testCookies()
4040
parent::testCookies();
4141
}
4242

43+
public function testWrite()
44+
{
45+
$this->markTestSkipped('Response proxy does not support chunk.');
46+
}
47+
4348
protected function newResponse()
4449
{
4550
$response = new ResponseStub();

tests/ResponseTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
namespace HyperfTest\HttpMessage;
1313

1414
use Hyperf\HttpMessage\Cookie\Cookie;
15+
use Hyperf\HttpMessage\Server\Connection\SwooleConnection;
1516
use Hyperf\HttpMessage\Server\Response;
17+
use Mockery;
1618
use PHPUnit\Framework\TestCase;
19+
use Swoole\Http\Response as SwooleResponse;
1720

1821
/**
1922
* @internal
@@ -52,6 +55,18 @@ public function testCookies()
5255
$this->assertSame(['hyperf.io' => ['/' => ['test' => $cookie]]], $response->getCookies());
5356
}
5457

58+
public function testWrite()
59+
{
60+
$content = 'hello';
61+
$swooleResponse = Mockery::mock(SwooleResponse::class);
62+
$swooleResponse->shouldReceive('write')->with($content)->once()->andReturn(true);
63+
64+
$response = $this->newResponse();
65+
$response->setConnection(new SwooleConnection($swooleResponse));
66+
$status = $response->write($content);
67+
$this->assertTrue($status);
68+
}
69+
5570
protected function newResponse()
5671
{
5772
return new Response();

0 commit comments

Comments
 (0)