Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added more intelligent JSON parsing

  • Loading branch information...
commit 4589333a2dee109aacea0e05211aa2dcf8cbcacb 1 parent 07a891a
@ritch ritch authored
Showing with 56 additions and 26 deletions.
  1. +45 −26 lib/util/http.js
  2. +11 −0 test/util.unit.js
View
71 lib/util/http.js
@@ -14,33 +14,41 @@ exports.setup = function(req, res, next) {
, handler = corser.create({supportsCredentials: true, methods: ALLOWED_METHODS, origins: origins});
handler(req, res, function () {
- if (req.method === "OPTIONS") {
- // End CORS preflight request.
- res.writeHead(204);
- res.end();
- } else {
- var mime = req.headers['content-type'] || '';
- mime = mime.split(';')[0]; //Just in case there's multiple mime types, pick the first
-
- req.cookies = res.cookies = new Cookies(req, res);
-
- if(~req.url.indexOf('?')) {
- try {
- req.query = parseQuery(req.url);
- } catch (ex) {
- res.setHeader('Content-Type', 'text/plain');
- res.statusCode = 400;
- res.end('Failed to parse querystring: ' + ex);
- return;
- }
+ req.cookies = res.cookies = new Cookies(req, res);
+
+ if(~req.url.indexOf('?')) {
+ try {
+ req.query = parseQuery(req.url);
+ } catch (ex) {
+ res.setHeader('Content-Type', 'text/plain');
+ res.statusCode = 400;
+ res.end('Failed to parse querystring: ' + ex);
+ return;
}
-
- if(autoParse[mime]) {
- autoParse[mime](req, res, mime, next);
- } else {
- if(req.headers['content-length']) req.pause();
+ }
+
+ switch(req.method) {
+ case 'OPTIONS':
+ // End CORS preflight request.
+ res.writeHead(204);
+ res.end();
+ break;
+ case 'POST':
+ case 'PUT':
+ case 'DELETE':
+ var mime = req.headers['content-type'] || 'application/json';
+ mime = mime.split(';')[0]; //Just in case there's multiple mime types, pick the first
+
+ if(autoParse[mime]) {
+ autoParse[mime](req, res, mime, next);
+ } else {
+ if(req.headers['content-length']) req.pause();
+ next();
+ }
+ break;
+ default:
next();
- }
+ break;
}
});
};
@@ -68,7 +76,18 @@ var parseBody = exports.parseBody = function(req, res, mime, callback) {
}
try {
- req.body = parser.parse(buf);
+ if(buf.length) {
+ if(mime === 'application/json' && '{' != buf[0] && '[' != buf[0]) {
+ res.setHeader('Content-Type', 'text/plain');
+ res.statusCode = 400;
+ res.end('Could not parse invalid JSON');
+ return;
+ }
+
+ req.body = parser.parse(buf);
+ } else {
+ req.body = {};
+ }
callback();
} catch (ex) {
res.setHeader('Content-Type', 'text/plain');
View
11 test/util.unit.js
@@ -83,6 +83,17 @@ describe('.parseBody()', function() {
req.emit('data', value);
req.emit('end');
});
+
+ it('should interpret an empty body as an empty object', function(done) {
+ var req = new Stream();
+
+ http.parseBody(req, this.res, 'application/json', function(err) {
+ expect(err).to.not.exist;
+ expect(req.body).to.eql({});
+ done();
+ });
+ req.emit('end');
+ });
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.