move response data from memory to local cache file

This commit is contained in:
OttoMao
2015-10-24 23:37:43 +08:00
parent 31daeebdd5
commit cfc817cb1f
13 changed files with 248 additions and 60 deletions

View File

@@ -2,15 +2,18 @@
var zlib = require('zlib'),
Datastore = require('nedb'),
util = require("util"),
path = require("path"),
fs = require("fs"),
events = require('events'),
iconv = require('iconv-lite'),
proxyUtil = require("./util"),
logUtil = require("./log");
//option.filename
function Recorder(option){
var self = this,
id = 1,
id = 1,
cachePath = proxyUtil.generateCacheDir(),
db;
option = option || {};
@@ -87,31 +90,40 @@ function Recorder(option){
};
//update recordBody if exits
//TODO : trigger update callback
var BODY_FILE_PRFIX = "res_body_";
self.updateRecordBody =function(id,info){
if(id == -1) return;
if(!id || !info.resBody) return;
//add to body map
//ignore image data
if(/image/.test(info.resHeader['content-type'])){
self.recordBodyMap[id] = "(image)";
}else{
self.recordBodyMap[id] = info.resBody;
}
var bodyFile = path.join(cachePath,BODY_FILE_PRFIX + id);
fs.writeFile(bodyFile, info.resBody);
};
self.getBody = function(id){
self.getBody = function(id,cb){
if(id < 0){
return "";
cb && cb("");
}
return self.recordBodyMap[id] || "";
var bodyFile = path.join(cachePath,BODY_FILE_PRFIX + id);
fs.access(bodyFile, fs.F_OK | fs.R_OK ,function(err){
if(err){
cb && cb(err);
}else{
fs.readFile(bodyFile,cb);
}
});
};
self.getBodyUTF8 = function(id,cb){
var bodyContent = self.getBody(id),
result = "";
self.getDecodedBody = function(id,cb){
var result = {
type : "unknown",
mime : "",
content : ""
};
GLOBAL.recorder.getSingleRecord(id,function(err,doc){
//check whether this record exists
if(!doc || !doc[0]){
@@ -119,24 +131,39 @@ function Recorder(option){
return;
}
if(!bodyContent){
cb(null,result);
}else{
var record = doc[0],
resHeader = record['resHeader'] || {};
try{
var charsetMatch = JSON.stringify(resHeader).match(/charset="?([a-zA-Z0-9\-]+)"?/);
if(charsetMatch && charsetMatch.length > 1){
var currentCharset = charsetMatch[1].toLowerCase();
if(currentCharset != "utf-8" && iconv.encodingExists(currentCharset)){
bodyContent = iconv.decode(bodyContent, currentCharset);
self.getBody(id,function(err,bodyContent){
if(err){
cb(err);
}else if(!bodyContent){
cb(null,result);
}else{
var record = doc[0],
resHeader = record['resHeader'] || {};
try{
var headerStr = JSON.stringify(resHeader),
charsetMatch = headerStr.match(/charset="?([a-zA-Z0-9\-]+)"?/),
imageMatch = resHeader && resHeader["content-type"];
if(charsetMatch && charsetMatch.length){
var currentCharset = charsetMatch[1].toLowerCase();
if(currentCharset != "utf-8" && iconv.encodingExists(currentCharset)){
bodyContent = iconv.decode(bodyContent, currentCharset);
}
result.type = "text";
result.content = bodyContent.toString();
}else if(imageMatch){
result.type = "image";
result.mime = imageMatch;
result.content = bodyContent;
}
}
}catch(e){}
}catch(e){}
cb(null,bodyContent.toString());
}
cb(null,result);
}
});
});
};