-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpdumper.js
64 lines (53 loc) · 1.25 KB
/
httpdumper.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
const chalk = require('chalk');
const formidable = require('formidable');
const urlDump = (req) => {
console.log(chalk.white.bold(req.method), chalk.white.bold(req.url));
return req.url;
};
const headerDump = (req) => {
const headerKeys = Object.keys(req.headers);
headerKeys.forEach((header) => {
console.log(
chalk.white.bold(header),
chalk.dim(': '),
chalk.white.bold(req.headers[header]),
);
});
return req.headers;
};
const httpDump = (req) => {
console.log('\n\n');
console.log(
chalk.white.bold('Date:'),
'\t',
chalk.bgGray(new Date().toISOString()),
);
urlDump(req);
headerDump(req);
};
const httpDumper = (req, res, proxy = null, host = null, uploadDir = null) => {
httpDump(req);
const body = [];
const form = formidable({
multiples: true,
uploadDir: uploadDir || undefined,
keepExtensions: true,
});
form.parse(req);
req
.on('data', (chunk) => {
body.push(chunk);
})
.on('end', () => {
console.log('\n');
console.log(chalk.white.bold(body));
const bufferBody = Buffer.concat(body).toString();
if (!host) {
res.end(bufferBody);
}
});
if (host) {
proxy(req, res, req.url, {});
}
};
module.exports = httpDumper;