Commit 30b0f4b3 authored by Medicean's avatar Medicean

(Enhancement:Modules:Request)优化返回包解码流程,增加自动猜解编码功能

parent 7fe0b815
...@@ -4,6 +4,10 @@ ...@@ -4,6 +4,10 @@
## `v(2.0.4-dev)` ## `v(2.0.4-dev)`
### 后端模块
* 优化返回包解码流程,增加自动猜解编码功能,该功能在中文较少时容易出错,出错了使用用户设置的字符编码
### Other ### Other
* 新增 Python2 Custom CGI shell 示例 * 新增 Python2 Custom CGI shell 示例
......
# AntSword [![release](https://img.shields.io/badge/release-v2.0.3-blue.svg?style=flat-square)][url-release] # AntSword [![release](https://img.shields.io/badge/release-v2.0.3.1-blue.svg?style=flat-square)][url-release]
> AntSword in your hands, no worries in your mind! > AntSword in your hands, no worries in your mind!
......
# 中国蚁剑 [![release](https://img.shields.io/badge/release-v2.0.3-blue.svg?style=flat-square)][url-release] # 中国蚁剑 [![release](https://img.shields.io/badge/release-v2.0.3.1-blue.svg?style=flat-square)][url-release]
> 一剑在手,纵横无忧! > 一剑在手,纵横无忧!
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
const fs = require('fs'), const fs = require('fs'),
iconv = require('iconv-lite'), iconv = require('iconv-lite'),
jschardet = require('jschardet'),
through = require('through'), through = require('through'),
CONF = require('./config'), CONF = require('./config'),
superagent = require('superagent'), superagent = require('superagent'),
...@@ -135,14 +136,20 @@ class Request { ...@@ -135,14 +136,20 @@ class Request {
} }
let buff = ret.hasOwnProperty('body') ? ret.body : new Buffer(); let buff = ret.hasOwnProperty('body') ? ret.body : new Buffer();
// 解码 // 解码
let text = iconv.decode(buff, opts['encode']); let text = "";
// 自动猜测编码
let encoding = detectEncoding(buff, {defaultEncoding:"unknown"});
logger.debug("detect encoding:", encoding);
encoding = encoding != "unknown" ? encoding : opts['encode'];
text = iconv.decode(buff, encoding);
if (err && text == "") { if (err && text == "") {
return event.sender.send('request-error-' + opts['hash'], err); return event.sender.send('request-error-' + opts['hash'], err);
}; };
// 回调数据 // 回调数据
event.sender.send('request-' + opts['hash'], { event.sender.send('request-' + opts['hash'], {
text: text, text: text,
buff: buff buff: buff,
encoding: encoding
}); });
}); });
} }
...@@ -280,4 +287,43 @@ class Request { ...@@ -280,4 +287,43 @@ class Request {
} }
/**
* 判断指定buffer对象的字符编码
* ref: https://github.com/LeoYuan/leoyuan.github.io/issues/25
* @param buffer
* @param options
* - defaultEncoding 指定默认编码集
* - minConfidence 指定可接受的最小confidence,如果判断结果小于此值,则用defaultEncoding
* - verbose 返回更加详细的字符编码数据
* @returns {*}
*/
function detectEncoding(buffer, options) {
options = options || {};
buffer = buffer || Buffer('');
var DEFAULT_ENCODING = 'GBK', MIN_CONFIDENCE = 0.96;
var verbose = options.verbose;
var defaultEncoding = options.defaultEncoding || DEFAULT_ENCODING;
var minConfidence = options.minConfidence || MIN_CONFIDENCE;
var ret = jschardet.detect(buffer), encoding = ret.encoding === 'ascii' ? 'utf-8' : ret.encoding,
confidence = ret.confidence;
// var VALID_ENCODINGS = ['gb2312', 'gbk', 'utf-8', 'big5', 'euc-kr','euc-jp'];
if (encoding === null || !iconv.encodingExists(encoding) || confidence < minConfidence) {
return verbose ? {
encoding: defaultEncoding,
oriEncoding: encoding,
confidence: confidence
} : defaultEncoding;
} else {
encoding = encoding.toUpperCase();
return verbose ? {
encoding: encoding,
oriEncoding: encoding,
confidence: confidence
} : encoding;
}
};
module.exports = Request; module.exports = Request;
{ {
"name": "antsword", "name": "antsword",
"version": "2.0.3", "version": "2.0.3.1",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
...@@ -446,6 +446,11 @@ ...@@ -446,6 +446,11 @@
"resolved": "http://registry.npm.taobao.org/isarray/download/isarray-1.0.0.tgz", "resolved": "http://registry.npm.taobao.org/isarray/download/isarray-1.0.0.tgz",
"integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE="
}, },
"jschardet": {
"version": "1.6.0",
"resolved": "http://registry.npm.taobao.org/jschardet/download/jschardet-1.6.0.tgz",
"integrity": "sha1-x9GnHtz/KDnbL57DD8XV69PBpng="
},
"jstransform": { "jstransform": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npm.taobao.org/jstransform/download/jstransform-3.0.0.tgz", "resolved": "https://registry.npm.taobao.org/jstransform/download/jstransform-3.0.0.tgz",
......
{ {
"name": "antsword", "name": "antsword",
"version": "2.0.3", "version": "2.0.3.1",
"description": "中国蚁剑是一款跨平台的开源网站管理工具", "description": "中国蚁剑是一款跨平台的开源网站管理工具",
"main": "app.js", "main": "app.js",
"dependencies": { "dependencies": {
"extract-zip": "^1.6.7", "extract-zip": "^1.6.7",
"geoips": "0.0.1", "geoips": "0.0.1",
"iconv-lite": "^0.4.23", "iconv-lite": "^0.4.23",
"jschardet": "^1.6.0",
"nedb": "^1.5.1", "nedb": "^1.5.1",
"superagent": "^3.8.3", "superagent": "^3.8.3",
"superagent-proxy": "^1.0.3", "superagent-proxy": "^1.0.3",
......
...@@ -223,6 +223,7 @@ class Base { ...@@ -223,6 +223,7 @@ class Base {
// 请求完毕返回数据{text,buff} // 请求完毕返回数据{text,buff}
.once(`request-${hash}`, (event, ret) => { .once(`request-${hash}`, (event, ret) => {
return res({ return res({
'encoding': ret['encoding']||"",
'text': ret['text'], 'text': ret['text'],
'buff': ret['buff'] 'buff': ret['buff']
}); });
......
...@@ -895,6 +895,12 @@ class FileManager { ...@@ -895,6 +895,12 @@ class FileManager {
).then((res) => { ).then((res) => {
let ret = res['text']; let ret = res['text'];
codes = ret; codes = ret;
let encoding = res['encoding'] || this.opts['encode'];
if(encoding.toUpperCase() == "UTF-8") {
encoding = "UTF8";
}
toolbar.setListOptionSelected('encode', `encode_${encoding}`);
win.progressOff(); win.progressOff();
// 初始化编辑器 // 初始化编辑器
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment