-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreception.js
163 lines (143 loc) · 4.64 KB
/
reception.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const express = require('express');
const mongoose = require('mongoose');
const fs = require('fs');
const envUtils = require('../helpers/envutils');
const crypto = require("crypto");
const router = express.Router();
//import models
require('../models/HttpDump');
require('../models/HttpReception');
const HttpDump = mongoose.model('httdump');
const HttpReception = mongoose.model('httpreception')
//routes
router.get("/:id", (req, res) => {
handleRequest(req, res, "GET");
});
router.post("/:id", (req, res) => {
handleRequest(req, res, "POST");
});
router.put("/:id", (req, res) => {
handleRequest(req, res, "PUT");
});
router.delete("/:id", (req, res) => {
handleRequest(req, res, "DELETE");
});
//route for downloading request payload
router.get("/download/requestpayload/:id", (req, res) => {
handleDownload(req, res, true);
});
//route for fownloading reesponse payload
router.get("/download/responsepayload/:id", (req, res) => {
handleDownload(req, res, false);
});
//end of routes
function handleRequest(req, res, method) {
HttpReception.findById({
_id: req.params.id
})
.then(httpReception => {
if (httpReception != null) {
processRequest(req, res, method, httpReception);
} else {
res.send("HTTP Reception is not defined or expired!");
}
})
.catch(err => {
console.log(err);
res.send("Error Occured!");
});
}
function processRequest(req, res, method, httpReception) {
const headers = req.headers
const headerMap = [];
for (var key in headers) {
//headerMap.set(key, headers[key]);
headerMap.push({
key: key,
value: headers[key]
})
}
let chunks = [];
let finalBuffer;
//record request body
req.on('data', chunk => {
chunks.push(chunk);
}).on('end', () => {//received all the data from the request
finalBuffer = Buffer.concat(chunks);
//headers to be sent
const responseHeaders = [];
var idx = 0;
for (; idx < httpReception.responseHeaders.length; idx++) {
const headerKey = httpReception.responseHeaders[idx].key;
const headerVal = httpReception.responseHeaders[idx].value;
responseHeaders.push({
key: headerKey,
value: headerVal
});
}
//persist
const newHttpDump = {
host: req.hostname,
ip: req.ip,
protocol: req.protocol,
method: method,
body: finalBuffer,
responseBody: httpReception.body,
headers: headerMap,
responseHeaders: responseHeaders,
httpReceptionId: httpReception._id
};
new HttpDump(newHttpDump).
save()
.then(httpdmp => {
var idx = 0;
for (; idx < responseHeaders.length; idx++) {
const headerKey = responseHeaders[idx].key;
const headerVal = responseHeaders[idx].value;
res.setHeader(headerKey, headerVal);
}
res.writeHead(httpReception.responseStatus, httpReception.reasonPhrase);
res.end(httpReception.body);
})
.catch(err => {
console.log(err);
res.send("Error occured " + err)
});
});
}
//handle common download request
function handleDownload(req, res, isRequestDownload) {
HttpDump.findById({
_id: req.params.id
})
.then(httpdmp => {
var buffer;
if(isRequestDownload){
buffer = httpdmp.body;
}else{
buffer = httpdmp.responseBody;
}
//write response payload to temp location
const randomFileName = crypto.randomBytes(16).toString("hex");
const filePath = envUtils.getTempDir() + randomFileName;
fs.writeFile(filePath, buffer, (err) => {
if (err) {
res.send("Unable to write file");
}
else {
//download written file
res.download(filePath, randomFileName, (err) => {
if (err) {
res.send("Unable to download file!");
}
//try to delete temp file
fs.unlinkSync(filePath);
});
}
});
})
.catch(err => {
res.send("Error when retrieving http dump");
});
}
module.exports = router;