Permalink
Browse files

headers successfully pass through from filed through to the response

  • Loading branch information...
0 parents commit 5dff71c86494b34fa8961d2ca17834e58a275be6 @substack committed Aug 28, 2012
Showing with 96 additions and 0 deletions.
  1. +1 −0 example/data.txt
  2. +8 −0 example/server.js
  3. +79 −0 index.js
  4. +8 −0 readme.markdown
@@ -0,0 +1 @@
+beep boop
@@ -0,0 +1,8 @@
+var oppressor = require('../');
+var filed = require('filed');
+var http = require('http');
+
+var server = http.createServer(function (req, res) {
+ filed(__dirname + '/data.txt').pipe(oppressor(req)).pipe(res);
+});
+server.listen(8000);
@@ -0,0 +1,79 @@
+var Negotiator = require('negotiator');
+var zlib = require('zlib');
+var through = require('through');
+
+module.exports = function (req) {
+ var negotiator = new Negotiator(req);
+ var enc = negotiator.preferredEncodings([ 'gzip', 'compress', 'identity' ]);
+ if (Array.isArray(enc)) enc = enc[0];
+ var createStream = {
+ gzip : zlib.createGzip,
+ compress : zlib.createDeflate,
+ identity : through,
+ }[enc];
+
+ if (!createStream) return unacceptable();
+ var stream = createStream();
+
+ stream = createStream();
+
+ var pipe = stream.pipe;
+ stream.pipe = function (target) {
+ if (target && target.writeHead && target.setHeader) {
+ if (target.statusCode === 200) {
+ target.statusCode = stream.statusCode;
+ }
+
+ if (!target._headers
+ || target._headers['content-encoding'] === undefined) {
+ target.setHeader('content-encoding', enc);
+ }
+
+ proxied.forEach(function (p) {
+ target[p.name].apply(target, p.arguments);
+ });
+
+ proxyResponse(stream, target);
+ }
+ return pipe.apply(this, arguments);
+ };
+
+ stream.statusCode = 200;
+ var proxied = proxyResponse(stream);
+ return stream;
+};
+
+function unacceptable () {
+ // as per http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
+ var stream = through();
+ var proxied = proxyResponse(stream);
+ stream.pipe = function (target) {
+ if (target.statusCode && target.write && target.end) {
+ target.statusCode = 406;
+ target.write('encoding not accepted');
+ target.end();
+ }
+ else stream.emit('error', 'encoding not accepted');
+ };
+ return stream;
+}
+
+function proxyResponse (stream, dst) {
+ // 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 () {
+ proxied.push({ name : name, arguments : arguments });
+ }
+ ;
+ })
+ ;
+ return proxied;
+}
@@ -0,0 +1,8 @@
+# oppressor
+
+streaming http compression negotiator
+
+# example
+
+``` js
+```

0 comments on commit 5dff71c

Please sign in to comment.