1
0
mirror of https://github.com/danog/MadelineProto.git synced 2025-01-22 13:53:04 +01:00

Fix: request headers support only strings

Fix: correct HttpStatus class
This commit is contained in:
Alexander Pankratov 2023-03-26 16:50:36 +02:00
parent 526acf2fe6
commit 8a2eb8d7a1
3 changed files with 13 additions and 12 deletions

View File

@ -38,6 +38,7 @@
"ext-fileinfo": "*",
"amphp/amp": "^3",
"amphp/http-client": "^5",
"amphp/http": "^2",
"amphp/socket": "^2",
"amphp/dns": "^2",
"amphp/byte-stream": "^2",

View File

@ -14,7 +14,7 @@ use Amp\File\Driver\BlockingFile;
use Amp\File\File;
use Amp\Http\Server\Request as ServerRequest;
use Amp\Http\Server\Response;
use Amp\Http\Status;
use Amp\Http\HttpStatus;
use Amp\Sync\LocalMutex;
use Amp\Sync\Lock;
use danog\MadelineProto\Exception;
@ -94,7 +94,7 @@ trait FilesLogic
}
\http_response_code($result->getCode());
if (!\in_array($result->getCode(), [Status::OK, Status::PARTIAL_CONTENT])) {
if (!\in_array($result->getCode(), [HttpStatus::OK, HttpStatus::PARTIAL_CONTENT])) {
Tools::echo($result->getCodeExplanation());
} elseif ($result->shouldServe()) {
if (!empty($messageMedia['name']) && !empty($messageMedia['ext'])) {
@ -198,13 +198,13 @@ trait FilesLogic
$pipe = new Pipe(1024 * 1024);
EventLoop::queue($this->downloadToStream(...), $messageMedia, $pipe->getSink(), $cb, ...$result->getServeRange());
$body = $pipe->getSource();
} elseif (!\in_array($result->getCode(), [Status::OK, Status::PARTIAL_CONTENT])) {
} elseif (!\in_array($result->getCode(), [HttpStatus::OK, HttpStatus::PARTIAL_CONTENT])) {
$body = $result->getCodeExplanation();
}
$response = new Response($result->getCode(), $result->getHeaders(), $body);
if ($result->shouldServe() && !empty($result->getHeaders()['Content-Length'])) {
$response->setHeader('content-length', $result->getHeaders()['Content-Length']);
$response->setHeader('content-length', (string)$result->getHeaders()['Content-Length']);
if (!empty($messageMedia['name']) && !empty($messageMedia['ext'])) {
$response->setHeader('content-disposition', "inline; filename=\"{$messageMedia['name']}{$messageMedia['ext']}\"");
}

View File

@ -20,7 +20,7 @@ declare(strict_types=1);
namespace danog\MadelineProto\MTProtoTools;
use Amp\Http\Status;
use Amp\Http\HttpStatus;
/**
* Obtain response information for file to server.
@ -46,7 +46,7 @@ final class ResponseInfo
/**
* HTTP response code.
*/
private int $code = Status::OK;
private int $code = HttpStatus::OK;
/**
* Header array.
*/
@ -76,7 +76,7 @@ final class ResponseInfo
[$range, $extra_ranges] = $list;
} else {
$this->serve = false;
$this->code = Status::RANGE_NOT_SATISFIABLE;
$this->code = HttpStatus::RANGE_NOT_SATISFIABLE;
$this->headers = self::NO_CACHE;
return;
}
@ -94,7 +94,7 @@ final class ResponseInfo
if (!empty($seek_start) && $seek_end < \abs(\intval($seek_start))) {
$this->serve = false;
$this->code = Status::RANGE_NOT_SATISFIABLE;
$this->code = HttpStatus::RANGE_NOT_SATISFIABLE;
$this->headers = self::NO_CACHE;
return;
}
@ -102,7 +102,7 @@ final class ResponseInfo
$this->serve = $method !== 'HEAD';
if ($seek_start > 0 || $seek_end < $size - 1) {
$this->code = Status::PARTIAL_CONTENT;
$this->code = HttpStatus::PARTIAL_CONTENT;
$this->headers['Content-Range'] = "bytes $seek_start-$seek_end/$size";
$this->headers['Content-Length'] = $seek_end - $seek_start + 1;
} elseif ($size > 0) {
@ -137,9 +137,9 @@ final class ResponseInfo
*/
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) {
$reason = HttpStatus::getReason($this->code);
$body = "<html lang='en'><body><h1>{$this->code} $reason</h1><br>";
if ($this->code === HttpStatus::RANGE_NOT_SATISFIABLE) {
$body .= '<p>Could not use selected range.</p>';
}
$body .= self::POWERED_BY;