node-http-proxy/index.js

67 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-01-25 00:54:48 +00:00
var net = require('net');
2019-01-25 03:07:42 +00:00
var http = require('http');
class HttpRequest {
constructor({...attrs}) {
this.type = attrs.type;
this.headers = attrs.headers;
this.body = attrs.body;
}
}
/**
* Parse an HTTP request.
* @param {String} bytes
*/
function parseHttpRequest(bytes) {
let httpRequestAttrs = {};
bytes.split("\r\n").map((row) => {
// Match against HTTP verbs from the first row.
if (row.match('GET|POST|PUT|PATCH|UPDATE|DELETE')) {
console.log("FIRST ROW");
console.log(row);
let typeInfo = row.split(" ");
if (typeInfo.length !== 3) {
console.log("Bad HTTP request");
return null;
}
httpRequestAttrs["type"] = typeInfo[0];
httpRequestAttrs["path"] = typeInfo[1];
httpRequestAttrs["httpVersion"] = typeInfo[2];
} else {
console.log(row);
}
})
}
2019-01-25 00:54:48 +00:00
2019-01-25 01:43:49 +00:00
/**
* Log the request and forward it.
* @param {String} bytes
*/
2019-01-25 00:54:48 +00:00
async function logAndSend(bytes) {
2019-01-25 01:43:49 +00:00
console.log(bytes)
2019-01-25 00:54:48 +00:00
}
var server = net.createServer((socket) => {
socket.on('end', (c) => {
console.log("Client disconnected");
});
2019-01-25 01:43:49 +00:00
socket.on("data", (c) => {
2019-01-25 03:07:42 +00:00
parseHttpRequest(c.toString());
2019-01-25 01:43:49 +00:00
});
2019-01-25 00:54:48 +00:00
})
2019-01-25 01:43:49 +00:00
server.on('connection', (c) => {
console.log("Client connected!" + c.read(10));
2019-01-25 00:54:48 +00:00
});
2019-01-25 01:43:49 +00:00
server.on("listening", (c) => {
console.log("LISTENING");
})
2019-01-25 00:54:48 +00:00
server.listen(8124, () => {
console.log("Server bound.");
})