Skip to content

Commit

Permalink
Merge pull request #30 from iamdoron/master
Browse files Browse the repository at this point in the history
fix #7
  • Loading branch information
Eran Hammer committed Jun 4, 2015
2 parents 720f4a0 + 5e566fa commit 07255e2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 18 deletions.
30 changes: 30 additions & 0 deletions lib/buffer-equal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// This code was originally from
// https://github.com/substack/node-buffer-equal
// and is under an MIT license

module.exports = function (a, b) {

if (!Buffer.isBuffer(a)) {
return undefined;
}

if (!Buffer.isBuffer(b)) {
return undefined;
}

if (typeof a.equals === 'function') {
return a.equals(b);
}

if (a.length !== b.length) {
return false;
}

for (var i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false;
}
}

return true;
};
48 changes: 31 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var Stream = require('stream');
var Util = require('util');
var Url = require('url');
var Hoek = require('hoek');
var BufferEqual = require('./buffer-equal');


// Declare internals
Expand Down Expand Up @@ -219,39 +220,41 @@ internals.finish = function (response, req, onEnd) {
var rawBuffer = Buffer.concat(raw, rawLength);

// Parse payload
res.payload = '';

var CRLF = '\r\n';
var sep = new Buffer(CRLF + CRLF);
var parts = internals.splitBufferInTwo(rawBuffer, sep);
var payloadBuffer = parts[1];

var output = rawBuffer.toString('binary');
var sep = output.indexOf(CRLF + CRLF);
var payloadBlock = output.slice(sep + 4);
var headerBlock = output.slice(0, sep);

res.rawPayload = new Buffer(rawBuffer.length - sep - 4);
rawBuffer.copy(res.rawPayload, 0, sep + 4);
res.rawPayload = payloadBuffer;

if (!res.headers['transfer-encoding']) {
res.payload = payloadBlock;
res.payload = payloadBuffer.toString();
return;
}

var rest = payloadBlock;
res.payload = '';

var CRLFBuffer = new Buffer(CRLF);
var rest = payloadBuffer;
var payloadBytes = [];
do {
var next = rest.indexOf(CRLF);
var size = parseInt(rest.slice(0, next), 16);
var payloadParts = internals.splitBufferInTwo(rest, CRLFBuffer);
var next = payloadParts[1];
var size = parseInt(payloadParts[0].toString(), 16);
if (size === 0) {
rest = rest.slice(3);
rest = next;
}
else {
res.payload += rest.substr(next + 2, size);
rest = rest.slice(next + 2 + size + 2);
var nextData = next.slice(0, size);
payloadBytes = payloadBytes.concat(Array.prototype.slice.call(nextData, 0));
res.payload += nextData.toString();
rest = next.slice(size + 2);
}
}
while (size);

var headers = rest.split(CRLF);
res.payloadBuffer = new Buffer(payloadBytes);
var headers = rest.toString().split(CRLF);
headers.forEach(function (header) {

var parts = header.split(':');
Expand All @@ -262,6 +265,17 @@ internals.finish = function (response, req, onEnd) {
};
};

internals.splitBufferInTwo = function (buffer, seperator) {

for (var i = 0; i < buffer.length - seperator.length; i++) {
if (BufferEqual(buffer.slice(i, i + seperator.length), seperator)) {
var part1 = buffer.slice(0, i);
var part2 = buffer.slice(i + seperator.length);
return [part1, part2];
}
}
return [buffer, new Buffer(0)];
};

exports.inject = function (dispatchFunc, options, callback) {

Expand Down
18 changes: 17 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ describe('inject()', function () {

Fs.readFile('./package.json', { encoding: 'utf-8' }, function (err, file) {

Zlib.unzip(new Buffer(res.payload, 'binary'), function (err, unzipped) {
Zlib.unzip(res.payloadBuffer, function (err, unzipped) {

expect(err).to.not.exist();
expect(unzipped.toString('utf-8')).to.deep.equal(file);
Expand Down Expand Up @@ -343,6 +343,22 @@ describe('inject()', function () {
});
});

it('echos object payload with non-english utf-8 string', function (done) {

var dispatch = function (req, res) {

res.writeHead(200, { 'content-type': req.headers['content-type'] });
req.pipe(res);
};

Shot.inject(dispatch, { method: 'post', url: '/test', payload: { a: '½½א' } }, function (res) {

expect(res.headers['content-type']).to.equal('application/json');
expect(res.payload).to.equal('{"a":"½½א"}');
done();
});
});

it('echos object payload without payload', function (done) {

var dispatch = function (req, res) {
Expand Down

0 comments on commit 07255e2

Please sign in to comment.