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) {
|
||||
if(!options) {
|
||||
/* if(!options) {
|
||||
throw new Error([
|
||||
"`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 ",
|
||||
" both missing "
|
||||
].join("\n"));
|
||||
}
|
||||
} */
|
||||
|
||||
options.ee = new events.EventEmitter2({ wildcard: true, delimiter: ':' });
|
||||
|
||||
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;
|
||||
}
|
||||
return new ProxyServer(options, httpProxy.createWebProxy(options), httpProxy.createWsProxy(options));
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
var httpProxy = exports,
|
||||
extend = require('util')._extend,
|
||||
parse_url = require('url').parse,
|
||||
EE3 = require('eventemitter3').EventEmitter,
|
||||
web = require('./passes/web-incoming'),
|
||||
ws = require('./passes/ws-incoming');
|
||||
|
||||
httpProxy.createWebProxy = createRightProxy('web');
|
||||
httpProxy.createWsProxy = createRightProxy('ws');
|
||||
httpProxy.Server = ProxyServer;
|
||||
|
||||
/**
|
||||
* Returns a function that creates the loader for
|
||||
@ -36,7 +38,6 @@ function createRightProxy(type) {
|
||||
var self = this,
|
||||
args = [].slice.call(arguments),
|
||||
cntr = args.length - 1,
|
||||
ev = 'http-proxy:' + type + ':incoming:',
|
||||
head;
|
||||
|
||||
if(
|
||||
@ -55,16 +56,13 @@ function createRightProxy(type) {
|
||||
head = args[cntr];
|
||||
}
|
||||
|
||||
options.ee.emit(ev + 'begin', req, res);
|
||||
|
||||
['target', 'forward'].forEach(function(e) {
|
||||
if (typeof options[e] === 'string')
|
||||
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
|
||||
* pass(req, res, options, head)
|
||||
@ -73,16 +71,38 @@ function createRightProxy(type) {
|
||||
* refer to the connection socket
|
||||
* pass(req, socket, options, head)
|
||||
*/
|
||||
|
||||
options.ee.emit(evnt + 'begin', req, res);
|
||||
val = pass(req, res, options, head);
|
||||
options.ee.emit(evnt + 'end');
|
||||
|
||||
return val;
|
||||
});
|
||||
|
||||
options.ee.emit(ev + 'end');
|
||||
if(passes[i](req, res, this, head)) { // passes can return a truthy value to halt the loop
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
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",
|
||||
|
||||
"dependencies" : {
|
||||
"eventemitter2" : "*"
|
||||
"eventemitter3" : "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"mocha" : "*",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user