Skip to content
Browse files

oh right and a readme

  • Loading branch information...
1 parent a0c403c commit a7cbb93bc4feb536af30bea878ec0276d2b7970d @substack committed Nov 14, 2011
Showing with 123 additions and 28 deletions.
  1. +93 −0 README.markdown
  2. +25 −0 example/route.js
  3. +2 −25 example/simple.js
  4. +3 −3 index.js
View
93 README.markdown
@@ -0,0 +1,93 @@
+https-detect
+============
+
+Detect whether a stream is https or http based on the first buffer on a stream
+and forward accordingly.
+
+example
+=======
+
+simple.js
+---------
+
+Listen on `:4050` routing http traffic to `:4051` and https traffic to `:4052`.
+
+``` js
+var httpsDetect = require('https-detect');
+httpsDetect({ http : 4051, https : 4052 }).listen(4050);
+```
+
+route.js
+--------
+
+Fire up http and https servers and route traffic to them based on what the
+incoming messages look like.
+
+``` js
+var httpsDetect = require('https-detect');
+var http = require('http');
+var https = require('https');
+
+var fs = require('fs');
+var opts = {
+ key : fs.readFileSync(__dirname + '/../keys/privatekey.pem'),
+ cert : fs.readFileSync(__dirname + '/../keys/certificate.pem'),
+};
+
+var servers = {
+ http : http.createServer(function (req, res) {
+ res.setHeader('content-type', 'text/plain');
+ res.end('I am an http server!\r\n');
+ }),
+ https : https.createServer(opts, function (req, res) {
+ res.setHeader('content-type', 'text/plain');
+ res.end('I AM TOTALLY SECURE AND STUFF YOU GUYS.'
+ + ' SELF SIGNED IS TOTALLY SECURE, WHATEVER.\r\n');
+ }),
+};
+
+servers.http.listen(4051);
+servers.https.listen(4052);
+httpsDetect({ http : 4051, https : 4052 }).listen(4050);
+```
+
+output:
+
+```
+$ curl http://localhost:4050
+I am an http server!
+$ curl -k https://localhost:4050
+I AM TOTALLY SECURE AND STUFF YOU GUYS. SELF SIGNED IS TOTALLY SECURE, WHATEVER.
+$
+```
+
+methods
+=======
+
+``` js
+var httpsDetect = require('https-detect');
+```
+
+httpsDetect(opts)
+-----------------
+
+opts should have 2 keys:
+
+* http - the http server `[port,host]`
+* https - the https server `[port,host]`
+
+`host` defaults to localhost for both keys.
+
+If the option argument isn't an array it will be lifted to be `[port]`.
+
+install
+=======
+
+With [npm](http://npmjs.org), do:
+
+ npm install https-detect
+
+license
+=======
+
+MIT/X11
View
25 example/route.js
@@ -0,0 +1,25 @@
+var httpsDetect = require('https-detect');
+var http = require('http');
+var https = require('https');
+
+var fs = require('fs');
+var opts = {
+ key : fs.readFileSync(__dirname + '/../keys/privatekey.pem'),
+ cert : fs.readFileSync(__dirname + '/../keys/certificate.pem'),
+};
+
+var servers = {
+ http : http.createServer(function (req, res) {
+ res.setHeader('content-type', 'text/plain');
+ res.end('I am an http server!\r\n');
+ }),
+ https : https.createServer(opts, function (req, res) {
+ res.setHeader('content-type', 'text/plain');
+ res.end('I AM TOTALLY SECURE AND STUFF YOU GUYS.'
+ + ' SELF SIGNED IS TOTALLY SECURE, WHATEVER.\r\n');
+ }),
+};
+
+servers.http.listen(4051);
+servers.https.listen(4052);
+httpsDetect({ http : 4051, https : 4052 }).listen(4050);
View
27 example/simple.js
@@ -1,25 +1,2 @@
-var detector = require('../');
-var http = require('http');
-var https = require('https');
-
-var fs = require('fs');
-var opts = {
- key : fs.readFileSync(__dirname + '/../keys/privatekey.pem'),
- cert : fs.readFileSync(__dirname + '/../keys/certificate.pem'),
-};
-
-var servers = {
- http : http.createServer(function (req, res) {
- res.setHeader('content-type', 'text/plain');
- res.end('I am an http server!\r\n');
- }),
- https : https.createServer(opts, function (req, res) {
- res.setHeader('content-type', 'text/plain');
- res.end('I AM TOTALLY SECURE AND STUFF YOU GUYS.'
- + ' SELF SIGNED IS TOTALLY SECURE, WHATEVER.\r\n');
- }),
-};
-
-servers.http.listen(4051);
-servers.https.listen(4052);
-detector({ http : 4051, https : 4052 }).listen(4050);
+var httpsDetect = require('https-detect');
+httpsDetect({ http : 4051, https : 4052 }).listen(4050);
View
6 index.js
@@ -1,7 +1,7 @@
var net = require('net');
var EventEmitter = require('events').EventEmitter;
-var exports = module.exports = function (opts) {
+module.exports = function (opts) {
var server = net.createServer(function (stream) {
detect(stream, handler);
});
@@ -31,7 +31,7 @@ var exports = module.exports = function (opts) {
return server;
};
-var detect = exports.detect = function (stream, cb) {
+function detect (stream, cb) {
stream.once('data', function (buf) {
if (buf.slice(0,8).toString().match(/^([A-Za-z]+) /)) {
cb('http', stream, buf);
@@ -40,4 +40,4 @@ var detect = exports.detect = function (stream, cb) {
cb('https', stream, buf);
}
});
-};
+}

0 comments on commit a7cbb93

Please sign in to comment.
Something went wrong with that request. Please try again.