rollback attachment limiter

This commit is contained in:
Andris Reinman 2019-08-07 11:41:49 +03:00
parent dcef8e22cd
commit 742ddf30f1
5 changed files with 88 additions and 11 deletions

View file

@ -50,7 +50,7 @@ module.exports = (response, isLogging) => {
}
return new Promise((resolve, reject) => {
expectedLength = expectedLength || 0;
expectedLength = maxLength ? Math.min(expectedLength, startFrom + maxLength) : expectedLength;
startFrom = startFrom || 0;
maxLength = maxLength || 0;
@ -65,7 +65,6 @@ module.exports = (response, isLogging) => {
stream.once('end', () => resolve());
} else {
// force limites
expectedLength = maxLength ? Math.min(expectedLength, startFrom + maxLength) : expectedLength;
let limiter = new LengthLimiter(expectedLength, ' ', startFrom);
stream.pipe(limiter).pipe(
output,

View file

@ -656,8 +656,8 @@ module.exports.getQueryResponse = function(query, message, options) {
mimeTree = indexer.parseMimeTree(message.raw);
}
value = indexer.getContents(mimeTree, false, {
startFrom: item.partial && item.partial.startFrom,
maxLength: item.partial && item.partial.maxLength
//startFrom: item.partial && item.partial.startFrom,
//maxLength: item.partial && item.partial.maxLength
});
} else {
// BODY[SELECTOR]
@ -665,8 +665,8 @@ module.exports.getQueryResponse = function(query, message, options) {
mimeTree = indexer.parseMimeTree(message.raw);
}
value = indexer.getContents(mimeTree, item, {
startFrom: item.partial && item.partial.startFrom,
maxLength: item.partial && item.partial.maxLength
//startFrom: item.partial && item.partial.startFrom,
//maxLength: item.partial && item.partial.maxLength
});
}

View file

@ -0,0 +1,78 @@
'use strict';
/**
* Calculates binary start and end offsets for folded base64 offsets.
*
* @param {Number} lineLength Line length
* @param {Number} base64StartOffset Base64 offset to start reading from
* @param {Number} base64MaxLength Max response length for base64 response
* @returns {Object} Offsets and padding for binary input and base64 output
*/
const base64Offset = (lineLength, base64StartOffset, base64MaxLength) => {
// current line
let base64ExpectedStartLine = Math.floor(base64StartOffset / (lineLength + 2));
// which byte of current b64 line is the start
let base64LineOffset = base64StartOffset - base64ExpectedStartLine * (lineLength + 2);
// b64 line start without line breaks
let unfoldedBase64LineStart = base64ExpectedStartLine * lineLength;
// is current b64 start byte \r (1) or \n (2)
let base64LineEndChar = 0;
if (base64LineOffset >= lineLength) {
base64LineEndChar = base64LineOffset + 1 - lineLength;
base64LineOffset -= base64LineEndChar; // stay with real last byte on current line
}
// actual b64 start byte without line breaks
let unfoldedBase64StartOffset = unfoldedBase64LineStart + base64LineOffset;
// find start of binary bytes that corresponds to the start of current base64 chunk
let binaryStartOffset = Math.floor(unfoldedBase64StartOffset / 4) * 3; // in the middle of b64 chunk
// WE START READING BYTES FROM binaryStartOffset
// next:
// 1. how many b64 bytes to skip from output
// 2. where to put newlines
let reversedBase64StartOffset = (binaryStartOffset / 3) * 4; // start of b64 chunk that corresponds to binaryStartOffset
let base64StartDiff = unfoldedBase64StartOffset - reversedBase64StartOffset; // how many b64 bytes to skip (without newlines)
// base64 chunk might have started on previous line which would give us an extra line break
let base64RealStartLine = Math.floor(reversedBase64StartOffset / lineLength);
let extraNewlines = (base64ExpectedStartLine - base64RealStartLine) * 2;
//base64StartDiff += extraNewlines;
// base64 (for binaryStartOffset) line offset without newlines
let startBase64LineOffset = reversedBase64StartOffset - base64RealStartLine * lineLength;
let binaryEndOffset = 0;
if (base64MaxLength) {
let binaryMaxLength = Math.ceil(base64MaxLength / 4) * 3;
binaryEndOffset = binaryStartOffset + binaryMaxLength;
}
return {
// line length
lineLength,
// start reading binary input from this offset
binaryStartOffset,
// end reading binary input at this offset
binaryEndOffset,
// padding string for base64 output to get line folding correct
base64Padding: '#'.repeat(startBase64LineOffset),
// how many bytes to ignore from the start of base64 output (includes padding)
base64SkipStartBytes: base64StartDiff + startBase64LineOffset + base64LineEndChar + extraNewlines,
// how many base64 bytes to return
base64LimitBytes: base64MaxLength
};
};
module.exports = base64Offset;

View file

@ -1398,7 +1398,7 @@ describe('IMAP Protocol integration tests', function() {
});
it('should return partial BODY[]', function(done) {
let cmds = ['T1 LOGIN testuser pass', 'T2 SELECT INBOX', 'T3 FETCH 4 BODY.PEEK[]<4.5>', 'T4 FETCH 4 BODY.PEEK[]<4.10000>', 'T5 LOGOUT'];
let cmds = ['T1 LOGIN testuser pass', 'T2 SELECT INBOX', 'T3 FETCH 4 BODY.PEEK[]<0>', 'T4 FETCH 4 BODY.PEEK[]<4.10000>', 'T5 LOGOUT'];
testClient(
{

View file

@ -45,16 +45,16 @@
"humanname": "0.2.2",
"iconv-lite": "0.5.0",
"ioredfour": "1.0.2-ioredis-02",
"ioredis": "4.11.2",
"ioredis": "4.14.0",
"isemail": "3.2.0",
"joi": "14.3.1",
"js-yaml": "3.13.1",
"key-fingerprint": "1.1.0",
"libbase64": "1.1.0",
"libbase64": "1.2.0",
"libmime": "4.1.3",
"libqp": "1.1.0",
"mailsplit": "4.4.1",
"mobileconfig": "2.3.0",
"mobileconfig": "2.3.1",
"mongo-cursor-pagination": "7.1.0",
"mongodb": "3.2.7",
"mongodb-extended-json": "1.10.1",
@ -65,7 +65,7 @@
"pem": "1.14.2",
"pwnedpasswords": "1.0.4",
"qrcode": "1.4.1",
"restify": "8.3.3",
"restify": "8.4.0",
"restify-logger": "2.0.1",
"seq-index": "1.1.0",
"smtp-server": "3.5.0",