From 631b6307cea43b8a0af3ae489b99ca18e4220501 Mon Sep 17 00:00:00 2001 From: OttoMao Date: Wed, 28 Jan 2015 16:22:48 +0800 Subject: [PATCH] add support for charsets other than gbk --- CHANGELOG | 6 +++++- README.md | 2 +- package.json | 2 +- proxy.js | 13 ++++++++++--- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 7499b19..f749c8c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,8 @@ -22 Jan 2015: anyproxy 3.1.1: +28 Jan 2015: anyproxy 3.1.2: + + * thanks to iconv-lite, almost webpage with any charset can be correctly decoded in web interface. + +28 Jan 2015: anyproxy 3.1.1: * convert GBK to UTF8 in web interface diff --git a/README.md b/README.md index e1573a8..61f2dc7 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Feature * work as http or https proxy * fully configurable, you can modify a request at any stage by your own javascript code * when working as https proxy, it can generate and intercept https requests for any domain without complaint by browser (after you trust its root CA) -* a web interface is availabe for you to view request details +* a web interface for you to watch realtime request details, where html string with (almost) any charset could be shown correctly * (beta)a web UI interface for you to replace some remote response with local data ![screenshot](http://gtms01.alicdn.com/tps/i1/TB1IdgqGXXXXXa9apXXLExM2pXX-854-480.gif) diff --git a/package.json b/package.json index d0a4710..e6f8246 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "anyproxy", - "version": "3.1.1", + "version": "3.1.2", "description": "A fully configurable proxy in NodeJS, which can handle HTTPS requests perfectly.", "main": "proxy.js", "bin": { diff --git a/proxy.js b/proxy.js index 547ff3c..86a88c1 100644 --- a/proxy.js +++ b/proxy.js @@ -156,9 +156,16 @@ function proxyServer(option){ bodyContent = GLOBAL.recorder.getBody(data.id); result = bodyContent; - if(/charset=gbk/i.test(JSON.stringify(resHeader))){ - result = iconv.decode(bodyContent, 'GBK'); - } + //detect charset and convert to utf8 for web interface + 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)){ + result = iconv.decode(bodyContent, currentCharset); + } + } + }catch(e){} child_webServer.send({ type : "body",