mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[fix] more jshint intendation
This commit is contained in:
parent
0aeaba7fe6
commit
17399e7c3e
@ -18,132 +18,132 @@ web_o = Object.keys(web_o).map(function(pass) {
|
||||
|
||||
[ // <--
|
||||
|
||||
/**
|
||||
* Sets `content-length` to '0' if request is of DELETE type.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {IncomingMessage} Res Response object
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
/**
|
||||
* Sets `content-length` to '0' if request is of DELETE type.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {IncomingMessage} Res Response object
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function deleteLength(req, res, options) {
|
||||
if(req.method === 'DELETE' && !req.headers['content-length']) {
|
||||
req.headers['content-length'] = '0';
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets timeout in request socket if it was specified in options.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {IncomingMessage} Res Response object
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function timeout(req, res, options) {
|
||||
if(options.timeout) {
|
||||
req.socket.setTimeout(options.timeout);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets `x-forwarded-*` headers if specified in config.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {IncomingMessage} Res Response object
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function XHeaders(req, res, options) {
|
||||
if(!options.xfwd) return;
|
||||
|
||||
var values = {
|
||||
for : req.connection.remoteAddress || req.socket.remoteAddress,
|
||||
port : req.connection.remotePort || req.socket.remotePort,
|
||||
proto: req.isSpdy ? 'https' : (req.connection.pair ? 'https' : 'http')
|
||||
};
|
||||
|
||||
['for', 'port', 'proto'].forEach(function(header) {
|
||||
req.headers['x-forwarded-' + header] =
|
||||
(req.headers['x-forwarded-' + header] || '') +
|
||||
(req.headers['x-forwarded-' + header] ? ',' : '') +
|
||||
values[header]
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Does the actual proxying. If `forward` is enabled fires up
|
||||
* a ForwardStream, same happens for ProxyStream. The request
|
||||
* just dies otherwise.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {IncomingMessage} Res Response object
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function stream(req, res, options) {
|
||||
if(options.forward) {
|
||||
// If forward enable, so just pipe the request
|
||||
var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
|
||||
common.setupOutgoing(options.ssl || {}, options, req, 'forward')
|
||||
);
|
||||
req.pipe(forwardReq);
|
||||
return res.end();
|
||||
}
|
||||
|
||||
// Request initalization
|
||||
var proxyReq = (options.target.protocol === 'https:' ? https : http).request(
|
||||
common.setupOutgoing(options.ssl || {}, options, req)
|
||||
);
|
||||
|
||||
// Error Handler
|
||||
proxyReq.on('error', function(err){
|
||||
var ev = 'caronte:outgoing:web:';
|
||||
// If no error listeners, so throw the error.
|
||||
if (options.ee.listeners(ev + 'error').length == 0){
|
||||
throw err;
|
||||
function deleteLength(req, res, options) {
|
||||
if(req.method === 'DELETE' && !req.headers['content-length']) {
|
||||
req.headers['content-length'] = '0';
|
||||
}
|
||||
// Also emit the error events
|
||||
options.ee.emit(ev + 'error', err, req, res);
|
||||
});
|
||||
},
|
||||
|
||||
req.pipe(proxyReq);
|
||||
/**
|
||||
* Sets timeout in request socket if it was specified in options.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {IncomingMessage} Res Response object
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
proxyReq.on('response', function(proxyRes) {
|
||||
var ev = 'caronte:outgoing:web:';
|
||||
function timeout(req, res, options) {
|
||||
if(options.timeout) {
|
||||
req.socket.setTimeout(options.timeout);
|
||||
}
|
||||
},
|
||||
|
||||
options.ee.emit(ev + 'begin', req, res);
|
||||
/**
|
||||
* Sets `x-forwarded-*` headers if specified in config.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {IncomingMessage} Res Response object
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
// When the previous request respond, we apply the
|
||||
// outgoing passes to the response
|
||||
web_o.some(function(pass) {
|
||||
var evnt = ev + pass.name.toLowerCase() + ':';
|
||||
function XHeaders(req, res, options) {
|
||||
if(!options.xfwd) return;
|
||||
|
||||
options.ee.emit(evnt + 'begin', req, res);
|
||||
// Call the pass with the proxy response
|
||||
// pass(ClientRequest, IncomingMessage, proxyResponse)
|
||||
var val = pass(req, res, proxyRes);
|
||||
options.ee.emit(evnt + 'end');
|
||||
var values = {
|
||||
for : req.connection.remoteAddress || req.socket.remoteAddress,
|
||||
port : req.connection.remotePort || req.socket.remotePort,
|
||||
proto: req.isSpdy ? 'https' : (req.connection.pair ? 'https' : 'http')
|
||||
};
|
||||
|
||||
return val;
|
||||
['for', 'port', 'proto'].forEach(function(header) {
|
||||
req.headers['x-forwarded-' + header] =
|
||||
(req.headers['x-forwarded-' + header] || '') +
|
||||
(req.headers['x-forwarded-' + header] ? ',' : '') +
|
||||
values[header];
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Does the actual proxying. If `forward` is enabled fires up
|
||||
* a ForwardStream, same happens for ProxyStream. The request
|
||||
* just dies otherwise.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {IncomingMessage} Res Response object
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function stream(req, res, options) {
|
||||
if(options.forward) {
|
||||
// If forward enable, so just pipe the request
|
||||
var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
|
||||
common.setupOutgoing(options.ssl || {}, options, req, 'forward')
|
||||
);
|
||||
req.pipe(forwardReq);
|
||||
return res.end();
|
||||
}
|
||||
|
||||
// Request initalization
|
||||
var proxyReq = (options.target.protocol === 'https:' ? https : http).request(
|
||||
common.setupOutgoing(options.ssl || {}, options, req)
|
||||
);
|
||||
|
||||
// Error Handler
|
||||
proxyReq.on('error', function(err){
|
||||
var ev = 'caronte:outgoing:web:';
|
||||
// If no error listeners, so throw the error.
|
||||
if (!options.ee.listeners(ev + 'error').length){
|
||||
throw err;
|
||||
}
|
||||
// Also emit the error events
|
||||
options.ee.emit(ev + 'error', err, req, res);
|
||||
});
|
||||
|
||||
options.ee.emit(ev + 'end');
|
||||
req.pipe(proxyReq);
|
||||
|
||||
proxyReq.on('response', function(proxyRes) {
|
||||
var ev = 'caronte:outgoing:web:';
|
||||
|
||||
options.ee.emit(ev + 'begin', req, res);
|
||||
|
||||
// When the previous request respond, we apply the
|
||||
// outgoing passes to the response
|
||||
web_o.some(function(pass) {
|
||||
var evnt = ev + pass.name.toLowerCase() + ':', val;
|
||||
|
||||
options.ee.emit(evnt + 'begin', req, res);
|
||||
// Call the pass with the proxy response
|
||||
// pass(ClientRequest, IncomingMessage, proxyResponse)
|
||||
val = pass(req, res, proxyRes);
|
||||
options.ee.emit(evnt + 'end');
|
||||
|
||||
return val;
|
||||
});
|
||||
|
||||
options.ee.emit(ev + 'end');
|
||||
|
||||
|
||||
proxyRes.pipe(res);
|
||||
});
|
||||
proxyRes.pipe(res);
|
||||
});
|
||||
|
||||
//proxyReq.end();
|
||||
}
|
||||
//proxyReq.end();
|
||||
}
|
||||
|
||||
] // <--
|
||||
.forEach(function(func) {
|
||||
|
||||
@ -37,16 +37,9 @@ var passes = exports;
|
||||
*/
|
||||
function setConnection(req, res, proxyRes) {
|
||||
if (req.httpVersion === '1.0') {
|
||||
if (req.headers.connection) {
|
||||
proxyRes.headers.connection = req.headers.connection
|
||||
} else {
|
||||
proxyRes.headers.connection = 'close'
|
||||
}
|
||||
proxyRes.headers.connection = req.headers.connection || 'close';
|
||||
} else if (!proxyRes.headers.connection) {
|
||||
if (req.headers.connection) { proxyRes.headers.connection = req.headers.connection }
|
||||
else {
|
||||
proxyRes.headers.connection = 'keep-alive'
|
||||
}
|
||||
proxyRes.headers.connection = req.headers.connection || 'keep-alive';
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@ -19,115 +19,117 @@ var http = require('http'),
|
||||
var passes = exports;
|
||||
|
||||
[
|
||||
/**
|
||||
* WebSocket requests must have the `GET` method and
|
||||
* the `upgrade:websocket` header
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {Socket} Websocket
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
/**
|
||||
* WebSocket requests must have the `GET` method and
|
||||
* the `upgrade:websocket` header
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {Socket} Websocket
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function checkMethodAndHeader (req, socket) {
|
||||
if (req.method !== 'GET' || !req.headers.upgrade) {
|
||||
socket.destroy(); return true;
|
||||
}
|
||||
|
||||
if (req.headers.upgrade.toLowerCase() !== 'websocket') {
|
||||
socket.destroy(); return true;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Set the proper configuration for sockets,
|
||||
* set no delay and set keep alive, also set
|
||||
* the timeout to 0.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {Socket} Websocket
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function setupSocket(req, socket) {
|
||||
socket.setTimeout(0);
|
||||
socket.setNoDelay(true);
|
||||
|
||||
socket.setKeepAlive(true, 0);
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets `x-forwarded-*` headers if specified in config.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {Socket} Websocket
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function XHeaders(req, socket, options) {
|
||||
if(!options.xfwd) return;
|
||||
|
||||
var values = {
|
||||
for : req.connection.remoteAddress || req.socket.remoteAddress,
|
||||
port : req.connection.remotePort || req.socket.remotePort,
|
||||
proto: req.connection.pair ? 'wss' : 'ws'
|
||||
};
|
||||
|
||||
['for', 'port', 'proto'].forEach(function(header) {
|
||||
req.headers['x-forwarded-' + header] =
|
||||
(req.headers['x-forwarded-' + header] || '') +
|
||||
(req.headers['x-forwarded-' + header] ? ',' : '') +
|
||||
values[header]
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Does the actual proxying. Make the request and upgrade it
|
||||
* send the Switching Protocols request and pipe the sockets.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {Socket} Websocket
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
function stream(req, socket, options, head) {
|
||||
common.setupSocket(socket);
|
||||
|
||||
if (head && head.length) socket.unshift(head);
|
||||
|
||||
|
||||
var proxyReq = (~['https:', 'wss:'].indexOf(options.target.protocol) ? https : http).request(
|
||||
common.setupOutgoing(options.ssl || {}, options, req)
|
||||
);
|
||||
// Error Handler
|
||||
proxyReq.on('error', function(err){
|
||||
var ev = 'caronte:outgoing:ws:';
|
||||
// If no error listeners, so throw the error.
|
||||
if (options.ee.listeners(ev + 'error').length == 0){
|
||||
throw err;
|
||||
function checkMethodAndHeader (req, socket) {
|
||||
if (req.method !== 'GET' || !req.headers.upgrade) {
|
||||
socket.destroy();
|
||||
return true;
|
||||
}
|
||||
// Also emit the error events
|
||||
options.ee.emit(ev + 'error', err, req, socket, head);
|
||||
});
|
||||
|
||||
proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
|
||||
common.setupSocket(proxySocket);
|
||||
if (req.headers.upgrade.toLowerCase() !== 'websocket') {
|
||||
socket.destroy();
|
||||
return true;
|
||||
}
|
||||
},
|
||||
|
||||
if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);
|
||||
/**
|
||||
* Set the proper configuration for sockets,
|
||||
* set no delay and set keep alive, also set
|
||||
* the timeout to 0.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {Socket} Websocket
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
socket.write('HTTP/1.1 101 Switching Protocols\r\n');
|
||||
socket.write(Object.keys(proxyRes.headers).map(function(i) {
|
||||
return i + ": " + proxyRes.headers[i];
|
||||
}).join('\r\n') + '\r\n\r\n');
|
||||
proxySocket.pipe(socket).pipe(proxySocket);
|
||||
});
|
||||
function setupSocket(req, socket) {
|
||||
socket.setTimeout(0);
|
||||
socket.setNoDelay(true);
|
||||
|
||||
proxyReq.end(); // XXX: CHECK IF THIS IS THIS CORRECT
|
||||
}
|
||||
socket.setKeepAlive(true, 0);
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets `x-forwarded-*` headers if specified in config.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {Socket} Websocket
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function XHeaders(req, socket, options) {
|
||||
if(!options.xfwd) return;
|
||||
|
||||
var values = {
|
||||
for : req.connection.remoteAddress || req.socket.remoteAddress,
|
||||
port : req.connection.remotePort || req.socket.remotePort,
|
||||
proto: req.connection.pair ? 'wss' : 'ws'
|
||||
};
|
||||
|
||||
['for', 'port', 'proto'].forEach(function(header) {
|
||||
req.headers['x-forwarded-' + header] =
|
||||
(req.headers['x-forwarded-' + header] || '') +
|
||||
(req.headers['x-forwarded-' + header] ? ',' : '') +
|
||||
values[header];
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Does the actual proxying. Make the request and upgrade it
|
||||
* send the Switching Protocols request and pipe the sockets.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {Socket} Websocket
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
function stream(req, socket, options, head) {
|
||||
common.setupSocket(socket);
|
||||
|
||||
if (head && head.length) socket.unshift(head);
|
||||
|
||||
|
||||
var proxyReq = (~['https:', 'wss:'].indexOf(options.target.protocol) ? https : http).request(
|
||||
common.setupOutgoing(options.ssl || {}, options, req)
|
||||
);
|
||||
// Error Handler
|
||||
proxyReq.on('error', function(err){
|
||||
var ev = 'caronte:outgoing:ws:';
|
||||
// If no error listeners, so throw the error.
|
||||
if (!options.ee.listeners(ev + 'error').length){
|
||||
throw err;
|
||||
}
|
||||
// Also emit the error events
|
||||
options.ee.emit(ev + 'error', err, req, socket, head);
|
||||
});
|
||||
|
||||
proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
|
||||
common.setupSocket(proxySocket);
|
||||
|
||||
if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);
|
||||
|
||||
socket.write('HTTP/1.1 101 Switching Protocols\r\n');
|
||||
socket.write(Object.keys(proxyRes.headers).map(function(i) {
|
||||
return i + ": " + proxyRes.headers[i];
|
||||
}).join('\r\n') + '\r\n\r\n');
|
||||
proxySocket.pipe(socket).pipe(proxySocket);
|
||||
});
|
||||
|
||||
proxyReq.end(); // XXX: CHECK IF THIS IS THIS CORRECT
|
||||
}
|
||||
|
||||
] // <--
|
||||
.forEach(function(func) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user