mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-08-04 21:39:04 +00:00
Add test cases for the proxy, and do some tiny fixes.
the fixes are: 1. add "content-type" in headers for when dealing with localresponse 2. make a more accurate tip for throttle rate when lower than 1 3. make the crtMgr funcionality a more independent one 4. uppercase the request header before sending it out update the tip
This commit is contained in:
62
lib/util.js
62
lib/util.js
@@ -1,8 +1,11 @@
|
||||
var fs = require("fs"),
|
||||
path = require("path"),
|
||||
mime = require('mime-types'),
|
||||
color = require('colorful'),
|
||||
logUtil = require("./log"),
|
||||
exec = require('child_process').exec;
|
||||
|
||||
|
||||
const changeCase = require('change-case');
|
||||
// {"Content-Encoding":"gzip"} --> {"content-encoding":"gzip"}
|
||||
module.exports.lower_keys = function(obj){
|
||||
for(var key in obj){
|
||||
@@ -34,7 +37,7 @@ module.exports.getAnyProxyHome = function(){
|
||||
|
||||
if(!fs.existsSync(home)){
|
||||
try{
|
||||
fs.mkdirSync(home,0777);
|
||||
fs.mkdirSync(home, '0777');
|
||||
}catch(e){
|
||||
return null;
|
||||
}
|
||||
@@ -48,7 +51,7 @@ module.exports.generateCacheDir = function(){
|
||||
var rand = Math.floor(Math.random() * 1000000),
|
||||
cachePath = path.join(util.getAnyProxyHome(),"./" + CACHE_DIR_PREFIX + rand);
|
||||
|
||||
fs.mkdirSync(cachePath,0777);
|
||||
fs.mkdirSync(cachePath, '0777');
|
||||
return cachePath;
|
||||
}
|
||||
|
||||
@@ -60,7 +63,7 @@ module.exports.clearCacheDir = function(cb){
|
||||
if(isWin){
|
||||
exec("for /D %f in (" + dirNameWildCard + ") do rmdir %f /s /q",{cwd : home},cb);
|
||||
}else{
|
||||
exec("rm -rf " + dirNameWildCard + "",{cwd : home},cb);
|
||||
exec("rm -rf " + dirNameWildCard + "",{cwd : home},cb);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,4 +105,53 @@ module.exports.filewalker = function(root,cb){
|
||||
|
||||
cb && cb.apply(null,[null,ret]);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* 获取文件所对应的content-type以及content-length等信息
|
||||
* 比如在useLocalResponse的时候会使用到
|
||||
*/
|
||||
module.exports.contentType = function (filepath) {
|
||||
return mime.contentType(path.extname(filepath));
|
||||
};
|
||||
|
||||
/*
|
||||
* 读取file的大小,以byte为单位
|
||||
*/
|
||||
module.exports.contentLength = function (filepath) {
|
||||
try {
|
||||
var stat = fs.statSync(filepath);
|
||||
return stat.size;
|
||||
} catch (e) {
|
||||
logUtil.printLog(color.red("\nfailed to ready local file : " + filepath));
|
||||
logUtil.printLog(color.red(e));
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.showRootInstallTip = function () {
|
||||
logUtil.printLog(color.red("can not find rootCA.crt or rootCA.key"), logUtil.T_ERR);
|
||||
logUtil.printLog(color.red("you may generate one by the following methods"), logUtil.T_ERR);
|
||||
logUtil.printLog(color.red("\twhen using globally : anyproxy --root"), logUtil.T_ERR);
|
||||
logUtil.printLog(color.red("\twhen using as a module : require(\"anyproxy\").generateRootCA();"), logUtil.T_ERR);
|
||||
logUtil.printLog(color.red("\tmore info : https://github.com/alibaba/anyproxy/wiki/How-to-config-https-proxy"), logUtil.T_ERR);
|
||||
};
|
||||
|
||||
/*
|
||||
* remove the cache before requering, the path SHOULD BE RELATIVE TO UTIL.JS
|
||||
*/
|
||||
module.exports.freshRequire = function (path) {
|
||||
delete require.cache[require.resolve(path)];
|
||||
return require(path);
|
||||
};
|
||||
|
||||
module.exports.upper_keys = function (obj) {
|
||||
var upperObject = {};
|
||||
for(var key in obj) {
|
||||
var upperKey = changeCase.headerCase(key);
|
||||
upperObject[upperKey] = obj[key];
|
||||
}
|
||||
|
||||
return upperObject;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user