-
-
Notifications
You must be signed in to change notification settings - Fork 685
Support parsing of multipart/byteranges #390
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
Comments
@felixge any help on that? Maybe https://github.com/jshttp/range-parser worth look? |
I don't understand how this is supposed to work. @jcready can you provide an example request and what you'd expect formidable to do with it? |
@felixge a multipart/byteranges response would look something like this (taken from RFC 7233):
I imagine the parsed body would end up looking like this: [
{
"headers": {
"content-type": "application/pdf",
"content-range": "bytes 500-999/8000"
},
"body": "...the first range..."
},
{
"headers": {
"content-type": "application/pdf",
"content-range": "bytes 7000-7999/8000"
},
"body": "...the second range"
}
] Ideally this functionality would just be an additional parser like formidable.IncomingForm.prototype.parse = function parseByteRanges (res, done) {
const parts = []
let totalLength = 0
res.on('error', done).on('data', (chunk) => {
parts.push(chunk)
totalLength += chunk.length
}).on('end', () => {
try {
const boundary = res.headers['content-type'].match(/boundary=(.+)$/)[1]
const body = Buffer.concat(parts, totalLength)
done(null, parseMultipartBody(body.toString(), boundary))
} catch (e) {
done(e)
}
})
}
function parseMultipartBody (body, boundary) {
return body.split('--' + boundary).reduce((memo, part) => {
if (part && part !== '--') {
const [ head, body ] = part.trim().split(/\r\n\r\n/g)
memo.push({
headers: head.split(/\r\n/).reduce((memo, header) => {
const [ key, val ] = header.split(/:\s+/)
memo[key.toLowerCase()] = val
return memo
}, {}),
body: body
})
}
return memo
}, [])
} |
@jcready hm, looks good. Can you try to PR and add some tests when you have time? We (the new maintainers) are here to help and review :) |
@jcready makes sense. But I wonder if this should really be part of formidable. If yes, it should use the streaming multipart parser that's used for multipart/form-data and avoid unbound memory allocation. If nobody has time for a good implementation and tests, I'd rather not see this supported. |
Currently formidable returns an empty body and a single item in the files object which is named 'null' and only contains the last part of the multipart byte-range body.
The text was updated successfully, but these errors were encountered: