summaryrefslogtreecommitdiff
path: root/cli/js/web/fetch/multipart.ts
blob: a632d86002048033f6adb62897a30853b71683c4 (plain)
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
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

import { Buffer } from "../../buffer.ts";
import { bytesSymbol } from "../blob.ts";
import { DomFileImpl } from "../dom_file.ts";
import { DenoBlob } from "../blob.ts";
import { TextEncoder, TextDecoder } from "../text_encoding.ts";
import { getHeaderValueParams } from "../util.ts";

const decoder = new TextDecoder();
const encoder = new TextEncoder();
const CR = "\r".charCodeAt(0);
const LF = "\n".charCodeAt(0);

interface MultipartHeaders {
  headers: Headers;
  disposition: Map<string, string>;
}

export class MultipartBuilder {
  readonly boundary: string;
  readonly writer = new Buffer();
  constructor(readonly formData: FormData, boundary?: string) {
    this.boundary = boundary ?? this.#createBoundary();
  }

  getContentType(): string {
    return `multipart/form-data; boundary=${this.boundary}`;
  }

  getBody(): Uint8Array {
    for (const [fieldName, fieldValue] of this.formData.entries()) {
      if (fieldValue instanceof DomFileImpl) {
        this.#writeFile(fieldName, fieldValue);
      } else this.#writeField(fieldName, fieldValue as string);
    }

    this.writer.writeSync(encoder.encode(`\r\n--${this.boundary}--`));

    return this.writer.bytes();
  }

  #createBoundary = (): string => {
    return (
      "----------" +
      Array.from(Array(32))
        .map(() => Math.random().toString(36)[2] || 0)
        .join("")
    );
  };

  #writeHeaders = (headers: string[][]): void => {
    let buf = this.writer.empty() ? "" : "\r\n";

    buf += `--${this.boundary}\r\n`;
    for (const [key, value] of headers) {
      buf += `${key}: ${value}\r\n`;
    }
    buf += `\r\n`;

    this.writer.write(encoder.encode(buf));
  };

  #writeFileHeaders = (
    field: string,
    filename: string,
    type?: string
  ): void => {
    const headers = [
      [
        "Content-Disposition",
        `form-data; name="${field}"; filename="${filename}"`,
      ],
      ["Content-Type", type || "application/octet-stream"],
    ];
    return this.#writeHeaders(headers);
  };

  #writeFieldHeaders = (field: string): void => {
    const headers = [["Content-Disposition", `form-data; name="${field}"`]];
    return this.#writeHeaders(headers);
  };

  #writeField = (field: string, value: string): void => {
    this.#writeFieldHeaders(field);
    this.writer.writeSync(encoder.encode(value));
  };

  #writeFile = (field: string, value: DomFileImpl): void => {
    this.#writeFileHeaders(field, value.name, value.type);
    this.writer.writeSync(value[bytesSymbol]);
  };
}

export class MultipartParser {
  readonly boundary: string;
  readonly boundaryChars: Uint8Array;
  readonly body: Uint8Array;
  constructor(body: Uint8Array, boundary: string) {
    if (!boundary) {
      throw new TypeError("multipart/form-data must provide a boundary");
    }

    this.boundary = `--${boundary}`;
    this.body = body;
    this.boundaryChars = encoder.encode(this.boundary);
  }

  #parseHeaders = (headersText: string): MultipartHeaders => {
    const headers = new Headers();
    const rawHeaders = headersText.split("\r\n");
    for (const rawHeader of rawHeaders) {
      const sepIndex = rawHeader.indexOf(":");
      if (sepIndex < 0) {
        continue; // Skip this header
      }
      const key = rawHeader.slice(0, sepIndex);
      const value = rawHeader.slice(sepIndex + 1);
      headers.set(key, value);
    }

    return {
      headers,
      disposition: getHeaderValueParams(
        headers.get("Content-Disposition") ?? ""
      ),
    };
  };

  parse(): FormData {
    const formData = new FormData();
    let headerText = "";
    let boundaryIndex = 0;
    let state = 0;
    let fileStart = 0;

    for (let i = 0; i < this.body.length; i++) {
      const byte = this.body[i];
      const prevByte = this.body[i - 1];
      const isNewLine = byte === LF && prevByte === CR;

      if (state === 1 || state === 2 || state == 3) {
        headerText += String.fromCharCode(byte);
      }
      if (state === 0 && isNewLine) {
        state = 1;
      } else if (state === 1 && isNewLine) {
        state = 2;
        const headersDone = this.body[i + 1] === CR && this.body[i + 2] === LF;

        if (headersDone) {
          state = 3;
        }
      } else if (state === 2 && isNewLine) {
        state = 3;
      } else if (state === 3 && isNewLine) {
        state = 4;
        fileStart = i + 1;
      } else if (state === 4) {
        if (this.boundaryChars[boundaryIndex] !== byte) {
          boundaryIndex = 0;
        } else {
          boundaryIndex++;
        }

        if (boundaryIndex >= this.boundary.length) {
          const { headers, disposition } = this.#parseHeaders(headerText);
          const content = this.body.subarray(fileStart, i - boundaryIndex - 1);
          // https://fetch.spec.whatwg.org/#ref-for-dom-body-formdata
          const filename = disposition.get("filename");
          const name = disposition.get("name");

          state = 5;
          // Reset
          boundaryIndex = 0;
          headerText = "";

          if (!name) {
            continue; // Skip, unknown name
          }

          if (filename) {
            const blob = new DenoBlob([content], {
              type: headers.get("Content-Type") || "application/octet-stream",
            });
            formData.append(name, blob, filename);
          } else {
            formData.append(name, decoder.decode(content));
          }
        }
      } else if (state === 5 && isNewLine) {
        state = 1;
      }
    }

    return formData;
  }
}