mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[feature] start working on the new server
This commit is contained in:
parent
a51b062278
commit
b79bd29d5e
@ -21,7 +21,7 @@ var http = require('http'),
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
proxy.createProxyServer = proxy.createServer = function createProxyServer(options) {
|
proxy.createProxyServer = proxy.createServer = function createProxyServer(options) {
|
||||||
if(!options) {
|
/* if(!options) {
|
||||||
throw new Error([
|
throw new Error([
|
||||||
"`options` is needed and it must have the following layout:",
|
"`options` is needed and it must have the following layout:",
|
||||||
" ",
|
" ",
|
||||||
@ -38,25 +38,7 @@ proxy.createProxyServer = proxy.createServer = function createProxyServer(option
|
|||||||
" `options.target and `options.forward` cannot be ",
|
" `options.target and `options.forward` cannot be ",
|
||||||
" both missing "
|
" both missing "
|
||||||
].join("\n"));
|
].join("\n"));
|
||||||
}
|
} */
|
||||||
|
|
||||||
options.ee = new events.EventEmitter2({ wildcard: true, delimiter: ':' });
|
return new ProxyServer(options, httpProxy.createWebProxy(options), httpProxy.createWsProxy(options));
|
||||||
|
|
||||||
return {
|
|
||||||
ee : options.ee,
|
|
||||||
web : httpProxy.createWebProxy(options),
|
|
||||||
ws : httpProxy.createWsProxy(options),
|
|
||||||
listen : function listen(port) {
|
|
||||||
var server = options.ssl ? https.createServer(options.ssl, this.web) : http.createServer(this.web);
|
|
||||||
|
|
||||||
if(options.ws) {
|
|
||||||
server.on('upgrade', this.ws);
|
|
||||||
}
|
|
||||||
|
|
||||||
server.listen(port);
|
|
||||||
|
|
||||||
return server;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,13 @@
|
|||||||
var httpProxy = exports,
|
var httpProxy = exports,
|
||||||
extend = require('util')._extend,
|
extend = require('util')._extend,
|
||||||
parse_url = require('url').parse,
|
parse_url = require('url').parse,
|
||||||
|
EE3 = require('eventemitter3').EventEmitter,
|
||||||
web = require('./passes/web-incoming'),
|
web = require('./passes/web-incoming'),
|
||||||
ws = require('./passes/ws-incoming');
|
ws = require('./passes/ws-incoming');
|
||||||
|
|
||||||
httpProxy.createWebProxy = createRightProxy('web');
|
httpProxy.createWebProxy = createRightProxy('web');
|
||||||
httpProxy.createWsProxy = createRightProxy('ws');
|
httpProxy.createWsProxy = createRightProxy('ws');
|
||||||
|
httpProxy.Server = ProxyServer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a function that creates the loader for
|
* Returns a function that creates the loader for
|
||||||
@ -36,7 +38,6 @@ function createRightProxy(type) {
|
|||||||
var self = this,
|
var self = this,
|
||||||
args = [].slice.call(arguments),
|
args = [].slice.call(arguments),
|
||||||
cntr = args.length - 1,
|
cntr = args.length - 1,
|
||||||
ev = 'http-proxy:' + type + ':incoming:',
|
|
||||||
head;
|
head;
|
||||||
|
|
||||||
if(
|
if(
|
||||||
@ -55,16 +56,13 @@ function createRightProxy(type) {
|
|||||||
head = args[cntr];
|
head = args[cntr];
|
||||||
}
|
}
|
||||||
|
|
||||||
options.ee.emit(ev + 'begin', req, res);
|
|
||||||
|
|
||||||
['target', 'forward'].forEach(function(e) {
|
['target', 'forward'].forEach(function(e) {
|
||||||
if (typeof options[e] === 'string')
|
if (typeof options[e] === 'string')
|
||||||
options[e] = parse_url(options[e]);
|
options[e] = parse_url(options[e]);
|
||||||
});
|
});
|
||||||
|
|
||||||
passes.some(function(pass) {
|
|
||||||
var evnt = ev + pass.name.toLowerCase() + ':', val;
|
|
||||||
|
|
||||||
|
for(var i=0; i < passes.length; i++) {
|
||||||
/**
|
/**
|
||||||
* Call of passes functions
|
* Call of passes functions
|
||||||
* pass(req, res, options, head)
|
* pass(req, res, options, head)
|
||||||
@ -73,16 +71,38 @@ function createRightProxy(type) {
|
|||||||
* refer to the connection socket
|
* refer to the connection socket
|
||||||
* pass(req, socket, options, head)
|
* pass(req, socket, options, head)
|
||||||
*/
|
*/
|
||||||
|
if(passes[i](req, res, this, head)) { // passes can return a truthy value to halt the loop
|
||||||
options.ee.emit(evnt + 'begin', req, res);
|
break;
|
||||||
val = pass(req, res, options, head);
|
}
|
||||||
options.ee.emit(evnt + 'end');
|
}
|
||||||
|
|
||||||
return val;
|
|
||||||
});
|
|
||||||
|
|
||||||
options.ee.emit(ev + 'end');
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function ProxyServer(options, web, ws) {
|
||||||
|
this.web = web;
|
||||||
|
this.ws = ws;
|
||||||
|
this.options = options;
|
||||||
|
}
|
||||||
|
|
||||||
|
ProxyServer.prototype.listen = function(port) {
|
||||||
|
var self = this,
|
||||||
|
closure = function(req, res) { self.web(req, res); },
|
||||||
|
server = options.ssl ?
|
||||||
|
https.createServer(this.options.ssl, closure) :
|
||||||
|
http.createServer(closure);
|
||||||
|
|
||||||
|
if(options.ws) {
|
||||||
|
server.on('upgrade', function(req, socket, head) { self.ws(req, socket, head); });
|
||||||
|
}
|
||||||
|
|
||||||
|
server.listen(port);
|
||||||
|
|
||||||
|
return server;
|
||||||
|
};
|
||||||
|
|
||||||
|
ProxyServer.prototype.before = function() {};
|
||||||
|
ProxyServer.prototype.after = function() {};
|
||||||
|
|
||||||
|
require('util').inherits(ProxyServer, EE);
|
||||||
|
|||||||
@ -11,7 +11,7 @@
|
|||||||
"main" : "index.js",
|
"main" : "index.js",
|
||||||
|
|
||||||
"dependencies" : {
|
"dependencies" : {
|
||||||
"eventemitter2" : "*"
|
"eventemitter3" : "*"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"mocha" : "*",
|
"mocha" : "*",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user