Handling multipart/form-data body #106
-
Hi, I'm working on a multipart body parser and wondering if you could reveal the chunking logic for that data type. It looks like it's kinda supposed to pass one field per chunk for text fields, separate headers and body for file fields (starting chunk with headers, then N chunks with data), and the last chunk would be the closing boundary. But sometimes the server concatenates all or some of the text fields, it also can concatenate (into one chunk) part of the file with field headers and/or closing boundary. With a file it's especially inconvenient since it makes steaming to the disk a complicated task. I've confirmed it with the very basic example described in examples/Upload.js (v15.8.0). I'm wondering if it's the intended behaviour or something went wrong (or maybe I'm doing something wrong ofc). Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
Well, I wasn't looking for help, more like reporting presumably unexpected behaviour. const uWS = require('uWebSockets.js');
const port = 3000;
uWS.App().post('/*', (res, req) => {
let q = 0;
res.onData((chunk, isLast) => {
/* Buffer this anywhere you want to */
console.log('Chunk #', q, ', size:', chunk.byteLength);
if (q === 0 || isLast) {
if (chunk.byteLength <= 180) {
console.log('Handling chunk', Buffer.from(chunk).toString());
} else {
console.error('Boundary expected!');
process.exit(1);
}
}
q++;
/* We respond when we are done */
if (isLast) {
res.end('Thanks for the data!');
}
});
res.onAborted(() => {
/* Request was prematurely aborted, stop reading */
console.log('Eh, okay. Thanks for nothing!');
});
}).listen(port, (token) => {
if (token) {
console.log('Listening to port ' + port);
} else {
console.log('Failed to listen to port ' + port);
}
}); Then I sent the same simple form with just 1 field (a file), and here is the output of 2 tries:
So, as you can see, 1st time chunks came as expected, and 2nd time field headers got merged with a part of the file data in the first chunk, and that looks wrong. |
Beta Was this translation helpful? Give feedback.
-
There is a very fast multipart parser built-in now: uWS.getParts(body) |
Beta Was this translation helpful? Give feedback.
-
sorry for my stupid question.
how about reuse data and fetch to another dist url via formdata either? simply I need a typeof File here. and I found the document had these :
what is this the "MultipartField" use for? |
Beta Was this translation helpful? Give feedback.
There is a very fast multipart parser built-in now: uWS.getParts(body)