Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

[api]SPDY support via node-spdy #32

Merged
merged 5 commits into from about 2 years ago

2 participants

Paweł Krefta Pavan Kumar Sunkara
Paweł Krefta

@indutny's node-spdy module is compatible with standard http/https interface so implementation was fairly simple and required changes only in core.createServer method. Because SPDY requires SSL we need to pass the same parameters as we pass into https but with a different option key - spdy instead of https. If client's browser doesn't support SPDY it will nicely degrade to https.
I didn't write any tests because I don't know any standard method to detect SPDY on server - if necessary I can add some additional header to response and write a simple test.

Paweł Krefta

Simple code how to use it

var union = require('./lib'),
    director = require('director');

var router = new director.http.Router();

var server = union.createServer({
  before: [
    function (req, res) {
      var found = router.dispatch(req, res);
      if (!found) {
        res.emit('next');
      }
    }
  ],
  spdy :{
    key: '../certs/privatekey.pem',
    cert: '../certs/certificate.pem'
  }
});

router.get(/foo/, function () {
  this.res.writeHead(200, { 'Content-Type': 'text/plain' })
  this.res.end('hello world\n');
});

server.listen(9090, function(){
  console.log('union with director running on 9090 with SPDY');
});
lib/core.js
((5 lines not shown))
54   - if (!options.https.key || !options.https.cert) {
55   - throw new Error('Both `options.https.key` and `options.https.cert` are required.');
  53 + //
  54 + // both https and spdy requires same params
  55 + //
  56 + if (options.https || options.spdy) {
  57 +
  58 + var key;
  59 +
  60 + if(options.spdy) {
  61 + key = 'spdy';
  62 + } else {
  63 + key = 'https';
  64 + }
  65 +
  66 + server_options = options[key];
1
Pavan Kumar Sunkara
pksunkara added a note
  • Memory leak, you didn't declare server_options
  • We don't use underscore in variable names. Use camelized names. serverOptions

Otherwise, lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Pavan Kumar Sunkara

And can you write the above example in examples folder and document the SPDY usage in README.md too?

Paweł Krefta

Yes of course - I'll provide fixes (forgotten var :/), examples and docs.

Paweł Krefta pkrefta referenced this pull request
Closed

SPDY support #31

Pavan Kumar Sunkara pksunkara merged commit 942cc6d into from
Pavan Kumar Sunkara pksunkara closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 3 changed files with 74 additions and 18 deletions. Show diff stats Hide diff stats

  1. +12 10 README.md
  2. +30 0 examples/simple/spdy.js
  3. +32 8 lib/core.js
22 README.md
Source Rendered
@@ -30,14 +30,12 @@ In addition, the response object passed to middlewares listens for a "next" even
30 30 ``` js
31 31 var fs = require('fs'),
32 32 union = require('../lib'),
33   - director = require('director'),
34   - favicon = require('./middleware/favicon');
  33 + director = require('director');
35 34
36 35 var router = new director.http.Router();
37 36
38 37 var server = union.createServer({
39 38 before: [
40   - favicon('./favicon.png'),
41 39 function (req, res) {
42 40 var found = router.dispatch(req, res);
43 41 if (!found) {
@@ -116,6 +114,8 @@ var server = union.createServer({
116 114 }).listen(3000);
117 115 ```
118 116
  117 +### SPDY enabled server example
  118 +
119 119 # API
120 120
121 121 ## union Static Members
@@ -149,6 +149,10 @@ Specification
149 149 (optional) A value that specifies the certificate and key necessary to create an instance of
150 150 `https.Server`.
151 151
  152 + @option spdy {Object}
  153 + (optional) A value that specifies the certificate and key necessary to create an instance of
  154 + `spdy.Server`.
  155 +
152 156 @option headers {Object}
153 157 (optional) An object representing a set of headers to set in every outgoing response
154 158 ```
@@ -169,15 +173,13 @@ var server = union.createServer({
169 173 });
170 174 ```
171 175
172   -An example of the `https` option.
  176 +An example of the `https` or `spdy` option.
173 177
174 178 ``` js
175 179 {
176   - https: {
177   - cert: 'path/to/cert.pem',
178   - key: 'path/to/key.pem',
179   - ca: 'path/to/ca.pem'
180   - }
  180 + cert: 'path/to/cert.pem',
  181 + key: 'path/to/key.pem',
  182 + ca: 'path/to/ca.pem'
181 183 }
182 184 ```
183 185
@@ -310,7 +312,7 @@ All tests are written with [vows][0] and should be run with [npm][1]:
310 312
311 313 (The MIT License)
312 314
313   -Copyright (c) 2010 Nodejitsu Inc. <http://www.twitter.com/nodejitsu>
  315 +Copyright (c) 2010-2012 Nodejitsu Inc. <http://www.twitter.com/nodejitsu>
314 316
315 317 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
316 318
30 examples/simple/spdy.js
... ... @@ -0,0 +1,30 @@
  1 +// In order to run this example you need to
  2 +// generate local ssl certificate
  3 +var union = require('../../lib'),
  4 + director = require('director');
  5 +
  6 +var router = new director.http.Router();
  7 +
  8 +var server = union.createServer({
  9 + before: [
  10 + function (req, res) {
  11 + var found = router.dispatch(req, res);
  12 + if (!found) {
  13 + res.emit('next');
  14 + }
  15 + }
  16 + ],
  17 + spdy :{
  18 + key: './certs/privatekey.pem',
  19 + cert: './certs/certificate.pem'
  20 + }
  21 +});
  22 +
  23 +router.get(/foo/, function () {
  24 + this.res.writeHead(200, { 'Content-Type': 'text/plain' })
  25 + this.res.end('hello world\n');
  26 +});
  27 +
  28 +server.listen(9090, function(){
  29 + console.log('union with director running on 9090 with SPDY');
  30 +});
40 lib/core.js
... ... @@ -1,5 +1,5 @@
1 1 /*
2   - * core.js: Core functionality for the Flatiron HTTP plugin.
  2 + * core.js: Core functionality for the Flatiron HTTP (with SPDY support) plugin.
3 3 *
4 4 * (C) 2011, Nodejitsu Inc.
5 5 * MIT LICENSE
@@ -50,18 +50,42 @@ core.createServer = function (options) {
50 50 req.pipe(routingStream);
51 51 }
52 52
53   - if (options.https) {
54   - if (!options.https.key || !options.https.cert) {
55   - throw new Error('Both `options.https.key` and `options.https.cert` are required.');
  53 + //
  54 + // both https and spdy requires same params
  55 + //
  56 + if (options.https || options.spdy) {
  57 +
  58 + if(options.https && options.spdy){
  59 + throw new Error('You shouldn\'t be using https and spdy simultaneously.')
  60 + }
  61 +
  62 + var key, serverOptions, credentials;
  63 +
  64 + if(options.spdy) {
  65 + key = 'spdy';
  66 + } else {
  67 + key = 'https';
  68 + }
  69 +
  70 + serverOptions = options[key];
  71 +
  72 + if (!serverOptions.key || !serverOptions.cert) {
  73 + throw new Error('Both options.'+key+'.`key` and options.'+key+'.`cert` are required.');
56 74 }
57 75
58 76 credentials = {
59   - key: fs.readFileSync(options.https.key),
60   - cert: fs.readFileSync(options.https.cert)
  77 + key: fs.readFileSync(serverOptions.key),
  78 + cert: fs.readFileSync(serverOptions.cert)
61 79 };
62 80
63   - if (options.https.ca) {
64   - credentials.ca = fs.readFileSync(options.https.ca);
  81 + if (serverOptions.ca) {
  82 + credentials.ca = fs.readFileSync(serverOptions.ca);
  83 + }
  84 +
  85 + if(options.spdy){
  86 + // spdy is optional so we require module here rather than on top
  87 + var spdy = require('spdy');
  88 + return spdy.createServer(credentials, requestHandler);
65 89 }
66 90
67 91 return https.createServer(credentials, requestHandler);

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.