<commit_before>let http = require("http"),
  path = require('path'),
  url = require('url'),
  fs = require('fs'),
  React = require("react"),
  Router = require("react-router"),
  routes = require("./routes.js"),
  models = require("./models.js"),
  lookupContentType = require('mime-types').contentType,
  git = require('git-rev'),
  Cookies = require("cookies"),
  qs = require('querystring');

const jspath = "/js/",
  csspath = "/css/",
  modelspath = "/data/",
  contentpath = "/content/",
  imgpath = "/img/",
  favicopath = "/favicon.ico",
  loginpath = "/api/login",
  registerpath = "/api/register",
  contentType = "Content-type",
  notFound = "Not Found",
  doctype = "<!DOCTYPE html>",
  footer = "</body></html>",
  script = `
function gotData(Handler, data) {
  data.linkify = linkify;
  routes.setGitRevision(data.gitRevision);
  React.render(React.createElement(Handler, data), document);
}

Router.run(routes.routes, Router.HistoryLocation, function (Handler, state) {
  if (cachedData) {
    var _d = cachedData;
    cachedData = null;
    gotData(Handler, _d);
    return;
  }

  var appnames = [],
    app = null;
  for (var i in state.routes) {
    if (!!state.routes[i].name) {
      appnames.push(state.routes[i].name);
      if (app === null) {
        app = state.routes[i];
      }
    }
  }
  if (!appnames.length) {
    console.error("Error: No app was routed");
    return;
  }

  console.log("Route to app", appnames[0]);

  if (appnames[0] === "shot") {
    var key = state.params.shotId + "/" + state.params.shotDomain,
      xhr = new XMLHttpRequest();

    xhr.onload = function () {
      if (xhr.status === 200) {
        var data = JSON.parse(xhr.responseText);
        gotData(Handler, {backend: location.origin, id: key, shot: data});
      } else {
        console.error("Error: Bad response: ", xhr.status, xhr.responseText);
      }
    };

    xhr.open("GET", "/data/" + key);
    xhr.send();
  }
});
`;

