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