-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbootstrap
executable file
·463 lines (410 loc) · 14.3 KB
/
bootstrap
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#!/opt/bin/php -c/opt/php.ini
<?php
/**
* AWS Lambda custom runtime for PHP
*/
/** These environment variables are set by AWS directly */
define('AWS_LAMBDA_RUNTIME_API', getenv('AWS_LAMBDA_RUNTIME_API')); // host for the Lambda runtime API
define('HANDLER', getenv('_HANDLER')); // path to the default PHP script
define('LAMBDA_TASK_ROOT', getenv('LAMBDA_TASK_ROOT')); // path where the function codebase exists
/** These environment variables are specific to this runtime bootstrap */
define('CONFIG_PATH', getenv('CONFIG_PATH') ?: LAMBDA_TASK_ROOT . '/php.ini');
define('EXTENSION_DIR', getenv('EXTENSION_DIR') ?: '/opt/lib/php/8.0/modules');
define('LAMBDA_RUNTIME_API_VERSION', '2018-06-01'); // defines the version of the Lambda runtime API to target
define(
'EVENT_TYPE_METHODS',
[
'/init/error' => 'POST',
'/invocation/next' => 'GET',
'/invocation/response' => 'POST',
'/invocation/error' => 'POST'
]
); // maps API routes to which HTTP method they use
define('MAX_EXECUTION_TIME', intval(getenv('MAX_EXECUTION_TIME') ?: 10)); // how long PHP should be allow to run for
define('ACCESS_LOG', getenv('ACCESS_LOG')); // should access log string be output
define('ACCESS_FORMAT', getenv('ACCESS_FORMAT') ?: '"%m %r%Q%q" %s %f %d'); // format access log string takes
/** Test required environment variables are set */
foreach ([AWS_LAMBDA_RUNTIME_API, HANDLER, LAMBDA_TASK_ROOT] as $value) {
if ($value !== false) {
continue;
}
echo 'Environment variables AWS_LAMBDA_RUNTIME_API, HANDLER and LAMBDA_TASK_ROOT must all be set';
exit(1);
}
/**
* Sends events to the Lambda custom runtime API.
* @return array
*/
function sendRuntimeEvent(string $type, array $body = null, array $invocation = null): array
{
$isValidEventType = array_key_exists($type, EVENT_TYPE_METHODS);
if (!$isValidEventType) {
throw new Exception("Unrecognised runtime event type: ${type}");
}
$method = EVENT_TYPE_METHODS[$type];
if ($method === 'GET' && $body !== null) {
throw new Exception('Cannot set body on a GET event request');
}
$host = AWS_LAMBDA_RUNTIME_API;
$version = LAMBDA_RUNTIME_API_VERSION;
if ($invocation !== null && array_key_exists('id', $invocation)) {
$invocationId = $invocation['id'];
$type = str_replace('/invocation/', "/invocation/${invocationId}/", $type);
}
$url = "http://${host}/${version}/runtime${type}";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
if ($method === 'POST') {
$bodyString = json_encode($body);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyString);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($bodyString),
]);
}
$responseHeaders = [];
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $header) use (&$responseHeaders) {
$isValidHeader = preg_match('/:\s*/', $header);
if (!$isValidHeader) {
return strlen($header);
}
[$rawKey, $value] = preg_split('/:\s*/', $header, 2);
$key = strtolower($rawKey);
if (!array_key_exists($key, $responseHeaders)) {
$responseHeaders[$key] = [];
}
$responseHeaders[$key][] = trim($value);
return strlen($header);
});
$responseBody = '';
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $chunk) use (&$responseBody) {
$responseBody .= $chunk;
return strlen($chunk);
});
curl_exec($ch);
if (curl_error($ch)) {
$error = curl_error($ch);
throw new Exception("Failed to reach Lambda runtime API: ${error}");
}
curl_close($ch);
$response = ['headers' => $responseHeaders, 'body' => $responseBody];
return $response;
}
/**
* If the runtime encounters an error during initialization, it posts an error
* message to the initialization error path.
*/
function sendInitilizationError($message): array
{
$body = ['errorMessage' => $message, 'errorType' => 'InitError'];
$response = sendRuntimeEvent('/init/error', $body);
return $response;
}
/**
* If the function returns an error, the runtime formats the error into a JSON
* document, and posts it to the invocation error path.
*/
function sendInvocationError($invocation, $message): array
{
$body = ['errorMessage' => $message, 'errorType' => 'InvocationError'];
$response = sendRuntimeEvent('/invocation/error', $body, $invocation);
return $response;
}
/**
* Calls the Lambda runtime API to obtain details of the next invocation.
*/
function getNextInvocation(): array
{
try {
$response = sendRuntimeEvent('/invocation/next');
} catch (Exception $error) {
$message = $error->getMessage();
throw new Exception("Failed to fetch next Lambda invocation: ${message}");
}
['headers' => $headers, 'body' => $body] = $response;
if (!array_key_exists('lambda-runtime-aws-request-id', $headers) ||
count($headers['lambda-runtime-aws-request-id']) !== 1) {
throw new Exception('Failed to determine Lambda invocation ID');
}
$id = $headers['lambda-runtime-aws-request-id'][0];
if (empty($body)) {
throw new Exception('Empty Lambda invocation response');
}
$event = (array) json_decode($body, true);
$invocation = ['id' => $id, 'event' => $event];
return $invocation;
}
/**
* Formats an incoming invocation event in to an HTTP request object for passing
* to the CGI process.
*
* @param array $event The invocation's API Gateway or ALB event object.
* @return array HTTP request object.
*/
function createRequest(array $invocation): array
{
['event' => $event] = $invocation;
['httpMethod' => $method, 'path' => $path, 'body' => $body] = $event;
$eventHeader = @$event['multiValueHeaders'] ?: @$event['headers'] ?: [];
$headers = array_map(
function ($value) {
return is_array($value) ? @$value[0] : $value;
},
$eventHeader
);
$eventQueryParameters = @$event['multiValueQueryStringParameters'] ?: @$event['queryStringParameters'] ?: [];
$queryString = implode(
'&',
array_reduce(
array_keys($eventQueryParameters),
function ($carry, $key) use ($eventQueryParameters) {
$values = is_array($eventQueryParameters[$key]) ? $eventQueryParameters[$key] : [$eventQueryParameters[$key]];
foreach ($values as $value) {
$carry[] = sprintf('%s=%s', $key, $value);
}
return $carry;
},
[]
)
);
$isBase64Encoded = array_key_exists('isBase64Encoded', $event) && $event['isBase64Encoded'];
if ($isBase64Encoded) {
$body = base64_decode($body);
}
$request = [
'method' => $method,
'path' => preg_replace('/\/{2,}/', '/', $path), // basic normalisation of the path
'query_string' => $queryString,
'headers' => $headers,
'body' => $body,
];
return $request;
}
/**
* Takes an incoming request object, formats it suitable for the PHP CGI
* process, opens a process and awaits the response.
*/
function performRequest(array $request): array
{
[
'method' => $method,
'path' => $path,
'query_string' => $queryString,
'headers' => $requestHeaders,
'body' => $requestBody,
] = $request;
$taskRoot = LAMBDA_TASK_ROOT;
$configPath = CONFIG_PATH;
$extensionDir = EXTENSION_DIR;
$absolutePath = $taskRoot . '/' . dirname(HANDLER);
$scriptFilename = $absolutePath . $path;
if (!is_file($scriptFilename)) {
$scriptFilename = rtrim($scriptFilename, '/') . '/index.php';
if (!is_file($scriptFilename)) {
$scriptFilename = ltrim(HANDLER, '/');
} else if (substr($path, -1) !== '/') {
return [
'status' => '301 Moved Permanently',
'statusCode' => 301,
'headers' => [
'Location' => [$path . '/'],
],
'body' => '',
];
}
}
$scriptName = str_replace($absolutePath, '', $scriptFilename);
if (file_exists('/opt/bin/php-cgi')) {
$cgiPath = '/opt/bin/';
} else {
$cgiPath = '';
}
$cmd = "${cgiPath}php-cgi -c \"${configPath}\" -d extension_dir=\"${extensionDir}\"";
$descriptorSpec = [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']];
$cwd = LAMBDA_TASK_ROOT;
$env = array_merge(
$_SERVER,
[
'CONTENT_LENGTH' => strlen($requestBody),
'CONTENT_TYPE' => (@$requestHeaders['content-type'] ?: ''),
'QUERY_STRING' => $queryString,
'REDIRECT_STATUS' => 200,
'REQUEST_METHOD' => $method,
'REQUEST_URI' => $path,
'SCRIPT_FILENAME' => $scriptFilename,
'SCRIPT_NAME' => $scriptName,
'SERVER_PROTOCOL' => 'HTTP/1.1',
]
);
unset($env['argv']);
foreach ($requestHeaders as $rawKey => $value) {
$key = 'HTTP_' . str_replace('-', '_', strtoupper($rawKey));
$env[$key] = $value;
}
$process = proc_open($cmd, $descriptorSpec, $pipes, $cwd, $env);
if (!is_resource($process)) {
$exception = new Exception('Failed to launch PHP process');
throw $exception;
}
fwrite($pipes[0], $requestBody);
fclose($pipes[0]);
stream_set_blocking($pipes[1], false);
stream_set_blocking($pipes[2], false);
$responseRaw = '';
$err = '';
$start = microtime(true);
$timeout = false;
while (!feof($pipes[1])) {
$duration = (microtime(true) - $start);
if ($duration > MAX_EXECUTION_TIME) {
$timeout = true;
break;
}
$responseRaw .= fread($pipes[1], 1024);
$err .= fread($pipes[2], 1024);
usleep(100);
}
fclose($pipes[1]);
fclose($pipes[2]);
proc_terminate($process, 2);
if ($err !== '') {
echo $err;
}
if ($timeout) {
$maxExecutionTime = MAX_EXECUTION_TIME;
echo "PHP took longer than $maxExecutionTime seconds to return response";
return [
'status' => '500 Internal Server Error',
'statusCode' => 500,
'headers' => [],
'body' => 'Internal Server Error',
];
}
$status = '200 OK';
[$responseHeadersRaw, $responseBody] = explode("\r\n\r\n", $responseRaw, 2);
$responseHeaders = array_reduce(
explode(PHP_EOL, $responseHeadersRaw),
function ($carry, $line) use (&$status) {
[$key, $value] = array_map('trim', explode(':', $line, 2));
if ($key === 'Status') {
$status = $value;
return $carry;
}
if (!array_key_exists($key, $carry)) {
$carry[$key] = [];
}
$carry[$key][] = $value;
return $carry;
},
[]
);
[$statusCode,] = explode(' ', $status, 2);
if (ACCESS_LOG) {
$patterns = [
'%m',
'%r',
'%Q',
'%q',
'%s',
'%f',
'%d',
];
$replacements = [
$method,
$path,
($queryString ? '?' : ''),
($queryString ?: ''),
$statusCode,
$scriptName,
sprintf('%.4f', $duration),
];
echo preg_replace_callback(
'/%{(.+?)}e/',
function ($matches) use ($env) {
$key = $matches[1];
return @$env[$key] ?: '-';
},
str_replace($patterns, $replacements, ACCESS_FORMAT)
) . PHP_EOL;
}
return [
'status' => $status,
'statusCode' => $statusCode,
'headers' => $responseHeaders,
'body' => $responseBody,
];
}
/**
* Formats an HTTP response in to an AWS (API Gateway Proxy or ALB) response
* object.
*/
function createResponse(array $invocation, array $httpResponse): array
{
['event' => $event] = $invocation;
['status' => $status, 'statusCode' => $statusCode, 'headers' => $headers, 'body' => $body] = $httpResponse;
$response = ['statusCode' => (int) $statusCode, 'body' => $body];
$isApplicationLoadBalancerRequest = array_key_exists('requestContext', $event);
if ($isApplicationLoadBalancerRequest) {
$response['statusDescription'] = $status;
}
$hasMultiValueHeaders = array_key_exists('multiValueHeaders', $event);
if ($hasMultiValueHeaders) {
$response['multiValueHeaders'] = $headers;
} else {
$response['headers'] = array_map(
function ($value) {
return $value[0];
},
$headers
);
}
$hasBase64EncodingProperty = array_key_exists('isBase64Encoded', $event);
if ($hasBase64EncodingProperty) {
$response['isBase64Encoded'] = false;
}
if ($isApplicationLoadBalancerRequest) {
$size = strlen(json_encode($response));
if ($size > 1000000) {
echo "Response size is too large for ALB ($size bytes)" . PHP_EOL;
$errorResponse = [
'status' => '500 Internal Server Error',
'statusCode' => 500,
'headers' => [],
'body' => 'Internal Server Error',
];
return createResponse($invocation, $errorResponse);
}
}
return $response;
}
/**
* Forwards the Lambda-formatted response object to the Lambda runtime API.
*/
function sendLambdaResponse(array $invocation, array $lambdaResponse): array
{
return sendRuntimeEvent('/invocation/response', $lambdaResponse, $invocation);
}
/**
* Performs the tasks required to handle an individual incoming request.
*/
function handleNextRequest(): void
{
try {
$invocation = getNextInvocation();
} catch (Exception $e) {
sendInitilizationError($e->getMessage());
return;
}
try {
$request = createRequest($invocation);
$httpResponse = performRequest($request);
$lambdaResponse = createResponse($invocation, $httpResponse);
sendLambdaResponse($invocation, $lambdaResponse);
} catch (Exception $e) {
sendInvocationError($invocation, $e->getMessage());
}
}
while (isset($loop) ? $loop : true) {
handleNextRequest();
}