<commit_before>"use strict";

module.exports = (function() {

  const url = require('url');
  const Template = require('./template.js');
  const Model = require('./model.js');
  const ComposerResult = require('./composer/composer_result.js');
  const ComposerRecord = require('./composer/record.js');
  const API = require('./api.js');

  class Controller {

    constructor(request, response, middlewareManager) {

      this._middlewareManager = middlewareManager;
      this._initializeTime = (new Date()).valueOf();
      this._request = request;
      this._response = response;
      this._path = url.parse(this._request.url, true).pathname;
      this._status = null;
      this._headers = {};

    }

    allowOrigin(value) {

      this.setHeader('Access-Control-Allow-Origin', value);

    }

    request() {
      return this._request;
    }

    path() {
      return this._path;
    }

    getStatus() {
      return this._status;
    }

    status(value) {
      this._status = value | 0;
      return this._status;
    }

    setHeaders(object) {
      let keys = Object.keys(object);
      let headers = {};
      for(let i = 0, len = keys.length; i < len; i++) {
        headers[keys[i]] = object[keys[i]];
      }
      this._headers = headers;
      return headers;
    }

    setHeader(key, value) {

      key = key.split('-').map(function(v) {
        return v[0].toUpperCase() + v.substr(1);
      }).join('-');

      if (key === 'Content-Type' && value.indexOf(';') === -1 && (
        value === 'application/javascript' ||
        value === 'application/json' ||
        value.indexOf('text/') === 0
      )) {
        value = value + '; charset=utf-8';
      }

      this._headers[key] = value;
      return value;

    }

    getHeader(key, defaultValue) {
      return this._headers.hasOwnProperty(key) ? this._headers[key] : defaultValue;
    }

    badRequest(msg) {
      this.status(400);
      this.render(API.error(msg || 'Bad Request'));
      return true;
    }

    unauthorized(msg) {
      this.status(401);
      this.render(API.error(msg || 'Unauthorized'));
      return true;
    }

    notFound(msg) {
      this.status(404);
      this.render(API.error(msg || 'Not Found'));
      return true;
    }

    error(msg) {
      this.status(500);
      this.render(API.error(msg || 'Internal Server Error'));
      return true;
    }

    render(data, templateData) {

      if(!data) { data = ''; }

      if (data instanceof Buffer) {<commit_msg>Add tooManyRequests to controller methods<commit_after>"use strict";

module.exports = (function() {

  const url = require('url');
  const Template = require('./template.js');
  const Model = require('./model.js');
  const ComposerResult = require('./composer/composer_result.js');
  const ComposerRecord = require('./composer/record.js');
  const API = require('./api.js');

  class Controller {

    constructor(request, response, middlewareManager) {

      this._middlewareManager = middlewareManager;
      this._initializeTime = (new Date()).valueOf();
      this._request = request;
      this._response = response;
      this._path = url.parse(this._request.url, true).pathname;
      this._status = null;
      this._headers = {};

    }

    allowOrigin(value) {

      this.setHeader('Access-Control-Allow-Origin', value);

    }

    request() {
      return this._request;
    }

    path() {
      return this._path;
    }

    getStatus() {
      return this._status;
    }

    status(value) {
      this._status = value | 0;
      return this._status;
    }

    setHeaders(object) {
      let keys = Object.keys(object);
      let headers = {};
      for(let i = 0, len = keys.length; i < len; i++) {
        headers[keys[i]] = object[keys[i]];
      }
      this._headers = headers;
      return headers;
    }

    setHeader(key, value) {

      key = key.split('-').map(function(v) {
        return v[0].toUpperCase() + v.substr(1);
      }).join('-');

      if (key === 'Content-Type' && value.indexOf(';') === -1 && (
        value === 'application/javascript' ||
        value === 'application/json' ||
        value.indexOf('text/') === 0
      )) {
        value = value + '; charset=utf-8';
      }

      this._headers[key] = value;
      return value;

    }

    getHeader(key, defaultValue) {
      return this._headers.hasOwnProperty(key) ? this._headers[key] : defaultValue;
    }

    badRequest(msg) {
      this.status(400);
      this.render(API.error(msg || 'Bad Request'));
      return true;
    }

    unauthorized(msg) {
      this.status(401);
      this.render(API.error(msg || 'Unauthorized'));
      return true;
    }

    notFound(msg) {
      this.status(404);
      this.render(API.error(msg || 'Not Found'));
      return true;
    }

    tooManyRequests(msg) {
      this.status(429);
      this.render(API.error(msg || 'Too Many Requests'));
      return true;
    }

    error(msg) {
      this.status(500);
      this.render(API.error(msg || 'Internal Server Error'));
      return true;
    }

    render(data, templateData) {

      if(!data) { data = ''; }

      if (data instanceof Buffer) {
{'fraction_non_alphanumeric': 0.16060664981528291, 'fraction_numerical': 0.007972000777756174, 'mean_word_length': 2.3272962483829236, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5495651', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1555, 'n_words': 308}