|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Runtime\Bref; |
| 4 | + |
| 5 | +use Bref\Context\Context; |
| 6 | +use Bref\Event\Http\HttpRequestEvent; |
| 7 | +use Bref\Event\Http\HttpResponse; |
| 8 | +use Riverline\MultiPartParser\StreamedPart; |
| 9 | +use Symfony\Component\HttpFoundation\File\UploadedFile; |
| 10 | +use Symfony\Component\HttpFoundation\Request; |
| 11 | +use Symfony\Component\HttpFoundation\Response; |
| 12 | + |
| 13 | +/** |
| 14 | + * Bridges Symfony requests and responses with API Gateway or ALB event/response |
| 15 | + * formats. |
| 16 | + * |
| 17 | + * @author Tobias Nyholm <[email protected]> |
| 18 | + * |
| 19 | + * @internal |
| 20 | + */ |
| 21 | +class SymfonyRequestBridge |
| 22 | +{ |
| 23 | + public static function convertRequest(HttpRequestEvent $event, Context $context): Request |
| 24 | + { |
| 25 | + Request::setTrustedProxies(['127.0.0.1'], Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO); |
| 26 | + |
| 27 | + // CGI Version 1.1 - Section 4.1 |
| 28 | + $server = array_filter([ |
| 29 | + 'AUTH_TYPE' => $event->getHeaders()['auth-type'] ?? null, // 4.1.1 |
| 30 | + 'CONTENT_LENGTH' => $event->getHeaders()['content-length'][0] ?? null, // 4.1.2 |
| 31 | + 'CONTENT_TYPE' => $event->getContentType(), // 4.1.3 |
| 32 | + 'QUERY_STRING' => $event->getQueryString(), // 4.1.7 |
| 33 | + 'REQUEST_METHOD' => $event->getMethod(), // 4.1.12 |
| 34 | + 'SERVER_PORT' => $event->getServerPort(), // 4.1.16 |
| 35 | + 'SERVER_PROTOCOL' => 'HTTP/'.$event->getProtocolVersion(), // 4.1.16 |
| 36 | + 'DOCUMENT_ROOT' => getcwd(), |
| 37 | + 'REQUEST_TIME' => time(), |
| 38 | + 'REQUEST_TIME_FLOAT' => microtime(true), |
| 39 | + 'REQUEST_URI' => $event->getUri(), |
| 40 | + 'REMOTE_ADDR' => '127.0.0.1', |
| 41 | + ], fn ($value) => null !== $value); |
| 42 | + |
| 43 | + foreach ($event->getHeaders() as $name => $values) { |
| 44 | + $server['HTTP_'.strtoupper($name)] = $values[0]; |
| 45 | + } |
| 46 | + |
| 47 | + [$files, $parsedBody, $bodyString] = self::parseBodyAndUploadedFiles($event); |
| 48 | + |
| 49 | + return new Request( |
| 50 | + $event->getQueryParameters(), |
| 51 | + $parsedBody ?? [], |
| 52 | + [], // Attributes |
| 53 | + $event->getCookies(), |
| 54 | + $files, |
| 55 | + $server, |
| 56 | + $bodyString |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + public static function convertResponse(Response $response): HttpResponse |
| 61 | + { |
| 62 | + return new HttpResponse($response->getContent(), $response->headers->all(), $response->getStatusCode()); |
| 63 | + } |
| 64 | + |
| 65 | + private static function parseBodyAndUploadedFiles(HttpRequestEvent $event): array |
| 66 | + { |
| 67 | + $bodyString = $event->getBody(); |
| 68 | + $files = []; |
| 69 | + $parsedBody = null; |
| 70 | + $contentType = $event->getContentType(); |
| 71 | + if (null !== $contentType && 'POST' === $event->getMethod()) { |
| 72 | + if ('application/x-www-form-urlencoded' === $contentType) { |
| 73 | + parse_str($bodyString, $parsedBody); |
| 74 | + } else { |
| 75 | + $stream = fopen('php://temp', 'rw'); |
| 76 | + fwrite($stream, "Content-type: $contentType\r\n\r\n".$bodyString); |
| 77 | + rewind($stream); |
| 78 | + $document = new StreamedPart($stream); |
| 79 | + if ($document->isMultiPart()) { |
| 80 | + $bodyString = ''; |
| 81 | + $parsedBody = []; |
| 82 | + foreach ($document->getParts() as $part) { |
| 83 | + if ($part->isFile()) { |
| 84 | + $tmpPath = tempnam(sys_get_temp_dir(), 'bref_upload_'); |
| 85 | + if (false === $tmpPath) { |
| 86 | + throw new \RuntimeException('Unable to create a temporary directory'); |
| 87 | + } |
| 88 | + file_put_contents($tmpPath, $part->getBody()); |
| 89 | + if (0 !== filesize($tmpPath) && '' !== $part->getFileName()) { |
| 90 | + $file = new UploadedFile($tmpPath, $part->getFileName(), $part->getMimeType(), UPLOAD_ERR_OK, true); |
| 91 | + } else { |
| 92 | + $file = null; |
| 93 | + } |
| 94 | + |
| 95 | + self::parseKeyAndInsertValueInArray($files, $part->getName(), $file); |
| 96 | + } else { |
| 97 | + self::parseKeyAndInsertValueInArray($parsedBody, $part->getName(), $part->getBody()); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return [$files, $parsedBody, $bodyString]; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Parse a string key like "files[id_cards][jpg][]" and do $array['files']['id_cards']['jpg'][] = $value. |
| 109 | + * |
| 110 | + * @param mixed $value |
| 111 | + */ |
| 112 | + private static function parseKeyAndInsertValueInArray(array &$array, string $key, $value): void |
| 113 | + { |
| 114 | + if (false === strpos($key, '[')) { |
| 115 | + $array[$key] = $value; |
| 116 | + |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + $parts = explode('[', $key); // files[id_cards][jpg][] => [ 'files', 'id_cards]', 'jpg]', ']' ] |
| 121 | + $pointer = &$array; |
| 122 | + |
| 123 | + foreach ($parts as $k => $part) { |
| 124 | + if (0 === $k) { |
| 125 | + $pointer = &$pointer[$part]; |
| 126 | + |
| 127 | + continue; |
| 128 | + } |
| 129 | + |
| 130 | + // Skip two special cases: |
| 131 | + // [[ in the key produces empty string |
| 132 | + // [test : starts with [ but does not end with ] |
| 133 | + if ('' === $part || ']' !== substr($part, -1)) { |
| 134 | + // Malformed key, we use it "as is" |
| 135 | + $array[$key] = $value; |
| 136 | + |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + $part = substr($part, 0, -1); // The last char is a ] => remove it to have the real key |
| 141 | + |
| 142 | + if ('' === $part) { // [] case |
| 143 | + $pointer = &$pointer[]; |
| 144 | + } else { |
| 145 | + $pointer = &$pointer[$part]; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + $pointer = $value; |
| 150 | + } |
| 151 | +} |
0 commit comments