|
| 1 | +#!/opt/bin/php -c/opt/php.ini |
| 2 | +<?php |
| 3 | + |
| 4 | +error_reporting(E_ALL | E_STRICT); |
| 5 | + |
| 6 | +$AWS_LAMBDA_RUNTIME_API = getenv('AWS_LAMBDA_RUNTIME_API'); |
| 7 | + |
| 8 | +/* https://gist.github.com/henriquemoody/6580488 */ |
| 9 | +$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']; |
| 10 | + |
| 11 | +function start_webserver() { |
| 12 | + $pid = pcntl_fork(); |
| 13 | + switch($pid) { |
| 14 | + case -1: |
| 15 | + die('Failed to fork webserver process'); |
| 16 | + |
| 17 | + case 0: |
| 18 | + // exec the command |
| 19 | + $HANDLER = getenv('_HANDLER'); |
| 20 | + chdir('/var/task'); |
| 21 | + exec("PHP_INI_SCAN_DIR=/opt/etc/php-7.2.d/:/var/task/php-7.2.d/ php -S localhost:8000 -c /var/task/php.ini -d extension_dir=/opt/lib/php/modules '$HANDLER'"); |
| 22 | + exit; |
| 23 | + |
| 24 | + // return the child pid to parent |
| 25 | + default: |
| 26 | + // Wait for child server to start |
| 27 | + sleep(1); |
| 28 | + return $pid; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function fail($AWS_LAMBDA_RUNTIME_API, $invocation_id, $message) { |
| 33 | + $ch = curl_init("http://$AWS_LAMBDA_RUNTIME_API/2018-06-01/runtime/invocation/$invocation_id/response"); |
| 34 | + |
| 35 | + $response = array(); |
| 36 | + |
| 37 | + $response['statusCode'] = 500; |
| 38 | + $response['body'] = $message; |
| 39 | + |
| 40 | + $response_json = json_encode($response); |
| 41 | + |
| 42 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); |
| 43 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
| 44 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $response_json); |
| 45 | + curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
| 46 | + 'Content-Type: application/json', |
| 47 | + 'Content-Length: ' . strlen($response_json) |
| 48 | + )); |
| 49 | + |
| 50 | + curl_exec($ch); |
| 51 | + curl_close($ch); |
| 52 | +} |
| 53 | + |
| 54 | +start_webserver(); |
| 55 | + |
| 56 | +while (true) { |
| 57 | + $ch = curl_init("http://$AWS_LAMBDA_RUNTIME_API/2018-06-01/runtime/invocation/next"); |
| 58 | + |
| 59 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); |
| 60 | + curl_setopt($ch, CURLOPT_FAILONERROR, TRUE); |
| 61 | + |
| 62 | + $invocation_id = ''; |
| 63 | + |
| 64 | + curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $header) use (&$invocation_id) { |
| 65 | + if (!preg_match('/:\s*/', $header)) { |
| 66 | + return strlen($header); |
| 67 | + } |
| 68 | + |
| 69 | + [$name, $value] = preg_split('/:\s*/', $header, 2); |
| 70 | + |
| 71 | + if (strtolower($name) == 'lambda-runtime-aws-request-id') { |
| 72 | + $invocation_id = trim($value); |
| 73 | + } |
| 74 | + |
| 75 | + return strlen($header); |
| 76 | + }); |
| 77 | + |
| 78 | + $body = ''; |
| 79 | + |
| 80 | + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $chunk) use (&$body) { |
| 81 | + $body .= $chunk; |
| 82 | + |
| 83 | + return strlen($chunk); |
| 84 | + }); |
| 85 | + |
| 86 | + curl_exec($ch); |
| 87 | + |
| 88 | + if (curl_error($ch)) { |
| 89 | + die('Failed to fetch next Lambda invocation: ' . curl_error($ch) . "\n"); |
| 90 | + } |
| 91 | + |
| 92 | + if ($invocation_id == '') { |
| 93 | + die('Failed to determine Lambda invocation ID'); |
| 94 | + } |
| 95 | + |
| 96 | + curl_close($ch); |
| 97 | + |
| 98 | + if (!$body) { |
| 99 | + die("Empty Lambda invocation response\n"); |
| 100 | + } |
| 101 | + |
| 102 | + $event = json_decode($body, TRUE); |
| 103 | + |
| 104 | + if (!array_key_exists('requestContext', $event)) { |
| 105 | + fail($AWS_LAMBDA_RUNTIME_API, $invocation_id, 'Event is not an API Gateway request'); |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + $uri = $event['path']; |
| 110 | + |
| 111 | + if (array_key_exists('multiValueQueryStringParameters', $event) && $event['multiValueQueryStringParameters']) { |
| 112 | + $first = TRUE; |
| 113 | + foreach ($event['multiValueQueryStringParameters'] as $name => $values) { |
| 114 | + foreach ($values as $value) { |
| 115 | + if ($first) { |
| 116 | + $uri .= "?"; |
| 117 | + $first = FALSE; |
| 118 | + } else { |
| 119 | + $uri .= "&"; |
| 120 | + } |
| 121 | + |
| 122 | + $uri .= $name; |
| 123 | + |
| 124 | + if ($value != '') { |
| 125 | + $uri .= '=' . $value; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + $ch = curl_init("http://localhost:8000$uri"); |
| 132 | + |
| 133 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); |
| 134 | + |
| 135 | + if (array_key_exists('multiValueHeaders', $event)) { |
| 136 | + $headers = array(); |
| 137 | + |
| 138 | + foreach ($event['multiValueHeaders'] as $name => $values) { |
| 139 | + foreach ($values as $value) { |
| 140 | + array_push($headers, "${name}: ${value}"); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 145 | + } |
| 146 | + |
| 147 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $event['httpMethod']); |
| 148 | + |
| 149 | + if (array_key_exists('body', $event)) { |
| 150 | + $body = $event['body']; |
| 151 | + if (array_key_exists('isBase64Encoded', $event) && $event['isBase64Encoded']) { |
| 152 | + $body = base64_decode($body); |
| 153 | + } |
| 154 | + } else { |
| 155 | + $body = ''; |
| 156 | + } |
| 157 | + |
| 158 | + if (strlen($body) > 0) { |
| 159 | + if($event['httpMethod'] === 'POST'){ |
| 160 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
| 161 | + } |
| 162 | + curl_setopt($ch, CURLOPT_INFILESIZE, strlen($body)); |
| 163 | + curl_setopt($ch, CURLOPT_READFUNCTION, function ($ch, $fd, $length) use ($body) { |
| 164 | + return $body; |
| 165 | + }); |
| 166 | + } |
| 167 | + |
| 168 | + $response = array(); |
| 169 | + $response['multiValueHeaders'] = array(); |
| 170 | + $response['body'] = ''; |
| 171 | + |
| 172 | + curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $header) use (&$response) { |
| 173 | + if (preg_match('/HTTP\/1.1 (\d+) .*/', $header, $matches)) { |
| 174 | + $response['statusCode'] = intval($matches[1]); |
| 175 | + return strlen($header); |
| 176 | + } |
| 177 | + |
| 178 | + if (!preg_match('/:\s*/', $header)) { |
| 179 | + return strlen($header); |
| 180 | + } |
| 181 | + |
| 182 | + [$name, $value] = preg_split('/:\s*/', $header, 2); |
| 183 | + |
| 184 | + $name = trim($name); |
| 185 | + $value = trim($value); |
| 186 | + |
| 187 | + if ($name == '') { |
| 188 | + return strlen($header); |
| 189 | + } |
| 190 | + |
| 191 | + if (!array_key_exists($name, $response['multiValueHeaders'])) { |
| 192 | + $response['multiValueHeaders'][$name] = array(); |
| 193 | + } |
| 194 | + |
| 195 | + array_push($response['multiValueHeaders'][$name], $value); |
| 196 | + |
| 197 | + return strlen($header); |
| 198 | + }); |
| 199 | + |
| 200 | + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $chunk) use (&$response) { |
| 201 | + $response['body'] .= $chunk; |
| 202 | + |
| 203 | + return strlen($chunk); |
| 204 | + }); |
| 205 | + |
| 206 | + curl_exec($ch); |
| 207 | + curl_close($ch); |
| 208 | + |
| 209 | + $ch = curl_init("http://$AWS_LAMBDA_RUNTIME_API/2018-06-01/runtime/invocation/$invocation_id/response"); |
| 210 | + |
| 211 | + $isALB = array_key_exists("elb", $event['requestContext']); |
| 212 | + if ($isALB) { // Add Headers For ALB |
| 213 | + $status = $response["statusCode"]; |
| 214 | + if (array_key_exists($status, $http_codes)) { |
| 215 | + $response["statusDescription"] = "$status ". $http_codes[$status]; |
| 216 | + } else { |
| 217 | + $response["statusDescription"] = "$status Unknown"; |
| 218 | + } |
| 219 | + $response["isBase64Encoded"] = false; |
| 220 | + } |
| 221 | + $response_json = json_encode($response); |
| 222 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); |
| 223 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
| 224 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $response_json); |
| 225 | + if (!$isALB){ |
| 226 | + curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
| 227 | + 'Content-Type: application/json', |
| 228 | + 'Content-Length: ' . strlen($response_json) |
| 229 | + )); |
| 230 | + } |
| 231 | + curl_exec($ch); |
| 232 | + curl_close($ch); |
| 233 | +} |
| 234 | + |
| 235 | +?> |
0 commit comments