Skip to content

Commit

Permalink
chore(docs): updating byte range requests docs
Browse files Browse the repository at this point in the history
  • Loading branch information
razshare committed Dec 8, 2023
1 parent 7da5aad commit 65be33a
Showing 1 changed file with 20 additions and 98 deletions.
118 changes: 20 additions & 98 deletions docs/7.byte-range-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,130 +18,52 @@ Here's a simple example of how you would use the service:

```php
use CatPaw\Web\Server;
use CatPaw\Web\Attributes\Header;
use CatPaw\Web\Attributes\Param;
use CatPaw\Web\Services\ByteRangeService;
use CatPaw\Web\Interfaces\ByteRangeWriterInterface;
use Amp\Http\Response;
use function CatPaw\duplex;
use Amp\Http\Request;
use Amp\Http\HttpStatus;
use function Amp\File\getSize;
use Amp\File\File;
use function Amp\async;

function main(){
$server = Server::create();
$server->router->get(
path: "/{filename}",
callback: function(
#[Param] string $filename,
#[Header("range")] false|array $range,
ByteRangeService $service,
string $filename,
Request $request,
Response $response,
){
$service->response(
response: $response,
rangeQuery: $range[0]??"",
headers: [
"Content-Type" => "text/html",
"Content-Length" => getSize($filename),
],
interface: new class($filename) implements ByteRangeWriterInterface {
private File $file;

public function __construct(private string $filename) { }

public function start():void {
$this->file = openFile($this->filename, "r");
}

public function data(callable $emit, int $start, int $length):void {
$this->file->seek($start);
$data = $this->file->read($length);
$emit($data);
}

public function end():void {
$this->file->close();
}
}
);
}
);
$srver->start();
}
```

Your endpoint will now serve bye range requests, but it will also throw an exception if the request is not a byte range
request or if the requested range is invalid.<br/>
In those cases you can catch the exception and resume to a normal file buffering.<br/>

```php
use CatPaw\Web\Server;
use CatPaw\Web\Attributes\Header;
use CatPaw\Web\Attributes\Param;
use CatPaw\Web\Services\ByteRangeService;
use CatPaw\Web\Interfaces\ByteRangeWriterInterface;
use Amp\Http\Response;
use function CatPaw\duplex;
use Amp\Http\HttpStatus;
use function Amp\File\getSize;
use Amp\File\File;
use function Amp\async;

function main(){
$server = Server::create();
$server->router->get(
path: "/{fileName}",
callback: function(
#[Param] string $fileName,
#[Header("range")] false|array $range,
ByteRangeService $service,
Response $response,
){
try {
$service->response(
response: $response,
rangeQuery: $range[0]??"",
headers: [
"Content-Type" => "text/html",
"Content-Length" => getSize($filename),
],
interface: new class($filename) implements ByteRangeWriterInterface {
private File $file;

public function __construct(private string $filename) { }

public function start():void {
$this->file = openFile($this->filename, "r");
}

public function data(callable $emit, int $start, int $length):void {
$this->file->seek($start);
$data = $this->file->read($length);
$emit($data);
}

public function end():void {
$this->file->close();
}
}
$byteRangeResponse = $byteRange->file(
range: $request->getHeader("range") ?? '',
fileName: $fileName,
);
$response->setStatus($byteRangeResponse->getStatus());
$response->setHeaders([
...(array)$byteRangeResponse->getHeaders(),
...$attachmentHeaders,
]);
$response->setBody($byteRangeResponse->getBody());
} catch(InvalidByteRangeQueryException) {
$fileSize = getSize($fileName);
$response->setStatus(HttpStatus::OK);
$response->setHeaders([
"Accept-Ranges" => "bytes",
"Content-Type" => Mime::findContentType($fileName),
"Content-Length" => $fileSize,
...$extraHeaders,
...$attachmentHeaders,
]);
$response->setBody(openFile($fileName, 'r'));
}
}
}
);
$server->start();
$srver->start();
}
```

Note that we're still letting the client know that we can serve byte range request by setting the `accpet-ranges: bytes`
header.
Your endpoint will now serve bye range requests.

> [!Note]
> The `ByteRangeService::file` method throws a `InvalidByteRangeQueryException` exception whenever the given range query is not valid.

0 comments on commit 65be33a

Please sign in to comment.