forked from stackery/php-lambda-layer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap
executable file
·232 lines (175 loc) · 7.66 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
#!/opt/bin/php -c/opt/php.ini
<?php
error_reporting(E_ALL | E_STRICT);
$AWS_LAMBDA_RUNTIME_API = getenv('AWS_LAMBDA_RUNTIME_API');
/* https://gist.github.com/henriquemoody/6580488 */
$http_codes = [100=>'Continue',101=>'Switching Protocols',102=>'Processing',200=>'OK',201=>'Created',202=>'Accepted',203=>'Non-Authoritative Information',204=>'No Content',205=>'Reset Content',206=>'Partial Content',207=>'Multi-Status',208=>'Already Reported',226=>'IM Used',300=>'Multiple Choices',301=>'Moved Permanently',302=>'Found',303=>'See Other',304=>'Not Modified',305=>'Use Proxy',306=>'Switch Proxy',307=>'Temporary Redirect',308=>'Permanent Redirect',400=>'Bad Request',401=>'Unauthorized',402=>'Payment Required',403=>'Forbidden',404=>'Not Found',405=>'Method Not Allowed',406=>'Not Acceptable',407=>'Proxy Authentication Required',408=>'Request Timeout',409=>'Conflict',410=>'Gone',411=>'Length Required',412=>'Precondition Failed',413=>'Request Entity Too Large',414=>'Request-URI Too Long',415=>'Unsupported Media Type',416=>'Requested Range Not Satisfiable',417=>'Expectation Failed',418=>'I\'m a teapot',419=>'Authentication Timeout',420=>'Enhance Your Calm',420=>'Method Failure',422=>'Unprocessable Entity',423=>'Locked',424=>'Failed Dependency',424=>'Method Failure',425=>'Unordered Collection',426=>'Upgrade Required',428=>'Precondition Required',429=>'Too Many Requests',431=>'Request Header Fields Too Large',444=>'No Response',449=>'Retry With',450=>'Blocked by Windows Parental Controls',451=>'Redirect',451=>'Unavailable For Legal Reasons',494=>'Request Header Too Large',495=>'Cert Error',496=>'No Cert',497=>'HTTP to HTTPS',499=>'Client Closed Request',500=>'Internal Server Error',501=>'Not Implemented',502=>'Bad Gateway',503=>'Service Unavailable',504=>'Gateway Timeout',505=>'HTTP Version Not Supported',506=>'Variant Also Negotiates',507=>'Insufficient Storage',508=>'Loop Detected',509=>'Bandwidth Limit Exceeded',510=>'Not Extended',511=>'Network Authentication Required',598=>'Network read timeout error',599=>'Network connect timeout error'];
function start_webserver() {
$pid = pcntl_fork();
switch($pid) {
case -1:
die('Failed to fork webserver process');
case 0:
// exec the command
$HANDLER = getenv('_HANDLER');
chdir('/var/task');
exec("PHP_INI_SCAN_DIR=/opt/etc/php-7.1.d/:/var/task/php-7.1.d/ php -S localhost:8000 -c /var/task/php.ini -d extension_dir=/opt/lib/php/7.1/modules '$HANDLER'");
exit;
// return the child pid to parent
default:
// Wait for child server to start
sleep(1);
return $pid;
}
}
function fail($AWS_LAMBDA_RUNTIME_API, $invocation_id, $message) {
$ch = curl_init("http://$AWS_LAMBDA_RUNTIME_API/2018-06-01/runtime/invocation/$invocation_id/response");
$response = array();
$response['statusCode'] = 500;
$response['body'] = $message;
$response_json = json_encode($response);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $response_json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($response_json)
));
curl_exec($ch);
curl_close($ch);
}
start_webserver();
while (true) {
$ch = curl_init("http://$AWS_LAMBDA_RUNTIME_API/2018-06-01/runtime/invocation/next");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
$invocation_id = '';
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $header) use (&$invocation_id) {
if (!preg_match('/:\s*/', $header)) {
return strlen($header);
}
[$name, $value] = preg_split('/:\s*/', $header, 2);
if (strtolower($name) == 'lambda-runtime-aws-request-id') {
$invocation_id = trim($value);
}
return strlen($header);
});
$body = '';
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $chunk) use (&$body) {
$body .= $chunk;
return strlen($chunk);
});
curl_exec($ch);
if (curl_error($ch)) {
die('Failed to fetch next Lambda invocation: ' . curl_error($ch) . "\n");
}
if ($invocation_id == '') {
die('Failed to determine Lambda invocation ID');
}
curl_close($ch);
if (!$body) {
die("Empty Lambda invocation response\n");
}
$event = json_decode($body, TRUE);
if (!array_key_exists('requestContext', $event)) {
fail($AWS_LAMBDA_RUNTIME_API, $invocation_id, 'Event is not an API Gateway request');
continue;
}
$uri = $event['path'];
if (array_key_exists('multiValueQueryStringParameters', $event) && $event['multiValueQueryStringParameters']) {
$first = TRUE;
foreach ($event['multiValueQueryStringParameters'] as $name => $values) {
foreach ($values as $value) {
if ($first) {
$uri .= "?";
$first = FALSE;
} else {
$uri .= "&";
}
$uri .= $name;
if ($value != '') {
$uri .= '=' . $value;
}
}
}
}
$ch = curl_init("http://localhost:8000$uri");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
if (array_key_exists('multiValueHeaders', $event)) {
$headers = array();
foreach ($event['multiValueHeaders'] as $name => $values) {
foreach ($values as $value) {
array_push($headers, "${name}: ${value}");
}
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $event['httpMethod']);
if (array_key_exists('body', $event)) {
$body = $event['body'];
if (array_key_exists('isBase64Encoded', $event) && $event['isBase64Encoded']) {
$body = base64_decode($body);
}
} else {
$body = '';
}
if (strlen($body) > 0) {
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($body));
curl_setopt($ch, CURLOPT_READFUNCTION, function ($ch, $fd, $length) use ($body) {
return $body;
});
}
$response = array();
$response['multiValueHeaders'] = array();
$response['body'] = '';
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $header) use (&$response) {
if (preg_match('/HTTP\/1.1 (\d+) .*/', $header, $matches)) {
$response['statusCode'] = intval($matches[1]);
return strlen($header);
}
if (!preg_match('/:\s*/', $header)) {
return strlen($header);
}
[$name, $value] = preg_split('/:\s*/', $header, 2);
$name = trim($name);
$value = trim($value);
if ($name == '') {
return strlen($header);
}
if (!array_key_exists($name, $response['multiValueHeaders'])) {
$response['multiValueHeaders'][$name] = array();
}
array_push($response['multiValueHeaders'][$name], $value);
return strlen($header);
});
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $chunk) use (&$response) {
$response['body'] .= $chunk;
return strlen($chunk);
});
curl_exec($ch);
curl_close($ch);
$ch = curl_init("http://$AWS_LAMBDA_RUNTIME_API/2018-06-01/runtime/invocation/$invocation_id/response");
$isALB = array_key_exists("elb", $event['requestContext']);
if ($isALB) { // Add Headers For ALB
$status = $response["statusCode"];
if (array_key_exists($status, $http_codes)) {
$response["statusDescription"] = "$status ". $http_codes[$status];
} else {
$response["statusDescription"] = "$status Unknown";
}
$response["isBase64Encoded"] = false;
}
$response_json = json_encode($response);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $response_json);
if (!$isALB){
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($response_json)
));
}
curl_exec($ch);
curl_close($ch);
}
?>