Skip to content

Commit

Permalink
Fix multipart encoding #771
Browse files Browse the repository at this point in the history
  • Loading branch information
tillre committed Apr 18, 2013
1 parent e8acab9 commit aed16d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
12 changes: 6 additions & 6 deletions lib/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ exports.read = function (request, next) {

// Set parser

internals.parse(payload.toString('utf8'), req.headers, function (err, result) {
internals.parse(payload, req.headers, function (err, result) {

if (err) {
return finish(err);
Expand Down Expand Up @@ -167,7 +167,7 @@ internals.isGzip = function (compareBuf) {
};


internals.parse = function (result, headers, callback) {
internals.parse = function (payload, headers, callback) {

callback = Utils.nextTick(callback);

Expand All @@ -179,7 +179,7 @@ internals.parse = function (result, headers, callback) {
// Text

if (mime.match(/^text\/.+$/)) {
return callback(result);
return callback(payload.toString('utf8'));
}

// JSON
Expand All @@ -189,7 +189,7 @@ internals.parse = function (result, headers, callback) {
var error = null;

try {
obj = JSON.parse(result);
obj = JSON.parse(payload.toString('utf8'));
}
catch (exp) {
error = Boom.badRequest('Invalid request payload format');
Expand All @@ -201,7 +201,7 @@ internals.parse = function (result, headers, callback) {
// Form-encoded

if (mime === 'application/x-www-form-urlencoded') {
return callback(null, Querystring.parse(result));
return callback(null, Querystring.parse(payload.toString('utf8')));
}

// Multipart
Expand Down Expand Up @@ -237,7 +237,7 @@ internals.parse = function (result, headers, callback) {
});

form.writeHeaders(headers);
form.write(new Buffer(result));
form.write(new Buffer(payload));
return;
}

Expand Down
15 changes: 12 additions & 3 deletions test/integration/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var Http = require('http');
var Path = require('path');
var Stream = require('stream');
var Zlib = require('zlib');
var Assert = require('assert');
var Hapi = require('../..');


Expand Down Expand Up @@ -542,11 +543,19 @@ describe('Payload', function () {

it('parses a file correctly', function (done) {

var file = Fs.readFileSync(Path.join(__dirname, '../../images/hapi.png'));
var path = Path.join(__dirname, '../../images/hapi.png');
var stats = Fs.statSync(path);
var fileStream = Fs.createReadStream(path);
var fileContents = Fs.readFileSync(path, { encoding: 'binary' });

var fileHandler = function (request) {

expect(request.raw.req.headers['content-type']).to.contain('multipart/form-data');
expect(request.payload['my_file']).to.contain('Photoshop');
expect(request.payload['my_file'].size).to.equal(stats.size);

var tmpContents = Fs.readFileSync(request.payload['my_file'].path, { encoding: 'binary' });
Assert.deepEqual(fileContents, tmpContents);

done();
};

Expand All @@ -556,7 +565,7 @@ describe('Payload', function () {

var r = Request.post(server.settings.uri + '/file');
var form = r.form();
form.append('my_file', file);
form.append('my_file', fileStream);
});
});
});
Expand Down

0 comments on commit aed16d0

Please sign in to comment.