Skip to content
Browse files

updated

  • Loading branch information...
1 parent 3e60ad7 commit 8d8f6d10cab243f929263d7fc7314f377d77cf71 @rehorn committed Apr 6, 2013
Showing with 28 additions and 13 deletions.
  1. +8 −5 lib/core.js
  2. +17 −4 lib/livepool/config.js
  3. +1 −0 lib/livepool/logger.js
  4. +1 −1 lib/livepool/responder/local.js
  5. +1 −3 rules/project2.js
View
13 lib/core.js
@@ -8,9 +8,6 @@ var http = require('http'),
path = require('path'),
_ = require('underscore');
-var request = require('./request'),
- response = require('./response');
-
var httpServer, httpsServer, https2http;
var livepool = module.exports = {};
@@ -20,7 +17,9 @@ livepool.startTime = (new Date()).getTime();
var config = liveRequire('config'),
logger = liveRequire('logger'),
util = liveRequire('util'),
- eventCenter = liveRequire('event');
+ eventCenter = liveRequire('event'),
+ request = liveRequire('request'),
+ response = liveRequire('response');
var global = config.global,
httpPort = global.http,
@@ -54,10 +53,14 @@ livepool.run = function (){
var reqInfo = request.getReqInfo(req);
var handler = config.getHandler(reqInfo);
+ var reqUrl = reqInfo.url;
+
+ console.log()
if(handler){
var responder = require('./livepool/responder/local');
- responder(filepath, req, res);
+ responder(handler.filepath, req, res);
}else{
+ logger.log('req proxied: ' + reqUrl.grey);
var host = req.headers.host;
proxy.proxyRequest(req, res, {
host: proxyAgent[0] || host,
View
21 lib/livepool/config.js
@@ -54,12 +54,21 @@ config.loadRules = function(){
var handler = rules.handler;
handler.forEach(function(item){
item.match = item.match || '';
+ var pathname = '/';
var split = item.match.split(/\s+/);
split.forEach(function(match){
+
+ // 处理match,获取pathname
+ var urlpath = match.match(/http[s]?/) ? match : 'http://' + match;
+ pathname = url.parse(urlpath.replace(/\*/g, '__ls__')).pathname.replace(/__ls__/g, '\*');
+ pathname = pathname.indexOf('*') > -1 ? pathname.substring(0, pathname.indexOf('*')) : pathname;
+ pathname = pathname.substring(0, pathname.lastIndexOf('/') + 1);
+
config.handlers.push({
base: rules.base,
match: match.replace(/\./g, '\\.').replace(/\*/g, '.*'),
matchRaw: match,
+ pathname: pathname,
action: item.action,
enabled: item.enabled
});
@@ -81,20 +90,24 @@ config.loadRules = function(){
});
});
});
+ console.log(config.handlers)
};
config.updateRules = function(rules){
};
config.getHandler = function(reqInfo){
- var urlRaw = req.url;
+ var urlRaw = reqInfo.url;
var reqUrl = url.parse(urlRaw);
var pathname = reqUrl.pathname;
return _.find(config.handlers, function(handler){
- var filepath = path.resolve(handler.base, pathname.substring(1));
- if(new RegExp(item.match).test(urlRaw) && fs.existsSync(filepath)){
- return true;
+ if(new RegExp(handler.match).test(urlRaw)){
+ var filepath = path.resolve(handler.base, pathname.replace(handler.pathname, ''));
+ if(fs.existsSync(filepath)){
+ handler.filepath = filepath;
+ return true;
+ }
}
return false;
});
View
1 lib/livepool/logger.js
@@ -108,6 +108,7 @@ process.on('exit', _onExit);
process.on('uncaughtException', function(err) {
// exports.error(err.stack || err);
exports.error('uncaughtException: ' + (err.stack || err));
+ // exports.error('uncaughtException: ' + err);
try{
fs.appendFile('log/log.txt', '\r\n' + (err.stack || err), function (err) {
View
2 lib/livepool/responder/local.js
@@ -18,7 +18,7 @@ function localFileResponder(filePath, req, res, next) {
res.statusCode = 200;
res.setHeader('Content-Length', stat.size);
res.setHeader('Content-Type', mime.lookup(filePath));
- res.setHeader('Server', 'nproxy');
+ res.setHeader('Server', 'livepool');
fs.createReadStream(filePath).pipe(res);
});
View
4 rules/project2.js
@@ -3,10 +3,8 @@ module.exports = {
index: 0,
enabled: 1,
base: "E:/connect/bapp/widget_trunk/web/chat/src",
- // host: "*find.qq.com pub.idqqimg.cn",
- // handleAll: 0
handler: [{
- match: "find.qq.com/ pub.idqqimg.cn/",
+ match: "chat.qq.com/ *.url.cn/chat/",
action: "./",
enabled: 1
}],

0 comments on commit 8d8f6d1

Please sign in to comment.
Something went wrong with that request. Please try again.