|
| 1 | +import { bytes } from "./std.ts"; |
| 2 | +import { FormDataBody, FormFile } from "./types.ts"; |
| 3 | + |
| 4 | +const encoder = new TextEncoder(); |
| 5 | +const decoder = new TextDecoder(); |
| 6 | + |
| 7 | +const encode = { |
| 8 | + contentType: encoder.encode("Content-Type"), |
| 9 | + filename: encoder.encode("filename"), |
| 10 | + name: encoder.encode("name"), |
| 11 | + dashdash: encoder.encode("--"), |
| 12 | + boundaryEqual: encoder.encode("boundary="), |
| 13 | + returnNewline2: encoder.encode("\r\n\r\n"), |
| 14 | + carriageReturn: encoder.encode("\r"), |
| 15 | +}; |
| 16 | + |
| 17 | +export async function multiParser( |
| 18 | + body: Deno.Reader, |
| 19 | + contentType: string |
| 20 | +): Promise<FormDataBody> { |
| 21 | + let buf = await Deno.readAll(body); |
| 22 | + let boundaryByte = getBoundary(contentType); |
| 23 | + |
| 24 | + if (!boundaryByte) { |
| 25 | + throw new Error("No boundary data information"); |
| 26 | + } |
| 27 | + |
| 28 | + // Generate an array of Uint8Array |
| 29 | + const pieces = getFieldPieces(buf, boundaryByte!); |
| 30 | + |
| 31 | + // Set all the pieces into one single object |
| 32 | + const form = getForm(pieces); |
| 33 | + |
| 34 | + return form; |
| 35 | +} |
| 36 | + |
| 37 | +function createFormData(): FormDataBody { |
| 38 | + return { |
| 39 | + fields: {}, |
| 40 | + files: [], |
| 41 | + getFile(key: string) { |
| 42 | + return this.files.find((i) => i.name === key); |
| 43 | + }, |
| 44 | + get(key: string) { |
| 45 | + return this.fields[key]; |
| 46 | + }, |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +function getForm(pieces: Uint8Array[]) { |
| 51 | + let form: FormDataBody = createFormData(); |
| 52 | + // let form: Form = { fields: {}, files: {} }; |
| 53 | + |
| 54 | + for (let piece of pieces) { |
| 55 | + const { headerByte, contentByte } = splitPiece(piece); |
| 56 | + const headers = getHeaders(headerByte); |
| 57 | + |
| 58 | + // it's a string field |
| 59 | + if (typeof headers === "string") { |
| 60 | + // empty content, discard it |
| 61 | + if (contentByte.byteLength === 1 && contentByte[0] === 13) { |
| 62 | + continue; |
| 63 | + } else { |
| 64 | + // headers = "field1" |
| 65 | + form.fields[headers] = decoder.decode(contentByte); |
| 66 | + } |
| 67 | + } // it's a file field |
| 68 | + else { |
| 69 | + let file: FormFile = { |
| 70 | + name: headers.name, |
| 71 | + filename: headers.filename, |
| 72 | + contentType: headers.contentType, |
| 73 | + size: contentByte.byteLength, |
| 74 | + content: contentByte, |
| 75 | + }; |
| 76 | + |
| 77 | + form.files.push(file); |
| 78 | + } |
| 79 | + } |
| 80 | + return form; |
| 81 | +} |
| 82 | + |
| 83 | +function getHeaders(headerByte: Uint8Array) { |
| 84 | + let contentTypeIndex = bytes.findIndex(headerByte, encode.contentType); |
| 85 | + |
| 86 | + // no contentType, it may be a string field, return name only |
| 87 | + if (contentTypeIndex < 0) { |
| 88 | + return getNameOnly(headerByte); |
| 89 | + } // file field, return with name, filename and contentType |
| 90 | + else { |
| 91 | + return getHeaderNContentType(headerByte, contentTypeIndex); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +function getHeaderNContentType( |
| 96 | + headerByte: Uint8Array, |
| 97 | + contentTypeIndex: number, |
| 98 | +) { |
| 99 | + let headers: Record<string, string> = {}; |
| 100 | + |
| 101 | + let contentDispositionByte = headerByte.slice(0, contentTypeIndex - 2); |
| 102 | + headers = getHeaderOnly(contentDispositionByte); |
| 103 | + |
| 104 | + // jump over <Content-Type: > |
| 105 | + let contentTypeByte = headerByte.slice( |
| 106 | + contentTypeIndex + encode.contentType.byteLength + 2, |
| 107 | + ); |
| 108 | + |
| 109 | + headers.contentType = decoder.decode(contentTypeByte); |
| 110 | + return headers; |
| 111 | +} |
| 112 | + |
| 113 | +function getHeaderOnly(headerLineByte: Uint8Array) { |
| 114 | + let headers: Record<string, string> = {}; |
| 115 | + |
| 116 | + let filenameIndex = bytes.findIndex(headerLineByte, encode.filename); |
| 117 | + if (filenameIndex < 0) { |
| 118 | + headers.name = getNameOnly(headerLineByte); |
| 119 | + } else { |
| 120 | + headers = getNameNFilename(headerLineByte, filenameIndex); |
| 121 | + } |
| 122 | + return headers; |
| 123 | +} |
| 124 | + |
| 125 | +function getNameNFilename(headerLineByte: Uint8Array, filenameIndex: number) { |
| 126 | + // fetch filename first |
| 127 | + let nameByte = headerLineByte.slice(0, filenameIndex - 2); |
| 128 | + let filenameByte = headerLineByte.slice( |
| 129 | + filenameIndex + encode.filename.byteLength + 2, |
| 130 | + headerLineByte.byteLength - 1, |
| 131 | + ); |
| 132 | + |
| 133 | + let name = getNameOnly(nameByte); |
| 134 | + let filename = decoder.decode(filenameByte); |
| 135 | + return { name, filename }; |
| 136 | +} |
| 137 | + |
| 138 | +function getNameOnly(headerLineByte: Uint8Array) { |
| 139 | + let nameIndex = bytes.findIndex(headerLineByte, encode.name); |
| 140 | + // jump <name="> and get string inside double quote => "string" |
| 141 | + let nameByte = headerLineByte.slice( |
| 142 | + nameIndex + encode.name.byteLength + 2, |
| 143 | + headerLineByte.byteLength - 1, |
| 144 | + ); |
| 145 | + return decoder.decode(nameByte); |
| 146 | +} |
| 147 | + |
| 148 | +function splitPiece(piece: Uint8Array) { |
| 149 | + const contentIndex = bytes.findIndex(piece, encode.returnNewline2); |
| 150 | + const headerByte = piece.slice(0, contentIndex); |
| 151 | + const contentByte = piece.slice(contentIndex + 4); |
| 152 | + |
| 153 | + return { headerByte, contentByte }; |
| 154 | +} |
| 155 | + |
| 156 | +function getFieldPieces( |
| 157 | + buf: Uint8Array, |
| 158 | + boundaryByte: Uint8Array, |
| 159 | +): Uint8Array[] { |
| 160 | + const startBoundaryByte = bytes.concat(encode.dashdash, boundaryByte); |
| 161 | + const endBoundaryByte = bytes.concat(startBoundaryByte, encode.dashdash); |
| 162 | + |
| 163 | + const pieces = []; |
| 164 | + |
| 165 | + while (!bytes.hasPrefix(buf, endBoundaryByte)) { |
| 166 | + // jump over boundary + '\r\n' |
| 167 | + buf = buf.slice(startBoundaryByte.byteLength + 2); |
| 168 | + let boundaryIndex = bytes.findIndex(buf, startBoundaryByte); |
| 169 | + // get field content piece |
| 170 | + pieces.push(buf.slice(0, boundaryIndex - 1)); |
| 171 | + buf = buf.slice(boundaryIndex); |
| 172 | + } |
| 173 | + |
| 174 | + return pieces; |
| 175 | +} |
| 176 | + |
| 177 | +function getBoundary(contentType: string): Uint8Array | undefined { |
| 178 | + let contentTypeByte = encoder.encode(contentType); |
| 179 | + let boundaryIndex = bytes.findIndex(contentTypeByte, encode.boundaryEqual); |
| 180 | + if (boundaryIndex >= 0) { |
| 181 | + // jump over 'boundary=' to get the real boundary |
| 182 | + let boundary = contentTypeByte.slice( |
| 183 | + boundaryIndex + encode.boundaryEqual.byteLength, |
| 184 | + ); |
| 185 | + return boundary; |
| 186 | + } else { |
| 187 | + return undefined; |
| 188 | + } |
| 189 | +} |
0 commit comments