<commit_before>/**
 * server.js
 * This file defines the server for chirp
 */
"use strict;"

var port = 4000;

/* global variables */
var multipart = require('./lib/multipart');
var template = require('./lib/template');
var fileserver = require('./lib/fileserver');
var http = require('http');
var url = require('url');
var fs = require('fs');
var encryption = require('./lib/encryption');
var urlencoded = require('./lib/form-urlencoded');
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('chirp.sqlite3');
var parseCookie = require('./lib/cookie-parser');

/* load public directory */
fileserver.loadDir('public');

/* load templates */
template.loadDir('templates');

function serveTemplate(req, res, url) {
  res.setHeader('Content-Type', 'text/html');
  switch(url) {
    case '':
    case 'home':
    case 'index.html':
    case 'index':
      loginRequired(req, res, function(req, res) {
        res.setHeader("Location", "/home");
        res.end(template.render('index.html', req.session));
      });
      break;

    case 'login':
      res.end(template.render('login.html', req.alert));
      break;

    case 'logout':
    case 'logout.html':
      res.setHeader("Location", "/login");
      res.setHeader("Set-Cookie", ["cryptsession="]);
      res.statusCode = 302;
      res.end(template.render('login.html'));
      break;

    case 'create-account':
      res.end(template.render('create-account.html', req.alert));
      break;

    default:
      res.statusCode = 404;
      res.setHeader("Location", "/page-not-found");
      res.end(template.render('page-not-found.html'));
  }
}

function login(req, res) {
  if(req.method == 'GET') {
    res.setHeader("Location", "/login");
    serveTemplate(req, res, 'login');
  } else {
    urlencoded(req, res, function(req, res) {
      var username = req.body.username;
      db.get('SELECT * FROM users WHERE username=?',[username], function(err, user) {
        if(user) {
          console.log(user);
          var salt = user.salt;
          var cryptedPass = user.crypted_password;
          if(cryptedPass == encryption.encipher(req.body.password + salt)) {
            console.log("correct password");
            var session = {username: username};
            var sessionData = JSON.stringify(session);
            var sessionCrypt = encryption.encipher(sessionData);
            res.setHeader("Set-Cookie", ["cryptsession=" + sessionCrypt + "; session;"]);
            res.statusCode = 302;
            serveTemplate(req, res, 'index');
          } else {
            req.alert = {alert: "Invalid Username/Password"};
            res.statusCode = 302;
            serveTemplate(req, res, 'login');
          }
        } else {
          req.alert = {alert: "Invalid Username/Password"};
          res.statusCode = 302;
          serveTemplate(req, res, 'login');
        }
      });
    });
  }
}<commit_msg>fixed redirect bug<commit_after>/**
 * server.js
 * This file defines the server for chirp
 */
"use strict;"

var port = 4000;

/* global variables */
var multipart = require('./lib/multipart');
var template = require('./lib/template');
var fileserver = require('./lib/fileserver');
var http = require('http');
var url = require('url');
var fs = require('fs');
var encryption = require('./lib/encryption');
var urlencoded = require('./lib/form-urlencoded');
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('chirp.sqlite3');
var parseCookie = require('./lib/cookie-parser');

/* load public directory */
fileserver.loadDir('public');

/* load templates */
template.loadDir('templates');

function serveTemplate(req, res, url) {
  res.setHeader('Content-Type', 'text/html');
  switch(url) {
    case '':
    case 'home':
    case 'index.html':
    case 'index':
      loginRequired(req, res, function(req, res) {
        res.setHeader("Location", "/home");
        res.end(template.render('index.html', req.session));
      });
      break;

    case 'login':
      res.end(template.render('login.html', req.alert));
      break;

    case 'logout':
    case 'logout.html':
      res.setHeader("Location", "/login");
      res.setHeader("Set-Cookie", ["cryptsession="]);
      res.statusCode = 302;
      res.end(template.render('login.html'));
      break;

    case 'create-account':
      res.end(template.render('create-account.html', req.alert));
      break;

    default:
      res.statusCode = 404;
      res.setHeader("Location", "/page-not-found");
      res.end(template.render('page-not-found.html'));
  }
}

function login(req, res) {
  if(req.method == 'GET') {
    res.setHeader("Location", "/login");
    serveTemplate(req, res, 'login');
  } else {
    urlencoded(req, res, function(req, res) {
      var username = req.body.username;
      db.get('SELECT * FROM users WHERE username=?',[username], function(err, user) {
        if(user) {
          console.log(user);
          var salt = user.salt;
          var cryptedPass = user.crypted_password;
          if(cryptedPass == encryption.encipher(req.body.password + salt)) {
            console.log("correct password");
            var session = {username: username};
            var sessionData = JSON.stringify(session);
            var sessionCrypt = encryption.encipher(sessionData);
            res.setHeader("Set-Cookie", ["cryptsession=" + sessionCrypt + "; session;"]);
            res.statusCode = 302;
            res.setHeader("Location", "/home");
            res.end();
          } else {
            req.alert = {alert: "Invalid Username/Password"};
            res.statusCode = 302;
            serveTemplate(req, res, 'login');
          }
        } else {
          req.alert = {alert: "Invalid Username/Password"};
          res.statusCode = 302;
          serveTemplate(req, res, 'login');
        }
      });
    });
  }
}
{'fraction_non_alphanumeric': 0.16786447638603696, 'fraction_numerical': 0.00787132101300479, 'mean_word_length': 2.6946902654867255, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6553660', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1709, 'n_words': 372}