Skip to content

Commit 5a5aa42

Browse files
committed
1 parent 5f06e47 commit 5a5aa42

File tree

3 files changed

+77
-18
lines changed

3 files changed

+77
-18
lines changed

index.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module.exports = function proxy(host, options) {
4040
var limit = options.limit || '1mb';
4141

4242
return function handleProxy(req, res, next) {
43-
if (filter && !filter(req, res)) next();
43+
if (filter && !filter(req, res)) return next();
4444

4545
var headers = options.headers || {};
4646
var path;
@@ -93,26 +93,42 @@ module.exports = function proxy(host, options) {
9393
var rspData = Buffer.concat(chunks, totalLength);
9494

9595
if (intercept) {
96-
intercept(rspData, req, res, function(err, rsp, sent) {
96+
intercept(rspData, req, res, function(err, rspd, sent) {
9797
if (err) {
9898
return next(err);
9999
}
100+
101+
var encode = 'utf8';
102+
if (rsp.headers && rsp.headers['content-type']) {
103+
var contentType = rsp.headers['content-type'];
104+
if (/charset=/.test(contentType)) {
105+
var attrs = contentType.split(';').map(function(str) { return str.trim(); });
106+
for(var i = 0, len = attrs.length; i < len; i++) {
107+
var attr = attrs[i];
108+
if (/charset=/.test(attr)) {
109+
// encode = attr.split('=')[1];
110+
break;
111+
}
112+
}
113+
}
114+
}
115+
116+
if (typeof rspd == 'string')
117+
rspd = new Buffer(rspd, encode);
100118

101-
if (typeof rsp == 'string')
102-
rsp = new Buffer(rsp, 'utf8');
103-
104-
if (!Buffer.isBuffer(rsp)) {
119+
if (!Buffer.isBuffer(rspd)) {
105120
next(new Error("intercept should return string or buffer as data"));
106121
}
107122

108123
if (!res.headersSent)
109-
res.set('content-length', rsp.length);
110-
else if (rsp.length != rspData.length) {
124+
res.set('content-length', rspd.length);
125+
else if (rspd.length != rspData.length) {
111126
next(new Error("'Content-Length' is already sent, the length of response data can not be changed"));
112127
}
113128

114-
if (!sent)
115-
res.send(rsp);
129+
if (!sent) {
130+
res.send(rspd);
131+
}
116132
});
117133
} else {
118134
res.send(rspData);

package.json

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "express-http-proxy",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "http proxy middleware for express",
55
"main": "index.js",
66
"scripts": {
@@ -27,27 +27,34 @@
2727
},
2828
"devDependencies": {
2929
"express": "^4.3.1",
30+
"iconv": "^2.1.4",
3031
"jshint": "^2.5.5",
31-
"mocha": "^1.19.0",
32+
"mocha": "^2.1.0",
3233
"supertest": "^0.13.0"
3334
},
3435
"dependencies": {
3536
"type-is": "^1.2.0",
3637
"raw-body": "^1.1.6"
3738
},
38-
"contributors": [{
39+
"contributors": [
40+
{
3941
"name": "Liam Bennett"
40-
}, {
42+
},
43+
{
4144
"name": "eldereal",
4245
"url": "https://github.com/eldereal"
43-
}, {
46+
},
47+
{
4448
"name": "Saulius Menkevičius",
4549
"url": "https://github.com/razzmatazz"
46-
}, {
50+
},
51+
{
4752
"name": "Jérémy Lal",
4853
"email": "kapouer@melix.org"
49-
}, {
54+
},
55+
{
5056
"name": "Wei Gao",
5157
"email": "jky239@gmail.com"
52-
}]
58+
}
59+
]
5360
}

test/test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,43 @@ describe('http-proxy', function() {
6565
});
6666
});
6767

68+
it('test intercept on html response',function(done) {
69+
var app = express();
70+
app.use(proxy('httpbin.org', {
71+
intercept: function(data, req, res, cb) {
72+
data = data.toString().replace('Oh','<strong>Hey</strong>');
73+
assert(data !== "");
74+
cb(null, data);
75+
}
76+
}));
77+
78+
request(app)
79+
.get('/html')
80+
.end(function(err, res) {
81+
if (err) return done(err);
82+
assert(res.text.indexOf('<strong>Hey</strong>') > -1);
83+
done();
84+
});
85+
});
6886

87+
it('test github api', function(done) {
88+
var app = express();
89+
app.use(proxy('https://api.github.com', {
90+
intercept: function(data, req, res, cb) {
91+
var Iconv = require('iconv').Iconv;
92+
var iconv = new Iconv('UTF-8', 'utf8');
93+
cb(null, data);
94+
}
95+
}));
96+
97+
request(app)
98+
.get('/')
99+
.end(function(err, res) {
100+
if (err) return done(err);
101+
done();
102+
});
103+
104+
});
69105
});
70106

71107

0 commit comments

Comments
 (0)