mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-08-04 21:39:04 +00:00
1.fix record issue with https 2.rewrite rule scheme
This commit is contained in:
75
rule_sample/rule_use_local_data.js
Normal file
75
rule_sample/rule_use_local_data.js
Normal file
@@ -0,0 +1,75 @@
|
||||
//replace all the images with local one
|
||||
|
||||
var url = require("url"),
|
||||
path = require("path"),
|
||||
fs = require("fs"),
|
||||
buffer = require("buffer");
|
||||
|
||||
var map = [
|
||||
{
|
||||
"host" :/./,
|
||||
"path" :/\.(png|gif|jpg|jpeg)/,
|
||||
"localFile" :"/Users/Stella/tmp/test.png",
|
||||
"localDir" :"~/"
|
||||
}
|
||||
];
|
||||
|
||||
module.exports = {
|
||||
shouldUseLocalResponse : function(req){
|
||||
var host = req.headers.host,
|
||||
urlPattern = url.parse(req.url),
|
||||
path = urlPattern.path;
|
||||
|
||||
for(var index in map){
|
||||
var rule = map[index];
|
||||
|
||||
var hostTest = new RegExp(rule.host).test(host),
|
||||
pathTest = new RegExp(rule.path).test(path);
|
||||
|
||||
if(hostTest && pathTest && (rule.localFile || rule.localDir) ){
|
||||
var targetLocalfile = rule.localFile;
|
||||
|
||||
//localfile not set, map to dir
|
||||
if(!targetLocalfile){ //find file in dir, /a/b/file.html -> dir + b/file.html
|
||||
var remotePathWithoutPrefix = path.replace(new RegExp(rule.path),""); //remove prefix
|
||||
targetLocalfile = pathUtil.join(rule.localDir,remotePathWithoutPrefix);
|
||||
}
|
||||
|
||||
if(fs.existsSync(targetLocalfile)){
|
||||
console.log("==>local file: " + targetLocalfile);
|
||||
req.replaceLocalFile = targetLocalfile; //add a flag to req object
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
dealLocalResponse : function(req,callback){
|
||||
if(req.replaceLocalFile){
|
||||
callback(200, {"content-type":"image/png"}, fs.readFileSync(req.replaceLocalFile) );
|
||||
}
|
||||
},
|
||||
|
||||
replaceRequestOption : function(req,option){
|
||||
},
|
||||
|
||||
replaceRequestProtocol:function(req,protocol){
|
||||
},
|
||||
|
||||
replaceResponseStatusCode: function(req,res,statusCode){
|
||||
},
|
||||
|
||||
replaceResponseHeader: function(req,res,header){
|
||||
},
|
||||
|
||||
replaceServerResData: function(req,res,serverResData){
|
||||
},
|
||||
|
||||
pauseBeforeSendingResponse : function(req,res){
|
||||
},
|
||||
|
||||
shouldInterceptHttpsReq :function(req){
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user