Permalink
Browse files

initial naive implementation, untested

  • Loading branch information...
0 parents commit 60118b3680ca4fc34cfe8fc5bf47d8b7f56c4522 @substack committed Nov 14, 2011
Showing with 35 additions and 0 deletions.
  1. +35 −0 index.js
@@ -0,0 +1,35 @@
+var net = require('net');
+var EventEmitter = require('events').EventEmitter;
+
+var exports = module.exports = function (opts) {
+ var server = net.createServer(detect, function (proto, stream, buf) {
+ var target = opts[proto];
+ if (typeof target === 'object' && target.write) {
+ target.write(buf);
+ stream.pipe(target);
+ target.pipe(stream);
+ }
+ else {
+ stream.pause();
+ var c = net.createConnection.apply(null, target);
+ c.on('connect', function () {
+ c.write(buf);
+ c.pipe(stream);
+ stream.pipe(c);
+ stream.resume();
+ });
+ }
+ });
+ return server;
+};
+
+var detect = exports.detect = function (stream, cb) {
+ stream.once('data', function (buf) {
+ if (buf.slice(0,8).toString().match(/^([A-Za-z]+) /)) {
+ cb('http', stream, buf);
+ }
+ else {
+ cb('https', stream, buf);
+ }
+ });
+};

0 comments on commit 60118b3

Please sign in to comment.