Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MultipartReader for big files #4865

Merged
merged 16 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions std/mime/multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ export class MultipartReader {
* null value means parsing or writing to file was failed in some reason.
* @param maxMemory maximum memory size to store file in memory. bytes. @default 1048576 (1MB)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ry go uses 10MB as default in readForm. Yet the comment in the code says 1MB, just want confirmation. I think 1MB it's a good value to keep in Buffer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should definitely follow Go's example. 10MB sounds good to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Reverted back to 10MB and updated comment.

* */
async readForm(maxMemory = 10 << 20): Promise<MultipartFormData> {
async readForm(maxMemory = 1 << 20): Promise<MultipartFormData> {
const fileMap = new Map<string, FormFile>();
const valueMap = new Map<string, string>();
let maxValueBytes = maxMemory + (10 << 20);
let maxValueBytes = maxMemory + (1 << 20);
const buf = new Buffer(new Uint8Array(maxValueBytes));
for (;;) {
const p = await this.nextPart();
Expand All @@ -308,7 +308,7 @@ export class MultipartReader {
}
// file
let formFile: FormFile | undefined;
const n = await copy(buf, p);
const n = await copyN(buf, p, maxValueBytes);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While testing I noticed that when parsing big files, it was crashing with: error: Uncaught Error: The buffer cannot be grown beyond the maximum size.

This happens because copy was being used instead of copyN, and was trying to fill the buffer with the whole file.

const contentType = p.headers.get("content-type");
assert(contentType != null, "content-type must be set");
if (n > maxMemory) {
Expand All @@ -319,11 +319,11 @@ export class MultipartReader {
postfix: ext,
});
try {
const size = await copyN(
file,
new MultiReader(buf, p),
maxValueBytes
);
// write buffer to file
let size = await copyN(file, buf, n);
// Write the rest of the file
size += await copy(file, new MultiReader(buf, p));

file.close();
formFile = {
filename: p.fileName,
Expand Down
47 changes: 32 additions & 15 deletions std/mime/multipart_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

const { Buffer, copy, open, test } = Deno;
const { Buffer, open, test } = Deno;
import {
assert,
assertEquals,
Expand All @@ -21,6 +21,7 @@ const e = new TextEncoder();
const boundary = "--abcde";
const dashBoundary = e.encode("--" + boundary);
const nlDashBoundary = e.encode("\r\n--" + boundary);
const testdataDir = path.resolve("mime", "testdata");

test(function multipartScanUntilBoundary1(): void {
const data = `--${boundary}`;
Expand Down Expand Up @@ -190,29 +191,45 @@ test({
});

test({
name: "[mime/multipart] readForm() should store big file in temp file",
name:
"[mime/multipart] readForm() should store big file completely in temp file",
async fn() {
const o = await open(path.resolve("./mime/testdata/sample.txt"));
const mr = new MultipartReader(
o,
"--------------------------434049563556637648550474"
);
const encoder = new TextEncoder();
const multipartFile = path.join(testdataDir, "form-data.dat");
const sampleFile = path.resolve(testdataDir, "bigfile.txt");
const writer = await open(multipartFile, { write: true, create: true });

const fileData = encoder.encode("a".repeat(1 << 24)); // 16mb
const size = fileData.length;

await Deno.writeFile(sampleFile, fileData);
const bigFile = await open(sampleFile, "r");

const mw = new MultipartWriter(writer);
await mw.writeField("deno", "land");
await mw.writeField("bar", "bar");
await mw.writeFile("file", "sample.txt", bigFile);

await mw.close();
writer.close();
bigFile.close();

const o = await Deno.open(multipartFile);
const mr = new MultipartReader(o, mw.boundary);
// use low-memory to write "file" into temp file.
const form = await mr.readForm(20);
try {
assertEquals(form.value("foo"), "foo");
assertEquals(form.value("deno"), "land");
assertEquals(form.value("bar"), "bar");
const file = form.file("file");
assert(file != null);
assertEquals(file.type, "application/octet-stream");
assert(file.tempfile != null);
const f = await open(file.tempfile);
const w = new StringWriter();
await copy(w, f);
const json = JSON.parse(w.toString());
assertEquals(json["compilerOptions"]["target"], "es2018");
f.close();
assertEquals(file.size, size);
assertEquals(file.type, "application/octet-stream");
// TODO checksum of tmp & sampleFile
} finally {
await Deno.remove(multipartFile);
await Deno.remove(sampleFile);
await form.removeAll();
o.close();
}
Expand Down