let server = http.createServer(function (req, res) {
  let cookies = new Cookies(req, res, models.keys);
  req.userId = cookies.get("userId", {signed: true});
  let parsed = url.parse(req.url, true),
    pth = parsed.pathname,
    query = parsed.query;

  console.log(req.method, pth, query);

  if (req.method === "GET" && (pth.startsWith(jspath) || pth.startsWith(csspath) || pth.startsWith(imgpath) || pth === favicopath)) {
    let filename = path.join(__dirname, pth);
    fs.exists(filename, function(exists) {
      if (exists) {
        res.setHeader(contentType, lookupContentType(path.extname(filename)));
        fs.createReadStream(filename).pipe(res);
      } else {
        res.writeHead(404); res.end(notFound);
      }
    });
    return;
  }
  let modelname = "",
    storeMap = null;

  if (pth.startsWith(registerpath)) {
    return handleRegister(req, res);
  } else if (pth.startsWith(loginpath)) {
    return handleLogin(req, res);
  }

  if (pth.startsWith(modelspath)) {
    modelname = pth.slice(modelspath.length);
    storeMap = models.modelMap;
  }

  if (modelname) {
    if (req.method === "PUT") {
      let body = "";
      req.on('data', function(chunk) {
        body = body + chunk;
      });
      req.on('end', function() {
        if (body.length && body[0] === "{") {
          let bodyObj = JSON.parse(body);
          if (! bodyObj.userId) {
            simpleResponse(res, "No userId in body", 400);
            return;
          }
          if (! req.userId) {
            simpleResponse(res, "Not logged in", 401);
            return;
          }
          if (req.userId != bodyObj.userId) {
            // FIXME: this doesn't make sense for comments or other stuff, see https://github.com/mozilla-services/pageshot/issues/245
            simpleResponse(res, "Cannot save a page on behalf of another user", 403);
          }
          // FIXME: this needs to confirm that the userid doesn't change on an
          // update; right now anyone can overwrite a shot so long as they
          // update the userId appropriately:
          storeMap.put(modelname, {value: body, userId: req.userId})
            .then(() => res.end())
            .catch((err) => {
              errorResponse(res, "Error saving object:", err);
            });
        } else {
          res.writeHead(400);
          res.end("Bad Request");
        }
      });
    } else if (req.method === 'GET') {
      storeMap.get(modelname, ["value"]).then((val) => {
        val = val && val.value;
        if (val === null) {
          res.writeHead(404);
          res.end(notFound);
        } else {
          res.end(val);
        }
      });
    } else {
      res.writeHead(405);
      res.end("Method Not Allowed");
    }
    return;
  }

  if (pth.startsWith(contentpath)) {
    let key = pth.slice(contentpath.length);
    models.modelMap.get(key).then(
      data => {
        let parsed = JSON.parse(data);
        res.writeHead(200);
        res.end('<!DOCTYPE html><head><script src="/js/content-helper.js"></script>' +
          parsed.head +
          '</head><body style="background-color: white; color: black; text-align: left">' +
          parsed.body +
          '</body></html>');
      }
    ).catch(function (e) {
      console.log("Error:", e.stack);
      res.setHeader(contentType, "text/plain; charset=utf-8");
      res.writeHead(500);
      if (e.code == "ECONNREFUSED") {
        // Database connection error
        res.end("Cannot connect to database");
      } else {
        res.end(e.stack);
      }<commit_msg>Fix bustage introduced by the user login merge.<commit_after>let http = require("http"),
  path = require('path'),
  url = require('url'),
  fs = require('fs'),
  React = require("react"),
  Router = require("react-router"),
  routes = require("./routes.js"),
  models = require("./models.js"),
  lookupContentType = require('mime-types').contentType,
  git = require('git-rev'),
  Cookies = require("cookies"),
  qs = require('querystring');

const jspath = "/js/",
  csspath = "/css/",
  modelspath = "/data/",
  contentpath = "/content/",
  imgpath = "/img/",
  favicopath = "/favicon.ico",
  loginpath = "/api/login",
  registerpath = "/api/register",
  contentType = "Content-type",
  notFound = "Not Found",
  doctype = "<!DOCTYPE html>",
  footer = "</body></html>",
  script = `
function gotData(Handler, data) {
  data.linkify = linkify;
  routes.setGitRevision(data.gitRevision);
  React.render(React.createElement(Handler, data), document);
}

Router.run(routes.routes, Router.HistoryLocation, function (Handler, state) {
  if (cachedData) {
    var _d = cachedData;
    cachedData = null;
    gotData(Handler, _d);
    return;
  }

  var appnames = [],
    app = null;
  for (var i in state.routes) {
    if (!!state.routes[i].name) {
      appnames.push(state.routes[i].name);
      if (app === null) {
        app = state.routes[i];
      }
    }
  }
  if (!appnames.length) {
    console.error("Error: No app was routed");
    return;
  }

  console.log("Route to app", appnames[0]);

  if (appnames[0] === "shot") {
    var key = state.params.shotId + "/" + state.params.shotDomain,
      xhr = new XMLHttpRequest();

    xhr.onload = function () {
      if (xhr.status === 200) {
        var data = JSON.parse(xhr.responseText);
        gotData(Handler, {backend: location.origin, id: key, shot: data});
      } else {
        console.error("Error: Bad response: ", xhr.status, xhr.responseText);
      }
    };

    xhr.open("GET", "/data/" + key);
    xhr.send();
  }
});
`;

let server = http.createServer(function (req, res) {
  let cookies = new Cookies(req, res, models.keys);
  req.userId = cookies.get("user", {signed: true});
  let parsed = url.parse(req.url, true),
    pth = parsed.pathname,
    query = parsed.query;

  console.log(req.method, pth, query);

  if (req.method === "GET" && (pth.startsWith(jspath) || pth.startsWith(csspath) || pth.startsWith(imgpath) || pth === favicopath)) {
    let filename = path.join(__dirname, pth);
    fs.exists(filename, function(exists) {
      if (exists) {
        res.setHeader(contentType, lookupContentType(path.extname(filename)));
        fs.createReadStream(filename).pipe(res);
      } else {
        res.writeHead(404); res.end(notFound);
      }
    });
    return;
  }
  let modelname = "",
    storeMap = null;

  if (pth.startsWith(registerpath)) {
    return handleRegister(req, res);
  } else if (pth.startsWith(loginpath)) {
    return handleLogin(req, res);
  }

  if (pth.startsWith(modelspath)) {
    modelname = pth.slice(modelspath.length);
    storeMap = models.modelMap;
  }

  if (modelname) {
    if (req.method === "PUT") {
      let body = "";
      req.on('data', function(chunk) {
        body = body + chunk;
      });
      req.on('end', function() {
        if (body.length && body[0] === "{") {
          let bodyObj = JSON.parse(body);
          if (! bodyObj.userId) {
            simpleResponse(res, "No userId in body", 400);
            return;
          }
          if (! req.userId) {
            simpleResponse(res, "Not logged in", 401);
            return;
          }
          if (req.userId != bodyObj.userId) {
            // FIXME: this doesn't make sense for comments or other stuff, see https://github.com/mozilla-services/pageshot/issues/245
            simpleResponse(res, "Cannot save a page on behalf of another user", 403);
          }
          // FIXME: this needs to confirm that the userid doesn't change on an
          // update; right now anyone can overwrite a shot so long as they
          // update the userId appropriately:
          storeMap.put(modelname, {value: body, userid: req.userId})
            .then(() => res.end())
            .catch((err) => {
              errorResponse(res, "Error saving object:", err);
            });
        } else {
          res.writeHead(400);
          res.end("Bad Request");
        }
      });
    } else if (req.method === 'GET') {
      storeMap.get(modelname, ["value"]).then((val) => {
        val = val && val.value;
        if (val === null) {
          res.writeHead(404);
          res.end(notFound);
        } else {
          res.end(val);
        }
      });
    } else {
      res.writeHead(405);
      res.end("Method Not Allowed");
    }
    return;
  }

  if (pth.startsWith(contentpath)) {
    let key = pth.slice(contentpath.length);
    models.modelMap.get(key, ["value"]).then(
      data => {
        if (data && data.value) {
          let parsed = JSON.parse(data.value);
          res.writeHead(200);
          res.end('<!DOCTYPE html><head><script src="/js/content-helper.js"></script>' +
            parsed.head +
            '</head><body style="background-color: white; color: black; text-align: left">' +
            parsed.body +
            '</body></html>');
        } else {
          res.writeHead(404);
          res.end("Not Found");
        }
      }
    ).catch(function (e) {
      console.log("Error:", e.stack);
      res.setHeader(contentType, "text/plain; charset=utf-8");
      res.writeHead(500);
      if (e.code == "ECONNREFUSED") {
        // Database connection error
        res.end("Cannot connect to database");
      } else {
        res.end(e.stack);
      }
{'fraction_non_alphanumeric': 0.16639885734690235, 'fraction_numerical': 0.006873772540617747, 'mean_word_length': 2.364264264264264, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 33, 'https://': 2, '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': '1508525', 'n_tokens_mistral': 3653, 'n_tokens_neox': 3599, 'n_words': 787}