/
util.js
52 lines (47 loc) · 1.27 KB
/
util.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
exports.slurp = (function () {
function join_buffers (buffers, length) {
var buffer = new Buffer(length);
var targetStart = 0;
buffers.forEach(function (x) {
x.copy(buffer, targetStart);
targetStart += x.length;
});
return buffer;
};
function finish_it (req, buffers, length, callback) {
req.content = join_buffers(buffers, length);
return callback(req.content);
};
function nop () {};
return function (req, callback) {
if (req.hasOwnProperty('content')) {
return callback(req.content);
};
var content = [];
var length = 0;
var end_handler = finish_it;
req.on('data', function (data) {
content.push(data);
length += data.length;
});
[ 'end', 'close' ].forEach(function (event) {
req.on(event, function () {
finish_it(req, content, length, callback);
end_handler = nop;
});
});
};
})();
exports.end = function (req, res, statusCode, headers, content) {
var myheaders = {
'Content-Length': content && content.length || 0
};
if (headers) {
Object.keys(headers).forEach(function (key) {
myheaders[key] = headers[key];
});
};
res.writeHead(statusCode, myheaders);
res.end(content);
console.log(req.method, req.url, statusCode);
};