Skip to content

Commit bf3a1a4

Browse files
committed
v0.0.7 add modifyHash plugin
1 parent b667fbe commit bf3a1a4

File tree

4 files changed

+165
-2
lines changed

4 files changed

+165
-2
lines changed

lib/ProxyServer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ ProxyServer.prototype.__addHost = function addHost(raw) {
147147

148148
//plugins
149149
ProxyServer.plugins = {
150-
replaceParamsInHash: require('./plugins/replaceParamsInHash.js')
150+
replaceParamsInHash: require('./plugins/replaceParamsInHash.js'),
151+
modifyHash: require('./plugins/modifyHash')
151152
};
152153

153154
module.exports = ProxyServer;

lib/plugins/modifyHash.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* User: jo
3+
* Date: 21.05.13
4+
* Time: 18:15
5+
*
6+
*/
7+
8+
var url = require('url'),
9+
async = require('async'),
10+
tools = require('../tools'),
11+
getHash = tools.getHash,
12+
getEndHadersIndex = tools.getEndHeaderIndex;
13+
14+
module.exports = function makeModifyHash(modFuncArr) {
15+
return function modifyHash(obj, next) {
16+
var raw = obj.raw,
17+
index = getEndHadersIndex(raw),
18+
head = parseHead(raw.slice(0, index));
19+
20+
head._raw = raw;
21+
head._hash = obj.hash;
22+
head.body = raw.slice(index + 4);
23+
24+
async.applyEachSeries(modFuncArr, head, function () {
25+
var newHead = makeHead(head),
26+
hash = tools.getHash()
27+
.update(newHead)
28+
.update(head.body)
29+
.end();
30+
31+
obj.hash = hash;
32+
next();
33+
});
34+
};
35+
};
36+
37+
38+
function parseHead(buff) {
39+
var lns = buff.toString('utf8').split(/\r\n/),
40+
first = lns[0].split(' '),
41+
version,
42+
urlObj = url.parse(first[1], true),
43+
headers = {};
44+
45+
for (var i = 1, l = lns.length; l > i; i += 1) {
46+
var ln = lns[i].match(/^([\w-]+): (.*)$/);
47+
if (!ln) continue;
48+
49+
headers[ln[1]] = ln[2];
50+
}
51+
52+
if (first[2]) {
53+
version = first[2].split('/')[1];
54+
}
55+
56+
57+
58+
return {
59+
method: first[0],
60+
uri: {
61+
pathname: urlObj.pathname,
62+
query: urlObj.query
63+
},
64+
version: version,
65+
headers: headers
66+
};
67+
}
68+
69+
function makeHead(obj) {
70+
var headers = obj.headers,
71+
arr = [
72+
obj.method.toUpperCase() + ' ' + url.format(obj.uri) + (obj.version ? ' HTTP/' + obj.version: '')
73+
];
74+
75+
for (var name in headers) {
76+
arr.push(name + ': ' + headers[name]);
77+
}
78+
79+
arr.push('\r\n');
80+
return new Buffer(arr.join('\r\n'));
81+
}

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.6",
4+
"version": "0.0.7",
55
"author": "Evgeny Reznichenko <[email protected]>",
66
"dependencies": {
77
"async": "0.2.8"

test/plugins.js

+81
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
var should = require('should'),
10+
url = require('url'),
1011
Proxy = require('../lib/ProxyServer.js'),
1112
tools = require('../lib/tools.js');
1213

@@ -46,4 +47,84 @@ describe('plugins', function () {
4647
});
4748
});
4849

50+
/**
51+
* Test for modifyHash
52+
*/
53+
describe('#modifyHash', function () {
54+
var modifyHash = plugins.modifyHash;
55+
56+
it('should call mod function and delegate parsed params', function (done) {
57+
var raw = new Buffer('POST /index?bar=1&foo=2 HTTP/1.1\r\nHost: localhost\r\n\r\nsome data'),
58+
hash = tools.getHash().update(raw).end(),
59+
is1 = false,
60+
is2 = false,
61+
urlObj = url.parse('/index?bar=1&foo=2', true),
62+
uri = {
63+
pathname: urlObj.pathname,
64+
query: urlObj.query
65+
};
66+
67+
var obj = { raw: raw, hash: hash };
68+
69+
function modfn1 (req, next) {
70+
is1 = true;
71+
req.should.have.property('_raw', raw);
72+
req.should.have.property('_hash', hash);
73+
req.should.have.property('method', 'POST');
74+
req.should.have.property('uri').and.obj.should.eql(uri);
75+
req.should.have.property('version', '1.1');
76+
req.should.have.property('headers').and.obj.should.eql({ 'Host': 'localhost' });
77+
req.should.have.property('body');
78+
next();
79+
}
80+
81+
function modfn2 (req, next) {
82+
is2 = true;
83+
req.should.have.property('_raw', raw);
84+
req.should.have.property('_hash', hash);
85+
req.should.have.property('method', 'POST');
86+
req.should.have.property('uri').and.obj.should.eql(uri);
87+
req.should.have.property('version', '1.1');
88+
req.should.have.property('headers').and.obj.should.eql({ 'Host': 'localhost' });
89+
req.should.have.property('body');
90+
next();
91+
}
92+
93+
var plugin = modifyHash([modfn1, modfn2]);
94+
95+
plugin(obj, function (err) {
96+
is1.should.be.ok;
97+
is2.should.be.ok;
98+
99+
done();
100+
});
101+
});
102+
103+
it('should calculate new hash sum', function (done) {
104+
var raw = new Buffer('GET /index?bar=1&foo=2 HTTP/1.1\r\nHost: localhost\r\n\r\n'),
105+
hash = tools.getHash().update(raw).end(),
106+
expectHash = tools.getHash()
107+
.update(new Buffer('GET /index?bar=1&foo=4 HTTP/1.1\r\nHost: localhost1\r\n\r\n'))
108+
.end();
109+
110+
var obj = { raw: raw, hash: hash };
111+
112+
function mod(req, next) {
113+
req.uri.query.foo = 4;
114+
req.headers.Host = 'localhost1';
115+
next();
116+
}
117+
118+
var plugin = modifyHash([mod]);
119+
120+
plugin(obj, function (err) {
121+
should.not.exist(err);
122+
obj.raw.should.eql(raw);
123+
obj.hash.should.eql(expectHash);
124+
125+
done();
126+
});
127+
});
128+
});
129+
49130
});

0 commit comments

Comments
 (0)