mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[minor] Strip trailing whitespace.
This commit is contained in:
parent
59b71c033f
commit
7fc39d77f4
@ -82,7 +82,7 @@ exports.createServer = function () {
|
||||
case 'function': callback = arg; handlers.push(callback); break;
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
//
|
||||
// Helper function to create intelligent error message(s)
|
||||
// for the very liberal arguments parsing performed by
|
||||
@ -91,32 +91,32 @@ exports.createServer = function () {
|
||||
function validArguments() {
|
||||
var conditions = {
|
||||
'port and host': function () {
|
||||
return port && host;
|
||||
return port && host;
|
||||
},
|
||||
'options.target or options.router': function () {
|
||||
return options && (options.router ||
|
||||
return options && (options.router ||
|
||||
(options.target && options.target.host && options.target.port));
|
||||
},
|
||||
'or proxy handlers': function () {
|
||||
return handlers && handlers.length;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var missing = Object.keys(conditions).filter(function (name) {
|
||||
return !conditions[name]();
|
||||
});
|
||||
|
||||
|
||||
if (missing.length === 3) {
|
||||
message = 'Cannot proxy without ' + missing.join(', ');
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!validArguments()) {
|
||||
//
|
||||
// If `host`, `port` and `options` are all not passed (with valid
|
||||
// If `host`, `port` and `options` are all not passed (with valid
|
||||
// options) then this server is improperly configured.
|
||||
//
|
||||
throw new Error(message);
|
||||
@ -131,7 +131,7 @@ exports.createServer = function () {
|
||||
options.target = options.target || {};
|
||||
options.target.port = options.target.port || port;
|
||||
options.target.host = options.target.host || host;
|
||||
|
||||
|
||||
if (options.target && options.target.host && options.target.port) {
|
||||
//
|
||||
// If an explicit `host` and `port` combination has been passed
|
||||
@ -149,31 +149,31 @@ exports.createServer = function () {
|
||||
// we have to assume that this is a "go-anywhere" Proxy (i.e. a `RoutingProxy`).
|
||||
//
|
||||
proxy = new RoutingProxy(options);
|
||||
|
||||
|
||||
if (options.router) {
|
||||
//
|
||||
// If a routing table has been supplied than we assume
|
||||
// If a routing table has been supplied than we assume
|
||||
// the user intends us to add the "proxy" middleware layer
|
||||
// for them
|
||||
// for them
|
||||
//
|
||||
handlers.push(function (req, res) {
|
||||
proxy.proxyRequest(req, res);
|
||||
});
|
||||
|
||||
|
||||
proxy.on('routes', function (routes) {
|
||||
server.emit('routes', routes);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Create the `http[s].Server` instance which will use
|
||||
// an instance of `httpProxy.HttpProxy`.
|
||||
//
|
||||
handler = handlers.length > 1
|
||||
handler = handlers.length > 1
|
||||
? exports.stack(handlers, proxy)
|
||||
: function (req, res) { handlers[0](req, res, proxy) };
|
||||
|
||||
|
||||
server = options.https
|
||||
? https.createServer(options.https, handler)
|
||||
: http.createServer(handler);
|
||||
@ -185,8 +185,8 @@ exports.createServer = function () {
|
||||
if (!callback) {
|
||||
//
|
||||
// If an explicit callback has not been supplied then
|
||||
// automagically proxy the request using the `HttpProxy`
|
||||
// instance we have created.
|
||||
// automagically proxy the request using the `HttpProxy`
|
||||
// instance we have created.
|
||||
//
|
||||
server.on('upgrade', function (req, socket, head) {
|
||||
proxy.proxyWebSocketRequest(req, socket, head);
|
||||
@ -223,7 +223,7 @@ exports.createServer = function () {
|
||||
//
|
||||
exports.buffer = function (obj) {
|
||||
var events = [],
|
||||
onData,
|
||||
onData,
|
||||
onEnd;
|
||||
|
||||
obj.on('data', onData = function (data, encoding) {
|
||||
@ -244,7 +244,7 @@ exports.buffer = function (obj) {
|
||||
this.resume = function () {
|
||||
console.error("Cannot resume buffer after destroying it.");
|
||||
};
|
||||
|
||||
|
||||
onData = onEnd = events = obj = null;
|
||||
},
|
||||
resume: function () {
|
||||
@ -279,10 +279,10 @@ exports.setMaxSockets = function (value) {
|
||||
//
|
||||
// ### function stack (middlewares, proxy)
|
||||
// #### @middlewares {Array} Array of functions to stack.
|
||||
// #### @proxy {HttpProxy|RoutingProxy} Proxy instance to
|
||||
// #### @proxy {HttpProxy|RoutingProxy} Proxy instance to
|
||||
// Iteratively build up a single handler to the `http.Server`
|
||||
// `request` event (i.e. `function (req, res)`) by wrapping
|
||||
// each middleware `layer` into a `child` middleware which
|
||||
// each middleware `layer` into a `child` middleware which
|
||||
// is in invoked by the parent (i.e. predecessor in the Array).
|
||||
//
|
||||
// adapted from https://github.com/creationix/stack
|
||||
@ -296,17 +296,17 @@ exports.stack = function stack (middlewares, proxy) {
|
||||
if (err) {
|
||||
if (res._headerSent) {
|
||||
res.destroy();
|
||||
}
|
||||
}
|
||||
else {
|
||||
res.statusCode = 500;
|
||||
res.setHeader('Content-Type', 'text/plain');
|
||||
res.end('Internal Server Error');
|
||||
}
|
||||
|
||||
|
||||
console.error('Error in middleware(s): %s', err.stack);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (child) {
|
||||
child(req, res);
|
||||
}
|
||||
@ -345,7 +345,7 @@ exports._getAgent = function _getAgent (options) {
|
||||
if (!options || !options.host) {
|
||||
throw new Error('`options.host` is required to create an Agent.');
|
||||
}
|
||||
|
||||
|
||||
if (!options.port) {
|
||||
options.port = options.https ? 443 : 80;
|
||||
}
|
||||
@ -364,8 +364,8 @@ exports._getAgent = function _getAgent (options) {
|
||||
//
|
||||
// ### function _getProtocol (options)
|
||||
// #### @options {Object} Options for the proxy target.
|
||||
// Returns the appropriate node.js core protocol module (i.e. `http` or `https`)
|
||||
// based on the `options` supplied.
|
||||
// Returns the appropriate node.js core protocol module (i.e. `http` or `https`)
|
||||
// based on the `options` supplied.
|
||||
//
|
||||
exports._getProtocol = function _getProtocol (options) {
|
||||
return options.https ? https : http;
|
||||
@ -381,7 +381,7 @@ exports._getProtocol = function _getProtocol (options) {
|
||||
//
|
||||
exports._getBase = function _getBase (options) {
|
||||
var result = function () {};
|
||||
|
||||
|
||||
if (options.https && typeof options.https === 'object') {
|
||||
['ca', 'cert', 'key'].forEach(function (key) {
|
||||
if (options.https[key]) {
|
||||
|
||||
@ -239,7 +239,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
|
||||
if (this.changeOrigin) {
|
||||
outgoing.headers.host = this.target.host + ':' + this.target.port;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Open new HTTP request to internal resource with will act
|
||||
// as a reverse proxy pass
|
||||
@ -366,7 +366,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
|
||||
socket.setTimeout(self.timeout);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
//
|
||||
// Handle 'error' events from the `req` (e.g. `Parse Error`).
|
||||
//
|
||||
@ -448,7 +448,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, upgradeHead,
|
||||
CRLF = '\r\n',
|
||||
//copy upgradeHead to avoid retention of large slab buffers used in node core
|
||||
head = new Buffer(upgradeHead.length);
|
||||
upgradeHead.copy(head);
|
||||
upgradeHead.copy(head);
|
||||
|
||||
//
|
||||
// WebSocket requests must have the `GET` method and
|
||||
@ -615,16 +615,16 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, upgradeHead,
|
||||
detach();
|
||||
|
||||
// Emit the `end` event now that we have completed proxying
|
||||
self.emit('websocket:end', req, socket, head);
|
||||
self.emit('websocket:end', req, socket, head);
|
||||
}
|
||||
|
||||
//
|
||||
// If the `reverseProxy` socket closes, then detach all
|
||||
// event listeners.
|
||||
//
|
||||
//
|
||||
listeners.onOutgoingClose = function () {
|
||||
proxySocket.destroy();
|
||||
detach();
|
||||
detach();
|
||||
}
|
||||
|
||||
proxySocket.on('end', listeners.onIncomingClose);
|
||||
@ -698,7 +698,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, upgradeHead,
|
||||
socket: socket,
|
||||
head: head
|
||||
};
|
||||
|
||||
|
||||
//
|
||||
// Here we set the handshake `headers` and `statusCode` data to the outgoing
|
||||
// request so that we can reuse this data later.
|
||||
@ -724,7 +724,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, upgradeHead,
|
||||
headers: res.headers,
|
||||
statusCode: res.statusCode,
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Prepare the socket for the reverseProxy request and begin to
|
||||
// stream data between the two sockets. Here it is important to
|
||||
@ -756,7 +756,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, upgradeHead,
|
||||
|
||||
headers = headers.concat('', '').join('\r\n');
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Ok, kind of harmfull part of code. Socket.IO sends a hash
|
||||
// at the end of handshake if protocol === 76, but we need
|
||||
|
||||
@ -213,9 +213,9 @@ ProxyTable.prototype.getProxyLocation = function (req) {
|
||||
var target = req.url;
|
||||
for (var i in this.routes) {
|
||||
var route = this.routes[i];
|
||||
//
|
||||
//
|
||||
// If we are matching pathname only, we remove the matched pattern.
|
||||
//
|
||||
//
|
||||
// IE /wiki/heartbeat
|
||||
// is redirected to
|
||||
// /heartbeat
|
||||
|
||||
@ -178,7 +178,7 @@ RoutingProxy.prototype.close = function () {
|
||||
//
|
||||
RoutingProxy.prototype.proxyRequest = function (req, res, options) {
|
||||
options = options || {};
|
||||
|
||||
|
||||
var location;
|
||||
|
||||
//
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user