Comparing version 0.1.0 to 0.2.0
@@ -7,14 +7,4 @@ /*global unescape*/ | ||
if (Buffer.isBuffer(stringOrObjectOrBuffer)) { | ||
var str = ''; | ||
for (var i = 0 ; i < stringOrObjectOrBuffer.length ; i += 1) { | ||
var octet = stringOrObjectOrBuffer[i]; | ||
if (octet > 127) { | ||
str += unescape('%' + octet.toString(16)); | ||
} else { | ||
str += String.fromCharCode(octet); | ||
} | ||
} | ||
stringOrObjectOrBuffer = str; | ||
} | ||
if (typeof stringOrObjectOrBuffer === 'string') { | ||
this.populateFromBuffer(stringOrObjectOrBuffer); | ||
} else if (typeof stringOrObjectOrBuffer === 'string') { | ||
this.populateFromString(stringOrObjectOrBuffer); | ||
@@ -26,3 +16,37 @@ } else if (stringOrObjectOrBuffer && typeof stringOrObjectOrBuffer === 'object') { | ||
Message.prototype.populateFromString = function (str) { | ||
Message.prototype.populateFromBuffer = function (buffer) { | ||
var endOfHeadersIndex = 0, | ||
latestLineBreaks = ''; | ||
while (endOfHeadersIndex < buffer.length) { | ||
var octet = buffer[endOfHeadersIndex]; | ||
if (octet === 0xd || octet === 0xa) { // \r | ||
latestLineBreaks += octet === 0xd ? '\r' : '\n'; | ||
if (/\r\r$|\n\n$|\r\n\r\n$|\n\r\n\r$/.test(latestLineBreaks)) { | ||
endOfHeadersIndex += 1; | ||
break; | ||
} | ||
} else { | ||
latestLineBreaks = ''; | ||
} | ||
endOfHeadersIndex += 1; | ||
} | ||
if (endOfHeadersIndex < buffer.length) { | ||
this.body = buffer.slice(endOfHeadersIndex); | ||
} | ||
// Hack: Interpret non-ASCII in headers as iso-8859-1: | ||
var str = ''; | ||
for (var i = 0 ; i < endOfHeadersIndex ; i += 1) { | ||
var octet = buffer[i]; | ||
if (octet > 127) { | ||
str += unescape('%' + octet.toString(16)); | ||
} else { | ||
str += String.fromCharCode(octet); | ||
} | ||
} | ||
this.populateFromString(str, true); | ||
}; | ||
Message.prototype.populateFromString = function (str, ignoreBody) { | ||
var that = this, | ||
@@ -48,10 +72,12 @@ state = 'startLine', | ||
// Parse error or terminating CRLFCRLF | ||
if (ch === '\r' && str[i + 1] === '\n' || (ch === '\n' && str[i + 1] === '\r')) { | ||
if (str.length >= i + 2) { | ||
that.body = str.substr(i + 2); | ||
if (!ignoreBody) { | ||
if (ch === '\r' && str[i + 1] === '\n' || (ch === '\n' && str[i + 1] === '\r')) { | ||
if (str.length >= i + 2) { | ||
that.body = str.substr(i + 2); | ||
} | ||
} else { | ||
if (str.length >= i + 1) { | ||
that.body = str.substr(i + 1); | ||
} | ||
} | ||
} else { | ||
if (str.length >= i + 1) { | ||
that.body = str.substr(i + 1); | ||
} | ||
} | ||
@@ -58,0 +84,0 @@ flush(); |
{ | ||
"name": "messy", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Object model for HTTP and RFC822 messages", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -37,2 +37,35 @@ /*global describe, it*/ | ||
it('should keep the buffer as a Buffer when the message is provided as a Buffer', function () { | ||
expect(new Message(Buffer.concat([ | ||
new Buffer( | ||
'From: foo@bar\r\n' + | ||
'\r\n', | ||
'utf-8' | ||
), | ||
new Buffer([0xf8]) | ||
])), 'to have properties', { | ||
body: new Buffer([0xf8]) | ||
}); | ||
}); | ||
it('should detect the end of the headers properly when separated by CRCR', function () { | ||
expect(new Message(Buffer.concat([ | ||
new Buffer('From: foo@bar\r\r', 'utf-8'), new Buffer([0xf8]) | ||
])), 'to have properties', { | ||
body: new Buffer([0xf8]) | ||
}); | ||
expect(new Message(new Buffer('From: foo@bar\r\r', 'utf-8')), 'not to have property', 'body'); | ||
}); | ||
it('should detect the end of the headers properly when separated by LFLF', function () { | ||
expect(new Message(Buffer.concat([ | ||
new Buffer('From: foo@bar\n\n', 'utf-8'), new Buffer([0xf8]) | ||
])), 'to have properties', { | ||
body: new Buffer([0xf8]) | ||
}); | ||
expect(new Message(new Buffer('From: foo@bar\n\n', 'utf-8')), 'not to have property', 'body'); | ||
}); | ||
it('should not read past CRLFCRLF when parsing', function () { | ||
@@ -123,7 +156,7 @@ var message = new Message('Subject: abc\r\n\r\nFrom: me'); | ||
it('should decode non-ASCII after CRLFCRLF as iso-8859-1 and preserve it when serializing', function () { | ||
it('should read an iso-8859-1 body after LFCRLFCR into a buffer and turn it into REPLACEMENT CHARACTER U+FFFD when serializing as text', function () { | ||
var message = new Message(Buffer.concat([new Buffer('foo: bar\n\r\n\rthis is the:body', 'utf-8'), new Buffer([0xf8, 0xe6])])); | ||
expect(message.toString(), 'to equal', 'Foo: bar\r\n\r\nthis is the:bodyøæ'); | ||
expect(message.body, 'to equal', Buffer.concat([new Buffer('this is the:body', 'utf-8'), new Buffer([0xf8, 0xe6])])); | ||
expect(message.toString(), 'to equal', 'Foo: bar\r\n\r\nthis is the:body��'); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41968
801