diff --git a/README.md b/README.md new file mode 100644 index 0000000..1d24ed3 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Node HTTP Proxy + +This package is an HTTP proxy, written in Node.js. Spun up on a virtual machine. \ No newline at end of file diff --git a/index.js b/index.js index 642c9e9..ace51f7 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,41 @@ 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. @@ -13,7 +50,7 @@ var server = net.createServer((socket) => { console.log("Client disconnected"); }); socket.on("data", (c) => { - logAndSend(c.toString()); + parseHttpRequest(c.toString()); }); })