Skip to content

Commit

Permalink
Added a separate config item to control POST form data size.
Browse files Browse the repository at this point in the history
  • Loading branch information
cskr committed Aug 29, 2010
1 parent 1141b31 commit ef922fe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 9 additions & 0 deletions grasshopper/lib/grasshopper.js
Expand Up @@ -34,6 +34,8 @@ var routes = {},
servers = [],
filters = [];

var maxFormSize = 1048576;

exports.addToContext = function() {
for(var i = 0; i < arguments.length; i++) {
for(var key in arguments[i]) {
Expand All @@ -54,6 +56,9 @@ exports.addFilters = function(regex) {
exports.addHelpers = ghp.addHelpers;

exports.configure = function(config) {
if(config.maxFormSize)
maxFormSize = config.maxFormSize;

renderer.configure(config);
multipart.configure(config);
session.configure(config);
Expand Down Expand Up @@ -166,6 +171,10 @@ function dispatch(req, res, routeMatcher) {
if(action) {
if((req.method == 'POST' || req.method == 'PUT')) {
if(req.headers['content-type'] && req.headers['content-type'].match(/^application\/x-www-form-urlencoded/)) {
if(Number(req.headers['content-length']) > maxFormSize) {
new renderer.RequestContext(req, res, {}).renderError(413);
return;
}
req.setEncoding('utf8');
var dataString = '';
req.on('data', function(data) {
Expand Down
8 changes: 4 additions & 4 deletions grasshopper/lib/multipart.js
Expand Up @@ -18,20 +18,20 @@ var fs = require('fs'),
uuid = require('./uuid'),
formidable = require('formidable');

var maxPostSize = undefined,
var maxUploadSize = undefined,
uploadsDir = '/tmp';

exports.configure = function(config) {
if(config.maxPostSize)
maxPostSize = config.maxPostSize;
if(config.maxUploadSize)
maxUploadSize = config.maxUploadSize;
if(config.uploadsDir)
uploadsDir = config.uploadsDir;
};

exports.parse = function(context, callback) {
context.params = {};
var req = context.request;
if(new Number(req.headers['content-length']) > maxPostSize) {
if(Number(req.headers['content-length']) > maxUploadSize) {
context.renderError(413);
return;
}
Expand Down

0 comments on commit ef922fe

Please sign in to comment.