-
-
Notifications
You must be signed in to change notification settings - Fork 377
Expand file tree
/
Copy pathPsr7Bridge.php
More file actions
156 lines (137 loc) · 5.58 KB
/
Copy pathPsr7Bridge.php
File metadata and controls
156 lines (137 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php declare(strict_types=1);
namespace Bref\Event\Http;
use Bref\Context\Context;
use Bref\Support\MultipartArray;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Stream;
use Nyholm\Psr7\UploadedFile;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Riverline\MultiPartParser\Part;
use RuntimeException;
use function str_starts_with;
/**
* Bridges PSR-7 requests and responses with API Gateway or ALB event/response formats.
*/
final class Psr7Bridge
{
private const UPLOADED_FILES_PREFIX = 'bref_upload_';
/**
* Create a PSR-7 server request from an AWS Lambda HTTP event.
*/
public static function convertRequest(HttpRequestEvent $event, Context $context): ServerRequestInterface
{
$headers = $event->getHeaders();
[$files, $parsedBody] = self::parseBodyAndUploadedFiles($event);
[$user, $password] = $event->getBasicAuthCredentials();
$server = array_filter([
'CONTENT_LENGTH' => $headers['content-length'][0] ?? null,
'CONTENT_TYPE' => $event->getContentType(),
'DOCUMENT_ROOT' => getcwd(),
'QUERY_STRING' => $event->getQueryString(),
'REQUEST_METHOD' => $event->getMethod(),
'SERVER_NAME' => $event->getServerName(),
'SERVER_PORT' => $event->getServerPort(),
'SERVER_PROTOCOL' => $event->getProtocol(),
'PATH_INFO' => $event->getPath(),
'HTTP_HOST' => $headers['host'] ?? null,
'REMOTE_ADDR' => $event->getSourceIp(),
'REMOTE_PORT' => $event->getRemotePort(),
'REQUEST_TIME' => time(),
'REQUEST_TIME_FLOAT' => microtime(true),
'REQUEST_URI' => $event->getUri(),
'PHP_AUTH_USER' => $user,
'PHP_AUTH_PW' => $password,
]);
foreach ($headers as $name => $values) {
$server['HTTP_' . strtoupper(str_replace('-', '_', (string) $name))] = $values[0];
}
/**
* Nyholm/psr7 does not rewind body streams, we do it manually
* so that users can fetch the content of the body directly.
*/
$bodyStream = Stream::create($event->getBody());
$bodyStream->rewind();
$request = new ServerRequest(
$event->getMethod(),
$event->getUri(),
$event->getHeaders(),
$bodyStream,
$event->getProtocolVersion(),
$server
);
foreach ($event->getPathParameters() as $key => $value) {
$request = $request->withAttribute($key, $value);
}
return $request->withUploadedFiles($files)
->withCookieParams($event->getCookies())
->withQueryParams($event->getQueryParameters())
->withParsedBody($parsedBody)
->withAttribute('lambda-event', $event)
->withAttribute('lambda-context', $context);
}
/**
* Create a ALB/API Gateway response from a PSR-7 response.
*/
public static function convertResponse(ResponseInterface $response): HttpResponse
{
$response->getBody()->rewind();
$body = $response->getBody()->getContents();
return new HttpResponse($body, $response->getHeaders(), $response->getStatusCode());
}
/**
* @return array{0: array<string, UploadedFile>, 1: array<string, mixed>|null}
*/
private static function parseBodyAndUploadedFiles(HttpRequestEvent $event): array
{
$contentType = $event->getContentType();
if ($contentType === null || $event->getMethod() !== 'POST') {
return [[], null];
}
if (str_starts_with($contentType, 'application/x-www-form-urlencoded')) {
$parsedBody = [];
parse_str($event->getBody(), $parsedBody);
return [[], $parsedBody];
}
// Parse the body as multipart/form-data
$document = new Part("Content-type: $contentType\r\n\r\n" . $event->getBody());
if (! $document->isMultiPart()) {
return [[], null];
}
$parsedBody = null;
$files = [];
foreach ($document->getParts() as $part) {
if ($part->isFile()) {
$tmpPath = tempnam(sys_get_temp_dir(), self::UPLOADED_FILES_PREFIX);
if ($tmpPath === false) {
throw new RuntimeException('Unable to create a temporary directory');
}
file_put_contents($tmpPath, $part->getBody());
$file = new UploadedFile($tmpPath, filesize($tmpPath), UPLOAD_ERR_OK, $part->getFileName(), $part->getMimeType());
$files = MultipartArray::setValue($files, $part->getName(), $file);
} else {
if ($parsedBody === null) {
$parsedBody = [];
}
$parsedBody = MultipartArray::setValue($parsedBody, $part->getName(), $part->getBody());
}
}
return [$files, $parsedBody];
}
/**
* Cleanup previously uploaded files.
*/
public static function cleanupUploadedFiles(): void
{
// See https://github.com/brefphp/bref/commit/c77d9f5abf021f29fa96b5720b7b84adbd199092#r137983026
$tmpFiles = glob(sys_get_temp_dir() . '/' . self::UPLOADED_FILES_PREFIX . '[A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9]');
if ($tmpFiles !== false) {
foreach ($tmpFiles as $file) {
if (is_file($file)) {
// Silence warnings, we don't want to crash the whole runtime
@unlink($file);
}
}
}
}
}