Skip to content

Commit 7b96731

Browse files
committed
v0.0.5 add replace Host on header
Add test for host change add replace host header
1 parent af40b23 commit 7b96731

File tree

5 files changed

+74
-11
lines changed

5 files changed

+74
-11
lines changed

lib/ProxyServer.js

+27-9
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,22 @@ function ProxyServer(opt) {
4141
ProxyServer.prototype.__requestListener = function __requestListener(client) {
4242
var self = this,
4343
parser = parsers(onMessageComplete),
44-
hash = tools.getHash(),
4544
chunks = [];
4645

4746
function clear() {
4847
client.removeListener('data', onClientData);
4948
}
5049

5150
function onMessageComplete () {
52-
var data = {
53-
raw: Buffer.concat(chunks),
54-
hash: hash.end()
55-
};
51+
var data,
52+
raw = Buffer.concat(chunks);
53+
54+
raw = self.__addHost(raw);
55+
56+
data = {
57+
raw: raw,
58+
hash: tools.getHash().update(raw).end()
59+
};
5660

5761
clear();
5862

@@ -62,11 +66,8 @@ ProxyServer.prototype.__requestListener = function __requestListener(client) {
6266
}
6367

6468
function onClientData(chunk) {
65-
process.nextTick(function () {
66-
parser.execute(chunk, 0, chunk.length);
67-
});
68-
hash.update(chunk);
6969
chunks.push(chunk);
70+
parser.execute(chunk, 0, chunk.length);
7071
}
7172

7273
client.on('data',onClientData);
@@ -127,6 +128,23 @@ ProxyServer.prototype.reset = function reset() {
127128
}
128129
};
129130

131+
ProxyServer.prototype.__addHost = function addHost(raw) {
132+
var index = tools.getEndHeaderIndex(raw),
133+
lns = raw.slice(0, index).toString('utf8').split('\r\n'),
134+
i, l;
135+
136+
for (i = 1, l = lns.length; l > i; i += 1) {
137+
if (!/^Host:/.test(lns[i])) continue;
138+
lns[i] = 'Host: ' + this.dest.host + ':' + this.dest.port;
139+
break;
140+
}
141+
142+
lns = lns.join('\r\n');
143+
raw = Buffer.concat([new Buffer(lns), raw.slice(index)]);
144+
145+
return raw;
146+
};
147+
130148
//plugins
131149
ProxyServer.plugins = {
132150
replaceParamsInHash: require('./plugins/replaceParamsInHash.js')

lib/tools.js

+19
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,22 @@ MD5.prototype.end = function end() {
2828
exports.getHash = function getHash() {
2929
return new MD5();
3030
};
31+
32+
33+
var R = 13, // \r
34+
N = 10; // \n
35+
/**
36+
* Return last header index from buffer
37+
*
38+
* @param {Buffer} buff Raw data
39+
* @return {Integer} Index
40+
*/
41+
exports.getEndHeaderIndex = function getEndHeaderIndex(buff) {
42+
var i, l;
43+
44+
for (i = 0, l = buff.length - 4; l > i; i += 1) {
45+
if (buff[i] === R && buff[i + 4] === N) break;
46+
}
47+
48+
return i;
49+
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "node-http-proxy-cache",
33
"description": "Http caching proxy for the development clients to different websites and api",
4-
"version": "0.0.4",
4+
"version": "0.0.5",
55
"author": "Evgeny Reznichenko <[email protected]>",
66
"dependencies": {
77
"async": "0.2.8"

test/proxyServer.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ describe('ProxyServer', function () {
6969
* Test for proxy
7070
*/
7171
describe('proxy', function () {
72-
var server, requestCount, proxy;
72+
var server, requestCount, proxy,
73+
lastRequestHeaders;
7374

7475
requestCount = 0;
7576
server = http.createServer(function (req, res) {
7677
requestCount += 1;
78+
lastRequestHeaders = req.headers;
7779
res.writeHead(200, { 'Content-Type': 'text/plain' });
7880
res.end('Hello');
7981
});
@@ -99,6 +101,7 @@ describe('ProxyServer', function () {
99101
beforeEach(function () {
100102
proxy.reset();
101103
requestCount = 0;
104+
lastRequestHeaders = null;
102105
});
103106

104107

@@ -121,6 +124,16 @@ describe('ProxyServer', function () {
121124

122125
makeReq(makeReq(done))();
123126
});
127+
128+
it('should change host', function (done) {
129+
req
130+
.get('localhost:' + LOCAL_PROXY_PORT)
131+
.end(function (err, res) {
132+
should.not.exist(err);
133+
lastRequestHeaders.should.have.property('host', proxy.dest.host + ':' + proxy.dest.port);
134+
done();
135+
});
136+
});
124137
});
125138

126139
/**

test/tools.js

+13
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,17 @@ describe('tools', function () {
3232
});
3333

3434
});
35+
36+
/**
37+
* Test for #getEndHeaderIndex
38+
*/
39+
describe('#getEndHeaderIndex', function () {
40+
var raw = new Buffer('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n'),
41+
last = raw.length - 4;
42+
43+
it('should return ' + last, function () {
44+
var index = tools.getEndHeaderIndex(raw);
45+
index.should.eql(last);
46+
});
47+
});
3548
});

0 commit comments

Comments
 (0)