Skip to content

Instantly share code, notes, and snippets.

@0xTenable
Created April 10, 2016 16:36
Show Gist options
  • Save 0xTenable/bae6df0b14204ab3e0baf2b6fc026422 to your computer and use it in GitHub Desktop.
Save 0xTenable/bae6df0b14204ab3e0baf2b6fc026422 to your computer and use it in GitHub Desktop.
Testing http
var http = require('http');
var fs = require('fs');
var body = [];
var formData;
//Create an HTTP server with a callback function
//Callback calls displayForm with res passed (so response can be sent back to client)
var server = http.createServer(function (req, response) {
if (req.method.toLowerCase() == 'get' && req.url.indexOf("?") > -1) {
console.log("HERE");
processAllFieldsOfTheForm(req,response,true);
} else if (req.method.toLowerCase() == 'get' && req.url.indexOf("?") == -1) {
displayForm(response);
} else if (req.method.toLowerCase() == 'post') {
req.on('data', function(chunk) {
console.log("Chunk: " + chunk);
body.push(chunk);
}).on('end', function() {
body = Buffer.concat(body).toString();
console.log("Body: " + body);
// at this point, `body` has the entire request body stored in it as a string
});
processAllFieldsOfTheForm(req, response, false);
}
console.log("HEADER: " + req.headers);
console.log("URL: " + req.url);
});
//Displays the html form
function displayForm(res) {
//Create a callback to read the form.html file and return data back to client.
fs.readFile('nodeForm.html', function (err, data) {
res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': data.length
});
res.write(data);
res.end();
});
}
//Start server at port 1185
server.listen(1185);
console.log("server listening on 1185");
//Variables for the form data
var username = "";
var magicnum = 0;
var userpassword = "";
var outputMsg = "";
function processAllFieldsOfTheForm(request, res, doGet) {
res.on('error', function(err) { console.error(err); });
res.writeHead(200, {'Content-type': 'text/html'});
console.log("What is this: " + request);
// Get formdata
if (process.env.CONTENT_LENGTH) {
//POST
process.stdin.setEncoding('utf-8');
process.stdin.on('data', function (data) {
formData = data;
formProcess(res);
});
} else {
//GET
formData = process.env.QUERY_STRING || "";
formProcess(res);
}
}
//Returns all form data in an array of key value pairs
function getQueryVariable(query) {
var vars = query.split('&');
var queries = [];
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
queries[pair[0]]= decodeURIComponent(pair[1]);
}
return queries;
}
function formProcess(response) {
if(formData) {
var data = getQueryVariable(formData);
magicnum = data["magicnum"];
username = data["username"];
userpassword = data["userpassword"];
}
if(!username || !username.trim() || !magicnum === NaN || !userpassword.trim())
outputMsg = "<h1> No Data </h1>";
else
for (var i = 0; i < magicnum; i++)
outputMsg += "<h1>Hello " + username + " with a password of " + userpassword + "!</h1>\n"
var HTMLStart = "<!doctype html>\n<html>\n<head>\n<title>Hello World</title>\n</head>\n";
var Body = "<body>\n" + outputMsg + "</body>\n</html>";
response.write(HTMLStart + Body);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment