/usr/share/passenger/helper-scripts/node-loader.js is in ruby-passenger 4.0.37-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | /*
* Phusion Passenger - https://www.phusionpassenger.com/
* Copyright (c) 2010-2013 Phusion
*
* "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
module.paths.unshift(__dirname + "/../node_lib");
var EventEmitter = require('events').EventEmitter;
var net = require('net');
var http = require('http');
var LineReader = require('phusion_passenger/line_reader').LineReader;
var RequestHandler = require('phusion_passenger/request_handler').RequestHandler;
var HttplibEmulation = require('phusion_passenger/httplib_emulation');
module.isApplicationLoader = true; // https://groups.google.com/forum/#!topic/compoundjs/4txxkNtROQg
GLOBAL.PhusionPassenger = exports.PhusionPassenger = new EventEmitter();
var stdinReader = new LineReader(process.stdin);
beginHandshake();
readInitializationHeader();
function beginHandshake() {
process.stdout.write("!> I have control 1.0\n");
}
function readInitializationHeader() {
stdinReader.readLine(function(line) {
if (line != "You have control 1.0\n") {
console.error('Invalid initialization header');
process.exit(1);
} else {
readOptions();
}
});
}
function readOptions() {
var options = {};
function readNextOption() {
stdinReader.readLine(function(line) {
if (line == "\n") {
setupEnvironment(options);
} else if (line == "") {
console.error("End of stream encountered while reading initialization options");
process.exit(1);
} else {
var matches = line.replace(/\n/, '').match(/(.*?) *: *(.*)/);
options[matches[1]] = matches[2];
readNextOption();
}
});
}
readNextOption();
}
function setupEnvironment(options) {
PhusionPassenger.options = options;
PhusionPassenger.configure = configure;
PhusionPassenger._requestHandler = new RequestHandler(loadApplication);
PhusionPassenger._appInstalled = false;
process.title = 'Passenger NodeApp: ' + options.app_root;
http.Server.prototype.originalListen = http.Server.prototype.listen;
http.Server.prototype.listen = installServer;
stdinReader.close();
stdinReader = undefined;
process.stdin.on('end', shutdown);
process.stdin.resume();
}
/**
* PhusionPassenger.configure(options)
*
* Configures Phusion Passenger's behavior inside this Node application.
*
* Options:
* autoInstall (boolean, default true)
* Whether to install the first HttpServer object for which listen() is called,
* as the Phusion Passenger request handler.
*/
function configure(_options) {
var options = {
autoInstall: true
};
for (var key in _options) {
options[key] = _options[key];
}
if (!options.autoInstall) {
http.Server.prototype.listen = listenAndMaybeInstall;
}
}
function loadApplication() {
var startupFile = PhusionPassenger.options.startup_file || 'app.js';
require(PhusionPassenger.options.app_root + '/' + startupFile);
}
function extractCallback(args) {
if (args.length > 1 && typeof(args[args.length - 1]) == 'function') {
return args[args.length - 1];
}
}
function installServer() {
var server = this;
if (!PhusionPassenger._appInstalled) {
PhusionPassenger._appInstalled = true;
PhusionPassenger._server = server;
server.address = function() {
return 'passenger';
}
finalizeStartup();
PhusionPassenger.on('request', function(headers, socket, bodyBegin) {
var req, res;
if (headers['HTTP_UPGRADE']) {
if (EventEmitter.listenerCount(server, 'upgrade') > 0) {
req = HttplibEmulation.createIncomingMessage(headers, socket, bodyBegin);
server.emit('upgrade', req, socket, bodyBegin);
} else {
socket.destroy();
}
} else {
req = HttplibEmulation.createIncomingMessage(headers, socket, bodyBegin);
res = HttplibEmulation.createServerResponse(req);
server.emit('request', req, res);
}
});
var callback = extractCallback(arguments);
if (callback) {
server.once('listening', callback);
}
server.emit('listening');
return server;
} else {
throw new Error("http.Server.listen() was called more than once, which " +
"is not allowed because Phusion Passenger is in auto-install mode. " +
"This means that the first http.Server object for which listen() is called, " +
"is automatically installed as the Phusion Passenger request handler. " +
"If you want to create and listen on multiple http.Server object then " +
"you should disable auto-install mode. Please read " +
"http://stackoverflow.com/questions/20645231/phusion-passenger-error-http-server-listen-was-called-more-than-once/20645549");
}
}
function listenAndMaybeInstall(port) {
if (port === 'passenger') {
if (!PhusionPassenger._appInstalled) {
return installServer.apply(this, arguments);
} else {
throw new Error("You may only call listen('passenger') once. Please read http://stackoverflow.com/questions/20645231/phusion-passenger-error-http-server-listen-was-called-more-than-once/20645549");
}
} else {
return this.originalListen.apply(this, arguments);
}
}
function finalizeStartup() {
process.stdout.write("!> Ready\n");
process.stdout.write("!> socket: main;tcp://127.0.0.1:" +
PhusionPassenger._requestHandler.server.address().port +
";session;0\n");
process.stdout.write("!> \n");
}
function shutdown() {
if (PhusionPassenger.listeners('exit').length == 0) {
process.exit(0);
} else {
PhusionPassenger.emit('exit');
}
}
|