Make line parsing state per-connection

fork
Brian White 12 years ago
parent 5ab5e55734
commit ef66bb5c57

@ -58,6 +58,7 @@ function ImapConnection (options) {
indata: {
literals: [],
line: undefined,
line_s: { p: 0, ret: undefined },
temp: undefined,
streaming: false,
expect: -1
@ -212,7 +213,7 @@ ImapConnection.prototype.connect = function(loginCb) {
return;
}
if ((r = utils.line(b)) === false)
if ((r = utils.line(b, indata.line_s)) === false)
return;
else {
m = RE_LITHEADER.exec(r);
@ -1121,6 +1122,8 @@ ImapConnection.prototype._reset = function() {
this._state.indata.literals = [];
this._state.indata.line = undefined;
this._state.indata.line_s.p = 0;
this._state.indata.line_s.ret = undefined;
this._state.indata.temp = undefined;
this._state.indata.streaming = false;
this._state.indata.expect = -1;

@ -315,52 +315,50 @@ exports.validateUIDList = function(uids) {
}
var CHARR_CRLF = [13, 10];
function line(b) {
function line(b, s) {
var len = b.length, p = b.p, start = p, ret = false, retest = false;
while (p < len && !ret) {
if (b[p] === CHARR_CRLF[line.p]) {
if (++line.p === 2)
if (b[p] === CHARR_CRLF[s.p]) {
if (++s.p === 2)
ret = true;
} else {
retest = (line.p > 0);
line.p = 0;
retest = (s.p > 0);
s.p = 0;
if (retest)
continue;
}
++p;
}
if (ret === false) {
if (line.ret)
line.ret += b.toString('ascii', start);
if (s.ret)
s.ret += b.toString('ascii', start);
else
line.ret = b.toString('ascii', start);
s.ret = b.toString('ascii', start);
} else {
var iCR = p - 2;
if (iCR < 0) {
// the CR is at the end of line.ret
if (line.ret && line.ret.length > 1)
ret = line.ret.substr(0, line.ret.length - 1);
// the CR is at the end of s.ret
if (s.ret && s.ret.length > 1)
ret = s.ret.substr(0, s.ret.length - 1);
else
ret = '';
} else {
// the entire CRLF is in b
if (iCR === 0)
ret = (line.ret ? line.ret : '');
ret = (s.ret ? s.ret : '');
else {
if (line.ret) {
ret = line.ret;
if (s.ret) {
ret = s.ret;
ret += b.toString('ascii', start, iCR);
} else
ret = b.toString('ascii', start, iCR);
}
}
line.p = 0;
line.ret = undefined;
s.p = 0;
s.ret = undefined;
}
b.p = p;
return ret;
}
line.p = 0;
line.ret = undefined;
exports.line = line;

Loading…
Cancel
Save