Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

oh right and a readme

  • Loading branch information...
commit a7cbb93bc4feb536af30bea878ec0276d2b7970d 1 parent a0c403c
James Halliday authored
93 README.markdown
Source Rendered
... ... @@ -0,0 +1,93 @@
  1 +https-detect
  2 +============
  3 +
  4 +Detect whether a stream is https or http based on the first buffer on a stream
  5 +and forward accordingly.
  6 +
  7 +example
  8 +=======
  9 +
  10 +simple.js
  11 +---------
  12 +
  13 +Listen on `:4050` routing http traffic to `:4051` and https traffic to `:4052`.
  14 +
  15 +``` js
  16 +var httpsDetect = require('https-detect');
  17 +httpsDetect({ http : 4051, https : 4052 }).listen(4050);
  18 +```
  19 +
  20 +route.js
  21 +--------
  22 +
  23 +Fire up http and https servers and route traffic to them based on what the
  24 +incoming messages look like.
  25 +
  26 +``` js
  27 +var httpsDetect = require('https-detect');
  28 +var http = require('http');
  29 +var https = require('https');
  30 +
  31 +var fs = require('fs');
  32 +var opts = {
  33 + key : fs.readFileSync(__dirname + '/../keys/privatekey.pem'),
  34 + cert : fs.readFileSync(__dirname + '/../keys/certificate.pem'),
  35 +};
  36 +
  37 +var servers = {
  38 + http : http.createServer(function (req, res) {
  39 + res.setHeader('content-type', 'text/plain');
  40 + res.end('I am an http server!\r\n');
  41 + }),
  42 + https : https.createServer(opts, function (req, res) {
  43 + res.setHeader('content-type', 'text/plain');
  44 + res.end('I AM TOTALLY SECURE AND STUFF YOU GUYS.'
  45 + + ' SELF SIGNED IS TOTALLY SECURE, WHATEVER.\r\n');
  46 + }),
  47 +};
  48 +
  49 +servers.http.listen(4051);
  50 +servers.https.listen(4052);
  51 +httpsDetect({ http : 4051, https : 4052 }).listen(4050);
  52 +```
  53 +
  54 +output:
  55 +
  56 +```
  57 +$ curl http://localhost:4050
  58 +I am an http server!
  59 +$ curl -k https://localhost:4050
  60 +I AM TOTALLY SECURE AND STUFF YOU GUYS. SELF SIGNED IS TOTALLY SECURE, WHATEVER.
  61 +$
  62 +```
  63 +
  64 +methods
  65 +=======
  66 +
  67 +``` js
  68 +var httpsDetect = require('https-detect');
  69 +```
  70 +
  71 +httpsDetect(opts)
  72 +-----------------
  73 +
  74 +opts should have 2 keys:
  75 +
  76 +* http - the http server `[port,host]`
  77 +* https - the https server `[port,host]`
  78 +
  79 +`host` defaults to localhost for both keys.
  80 +
  81 +If the option argument isn't an array it will be lifted to be `[port]`.
  82 +
  83 +install
  84 +=======
  85 +
  86 +With [npm](http://npmjs.org), do:
  87 +
  88 + npm install https-detect
  89 +
  90 +license
  91 +=======
  92 +
  93 +MIT/X11
25 example/route.js
... ... @@ -0,0 +1,25 @@
  1 +var httpsDetect = require('https-detect');
  2 +var http = require('http');
  3 +var https = require('https');
  4 +
  5 +var fs = require('fs');
  6 +var opts = {
  7 + key : fs.readFileSync(__dirname + '/../keys/privatekey.pem'),
  8 + cert : fs.readFileSync(__dirname + '/../keys/certificate.pem'),
  9 +};
  10 +
  11 +var servers = {
  12 + http : http.createServer(function (req, res) {
  13 + res.setHeader('content-type', 'text/plain');
  14 + res.end('I am an http server!\r\n');
  15 + }),
  16 + https : https.createServer(opts, function (req, res) {
  17 + res.setHeader('content-type', 'text/plain');
  18 + res.end('I AM TOTALLY SECURE AND STUFF YOU GUYS.'
  19 + + ' SELF SIGNED IS TOTALLY SECURE, WHATEVER.\r\n');
  20 + }),
  21 +};
  22 +
  23 +servers.http.listen(4051);
  24 +servers.https.listen(4052);
  25 +httpsDetect({ http : 4051, https : 4052 }).listen(4050);
27 example/simple.js
... ... @@ -1,25 +1,2 @@
1   -var detector = require('../');
2   -var http = require('http');
3   -var https = require('https');
4   -
5   -var fs = require('fs');
6   -var opts = {
7   - key : fs.readFileSync(__dirname + '/../keys/privatekey.pem'),
8   - cert : fs.readFileSync(__dirname + '/../keys/certificate.pem'),
9   -};
10   -
11   -var servers = {
12   - http : http.createServer(function (req, res) {
13   - res.setHeader('content-type', 'text/plain');
14   - res.end('I am an http server!\r\n');
15   - }),
16   - https : https.createServer(opts, function (req, res) {
17   - res.setHeader('content-type', 'text/plain');
18   - res.end('I AM TOTALLY SECURE AND STUFF YOU GUYS.'
19   - + ' SELF SIGNED IS TOTALLY SECURE, WHATEVER.\r\n');
20   - }),
21   -};
22   -
23   -servers.http.listen(4051);
24   -servers.https.listen(4052);
25   -detector({ http : 4051, https : 4052 }).listen(4050);
  1 +var httpsDetect = require('https-detect');
  2 +httpsDetect({ http : 4051, https : 4052 }).listen(4050);
6 index.js
... ... @@ -1,7 +1,7 @@
1 1 var net = require('net');
2 2 var EventEmitter = require('events').EventEmitter;
3 3
4   -var exports = module.exports = function (opts) {
  4 +module.exports = function (opts) {
5 5 var server = net.createServer(function (stream) {
6 6 detect(stream, handler);
7 7 });
@@ -31,7 +31,7 @@ var exports = module.exports = function (opts) {
31 31 return server;
32 32 };
33 33
34   -var detect = exports.detect = function (stream, cb) {
  34 +function detect (stream, cb) {
35 35 stream.once('data', function (buf) {
36 36 if (buf.slice(0,8).toString().match(/^([A-Za-z]+) /)) {
37 37 cb('http', stream, buf);
@@ -40,4 +40,4 @@ var detect = exports.detect = function (stream, cb) {
40 40 cb('https', stream, buf);
41 41 }
42 42 });
43   -};
  43 +}

0 comments on commit a7cbb93

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