This repository has been archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
multipart-handler.js
70 lines (65 loc) · 2.09 KB
/
multipart-handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* MOST Web Framework
* A JavaScript Web Framework
* http://themost.io
*
* Copyright (c) 2014, Kyriakos Barbounakis k.barbounakis@gmail.com, Anthi Oikonomou anthioikonomou@gmail.com
*
* Released under the BSD3-Clause license
* Date: 2014-12-02
*/
/**
* @ignore
*/
var formidable = require('formidable'), util = require('util');
if (process.version>="v6.0.0") {
var multipart_parser = require('formidable/lib/multipart_parser'),
MultipartParser = multipart_parser.MultipartParser;
MultipartParser.prototype.initWithBoundary = function(str) {
this.boundary = new Buffer(str.length+4);
this.boundary.write('\r\n--', 0, 4 , 'ascii');
this.boundary.write(str, 4, str.length, 'ascii');
this.lookbehind = new Buffer(this.boundary.length+8);
this.state = multipart_parser.START;
this.boundaryChars = {};
for (var i = 0; i < this.boundary.length; i++) {
this.boundaryChars[this.boundary[i]] = true;
}
};
}
function MultipartHandler() {
}
MultipartHandler.prototype.beginRequest = function(context, callback) {
var request = context.request;
request.headers = request.headers || {};
var contentType = request.headers['content-type'];
if (/^multipart\/form-data/i.test(contentType)) {
//use formidable to parse request data
var f = new formidable.IncomingForm(), web = require('./index');
f.parse(request, function (err, form, files) {
if (err) {
callback(err);
return;
}
try {
//add form
if (form) {
util._extend(context.params, web.common.parseForm(form));
}
//add files
if (files)
util._extend(context.params, files);
callback();
}
catch (e) {
callback(e);
}
});
}
else {
callback();
}
};
if (typeof exports !== 'undefined') {
module.exports.createInstance = function() { return new MultipartHandler(); };
}