Permalink
Browse files

fix for multiparser, attach to request in the same tick as request co…

…mes in
  • Loading branch information...
1 parent 9b00642 commit 3aeac91dc6229b15ec4d9c1e3735389cc81365f9 @Shogun147 committed Feb 20, 2013
Showing with 51 additions and 52 deletions.
  1. +15 −12 lib/katana.js
  2. +36 −40 lib/multiparser.js
View
27 lib/katana.js
@@ -20,6 +20,7 @@ var routing = Config('routing');
var View = require('./view');
var Model = require('./model');
+var Multiparser = require('./multiparser');
var Log = require('winston');
@@ -63,18 +64,20 @@ Class('Katana', {
}
App.server.on('request', function(request, response) {
- App.series('connection', function(error, results) {
- if (error instanceof Error) {
- App.series('request.error_connection', function(err, res) {
- if ((err instanceof Error) || err !== true) {
- response.writeHead(500, { 'Content-Type': 'text/html' });
- response.end('Error occured while handling request, please try again.');
+ Multiparser.parse(request, response, function() {
+ App.series('connection', function(error, results) {
+ if (error instanceof Error) {
+ App.series('request.error_connection', function(err, res) {
+ if ((err instanceof Error) || err !== true) {
+ response.writeHead(500, { 'Content-Type': 'text/html' });
+ response.end('Error occured while handling request, please try again.');
+ }
+ }, error, request, response);
+ } else if (error !== true) {
+ App.handle_connection(request, response);
}
- }, error, request, response);
- } else if (error !== true) {
- App.handle_connection(request, response);
- }
- }, request, response);
+ }, request, response);
+ });
});
App.server.listen(config.port, config.host);
@@ -179,7 +182,7 @@ module.exports.Store = require('./store');
module.exports.View = require('./view');
module.exports.Router = require('./router');
module.exports.Static = require('./static');
-module.exports.Multiparser = require('./multiparser');
+//module.exports.Multiparser = require('./multiparser');
module.exports.Request = require('./request');
module.exports.Response = require('./response');
module.exports.Model = require('./model');
View
76 lib/multiparser.js
@@ -2,66 +2,62 @@ var Formidable = require('formidable');
var EventEmitter = require('events').EventEmitter;
-var config = App.Config().multipart;
+var config = require('./config')().multipart;
-App.on('connection', function(request, response, callback) {
+module.exports.parse = function(request, response, callback) {
var need_parse = request.method != 'GET' && request.method != 'HEAD';
-
+
request.form = null;
-
- request.on('end', function() {
- if (!need_parse) {
- callback();
- }
- });
-
+
+ if (!need_parse) {
+ callback();
+ }
+
if (need_parse) {
- var mime = (request.headers['content-type'] || '').split(';')[0];
+ var mime = (request.headers['content-type'] || '').split(';')[0];
- if (mime != 'multipart/form-data' && mime != 'application/x-www-form-urlencoded') {
- return callback();
- }
+ if (mime != 'multipart/form-data' && mime != 'application/x-www-form-urlencoded') {
+ return callback();
+ }
- var Form = new Formidable.IncomingForm();
- Form.data = {};
- Form.files = {};
+ var Form = new Formidable.IncomingForm();
+ Form.data = {};
+ Form.files = {};
- request.form = Form;
-
- App.emit('request.multipart', request);
+ request.form = Form;
for (key in config) {
- Form[key] = config[key];
+ Form[key] = config[key];
}
- Form.on('field', function(name, value) {
- on_data(name, value, Form.data);
- });
+ Form.on('field', function(name, value) {
+ on_data(name, value, Form.data);
+ });
- Form.on('file', function(name, value) {
- on_data(name, value, Form.files);
- });
+ Form.on('file', function(name, value) {
+ on_data(name, value, Form.files);
+ });
- Form.on('error', function(error) {
- config.wait_end && callback(error);
- });
+ Form.on('error', function(error) {
+ config.wait_end && callback(error);
+ });
- Form.on('end', function() {
- config.wait_end && callback();
- });
+ Form.on('end', function() {
+ config.wait_end && callback();
+ });
- Form.parse(request);
-
- !config.wait_end && callback();
+ Form.parse(request);
+
+ !config.wait_end && callback();
}
-});
+}
function on_data(name, value, data) {
if (Array.isArray(data[name])) {
- data[name].push(value);
+ data[name].push(value);
} else if (data[name]) {
- data[name] = [data[name], value];
+ data[name] = [data[name], value];
} else {
- data[name] = value;
+ data[name] = value;
}
}

0 comments on commit 3aeac91

Please sign in to comment.