Commit dd23d134 authored by antoor's avatar antoor

优化数据截取处理算法

增加了判断是否已经获取到前截断符,避免无效数据混入
parent 1214be30
...@@ -49,24 +49,30 @@ class Request { ...@@ -49,24 +49,30 @@ class Request {
// 数据转换二进制处理 // 数据转换二进制处理
res.setEncoding('binary'); res.setEncoding('binary');
res.data = ''; res.data = '';
let foundTagS = false;
let foundTagE = false;
res.on('data', (chunk) => { res.on('data', (chunk) => {
let temp = ''; let temp = '';
// 如果包含前后截断,则截取中间 // 如果包含前后截断,则截取中间
if (chunk.indexOf([tag_s]) >= 0 && chunk.lastIndexOf(tag_e) >= 0) { if (chunk.indexOf(tag_s) >= 0 && chunk.lastIndexOf(tag_e) >= 0) {
const index_s = chunk.indexOf(tag_s); const index_s = chunk.indexOf(tag_s);
const index_e = chunk.lastIndexOf(tag_e); const index_e = chunk.lastIndexOf(tag_e);
temp = chunk.substr(index_s + tag_s.length, index_e - index_s - tag_e.length); temp = chunk.substr(index_s + tag_s.length, index_e - index_s - tag_e.length);
foundTagS = foundTagE = true;
} }
// 如果只包含前截断,则截取后边 // 如果只包含前截断,则截取后边
else if (chunk.indexOf(tag_s) >= 0 && chunk.lastIndexOf(tag_e) === -1) { else if (chunk.indexOf(tag_s) >= 0 && chunk.lastIndexOf(tag_e) === -1) {
temp = chunk.split(tag_s)[1]; temp = chunk.split(tag_s)[1];
foundTagS = true;
} }
// 如果只包含后截断,则截取前边 // 如果只包含后截断,则截取前边
else if (chunk.indexOf(tag_s) === -1 && chunk.lastIndexOf(tag_e) >= 0) { else if (chunk.indexOf(tag_s) === -1 && chunk.lastIndexOf(tag_e) >= 0) {
temp = chunk.split(tag_e)[0]; temp = chunk.split(tag_e)[0];
foundTagE = true;
} }
// 如果有没有,那就是中途迷路的数据啦 ^.^ // 如果有没有,那就是中途迷路的数据啦 ^.^
else { else if (foundTagS && !foundTagE) {
temp = chunk; temp = chunk;
} }
......
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