Merge #840
This commit is contained in:
parent
d177762371
commit
cd1716645d
@ -197,13 +197,6 @@ class MTProto extends AsyncConstruct implements TLCallback
|
||||
'msg_resend_ans_req',
|
||||
];
|
||||
const DEFAULT_GETUPDATES_PARAMS = ['offset' => 0, 'limit' => null, 'timeout' => 0];
|
||||
|
||||
|
||||
const POWERED_BY = "<p><small>Powered by <a href='https://docs.madelineproto.xyz'>MadelineProto</a></small></p>";
|
||||
const NO_CACHE = [
|
||||
'Cache-Control' => ['no-store, no-cache, must-revalidate, max-age=0', 'post-check=0, pre-check=0'],
|
||||
'Pragma' => 'no-cache'
|
||||
];
|
||||
/**
|
||||
* Instance of wrapper API.
|
||||
*
|
||||
|
@ -38,7 +38,6 @@ use Amp\Promise;
|
||||
use Amp\Success;
|
||||
use danog\MadelineProto\Exception;
|
||||
use danog\MadelineProto\FileCallbackInterface;
|
||||
use danog\MadelineProto\MTProto;
|
||||
use danog\MadelineProto\Stream\Common\BufferedRawStream;
|
||||
use danog\MadelineProto\Stream\Common\SimpleBufferedRawStream;
|
||||
use danog\MadelineProto\Stream\ConnectionContext;
|
||||
@ -927,13 +926,13 @@ trait Files
|
||||
}
|
||||
|
||||
$messageMedia = yield from $this->getDownloadInfo($messageMedia);
|
||||
$result = self::parseHeaders(
|
||||
$result = ResponseInfo::parseHeaders(
|
||||
$_SERVER['REQUEST_METHOD'],
|
||||
$headers,
|
||||
$messageMedia
|
||||
);
|
||||
|
||||
foreach ($result['headers'] as $key => $value) {
|
||||
foreach ($result->getHeaders() as $key => $value) {
|
||||
if (\is_array($value)) {
|
||||
foreach ($value as $subValue) {
|
||||
\header("$key: $subValue", false);
|
||||
@ -942,14 +941,14 @@ trait Files
|
||||
\header("$key: $value");
|
||||
}
|
||||
}
|
||||
\http_response_code($result['code']);
|
||||
\http_response_code($result->getCode());
|
||||
|
||||
if (!\in_array($result['code'], [Status::OK, Status::PARTIAL_CONTENT])) {
|
||||
yield Tools::echo(self::getExplanation($result['code']));
|
||||
} elseif ($result['serve']) {
|
||||
if (!\in_array($result->getCode(), [Status::OK, Status::PARTIAL_CONTENT])) {
|
||||
yield Tools::echo($result->getCodeExplanation());
|
||||
} elseif ($result->shouldServe()) {
|
||||
\ob_end_flush();
|
||||
\ob_implicit_flush();
|
||||
yield from $this->downloadToStream($messageMedia, \fopen('php://output', 'w'), $cb, ...$result['serve']);
|
||||
yield from $this->downloadToStream($messageMedia, \fopen('php://output', 'w'), $cb, ...$result->getServeRange());
|
||||
}
|
||||
}
|
||||
/**
|
||||
@ -972,14 +971,14 @@ trait Files
|
||||
|
||||
$messageMedia = yield from $this->getDownloadInfo($messageMedia);
|
||||
|
||||
$result = self::parseHeaders(
|
||||
$result = ResponseInfo::parseHeaders(
|
||||
$request->getMethod(),
|
||||
\array_map(fn (array $headers) => $headers[0], $request->getHeaders()),
|
||||
$messageMedia
|
||||
);
|
||||
|
||||
$body = null;
|
||||
if ($result['serve']) {
|
||||
if ($result->shouldServe()) {
|
||||
$body = new IteratorStream(
|
||||
new Producer(
|
||||
function (callable $emit) use (&$messageMedia, &$cb, &$result) {
|
||||
@ -987,121 +986,21 @@ trait Files
|
||||
yield $emit($payload);
|
||||
return \strlen($payload);
|
||||
};
|
||||
yield Tools::call($this->downloadToCallable($messageMedia, $emit, $cb, false, ...$result['serve']));
|
||||
yield Tools::call($this->downloadToCallable($messageMedia, $emit, $cb, false, ...$result->getServeRange()));
|
||||
}
|
||||
)
|
||||
);
|
||||
} elseif (!\in_array($result['code'], [Status::OK, Status::PARTIAL_CONTENT])) {
|
||||
$body = self::getExplanation($result['code']);
|
||||
} elseif (!\in_array($result->getCode(), [Status::OK, Status::PARTIAL_CONTENT])) {
|
||||
$body = $result->getCodeExplanation();
|
||||
}
|
||||
|
||||
$response = new Response($result['code'], $result['headers'], $body);
|
||||
if ($result['serve'] && !empty($result['headers']['Content-Length'])) {
|
||||
$response->setHeader('content-length', $result['headers']['Content-Length']);
|
||||
$response = new Response($result->getCode(), $result->getHeaders(), $body);
|
||||
if ($result->shouldServe() && !empty($result->getHeaders()['Content-Length'])) {
|
||||
$response->setHeader('content-length', $result->getHeaders()['Content-Length']);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
/**
|
||||
* Get explanation for HTTP error.
|
||||
*
|
||||
* @param integer $code HTTP error code
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function getExplanation(int $code): string
|
||||
{
|
||||
$reason = Status::getReason($code);
|
||||
$body = "<html><body><h1>$code $reason</h1><br>";
|
||||
if ($code === Status::RANGE_NOT_SATISFIABLE) {
|
||||
$body .= "<p>Could not use selected range.</p>";
|
||||
}
|
||||
$body .= MTProto::POWERED_BY;
|
||||
$body .= "</body></html>";
|
||||
return $body;
|
||||
}
|
||||
/**
|
||||
* Parse headers.
|
||||
*
|
||||
* @param string $method HTTP method
|
||||
* @param array $headers HTTP headers
|
||||
* @param array $messageMedia Media info
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @return array Info about headers
|
||||
*/
|
||||
private static function parseHeaders(string $method, array $headers, array $messageMedia): array
|
||||
{
|
||||
if (isset($headers['range'])) {
|
||||
$range = \explode('=', $headers['range'], 2);
|
||||
if (\count($range) == 1) {
|
||||
$range[1] = '';
|
||||
}
|
||||
[$size_unit, $range_orig] = $range;
|
||||
if ($size_unit == 'bytes') {
|
||||
//multiple ranges could be specified at the same time, but for simplicity only serve the first range
|
||||
//http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt
|
||||
$list = \explode(',', $range_orig, 2);
|
||||
if (\count($list) == 1) {
|
||||
$list[1] = '';
|
||||
}
|
||||
[$range, $extra_ranges] = $list;
|
||||
} else {
|
||||
return [
|
||||
'serve' => false,
|
||||
'code' => Status::RANGE_NOT_SATISFIABLE,
|
||||
'headers' => self::NO_CACHE
|
||||
];
|
||||
}
|
||||
} else {
|
||||
$range = '';
|
||||
}
|
||||
$listseek = \explode('-', $range, 2);
|
||||
if (\count($listseek) == 1) {
|
||||
$listseek[1] = '';
|
||||
}
|
||||
[$seek_start, $seek_end] = $listseek;
|
||||
|
||||
$size = $messageMedia['size'] ?? 0;
|
||||
$seek_end = empty($seek_end) ? ($size - 1) : \min(\abs(\intval($seek_end)), $size - 1);
|
||||
|
||||
if (!empty($seek_start) && $seek_end < \abs(\intval($seek_start))) {
|
||||
return [
|
||||
'serve' => false,
|
||||
'code' => Status::RANGE_NOT_SATISFIABLE,
|
||||
'headers' => self::NO_CACHE
|
||||
];
|
||||
}
|
||||
$seek_start = empty($seek_start) ? 0 : \abs(\intval($seek_start));
|
||||
|
||||
$result = [
|
||||
'serve' => $method !== 'HEAD',
|
||||
'code' => Status::OK,
|
||||
'headers' => []
|
||||
];
|
||||
if ($seek_start > 0 || $seek_end < $size - 1) {
|
||||
$result['code'] = Status::PARTIAL_CONTENT;
|
||||
$result['headers']['Content-Range'] = "bytes ${seek_start}-${seek_end}/${$size}";
|
||||
$result['headers']['Content-Length'] = $seek_end - $seek_start + 1;
|
||||
} elseif ($size > 0) {
|
||||
$result['headers']['Content-Length'] = $size;
|
||||
}
|
||||
$result['headers']['Content-Type'] = $messageMedia['mime'];
|
||||
$result['headers']['Cache-Control'] = 'max-age=31556926';
|
||||
$result['headers']['Content-Transfer-Encoding'] = 'Binary';
|
||||
$result['headers']['Accept-Ranges'] = 'bytes';
|
||||
|
||||
if ($result['serve']) {
|
||||
if ($seek_start === 0 && $seek_end === -1) {
|
||||
$result['serve'] = [0, -1];
|
||||
} else {
|
||||
$result['serve'] = [$seek_start, $seek_end + 1];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
/**
|
||||
* Download file to directory.
|
||||
*
|
||||
|
189
src/danog/MadelineProto/MTProtoTools/ResponseInfo.php
Normal file
189
src/danog/MadelineProto/MTProtoTools/ResponseInfo.php
Normal file
@ -0,0 +1,189 @@
|
||||
<?php
|
||||
/**
|
||||
* Response information module.
|
||||
*
|
||||
* This file is part of MadelineProto.
|
||||
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU Affero General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License along with MadelineProto.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @author Daniil Gentili <daniil@daniil.it>
|
||||
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
|
||||
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
|
||||
*
|
||||
* @link https://docs.madelineproto.xyz MadelineProto documentation
|
||||
*/
|
||||
|
||||
namespace danog\MadelineProto\MTProtoTools;
|
||||
|
||||
use Amp\Http\Status;
|
||||
|
||||
/**
|
||||
* Obtain response information for file to server.
|
||||
*/
|
||||
class ResponseInfo
|
||||
{
|
||||
private const POWERED_BY = "<p><small>Powered by <a href='https://docs.madelineproto.xyz'>MadelineProto</a></small></p>";
|
||||
private const NO_CACHE = [
|
||||
'Cache-Control' => ['no-store, no-cache, must-revalidate, max-age=0', 'post-check=0, pre-check=0'],
|
||||
'Pragma' => 'no-cache'
|
||||
];
|
||||
|
||||
/**
|
||||
* Whether to serve file.
|
||||
*/
|
||||
private bool $serve = false;
|
||||
/**
|
||||
* Serving range.
|
||||
*/
|
||||
private array $serveRange = [];
|
||||
/**
|
||||
* HTTP response code.
|
||||
*/
|
||||
private int $code = Status::OK;
|
||||
/**
|
||||
* Header array.
|
||||
*/
|
||||
private array $headers = [];
|
||||
/**
|
||||
* Parse headers.
|
||||
*
|
||||
* @param string $method HTTP method
|
||||
* @param array $headers HTTP headers
|
||||
* @param array $messageMedia Media info
|
||||
*/
|
||||
private function __construct(string $method, array $headers, array $messageMedia)
|
||||
{
|
||||
if (isset($headers['range'])) {
|
||||
$range = \explode('=', $headers['range'], 2);
|
||||
if (\count($range) == 1) {
|
||||
$range[1] = '';
|
||||
}
|
||||
[$size_unit, $range_orig] = $range;
|
||||
if ($size_unit == 'bytes') {
|
||||
//multiple ranges could be specified at the same time, but for simplicity only serve the first range
|
||||
//http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt
|
||||
$list = \explode(',', $range_orig, 2);
|
||||
if (\count($list) == 1) {
|
||||
$list[1] = '';
|
||||
}
|
||||
[$range, $extra_ranges] = $list;
|
||||
} else {
|
||||
$this->serve = false;
|
||||
$this->code = Status::RANGE_NOT_SATISFIABLE;
|
||||
$this->headers = self::NO_CACHE;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
$range = '';
|
||||
}
|
||||
$listseek = \explode('-', $range, 2);
|
||||
if (\count($listseek) == 1) {
|
||||
$listseek[1] = '';
|
||||
}
|
||||
[$seek_start, $seek_end] = $listseek;
|
||||
|
||||
$size = $messageMedia['size'] ?? 0;
|
||||
$seek_end = empty($seek_end) ? ($size - 1) : \min(\abs(\intval($seek_end)), $size - 1);
|
||||
|
||||
if (!empty($seek_start) && $seek_end < \abs(\intval($seek_start))) {
|
||||
$this->serve = false;
|
||||
$this->code = Status::RANGE_NOT_SATISFIABLE;
|
||||
$this->headers = self::NO_CACHE;
|
||||
return;
|
||||
}
|
||||
$seek_start = empty($seek_start) ? 0 : \abs(\intval($seek_start));
|
||||
|
||||
$this->serve = $method !== 'HEAD';
|
||||
if ($seek_start > 0 || $seek_end < $size - 1) {
|
||||
$this->code = Status::PARTIAL_CONTENT;
|
||||
$this->headers['Content-Range'] = "bytes ${seek_start}-${seek_end}/${size}";
|
||||
$this->headers['Content-Length'] = $seek_end - $seek_start + 1;
|
||||
} elseif ($size > 0) {
|
||||
$this->headers['Content-Length'] = $size;
|
||||
}
|
||||
$this->headers['Content-Type'] = $messageMedia['mime'];
|
||||
$this->headers['Cache-Control'] = 'max-age=31556926';
|
||||
$this->headers['Content-Transfer-Encoding'] = 'Binary';
|
||||
$this->headers['Accept-Ranges'] = 'bytes';
|
||||
|
||||
if ($this->serve) {
|
||||
if ($seek_start === 0 && $seek_end === -1) {
|
||||
$this->serveRange = [0, -1];
|
||||
} else {
|
||||
$this->serveRange = [$seek_start, $seek_end + 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Parse headers.
|
||||
*
|
||||
* @param string $method HTTP method
|
||||
* @param array $headers HTTP headers
|
||||
* @param array $messageMedia Media info
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public static function parseHeaders(string $method, array $headers, array $messageMedia): self
|
||||
{
|
||||
return new self($method, $headers, $messageMedia);
|
||||
}
|
||||
/**
|
||||
* Get explanation for HTTP code.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCodeExplanation(): string
|
||||
{
|
||||
$reason = Status::getReason($this->code);
|
||||
$body = "<html><body><h1>{$this->code} $reason</h1><br>";
|
||||
if ($this->code === Status::RANGE_NOT_SATISFIABLE) {
|
||||
$body .= "<p>Could not use selected range.</p>";
|
||||
}
|
||||
$body .= self::POWERED_BY;
|
||||
$body .= "</body></html>";
|
||||
return $body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to serve file.
|
||||
*
|
||||
* @return bool Whether to serve file
|
||||
*/
|
||||
public function shouldServe(): bool
|
||||
{
|
||||
return $this->serve;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get serving range.
|
||||
*
|
||||
* @return array HTTP serving range
|
||||
*/
|
||||
public function getServeRange(): array
|
||||
{
|
||||
return $this->serveRange;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get HTTP response code.
|
||||
*
|
||||
* @return int HTTP response code
|
||||
*/
|
||||
public function getCode(): int
|
||||
{
|
||||
return $this->code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get header array.
|
||||
*
|
||||
* @return array Header array
|
||||
*/
|
||||
public function getHeaders(): array
|
||||
{
|
||||
return $this->headers;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user