Permalink
Browse files

updated for latest node (addListener -> on, renamed server.on -> serv…

…er.handle) and added a new optional argument to server handlers which will be passed to oui.util.sanitizeInput, taking care of input sanitation and validation + etc
  • Loading branch information...
rsms committed Aug 2, 2010
1 parent 51e2df5 commit 2c52b1a58a9c55969d6e408ee07c85434e10276e
View
@@ -127,7 +127,7 @@ function main(argv, options, callback) {
}
var builder = mkbuilder(options, argv);
if (!options.once) {
- builder.addListener('collect', watcher);
+ builder.on('collect', watcher);
isBuilding = true;
builder.all(function(err){
isBuilding = false;
@@ -91,10 +91,10 @@ function compile_remote(opt, handler) {
//promise.emitError(err);
request.finish(function (response) {
var rsp_body = '';
- response.addListener('body', function (chunk) {
+ response.on('body', function (chunk) {
rsp_body += chunk;
});
- response.addListener('complete', function() {
+ response.on('complete', function() {
//sys.debug('status => '+response.statusCode);
var rsp = JSON.parse(rsp_body);
if (rsp.serverErrors && rsp.serverErrors.length) {
View
@@ -778,7 +778,7 @@ mixin(Builder.prototype, {
filter: this.srcFilter,
unbuffered: true,
}, callback);
- eve.addListener('file', function(relpath, abspath, ctx){
+ eve.on('file', function(relpath, abspath, ctx){
var source;
/*if (!didAddStdlib && self.stdlib && self.stdlibJSPath) {
source = new Source(self, self.stdlibJSPath, '');
@@ -63,19 +63,17 @@ mixin(http.IncomingMessage.prototype, {
// session
this.parseSession(function(err){
if (err) return callback(err); // forward
-
// content
if (self.method === 'POST' || self.method === 'PUT') {
try {
- self.parseRequestEntity();
+ self.startParsingRequestEntity();
} catch (err) {
return callback(err);
}
}
-
- // done
- callback();
});
+ //done
+ callback();
},
parseSession: function (callback) {
@@ -121,43 +119,57 @@ mixin(http.IncomingMessage.prototype, {
callback();
},
- parseRequestEntity: function() {
+ startParsingRequestEntity: function(callback) {
// todo: handle other kind of content, like file uploads and arbitrary data.
var server = this.connection.server, res = this.response;
- if (this.contentLength < 1)
- return res.sendError(411, 'Length Required');
+ if (this.contentLength < 1) {
+ var e = new Error();
+ e.title = 'Length Required';
+ e.statusCode = 411;
+ res.sendError(e);
+ }
// state: if a request's content was buffered, .content is set.
if (server.bufferableRequestTypes.indexOf(this.contentType) !== -1) {
var send413 = function(){
- res.sendError(413, "Request Entity Too Large", "Maximum size is "+
- server.maxRequestBodySize.toString())
+ var e = new Error("Maximum size is "+server.maxRequestBodySize);
+ e.title = "Request Entity Too Large";
+ e.statusCode = 413;
+ res.sendError(e);
};
this.content = '';
if (this.contentLength > 0) {
+ var fillcb;
// limited buffer
if (typeof server.maxRequestBodySize === 'number') {
if (this.contentLength > server.maxRequestBodySize) {
return send413();
- } else {
- var fillcb;
- fillcb = function(chunk) {
- var z = this.content.length+chunk.length;
- if (z > server.maxRequestBodySize) {
- this.content += chunk.substr(0, server.maxRequestBodySize - z);
- this.removeListener('data', fillcb);
- // clipped the input, which is a good thing
- if (!this.started)
- send413();
- } else {
- this.content += chunk;
- }
- }
- this.addListener('data', fillcb);
}
+ fillcb = function(chunk) {
+ var z = this.content.length+chunk.length;
+ if (z > server.maxRequestBodySize) {
+ // abort
+ this.content += chunk.substr(0, server.maxRequestBodySize - z);
+ this.removeListener('data', fillcb);
+ // clipped the input, which is a good thing
+ return send413();
+ }
+ this.content += chunk;
+ if (z === this.contentLength) {
+ this.removeListener('data', fillcb);
+ // done
+ }
+ };
} else {
// unlimited buffer -- might be dangerous
- this.addListener('data', function(chunk) {this.content += chunk })
+ fillcb = function(chunk) {
+ this.content += chunk;
+ if (this.content.length === this.contentLength) {
+ this.removeListener('data', fillcb);
+ // done
+ }
+ };
}
+ this.on('data', fillcb);
}
}
},
@@ -173,12 +185,16 @@ mixin(http.IncomingMessage.prototype, {
try {
obj = JSON.parse(data);
} catch (exc) {
- return this.response.sendError(400, 'Bad JSON', exc.message+' -- received '+data);
+ return this.response.sendError(400, 'Bad JSON',
+ exc.message+' -- received '+data);
}
- if (typeof obj !== 'object')
- return this.response.sendError(400, 'Bad JSON', 'Root object must be a list or a dict');
- for (var k in obj)
+ if (typeof obj !== 'object') {
+ return this.response.sendError(400, 'Bad JSON',
+ 'Root object must be an array or a map');
+ }
+ for (var k in obj) {
this.params[k] = obj[k];
+ }
},
solveRoute: function() {
@@ -362,10 +362,9 @@ mixin(http.ServerResponse.prototype, {
res.end();
return callback(null, 0);
}
- res.flush();
// forward
- var enc = 'binary', rz = 8*1024;
+ const enc = 'binary', rz = 8192;
fs.open(abspath, process.O_RDONLY, 0666, function(err, fd) {
if (err) return errorClosure(err);
var pos = 0;
Oops, something went wrong.

0 comments on commit 2c52b1a

Please sign in to comment.