Skip to content

Commit

Permalink
Separate processing of lines starting with whitespace into its own lo…
Browse files Browse the repository at this point in the history
…op, so the other parsing loop can be done from top to bottom
  • Loading branch information
sadikinfosec committed May 19, 2022
1 parent 2a6b5e9 commit 6f502d9
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions src/mime-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,32 +197,36 @@ export default class MimeNode {
}

processHeaders() {
for (let i = 0; i <= this.headerLines.length - 1; i++) {
for (let i = this.headerLines.length - 1; i >= 0; i--) {
let line = this.headerLines[i];
if (i && /^\s/.test(line)) {
continue;
} else {
// remove folding and extra WS
line = line.replace(/\s+/g, ' ');
let sep = line.indexOf(':');
let key = sep < 0 ? line.trim() : line.substr(0, sep).trim();
let value = sep < 0 ? '' : line.substr(sep + 1).trim();
this.headers.push({ key: key.toLowerCase(), originalKey: key, value });

switch (key.toLowerCase()) {
case 'content-type':
this.contentType = { value, parsed: {} };
break;
case 'content-transfer-encoding':
this.contentTransferEncoding = { value, parsed: {} };
break;
case 'content-disposition':
this.contentDisposition = { value, parsed: {} };
break;
case 'content-id':
this.contentId = value;
break;
}
this.headerLines[i - 1] += '\n' + line;
this.headerLines.splice(i, 1);
}
}

for (let i = 0; i <= this.headerLines.length - 1; i++) {
let line = this.headerLines[i];
// remove folding and extra WS
line = line.replace(/\s+/g, ' ');
let sep = line.indexOf(':');
let key = sep < 0 ? line.trim() : line.substr(0, sep).trim();
let value = sep < 0 ? '' : line.substr(sep + 1).trim();
this.headers.push({ key: key.toLowerCase(), originalKey: key, value });

switch (key.toLowerCase()) {
case 'content-type':
this.contentType = { value, parsed: {} };
break;
case 'content-transfer-encoding':
this.contentTransferEncoding = { value, parsed: {} };
break;
case 'content-disposition':
this.contentDisposition = { value, parsed: {} };
break;
case 'content-id':
this.contentId = value;
break;
}
}

Expand Down

0 comments on commit 6f502d9

Please sign in to comment.