Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[api]SPDY support via node-spdy #32

Merged
merged 5 commits into from

2 participants

@pkrefta

@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.

@pkrefta

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))
- if (!options.https.key || !options.https.cert) {
- throw new Error('Both `options.https.key` and `options.https.cert` are required.');
+ //
+ // both https and spdy requires same params
+ //
+ if (options.https || options.spdy) {
+
+ var key;
+
+ if(options.spdy) {
+ key = 'spdy';
+ } else {
+ key = 'https';
+ }
+
+ server_options = options[key];
  • 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
@pksunkara

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

@pkrefta

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

@pkrefta pkrefta referenced this pull request
Closed

SPDY support #31

@pksunkara pksunkara merged commit 942cc6d into from
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 with 74 additions and 18 deletions.
  1. +12 −10 README.md
  2. +30 −0 examples/simple/spdy.js
  3. +32 −8 lib/core.js
View
22 README.md
@@ -30,14 +30,12 @@ In addition, the response object passed to middlewares listens for a "next" even
``` js
var fs = require('fs'),
union = require('../lib'),
- director = require('director'),
- favicon = require('./middleware/favicon');
+ director = require('director');
var router = new director.http.Router();
var server = union.createServer({
before: [
- favicon('./favicon.png'),
function (req, res) {
var found = router.dispatch(req, res);
if (!found) {
@@ -116,6 +114,8 @@ var server = union.createServer({
}).listen(3000);
```
+### SPDY enabled server example
+
# API
## union Static Members
@@ -149,6 +149,10 @@ Specification
(optional) A value that specifies the certificate and key necessary to create an instance of
`https.Server`.
+ @option spdy {Object}
+ (optional) A value that specifies the certificate and key necessary to create an instance of
+ `spdy.Server`.
+
@option headers {Object}
(optional) An object representing a set of headers to set in every outgoing response
```
@@ -169,15 +173,13 @@ var server = union.createServer({
});
```
-An example of the `https` option.
+An example of the `https` or `spdy` option.
``` js
{
- https: {
- cert: 'path/to/cert.pem',
- key: 'path/to/key.pem',
- ca: 'path/to/ca.pem'
- }
+ cert: 'path/to/cert.pem',
+ key: 'path/to/key.pem',
+ ca: 'path/to/ca.pem'
}
```
@@ -310,7 +312,7 @@ All tests are written with [vows][0] and should be run with [npm][1]:
(The MIT License)
-Copyright (c) 2010 Nodejitsu Inc. <http://www.twitter.com/nodejitsu>
+Copyright (c) 2010-2012 Nodejitsu Inc. <http://www.twitter.com/nodejitsu>
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:
View
30 examples/simple/spdy.js
@@ -0,0 +1,30 @@
+// In order to run this example you need to
+// generate local ssl certificate
+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');
+});
View
40 lib/core.js
@@ -1,5 +1,5 @@
/*
- * core.js: Core functionality for the Flatiron HTTP plugin.
+ * core.js: Core functionality for the Flatiron HTTP (with SPDY support) plugin.
*
* (C) 2011, Nodejitsu Inc.
* MIT LICENSE
@@ -50,18 +50,42 @@ core.createServer = function (options) {
req.pipe(routingStream);
}
- if (options.https) {
- if (!options.https.key || !options.https.cert) {
- throw new Error('Both `options.https.key` and `options.https.cert` are required.');
+ //
+ // both https and spdy requires same params
+ //
+ if (options.https || options.spdy) {
+
+ if(options.https && options.spdy){
+ throw new Error('You shouldn\'t be using https and spdy simultaneously.')
+ }
+
+ var key, serverOptions, credentials;
+
+ if(options.spdy) {
+ key = 'spdy';
+ } else {
+ key = 'https';
+ }
+
+ serverOptions = options[key];
+
+ if (!serverOptions.key || !serverOptions.cert) {
+ throw new Error('Both options.'+key+'.`key` and options.'+key+'.`cert` are required.');
}
credentials = {
- key: fs.readFileSync(options.https.key),
- cert: fs.readFileSync(options.https.cert)
+ key: fs.readFileSync(serverOptions.key),
+ cert: fs.readFileSync(serverOptions.cert)
};
- if (options.https.ca) {
- credentials.ca = fs.readFileSync(options.https.ca);
+ if (serverOptions.ca) {
+ credentials.ca = fs.readFileSync(serverOptions.ca);
+ }
+
+ if(options.spdy){
+ // spdy is optional so we require module here rather than on top
+ var spdy = require('spdy');
+ return spdy.createServer(credentials, requestHandler);
}
return https.createServer(credentials, requestHandler);
Something went wrong with that request. Please try again.