Skip to content

Commit

Permalink
feat(http2): add native HTTP/2 support (#1489)
Browse files Browse the repository at this point in the history
  • Loading branch information
hekike committed Nov 2, 2017
1 parent 6598949 commit 6b20285
Show file tree
Hide file tree
Showing 14 changed files with 1,792 additions and 1,500 deletions.
18 changes: 9 additions & 9 deletions docs/_api/request.md
Expand Up @@ -40,7 +40,7 @@ permalink: /docs/request-api/
**Extends http.IncomingMessage**

Wraps all of the node
[http.IncomingMessage](https://nodejs.org/api/http.html#http_http_incomingmessage)
[http.IncomingMessage](https://nodejs.org/api/http.html)
APIs, events and properties, plus the following.

### accepts
Expand All @@ -55,8 +55,8 @@ Otherwise the given type is matched by an exact match, and then subtypes.

**Examples**

_You may pass the subtype such as html which is then converted internally to
text/html using the mime lookup table:_
_You may pass the subtype such as html which is then converted internally
to text/html using the mime lookup table:_

```javascript
// Accept: text/html
Expand Down Expand Up @@ -95,8 +95,8 @@ Returns **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer

### getContentType

Returns the value of the content-type header. If a content-type is not set,
this will return a default value of `application/octet-stream`
Returns the value of the content-type header. If a content-type is not
set, this will return a default value of `application/octet-stream`

Returns **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**

Expand Down Expand Up @@ -300,8 +300,8 @@ Returns **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer
Start the timer for a request handler.
By default, restify uses calls this automatically for all handlers
registered in your handler chain.
However, this can be called manually for nested functions inside the handler
chain to record timing information.
However, this can be called manually for nested functions inside the
handler chain to record timing information.

**Parameters**

Expand All @@ -310,8 +310,8 @@ chain to record timing information.
**Examples**

_You must explicitly invoke
endHandlerTimer() after invoking this function. Otherwise timing information
will be inaccurate._
endHandlerTimer() after invoking this function. Otherwise timing
information will be inaccurate._

```javascript
server.get('/', function fooHandler(req, res, next) {
Expand Down
31 changes: 17 additions & 14 deletions docs/_api/response.md
Expand Up @@ -27,7 +27,7 @@ permalink: /docs/response-api/
**Extends http.ServerResponse**

Wraps all of the node
[http.ServerResponse](https://nodejs.org/docs/latest/api/http.html#http.ServerResponse)
[http.ServerResponse](https://nodejs.org/docs/latest/api/http.html)
APIs, events and properties, plus the following.

### cache
Expand Down Expand Up @@ -154,10 +154,10 @@ formatter based on the `content-type` header.

_You can use send() to wrap up all the usual writeHead(), write(), end()
calls on the HTTP API of node.
You can pass send either a `code` and `body`, or just a body. body can be an
`Object`, a `Buffer`, or an `Error`.
When you call `send()`, restify figures out how to format the response based
on the `content-type`._
You can pass send either a `code` and `body`, or just a body. body can be
an `Object`, a `Buffer`, or an `Error`.
When you call `send()`, restify figures out how to format the response
based on the `content-type`._

```javascript
res.send({hello: 'world'});
Expand All @@ -169,8 +169,8 @@ Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer

### sendRaw

Like `res.send()`, but skips formatting. This can be useful when the payload
has already been preformatted.
Like `res.send()`, but skips formatting. This can be useful when the
payload has already been preformatted.
Sends the response object. pass through to internal `__send` that skips
formatters entirely and sends the content as is.

Expand All @@ -189,7 +189,8 @@ Uses `header()` underneath the hood, enabling multi-value headers.

**Parameters**

- `name` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object))** name of the header or `Object` of headers
- `name` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object))** name of the header or
`Object` of headers
- `val` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** value of the header

**Examples**
Expand Down Expand Up @@ -236,13 +237,15 @@ Redirect is sugar method for redirecting.
- `options.hostname` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** redirect location's hostname
- `options.pathname` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** redirect location's pathname
- `options.port` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** redirect location's port number
- `options.query` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** redirect location's query string parameters
- `options.overrideQuery` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** if true, `options.query` stomps over
any existing query parameters on current URL.
by default, will merge the two.
- `options.query` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** redirect location's query string
parameters
- `options.overrideQuery` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** if true, `options.query`
stomps over any existing query
parameters on current URL.
by default, will merge the two.
- `options.permanent` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** if true, sets 301. defaults to 302.
- `next` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** mandatory, to complete the response and trigger audit
logger.
- `next` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** mandatory, to complete the response and trigger
audit logger.

**Examples**

Expand Down
2 changes: 2 additions & 0 deletions docs/_api/server.md
Expand Up @@ -116,6 +116,8 @@ Creates a new Server.
response header, default is `restify`. Pass empty string to unset the header. (optional, default `false`)
- `options.spdy` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** Any options accepted by
[node-spdy](https://github.com/indutny/node-spdy).
- `options.http2` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** Any options accepted by
[http2.createSecureServer](https://nodejs.org/api/http2.html).
- `options.handleUpgrades` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Hook the `upgrade` event
from the node HTTP server, pushing `Connection: Upgrade` requests through the
regular request handling chain. (optional, default `false`)
Expand Down
33 changes: 33 additions & 0 deletions examples/http2/http2.js
@@ -0,0 +1,33 @@
var path = require('path');
var fs = require('fs');
var bunyan = require('bunyan');
var restify = require('../../lib');

var srv = restify.createServer({
http2: {
cert: fs.readFileSync(path.join(__dirname, './keys/http2-cert.pem')),
key: fs.readFileSync(path.join(__dirname, './keys/http2-key.pem')),
ca: fs.readFileSync(path.join(__dirname, 'keys/http2-csr.pem'))
}
});

srv.get('/', function(req, res, next) {
res.send({ hello: 'world' });
next();
});

srv.on(
'after',
restify.plugins.auditLogger({
event: 'after',
body: true,
log: bunyan.createLogger({
name: 'audit',
stream: process.stdout
})
})
);

srv.listen(8080, function() {
console.log('ready on %s', srv.url);
});
15 changes: 15 additions & 0 deletions examples/http2/keys/http2-cert.pem
@@ -0,0 +1,15 @@
-----BEGIN CERTIFICATE-----
MIICHzCCAYgCCQCPPSUAa8QZojANBgkqhkiG9w0BAQUFADBUMQswCQYDVQQGEwJS
VTETMBEGA1UECBMKU29tZS1TdGF0ZTENMAsGA1UEBxMET21zazEhMB8GA1UEChMY
SW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMB4XDTExMDQwOTEwMDY0NVoXDTExMDUw
OTEwMDY0NVowVDELMAkGA1UEBhMCUlUxEzARBgNVBAgTClNvbWUtU3RhdGUxDTAL
BgNVBAcTBE9tc2sxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDCB
nzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA1bn25sPkv46wl70BffxradlkRd/x
p5Xf8HDhPSfzNNctERYslXT2fX7Dmfd5w1XTVqqGqJ4izp5VewoVOHA8uavo3ovp
gNWasil5zADWaM1T0nnV0RsFbZWzOTmm1U3D48K8rW3F5kOZ6f4yRq9QT1gF/gN7
5Pt494YyYyJu/a8CAwEAATANBgkqhkiG9w0BAQUFAAOBgQBuRZisIViI2G/R+w79
vk21TzC/cJ+O7tKsseDqotXYTH8SuimEH5IWcXNgnWhNzczwN8s2362NixyvCipV
yd4wzMpPbjIhnWGM0hluWZiK2RxfcqimIBjDParTv6CMUIuwGQ257THKY8hXGg7j
Uws6Lif3P9UbsuRiYPxMgg98wg==
-----END CERTIFICATE-----

12 changes: 12 additions & 0 deletions examples/http2/keys/http2-csr.pem
@@ -0,0 +1,12 @@
-----BEGIN CERTIFICATE REQUEST-----
MIIBkzCB/QIBADBUMQswCQYDVQQGEwJSVTETMBEGA1UECBMKU29tZS1TdGF0ZTEN
MAsGA1UEBxMET21zazEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRk
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDVufbmw+S/jrCXvQF9/Gtp2WRF
3/Gnld/wcOE9J/M01y0RFiyVdPZ9fsOZ93nDVdNWqoaoniLOnlV7ChU4cDy5q+je
i+mA1ZqyKXnMANZozVPSedXRGwVtlbM5OabVTcPjwrytbcXmQ5np/jJGr1BPWAX+
A3vk+3j3hjJjIm79rwIDAQABoAAwDQYJKoZIhvcNAQEFBQADgYEAiNWhz6EppIVa
FfUaB3sLeqfamb9tg9kBHtvqj/FJni0snqms0kPWaTySEPHZF0irIb7VVdq/sVCb
3gseMVSyoDvPJ4lHC3PXqGQ7kM1mIPhDnR/4HDA3BhlGhTXSDIHgZnvI+HMBdsyC
hC3dz5odyKqe4nmoofomALkBL9t4H8s=
-----END CERTIFICATE REQUEST-----

16 changes: 16 additions & 0 deletions examples/http2/keys/http2-key.pem
@@ -0,0 +1,16 @@
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDVufbmw+S/jrCXvQF9/Gtp2WRF3/Gnld/wcOE9J/M01y0RFiyV
dPZ9fsOZ93nDVdNWqoaoniLOnlV7ChU4cDy5q+jei+mA1ZqyKXnMANZozVPSedXR
GwVtlbM5OabVTcPjwrytbcXmQ5np/jJGr1BPWAX+A3vk+3j3hjJjIm79rwIDAQAB
AoGAAv2QI9h32epQND9TxwSCKD//dC7W/cZOFNovfKCTeZjNK6EIzKqPTGA6smvR
C1enFl5adf+IcyWqAoe4lkqTvurIj+2EhtXdQ8DBlVuXKr3xvEFdYxXPautdTCF6
KbXEyS/s1TZCRFjYftvCrXxc3pK45AQX/wg7z1K+YB5pyIECQQD0OJvLoxLYoXAc
FZraIOZiDsEbGuSHqoCReFXH75EC3+XGYkH2bQ/nSIZ0h1buuwQ/ylKXOlTPT3Qt
Xm1OQEBvAkEA4AjWsIO/rRpOm/Q2aCrynWMpoUXTZSbL2yGf8pxp/+8r2br5ier0
M1LeBb/OPY1+k39NWLXxQoo64xoSFYk2wQJAd2wDCwX4HkR7HNCXw1hZL9QFK6rv
20NN0VSlpboJD/3KT0MW/FiCcVduoCbaJK0Au+zEjDyy4hj5N4I4Mw6KMwJAXVAx
I+psTsxzS4/njXG+BgIEl/C+gRYsuMQDnAi8OebDq/et8l0Tg8ETSu++FnM18neG
ntmBeMacinUUbTXuwQJBAJp/onZdsMzeVulsGrqR1uS+Lpjc5Q1gt5ttt2cxj91D
rio48C/ZvWuKNE8EYj2ALtghcVKRvgaWfOxt2GPguGg=
-----END RSA PRIVATE KEY-----

0 comments on commit 6b20285

Please sign in to comment.