-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
219 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Sender\Frontend\Event; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
abstract class Asset | ||
{ | ||
public function __construct( | ||
public readonly string $uuid, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Sender\Frontend\Event; | ||
|
||
use Psr\Http\Message\UploadedFileInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class AttachedFile extends Asset | ||
{ | ||
/** | ||
* @param non-empty-string $id | ||
*/ | ||
public function __construct( | ||
string $id, | ||
public readonly UploadedFileInterface $file, | ||
) { | ||
parent::__construct($id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Sender\Frontend\Http; | ||
|
||
use Buggregator\Trap\Handler\Http\Middleware; | ||
use Buggregator\Trap\Logger; | ||
use Buggregator\Trap\Sender\Frontend\Event\AttachedFile; | ||
use Buggregator\Trap\Sender\Frontend\EventsStorage; | ||
use Nyholm\Psr7\Response; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
/** | ||
* @internal | ||
* @psalm-internal Buggregator\Trap | ||
*/ | ||
final class EventAssets implements Middleware | ||
{ | ||
public function __construct( | ||
private readonly Logger $logger, | ||
private readonly EventsStorage $eventsStorage, | ||
) { | ||
} | ||
|
||
public function handle(ServerRequestInterface $request, callable $next): ResponseInterface | ||
{ | ||
$path = $request->getUri()->getPath(); | ||
if (\preg_match('#^/api/smtp/([^/]++)/attachment/([^/]++)#', $path, $matches) !== 1) { | ||
return $next($request); | ||
} | ||
|
||
|
||
// Find event | ||
$event = $this->eventsStorage->get($matches[1]); | ||
if ($event === null) { | ||
$this->logger->debug('Get attachment %s for event %s. Event not found.', $matches[2], $matches[1]); | ||
return new Response(404); | ||
} | ||
|
||
// Find attachment | ||
$attachment = $event->assets[$matches[2]] ?? null; | ||
|
||
if (!$attachment instanceof AttachedFile) { | ||
$this->logger->debug('Get attachment %s for event %s. Attached file not found.', $matches[2], $matches[1]); | ||
return new Response(404); | ||
} | ||
|
||
return new Response( | ||
200, | ||
[ | ||
'Content-Type' => $attachment->file->getClientMediaType(), | ||
'Content-Disposition' => \sprintf( | ||
"attachment; filename=\"%s\"", | ||
\rawurlencode($attachment->file->getClientFilename()), | ||
), | ||
'Content-Length' => (string)$attachment->file->getSize(), | ||
'Cache-Control' => 'no-cache', | ||
], | ||
$attachment->file->getStream(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Sender\Frontend\Mapper; | ||
|
||
use ArrayObject; | ||
use Buggregator\Trap\Proto\Frame\Http as HttpFrame; | ||
use Buggregator\Trap\Sender\Frontend\Event; | ||
use Buggregator\Trap\Support\Uuid; | ||
use Psr\Http\Message\UploadedFileInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class HttpRequest | ||
{ | ||
public function map(HttpFrame $frame): Event | ||
{ | ||
$request = $frame->request; | ||
|
||
$uri = \ltrim($request->getUri()->getPath(), '/'); | ||
$assets = new ArrayObject(); | ||
|
||
return new Event( | ||
uuid: $uuid = Uuid::uuid4(), | ||
type: 'http-dump', | ||
payload: [ | ||
'received_at' => $frame->time->format('Y-m-d H:i:s'), | ||
'host' => $request->getHeaderLine('Host'), | ||
'request' => [ | ||
'method' => $request->getMethod(), | ||
'uri' => $uri, | ||
'headers' => $request->getHeaders(), | ||
'body' => (string)$request->getBody(), | ||
'query' => $request->getQueryParams(), | ||
'post' => $request->getParsedBody() ?? [], | ||
'cookies' => $request->getCookieParams(), | ||
'files' => \array_map( | ||
static function (UploadedFileInterface $attachment) use ($assets, $uuid): array { | ||
$asset = new Event\AttachedFile( | ||
id: Uuid::uuid4(), | ||
file: $attachment, | ||
); | ||
$uri = $uuid . '/' . $asset->uuid; | ||
$assets->offsetSet($asset->uuid, $asset); | ||
|
||
return [ | ||
'id' => $asset->uuid, | ||
'name' => $attachment->getClientFilename(), | ||
'uri' => $uri, | ||
'size' => $attachment->getSize(), | ||
'mime' => $attachment->getClientMediaType(), | ||
]; | ||
}, | ||
\iterator_to_array($frame->iterateUploadedFiles(), false), | ||
), | ||
], | ||
], | ||
timestamp: (float)$frame->time->format('U.u'), | ||
assets: $assets, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.