Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multipart encoding #771 #774

Merged
merged 1 commit into from
Apr 18, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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