| 27 | } |
| 28 | |
| 29 | function parseMultipart(body: Buffer, boundary: string): Record<string, string> { |
| 30 | const form: Record<string, string> = {}; |
| 31 | const delimiter = Buffer.from(`--${boundary}`); |
| 32 | const separator = Buffer.from('\r\n\r\n'); |
| 33 | |
| 34 | let start = body.indexOf(delimiter); |
| 35 | while (start !== -1) { |
| 36 | start += delimiter.length; |
| 37 | const next = body.indexOf(delimiter, start); |
| 38 | if (next === -1) break; |
| 39 | |
| 40 | const part = body.subarray(start, next); |
| 41 | const headerEnd = part.indexOf(separator); |
| 42 | if (headerEnd !== -1) { |
| 43 | const headerText = part.subarray(0, headerEnd).toString('utf8'); |
| 44 | const nameMatch = headerText.match(/name="([^"]+)"/); |
| 45 | if (nameMatch) { |
| 46 | // Strip the trailing CRLF that precedes the next boundary. |
| 47 | const value = part.subarray(headerEnd + separator.length, part.length - 2).toString('utf8'); |
| 48 | form[nameMatch[1]] = value; |
| 49 | } |
| 50 | } |
| 51 | start = next; |
| 52 | } |
| 53 | |
| 54 | return form; |
| 55 | } |
| 56 | |
| 57 | export async function runServer(port: number): Promise<Server> { |
| 58 | const app = express(); |