<commit_before>/*
 * router.js
 *
 * Copyright (C) 2009-13 by RStudio, Inc.
 *
 * This program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var url = require('url');
var util = require('util');
var Q = require('q');
var _ = require('underscore');
var qutil = require('../core/qutil');
var posix = require('../../build/Release/posix');
var AppSpec = require('../worker/app-spec');

var regexp_quote = require('regexp-quote');

exports.NullRouter = NullRouter;
function NullRouter() {
}
(function() {
  this.getAppSpec_p = function(req, res) {
    return Q.resolve(null);
  };
}).call(NullRouter.prototype);

exports.DummyRouter = DummyRouter;
function DummyRouter(appDir, runAs) {
  this.appSpec = new AppSpec(appDir, runAs, '', null, {});
}

(function() {
  /**
   * Possible input values:
   * - In the case of normal HTTP request, req is a ServerRequest and res is a
   *   ServerResponse
   * - In the case of a SockJS request, req is a SockJS connection object and
   *   res is undefined
   *
   * Possible return values:
   * - An AppSpec object means to launch/reuse the specified app and proxy the
   *   request to it
   * - true means we already fully responded to the request, no other action is
   *   necessary (such as performing a redirect, for example)
   * - Falsy values means this router doesn't know how to handle this
   *   particular request
   */
  this.getAppSpec_p = function(req, res) {
    return Q.resolve(this.appSpec);
  };
}).call(DummyRouter.prototype);


exports.IndirectRouter = IndirectRouter;
/**
 * A router that wraps another router, where the latter can be replaced at any
 * time.
 */
function IndirectRouter(router) {
  this.$router = router;
}
(function() {
  this.getAppSpec_p = function(req, res) {
    return this.$router.getAppSpec_p.apply(this.$router, arguments);
  };
  this.setRouter = function(router) {
    this.$router = router;
  };
}).call(IndirectRouter.prototype);


// Router that annotates the AppSpec with $APPDIR/restart.txt timestamp,
// if the file exists. This makes it possible for app developers and
// admins to use "touch restart.txt" to force an app to start a fresh
// instance on subsequent loads.
exports.RestartRouter = RestartRouter;
function RestartRouter(router) {
  this.$router = router;
}
(function() {
  this.getAppSpec_p = function(req, res) {
    return this.$router.getAppSpec_p.apply(this.$router, arguments)
    .then(function(appSpec) {
      if (appSpec) {
        return Q.nfcall(fs.stat, path.join(appSpec.appDir, 'restart.txt'))
        .then(
          function(stat) {
            appSpec.settings.restart = stat.mtime.getTime();
            return appSpec;
          },
          function(err) {
            return appSpec;
          }<commit_msg>Fix crash when serving static file on Node 0.10.x<commit_after>/*
 * router.js
 *
 * Copyright (C) 2009-13 by RStudio, Inc.
 *
 * This program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var url = require('url');
var util = require('util');
var Q = require('q');
var _ = require('underscore');
var qutil = require('../core/qutil');
var posix = require('../../build/Release/posix');
var AppSpec = require('../worker/app-spec');

var regexp_quote = require('regexp-quote');

exports.NullRouter = NullRouter;
function NullRouter() {
}
(function() {
  this.getAppSpec_p = function(req, res) {
    return Q.resolve(null);
  };
}).call(NullRouter.prototype);

exports.DummyRouter = DummyRouter;
function DummyRouter(appDir, runAs) {
  this.appSpec = new AppSpec(appDir, runAs, '', null, {});
}

(function() {
  /**
   * Possible input values:
   * - In the case of normal HTTP request, req is a ServerRequest and res is a
   *   ServerResponse
   * - In the case of a SockJS request, req is a SockJS connection object and
   *   res is undefined
   *
   * Possible return values:
   * - An AppSpec object means to launch/reuse the specified app and proxy the
   *   request to it
   * - true means we already fully responded to the request, no other action is
   *   necessary (such as performing a redirect, for example)
   * - Falsy values means this router doesn't know how to handle this
   *   particular request
   */
  this.getAppSpec_p = function(req, res) {
    return Q.resolve(this.appSpec);
  };
}).call(DummyRouter.prototype);


exports.IndirectRouter = IndirectRouter;
/**
 * A router that wraps another router, where the latter can be replaced at any
 * time.
 */
function IndirectRouter(router) {
  this.$router = router;
}
(function() {
  this.getAppSpec_p = function(req, res) {
    return this.$router.getAppSpec_p.apply(this.$router, arguments);
  };
  this.setRouter = function(router) {
    this.$router = router;
  };
}).call(IndirectRouter.prototype);


// Router that annotates the AppSpec with $APPDIR/restart.txt timestamp,
// if the file exists. This makes it possible for app developers and
// admins to use "touch restart.txt" to force an app to start a fresh
// instance on subsequent loads.
exports.RestartRouter = RestartRouter;
function RestartRouter(router) {
  this.$router = router;
}
(function() {
  this.getAppSpec_p = function(req, res) {
    return this.$router.getAppSpec_p.apply(this.$router, arguments)
    .then(function(appSpec) {
      if (appSpec && typeof(appSpec) === 'object') {
        return Q.nfcall(fs.stat, path.join(appSpec.appDir, 'restart.txt'))
        .then(
          function(stat) {
            appSpec.settings.restart = stat.mtime.getTime();
            return appSpec;
          },
          function(err) {
            return appSpec;
          }
{'fraction_non_alphanumeric': 0.12231691919191919, 'fraction_numerical': 0.0033143939393939395, 'mean_word_length': 3.3553264604810997, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5176054', 'n_tokens_mistral': 1936, 'n_tokens_neox': 1898, 'n_words': 618}