Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

0.0.2 => update core & examples

  • Loading branch information...
commit ef6f75c7e6dc0a637aba9347e0700b6fc3d8ec43 1 parent 5b9ed79
@rehorn authored
View
7 lib/core.js
@@ -7,7 +7,7 @@ var http = require('http'),
path = require('path'),
_ = require('underscore');
-var responder = require('./livepool/responder');
+var responders = require('./livepool/responder');
var httpServer, httpsServer, https2http;
@@ -56,12 +56,13 @@ livepool.run = function (){
var handler = config.getHandler(reqInfo);
var reqUrl = reqInfo.url;
// local replacement
- if(handler && (responder = responder[handler.respond.type])){
+ console.log(handler)
+ if(handler && (responder = responders[handler.respond.type])){
logger.log('req handler [ ' + handler.respond.type.grey + ' ]: ' + reqUrl.grey);
responder(handler, req, res);
}else{
// remote route
- responder = responder['route'];
+ responder = responders['route'];
responder(null, req, res);
}
View
59 lib/livepool/config.js
@@ -2,7 +2,8 @@
var fs = require('fs'),
url = require('url'),
path = require('path'),
- _ = require('underscore');
+ _ = require('underscore'),
+ util = require('./util');
var config = module.exports = {};
@@ -15,7 +16,8 @@ config.init = function (eventCenter){
config.global = {
http: 8000,
https: 8001,
- proxy: "proxy.tencent.com:8080"
+ index: "index.html",
+ // proxy: "proxy.tencent.com:8080"
};
var ruleGroup = {
@@ -64,6 +66,8 @@ config.loadRules = function(){
var module = '../../' + path.dirname(item) + '/' + path.basename(item, '.js');
var rules = require(module);
rules.file = item;
+ rules.enabled = _.isUndefined(item.enabled) ? 1 : item.enabled;
+ rules.index = _.isUndefined(item.index) ? 0 : item.index;
config.rulesGroup.push(rules);
rules = _.extend(ruleGroup, rules);
@@ -80,9 +84,10 @@ config.loadRules = function(){
matchRaw: match,
action: item.action,
actionRaw: item.action,
+ indexPage: rules.index || config.global.index,
hostname: urlInfo.hostname,
pathname: urlInfo.pathname,
- enabled: item.enabled
+ enabled: _.isUndefined(item.enabled) ? 1 : item.enabled
});
});
});
@@ -102,12 +107,12 @@ config.loadRules = function(){
actionRaw: item.action,
hostname: urlInfo.hostname,
pathname: urlInfo.pathname,
- enabled: item.enabled
+ enabled: _.isUndefined(item.enabled) ? 1 : item.enabled
});
});
});
});
- // console.log(config.handlers)
+ console.log(config.handlers);
console.log(config.routers);
};
@@ -116,30 +121,43 @@ config.updateRules = function(rules){
};
config.getHandler = function(reqInfo){
+ var action, extname, filepath;
var urlRaw = reqInfo.url;
var reqUrl = url.parse(urlRaw);
var pathname = reqUrl.pathname;
+
return _.find(config.handlers, function(handler){
if(new RegExp(handler.match).test(urlRaw)){
- var filepath = path.resolve(handler.base, pathname.replace(handler.pathname, ''));
- var extname = path.extname(filepath);
- var repsond = {
- filepath: filepath
+ // 处理默认页访问 www.livepool.com
+ pathname = (pathname == '/') ? ('/' + handler.indexPage) : pathname;
+ action = handler.actionRaw;
+ var respond = {
+ type: 'local'
};
- if(_.isArray(handler.action)){
- repsond.type = 'combo';
- }else if(handler.action.match(/http[s]?/)){
- repsond.type = 'remote';
- }else if(extname == '.qzmin'){
+ if(_.isArray(action)){
+ respond.type = 'combo';
+ filepath = path.resolve(handler.base, pathname.replace('/', ''));
+ }else if(action.match(/http[s]?/)){
+ respond.type = 'remote';
+ }else if(path.extname(action) == '.qzmin'){
respond.type = 'qzmin';
- }else if(fs.existsSync(filepath)){
- respond.type = 'local';
}else{
- return false;
+
+ if(util.detectDestType(action) == 'file'){
+ filepath = path.resolve(handler.base, action);
+ }else{
+ filepath = path.resolve(handler.base, action, pathname.replace(handler.pathname, ''));
+ }
+
+ if(fs.existsSync(filepath)){
+ respond.type = 'local';
+ }else{
+ return false;
+ }
}
-
- handler.repsond = repsond;
+ respond.filepath = filepath;
+ handler.respond = respond;
return true;
}
return false;
@@ -148,9 +166,6 @@ config.getHandler = function(reqInfo){
config.getRouter = function(reqInfo){
var urlRaw = reqInfo.url;
- var reqUrl = url.parse(urlRaw);
- var pathname = reqUrl.pathname;
-
return _.find(config.routers, function(router){
if(new RegExp(router.match).test(urlRaw)){
return true;
View
11 lib/livepool/responder/combo.js
@@ -1,5 +1,6 @@
-var fs =require('fs'),
+var fs = require('fs'),
+ path = require('path'),
mime = require('mime'),
_ = require('underscore');
@@ -45,7 +46,7 @@ function comboResponder(handler, req, res, options) {
item = path.resolve(handler.base, item);
filepath.push(item);
// fileNotFound
- if(!fs.existSync(item)){
+ if(!fs.existsSync(item)){
fileNotFound = true;
return ;
}
@@ -63,7 +64,11 @@ function comboResponder(handler, req, res, options) {
}
// check target is exits and out-of-date(modify time is shorten than 500ms)
- if(opt.forceUpdate || !(fs.existSync(target) && targetStat = fs.statSync(target) && (targetStat.mtime.getTime() - maxMTime) < 500)){
+ if(opt.forceUpdate ||
+ !(fs.existsSync(target) &&
+ (targetStat = fs.statSync(target)) &&
+ (targetStat.mtime.getTime() - maxMTime) > 500)
+ ){
comboFiles(target, filepath);
}
respondFile(target, res);
View
2  lib/livepool/responder/index.js
@@ -2,4 +2,4 @@ exports.combo = require('./combo');
exports.local = require('./local');
exports.qzmin = require('./qzmin');
exports.remote = require('./remote');
-exports.proxy = require('./route');
+exports.route = require('./route');
View
8 lib/livepool/responder/local.js
@@ -3,12 +3,14 @@ var fs = require('fs');
var mime = require('mime');
function localFileResponder(handler, req, res) {
- var filepath = handler.repsond.filepath;
-
- fs.existSync(filepath) && fs.stat(filepath, function(err, stat) {
+
+ var filepath = handler.respond.filepath;
+ console.log(filepath)
+ fs.existsSync(filepath) && fs.stat(filepath, function(err, stat) {
if (err) {
throw err;
}
+
if (!stat.isFile()) {
throw new Error('The responder is not a file!');
}
View
20 lib/livepool/responder/qzmin.js
@@ -1,10 +1,16 @@
var fs =require('fs'),
+ path = require('path'),
+ _ = require('underscore'),
comboResponder = require('./combo');
-var qzminCache = [];
+var qzminCache = {};
function js2json(jsString){
+ // Remove BOM
+ if (jsString.charCodeAt(0) === 0xFEFF) {
+ jsString = jsString.substring(1);
+ }
var result = jsString.replace(/([^"\s]\S+[^"])\s*:/g, '"$1":');
return result.replace(/\'/g, '"');
};
@@ -17,13 +23,13 @@ function respond404(res){
function qzminResponder(handler, req, res) {
var qzminFilepath = path.join(handler.base, handler.actionRaw);
- if(!fs.existSync(qzminResponder)){
+ if(!fs.existsSync(qzminFilepath)){
respond404(res);
return ;
}
- var qzminDirPath = path.basename(qzminFilepath);
- var qzminJson = js2json(fs.readFileSync(qzminFilepath));
+ var qzminDirPath = path.dirname(qzminFilepath);
+ var qzminJson = js2json(fs.readFileSync(qzminFilepath, 'utf8'));
var qzmin = JSON.parse(qzminJson);
// qzmin 要配合willow其实只有projects有效
var project = qzmin.projects[0];
@@ -32,7 +38,10 @@ function qzminResponder(handler, req, res) {
var options = {},
qzminStat;
// check if qzmin combo out of date
- if(!qzminCache[qzminFilepath] && !(qzminStat = fs.statSync(qzminFilepath) && (qzminStat.mtime.getTime() - qzminCache[qzminFilepath]) < 500)){
+ if(!qzminCache[qzminFilepath] ||
+ !((qzminStat = fs.statSync(qzminFilepath)) &&
+ (qzminCache[qzminFilepath] - qzminStat.mtime.getTime()) > 500)
+ ){
// resolve include path
var includes = _.map(project.include, function(value){
return path.join(qzminDirPath, value);
@@ -43,6 +52,7 @@ function qzminResponder(handler, req, res) {
options.forceUpdate = true;
}
+ handler.respond.filepath = target;
comboResponder(handler, req, res, options);
};
View
31 lib/livepool/responder/route.js
@@ -1,7 +1,9 @@
var config = require('../config'),
- request = request('../request');
+ request = require('../request'),
+ logger = require('../logger');
+var httpProxy = require('http-proxy');
var proxy = new httpProxy.RoutingProxy();
var global = config.global,
@@ -9,27 +11,34 @@ var global = config.global,
proxyAgent = proxyAgent.split(':');
function routeResponder(router, req, res){
- logger.log('req proxied: ' + reqUrl.grey);
- var host = req.headers.host;
+ var proxyHost = '';
var reqInfo = request.getReqInfo(req);
router = router || config.getRouter(reqInfo);
-
if(router){
// proxy to specified server
- if(router)
+ // directly proxy
+ if(router.action == '-'){
+ goProxy(req, res);
+ }else{
+ proxyHost = router.action.split(':');
+ goProxy(req, res, proxyHost[0], proxyHost[1]);
+ }
}else{
// directly proxy
goProxy(req, res, proxyAgent[0], proxyAgent[1]);
- proxy.proxyRequest(req, res, {
- host: proxyAgent[0] || host,
- port: proxyAgent[1] || 80
- });
}
};
-function goProxy = function(req, res, host, port){
-
+function goProxy(req, res, host, port){
+ var dhost = req.headers.host.split(':');
+ host = host || dhost[0];
+ port = port || dhost[1] || 80;
+ logger.log('req proxied, host:' + host + ', port:' + port + ', url:' + req.url);
+ proxy.proxyRequest(req, res, {
+ host: host,
+ port: port
+ });
};
module.exports = routeResponder;
View
8 lib/livepool/util.js
@@ -6,3 +6,11 @@ var _ = require('underscore'),
var util = module.exports = {};
util._ = _;
+
+util.endsWith = function(src, str){
+ return str.length > 0 && src.substring(src.length - str.length, src.length) === str;
+}
+
+util.detectDestType = function(target) {
+ return (util.endsWith(target, '\\') || util.endsWith(target, '/')) ? 'dir' : 'file';
+};
View
44 rules/project1.js
@@ -1,24 +1,38 @@
module.exports = {
name: "project1",
- index: 0,
- enabled: 1,
- base: "E:/Server/Node/livepool/test/examples",
+ // index: 0, // default is 0
+ // enabled: 1, // default is 1
+ base: "E:/Server/Node/livepool/test/examples/",
handler: [{
- match: "examples.com",
- action: "./",
- enabled: 1
+ // test => http://www.livepool.com/index.html
+ match: "www.livepool.com/index.html",
+ action: "./__index.html"
+ // enabled: 1 // default is 1
}, {
- match: "*.url.cn/chat/",
- action: "./",
- enabled: 1
+ // test => http://www.livepool.com/static-web/site/index.html
+ // test => http://www.livepool.com/static-web/index.html
+ match: "www.livepool.com/static-web/",
+ action: "./static/"
+ }, {
+ // test => http://www.livepool.com/js/chat.all.js
+ match: "www.livepool.com/js/chat.all.js",
+ action: "./tools/chat.all.qzmin"
+ }, {
+ // test => http://www.livepool.com/css/main.css
+ match: "www.livepool.com/css/main.css",
+ action: ["./css/sub1.css", "./css/sub2.css"]
+ }, {
+ match: "www.livepool.com/css/remote.css",
+ action: "http://chat.qq.com/css/remote2.css"
+ }, {
+ match: "www.livepool.com/ *.cdn.livepool.cn/examples/",
+ action: "./"
}],
router: [{
- match: "chat.qq.com/cgi-bin/ srv.chat.qq.com",
- action: "-",
- enabled: 1
+ match: "www.livepool.com/cgi-bin/ srv.livepool.com/",
+ action: "-"
}, {
- match: "chat.qq.com",
- action: "172.23.136.86",
- enabled: 1
+ match: "www.livepool.com",
+ action: "127.0.0.1"
}]
};
View
38 test.js
@@ -0,0 +1,38 @@
+var fs = require('fs'),
+ http = require('http'),
+ https = require('https'),
+ httpProxy = require('http-proxy');
+
+var options = {
+ https: {
+ key: fs.readFileSync('lib/keys/key.pem', 'utf8'),
+ cert: fs.readFileSync('lib/keys/cert.pem', 'utf8')
+ }
+};
+
+//
+// Create a standalone HTTPS proxy server
+//
+httpProxy.createServer(8000, 'localhost', options).listen(8001);
+
+//
+// Create an instance of HttpProxy to use with another HTTPS server
+//
+var proxy = new httpProxy.HttpProxy({
+ target: {
+ host: 'localhost',
+ port: 8000
+ }
+});
+https.createServer(options.https, function (req, res) {
+ proxy.proxyRequest(req, res)
+}).listen(8002);
+
+//
+// Create the target HTTPS server for both cases
+//
+http.createServer(function (req, res) {
+ res.writeHead(200, { 'Content-Type': 'text/plain' });
+ res.write('hello https\n');
+ res.end();
+}).listen(8000);
View
14 test/examples/__index.html
@@ -0,0 +1,14 @@
+<!doctype html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <title>LivePool Test Project</title>
+</head>
+<body>
+ <h1>__index</h1>
+ <h1>__index</h1>
+ <h1>__index</h1>
+ <h1>This is a LivePool Test Project</h1>
+ <h1>This is a LivePool Test Project</h1>
+</body>
+</html>
View
10 test/examples/css/main.css
@@ -0,0 +1,10 @@
+#test{
+ color: red;
+}
+#test2{
+ color: yellow;
+}
+
+#test3{
+ color: red;
+}
View
3  test/examples/css/sub1.css
@@ -0,0 +1,3 @@
+#test{
+ color: red;
+}
View
7 test/examples/css/sub2.css
@@ -0,0 +1,7 @@
+#test2{
+ color: yellow;
+}
+
+#test3{
+ color: red;
+}
View
BIN  test/examples/img/module/qq.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
0  test/examples/img/module/readme
No changes.
View
BIN  test/examples/img/qq.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
0  test/examples/img/readme
No changes.
View
3  test/examples/js/jquery.js
@@ -0,0 +1,3 @@
+function(){
+ return true;
+}
View
4 test/examples/js/main.js
@@ -0,0 +1,4 @@
+function(){
+ // updated 2
+ return true;
+}
View
3  test/examples/js/sub.js
@@ -0,0 +1,3 @@
+function(){
+ return true;
+}
View
14 test/examples/static/index.html
@@ -0,0 +1,14 @@
+<!doctype html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <title>LivePool Test Project</title>
+</head>
+<body>
+ <h1>Static / Index</h1>
+ <h1>This is a LivePool Test Project</h1>
+ <h1>This is a LivePool Test Project</h1>
+ <h1>This is a LivePool Test Project</h1>
+ <h1>This is a LivePool Test Project</h1>
+</body>
+</html>
View
14 test/examples/static/site/index.html
@@ -0,0 +1,14 @@
+<!doctype html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <title>LivePool Test Project</title>
+</head>
+<body>
+ <h1>Static / Site / Index</h1>
+ <h1>This is a LivePool Test Project</h1>
+ <h1>This is a LivePool Test Project</h1>
+ <h1>This is a LivePool Test Project</h1>
+ <h1>This is a LivePool Test Project</h1>
+</body>
+</html>
View
10 test/examples/tools/chat.all.js
@@ -0,0 +1,10 @@
+function(){
+ return true;
+}
+function(){
+ // updated 2
+ return true;
+}
+function(){
+ return true;
+}
View
17 test/examples/tools/chat.all.qzmin
@@ -0,0 +1,17 @@
+{
+ projects: [
+ {
+ name: "Jx Custom Version for Q+ Explorer",
+ target: "./chat.all.js",
+ include: [
+ '../js/jquery.js',
+ '../js/main.js',
+ '../js/sub.js'
+ ]
+ }
+ ],
+ level: 0,
+ shrink: false,
+ encode: "utf-8",
+ comment: "Q+ Group 2010"
+}
Please sign in to comment.
Something went wrong with that request. Please try again.