Browse Source

Bugfix: Handle Content-Type headers with charset

Some HTTP clients include a charset parameter in the Content-Type, e.g:

multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY

This patch makes the multipart parser more forgiving towards unexpected
information included in the Content-Type header.
v0.7.4-release
Felix Geisendörfer 15 years ago
committed by Ryan Dahl
parent
commit
ac2abe5b1e
  1. 8
      lib/multipart.js
  2. 10
      test/mjsunit/test-multipart.js

8
lib/multipart.js

@ -56,11 +56,15 @@ proto.init = function(options) {
throw new Error('Content-Type header not set');
}
var boundary = contentType.match(/^multipart\/form-data; ?boundary=(.+)$/i)
if (!boundary) {
if (!contentType.match(/^multipart\/form-data/i)) {
throw new Error('Content-Type is not multipart: "'+contentType+'"');
}
var boundary = contentType.match(/boundary=([^;]+)/i)
if (!boundary) {
throw new Error('No boundary in Content-Type header: "'+contentType+'"');
}
this.boundary = '--'+boundary[1];
this.bytesTotal = req.headers['content-length'];

10
test/mjsunit/test-multipart.js

@ -14,7 +14,7 @@ var
respond = function(res, text) {
requests++;
if (requests == 4) {
if (requests == 5) {
server.close();
}
@ -113,6 +113,14 @@ var simpleBadRequest = client.request('POST', '/', {
simpleBadRequest.sendBody(fixture.reply, 'binary');
simpleBadRequest.finish();
var requestWithCharset = client.request('POST', '/', {
'X-Use-Simple-Api': 'yes',
'Content-Type': 'multipart/form-data; charset=utf-8; boundary=AaB03x',
'Content-Length': fixture.reply.length
});
requestWithCharset.sendBody(fixture.reply, 'binary');
requestWithCharset.finish();
process.addListener('exit', function() {
puts("done");
assert.equal(2, partsComplete);

Loading…
Cancel
Save