mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-04-24 08:41:31 +00:00
76 lines
2.1 KiB
JavaScript
76 lines
2.1 KiB
JavaScript
//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){
|
|
}
|
|
};
|
|
|