Starting HTTP parsing
This commit is contained in:
parent
ea45d63909
commit
c7c1bb5392
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Node HTTP Proxy
|
||||
|
||||
This package is an HTTP proxy, written in Node.js. Spun up on a virtual machine.
|
39
index.js
39
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());
|
||||
});
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user