Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

hacks to intercept and destroy content-length in non-identity mode

  • Loading branch information...
commit d3ba806446805cd8d510cc3942fd4a0b4fad48c3 1 parent 120100f
@substack authored
Showing with 36 additions and 17 deletions.
  1. +4 −1 example/server.js
  2. +32 −16 index.js
View
5 example/server.js
@@ -3,6 +3,9 @@ var filed = require('filed');
var http = require('http');
var server = http.createServer(function (req, res) {
- filed(__dirname + '/data.txt').pipe(oppressor(req)).pipe(res);
+ filed(__dirname + '/data.txt')
+ .pipe(oppressor(req))
+ .pipe(res)
+ ;
});
server.listen(8000);
View
48 index.js
@@ -31,13 +31,14 @@ module.exports = function (req) {
target[p.name].apply(target, p.arguments);
});
- proxyResponse(stream, target);
+ target.removeHeader('content-length');
+ proxyResponse(stream, target, enc !== 'identity');
}
return pipe.apply(this, arguments);
};
stream.statusCode = 200;
- var proxied = proxyResponse(stream);
+ var proxied = proxyResponse(stream, null, enc !== 'identity');
return stream;
};
@@ -56,23 +57,38 @@ function unacceptable () {
return stream;
}
-function proxyResponse (stream, dst) {
+function proxyResponse (stream, dst, doIntercept) {
// proxy calls through so this module works with request and filed-style
// streaming pipe http response hijacking
var proxied = [];
[ 'writeContinue', 'writeHead', 'setHeader', 'sendDate', 'getHeader',
- 'removeHeader', 'addTrailers' ]
- .forEach(function (name) {
- stream[name] = dst
- ? function () {
- return dst[name].apply(dst, arguments);
- }
- : function () {
- // hopefully the return value wasn't important >_<
- proxied.push({ name : name, arguments : arguments });
- }
- ;
- })
- ;
+ 'removeHeader', 'addTrailers' ].forEach(function (name) {
+ stream[name] = dst
+ ? function () {
+ if (doIntercept && intercept(name, arguments)) return;
+ return dst[name].apply(dst, arguments);
+ }
+ : function () {
+ // hopefully the return value wasn't important >_<
+ if (doIntercept && intercept(name, arguments)) return;
+ proxied.push({ name : name, arguments : arguments });
+ }
+ ;
+ });
return proxied;
}
+
+function intercept (name, args) {
+ if (name === 'setHeader'
+ && String(args[0].toLowerCase()) === 'content-length') {
+
+ return true;
+ }
+ if (name === 'writeHead' && typeof args[1] === 'object') {
+ Object.keys(args[1]).forEach(function (key) {
+ if (String(key).toLowerCase() === 'content-length') {
+ delete args[0][key];
+ }
+ });
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.