arnaucube
arnaucube/socketioMEANseedPublic Self-hosted repository
socketioMEANseed/www/node_modules/union/lib/core.js
www/node_modules/union/lib/core.jsRaw
  1/*
  2 * core.js: Core functionality for the Flatiron HTTP (with SPDY support) plugin.
  3 *
  4 * (C) 2011, Charlie Robbins & the Contributors
  5 * MIT LICENSE
  6 *
  7 */
  8
  9var http = require('http'),
 10    https = require('https'),
 11    fs = require('fs'),
 12    stream = require('stream'),
 13    HttpStream = require('./http-stream'),
 14    RoutingStream = require('./routing-stream');
 15
 16var core = exports;
 17
 18core.createServer = function (options) {
 19  var isArray = Array.isArray(options.after),
 20      credentials;
 21
 22  if (!options) {
 23    throw new Error('options is required to create a server');
 24  }
 25
 26  function requestHandler(req, res) {
 27    var routingStream = new RoutingStream({
 28      before: options.before,
 29      buffer: options.buffer,
 30      //
 31      // Remark: without new after is a huge memory leak that
 32      // pipes to every single open connection
 33      //
 34      after: isArray && options.after.map(function (After) {
 35        return new After;
 36      }),
 37      request: req,
 38      response: res,
 39      limit: options.limit,
 40      headers: options.headers
 41    });
 42
 43    routingStream.on('error', function (err) {
 44      var fn = options.onError || core.errorHandler;
 45      fn(err, routingStream, routingStream.target, function () {
 46        routingStream.target.emit('next');
 47      });
 48    });
 49
 50    req.pipe(routingStream);
 51  }
 52
 53  //
 54  // both https and spdy requires same params
 55  //
 56  if (options.https || options.spdy) {
 57    if (options.https && options.spdy) {
 58      throw new Error('You shouldn\'t be using https and spdy simultaneously.');
 59    }
 60
 61    var serverOptions,
 62        credentials,
 63        key = !options.spdy
 64          ? 'https'
 65          : 'spdy';
 66
 67    serverOptions = options[key];
 68    if (!serverOptions.key || !serverOptions.cert) {
 69      throw new Error('Both options.' + key + '.`key` and options.' + key + '.`cert` are required.');
 70    }
 71
 72    credentials = {
 73      key:  fs.readFileSync(serverOptions.key),
 74      cert: fs.readFileSync(serverOptions.cert)
 75    };
 76
 77    if (serverOptions.ca) {
 78      serverOptions.ca = !Array.isArray(serverOptions.ca)
 79        ? [serverOptions.ca]
 80        : serverOptions.ca
 81
 82      credentials.ca = serverOptions.ca.map(function (ca) {
 83        return fs.readFileSync(ca);
 84      });
 85    }
 86
 87    if (options.spdy) {
 88      // spdy is optional so we require module here rather than on top
 89      var spdy = require('spdy');
 90      return spdy.createServer(credentials, requestHandler);
 91    }
 92
 93    return https.createServer(credentials, requestHandler);
 94  }
 95
 96  return http.createServer(requestHandler);
 97};
 98
 99core.errorHandler = function error(err, req, res) {
100  if (err) {
101    (this.res || res).writeHead(err.status || 500, err.headers || { "Content-Type": "text/plain" });
102    (this.res || res).end(err.message + "\n");
103    return;
104  }
105
106  (this.res || res).writeHead(404, {"Content-Type": "text/plain"});
107  (this.res || res).end("Not Found\n");
108};