code backup

This commit is contained in:
OttoMao
2015-04-20 09:39:27 +08:00
parent c70388f81a
commit db4ab7d2f9
6 changed files with 218 additions and 170 deletions

View File

@@ -4,6 +4,7 @@ var zlib = require('zlib'),
util = require("util"),
fs = require("fs"),
events = require('events'),
iconv = require('iconv-lite'),
logUtil = require("./log");
//option.filename
@@ -82,8 +83,6 @@ function Recorder(option){
}
};
self.getBody = function(id){
if(id < 0){
return "";
@@ -92,6 +91,39 @@ function Recorder(option){
return self.recordBodyMap[id] || "";
};
self.getBodyUTF8 = function(id,cb){
var bodyContent = self.getBody(id),
result = "";
GLOBAL.recorder.getSingleRecord(id,function(err,doc){
//check whether this record exists
if(!doc || !doc[0]){
cb(new Error("failed to find record for this id"));
return;
}
if(!bodyContent){
cb(null,result);
}
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);
}
}
}catch(e){}
cb(null,bodyContent.toString());
});
}
self.getSingleRecord = function(id,cb){
db.find({_id:parseInt(id)},cb);
}