Files
kiri-core/Server/Constrict/ResponseEmitter.php
T

123 lines
2.9 KiB
PHP
Raw Normal View History

2021-08-10 16:40:01 +08:00
<?php
namespace Server\Constrict;
use Exception;
use HttpServer\Http\Formatter\FileFormatter;
2021-08-11 19:14:26 +08:00
use HttpServer\IInterface\IFormatter;
use Kiri\Exception\NotFindClassException;
2021-08-10 17:49:46 +08:00
use ReflectionException;
2021-08-10 16:40:01 +08:00
use Server\ResponseInterface;
2021-08-11 19:14:26 +08:00
use Swoole\Server;
2021-08-10 16:40:01 +08:00
/**
*
*/
class ResponseEmitter
{
/**
2021-08-11 19:14:26 +08:00
* @param \Swoole\Http\Response|\Swoole\Http2\Response|Server $response
2021-08-10 16:40:01 +08:00
* @param ResponseInterface $emitter
2021-08-10 17:49:46 +08:00
* @throws NotFindClassException
2021-08-11 19:14:26 +08:00
* @throws ReflectionException
2021-08-10 16:40:01 +08:00
* @throws Exception
*/
2021-08-11 19:14:26 +08:00
public function sender(\Swoole\Http\Response|\Swoole\Http2\Response|Server $response, ResponseInterface $emitter)
2021-08-10 16:40:01 +08:00
{
$content = $emitter->configure($response)->getContent();
2021-08-11 19:14:26 +08:00
if ($response instanceof Server) {
$this->sendTcpData($response, $emitter, $content);
return;
}
2021-08-10 17:04:27 +08:00
if ($content instanceof FileFormatter) {
$this->download($content->getData(), $response);
} else {
2021-08-10 17:52:07 +08:00
$response->header('Content-Type', $emitter->getResponseFormat());
2021-08-10 16:40:01 +08:00
$response->end($content->getData());
}
}
2021-08-11 19:14:26 +08:00
/**
* @param Server $response
* @param ResponseInterface $emitter
* @param IFormatter $formatter
*/
private function sendTcpData(Server $response, ResponseInterface $emitter, IFormatter $formatter)
{
if ($formatter instanceof FileFormatter) {
$response->sendfile($emitter->getClientId(), $formatter->getData());
} else {
$response->send($emitter->getClientId(), $formatter->getData());
}
}
2021-08-10 17:35:27 +08:00
const IMAGES = [
'png' => 'image/png',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'svg' => 'image/svg+xml',
];
2021-08-10 16:40:01 +08:00
/**
* @param array $content
* @param \Swoole\Http\Response $response
*/
private function download(array $content, \Swoole\Http\Response $response)
{
2021-08-10 16:47:55 +08:00
$explode = explode('/', $content['path']);
$response->header('Pragma', 'public');
$response->header('Expires', '0');
$response->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
$response->header('Content-Disposition', 'attachment;filename=' . end($explode));
2021-08-10 17:35:27 +08:00
$response->header('Content-Type', $type = get_file_extension($content['path']));
if (!in_array($type, self::IMAGES)) {
$response->header('Content-Transfer-Encoding', 'binary');
} else {
$response->end(file_get_contents($content['path']));
return;
}
2021-08-10 16:40:01 +08:00
if ($content['isChunk'] === false) {
$response->sendfile($content['path']);
2021-08-10 17:08:33 +08:00
} else {
$this->chunk($content, $response);
2021-08-10 16:40:01 +08:00
}
2021-08-10 17:08:33 +08:00
}
2021-08-10 16:40:01 +08:00
2021-08-10 17:08:33 +08:00
/**
* @param $content
* @param $response
*/
private function chunk($content, $response): void
{
2021-08-10 16:40:01 +08:00
$resource = fopen($content['path'], 'r');
$state = fstat($resource);
2021-08-10 17:35:27 +08:00
$offset = $content['offset'];
2021-08-10 16:40:01 +08:00
$response->header('Content-length', $state['size']);
while ($file = fread($resource, $content['limit'])) {
$response->write($file);
fseek($resource, $offset);
if ($offset >= $state['size']) {
break;
}
$offset += $content['limit'];
}
$response->end();
}
}