Starting HTTP parsing

This commit is contained in:
Jacob Windle 2019-01-24 22:07:42 -05:00
parent ea45d63909
commit c7c1bb5392
2 changed files with 41 additions and 1 deletions

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# Node HTTP Proxy
This package is an HTTP proxy, written in Node.js. Spun up on a virtual machine.

View File

@ -1,4 +1,41 @@
var net = require('net'); var net = require('net');
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);
}
})
}
/** /**
* Log the request and forward it. * Log the request and forward it.
@ -13,7 +50,7 @@ var server = net.createServer((socket) => {
console.log("Client disconnected"); console.log("Client disconnected");
}); });
socket.on("data", (c) => { socket.on("data", (c) => {
logAndSend(c.toString()); parseHttpRequest(c.toString());
}); });
}) })