mieps_http-2 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.autotest +20 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +17 -0
- data/.gitmodules +3 -0
- data/.rspec +4 -0
- data/.rubocop.yml +18 -0
- data/.rubocop_todo.yml +46 -0
- data/.travis.yml +13 -0
- data/Gemfile +18 -0
- data/README.md +285 -0
- data/Rakefile +11 -0
- data/example/README.md +40 -0
- data/example/client.rb +117 -0
- data/example/helper.rb +19 -0
- data/example/keys/mycert.pem +23 -0
- data/example/keys/mykey.pem +27 -0
- data/example/server.rb +97 -0
- data/example/upgrade_server.rb +193 -0
- data/http-2.gemspec +23 -0
- data/lib/http/2/buffer.rb +34 -0
- data/lib/http/2/client.rb +51 -0
- data/lib/http/2/compressor.rb +557 -0
- data/lib/http/2/connection.rb +654 -0
- data/lib/http/2/emitter.rb +45 -0
- data/lib/http/2/error.rb +44 -0
- data/lib/http/2/flow_buffer.rb +67 -0
- data/lib/http/2/framer.rb +440 -0
- data/lib/http/2/huffman.rb +323 -0
- data/lib/http/2/huffman_statemachine.rb +272 -0
- data/lib/http/2/server.rb +132 -0
- data/lib/http/2/stream.rb +576 -0
- data/lib/http/2/version.rb +3 -0
- data/lib/http/2.rb +13 -0
- data/lib/tasks/generate_huffman_table.rb +166 -0
- data/spec/buffer_spec.rb +21 -0
- data/spec/client_spec.rb +92 -0
- data/spec/compressor_spec.rb +535 -0
- data/spec/connection_spec.rb +581 -0
- data/spec/emitter_spec.rb +54 -0
- data/spec/framer_spec.rb +487 -0
- data/spec/helper.rb +128 -0
- data/spec/hpack_test_spec.rb +79 -0
- data/spec/huffman_spec.rb +68 -0
- data/spec/server_spec.rb +51 -0
- data/spec/stream_spec.rb +794 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 13402c073dfd886f238e4f75e8ee2475581a4276
|
4
|
+
data.tar.gz: c3e2b71f0a64d0c26af9b1ff90c74ee9451a04a7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4c440d093fba7be69098551f2248b3e58b6d910d687d636b0d8f0136c5eee676c74b2478237b25d3b502b16a4bc5508528a7de4b5c341ce3a720349a6ac37afc
|
7
|
+
data.tar.gz: c9730a5eedbcf01baaf44de42653672e4459b52411c0458b7504e14d3fdd6726e09d000c5b03f5bbd436ecb8cd733535e8037f90c4c7167d7e27d9315ca644b8
|
data/.autotest
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'autotest/growl'
|
2
|
+
|
3
|
+
Autotest.add_hook(:initialize) {|at|
|
4
|
+
at.add_exception %r{^\.git|\.yardoc}
|
5
|
+
at.add_exception %r{^./tmp}
|
6
|
+
at.add_exception %r{coverage}
|
7
|
+
|
8
|
+
at.clear_mappings
|
9
|
+
|
10
|
+
at.add_mapping(%r%^spec/.*_spec\.rb$%) { |filename, _|
|
11
|
+
filename
|
12
|
+
}
|
13
|
+
at.add_mapping(%r%^lib/(.*)\.rb$%) { |_, m|
|
14
|
+
["spec/#{m[1].split('/').last}_spec.rb"]
|
15
|
+
}
|
16
|
+
at.add_mapping(%r%^spec/(spec_helper|shared/.*)\.rb$%) {
|
17
|
+
files_matching %r%^spec/.*_spec\.rb$%
|
18
|
+
}
|
19
|
+
nil
|
20
|
+
}
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
ADDED
data/.gitmodules
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
DisplayCopNames: true
|
5
|
+
Exclude:
|
6
|
+
- 'bin/*'
|
7
|
+
|
8
|
+
Lint/EndAlignment:
|
9
|
+
AlignWith: variable
|
10
|
+
|
11
|
+
Style/CaseIndentation:
|
12
|
+
IndentWhenRelativeTo: end
|
13
|
+
|
14
|
+
Style/IndentHash:
|
15
|
+
EnforcedStyle: consistent
|
16
|
+
|
17
|
+
Style/TrailingComma:
|
18
|
+
EnforcedStyleForMultiline: comma
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# This configuration was generated by `rubocop --auto-gen-config`
|
2
|
+
# on 2015-04-06 10:04:21 -0700 using RuboCop version 0.30.0.
|
3
|
+
# The point is for the user to remove these configuration records
|
4
|
+
# one by one as the offenses are removed from the code base.
|
5
|
+
# Note that changes in the inspected code, or installation of new
|
6
|
+
# versions of RuboCop, may require this file to be generated again.
|
7
|
+
|
8
|
+
# Offense count: 23
|
9
|
+
Metrics/AbcSize:
|
10
|
+
Max: 186
|
11
|
+
|
12
|
+
# Offense count: 16
|
13
|
+
Metrics/BlockNesting:
|
14
|
+
Max: 5
|
15
|
+
|
16
|
+
# Offense count: 7
|
17
|
+
# Configuration parameters: CountComments.
|
18
|
+
Metrics/ClassLength:
|
19
|
+
Max: 331
|
20
|
+
|
21
|
+
# Offense count: 12
|
22
|
+
Metrics/CyclomaticComplexity:
|
23
|
+
Max: 60
|
24
|
+
|
25
|
+
# Offense count: 349
|
26
|
+
# Configuration parameters: AllowURI, URISchemes.
|
27
|
+
Metrics/LineLength:
|
28
|
+
Max: 231
|
29
|
+
|
30
|
+
# Offense count: 27
|
31
|
+
# Configuration parameters: CountComments.
|
32
|
+
Metrics/MethodLength:
|
33
|
+
Max: 134
|
34
|
+
|
35
|
+
# Offense count: 1
|
36
|
+
# Configuration parameters: CountKeywordArgs.
|
37
|
+
Metrics/ParameterLists:
|
38
|
+
Max: 6
|
39
|
+
|
40
|
+
# Offense count: 10
|
41
|
+
Metrics/PerceivedComplexity:
|
42
|
+
Max: 46
|
43
|
+
|
44
|
+
# Offense count: 7
|
45
|
+
Style/Documentation:
|
46
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'rake'
|
4
|
+
gem 'yard'
|
5
|
+
|
6
|
+
group :test do
|
7
|
+
gem 'activesupport'
|
8
|
+
gem 'autotest-growl'
|
9
|
+
gem 'autotest-standalone'
|
10
|
+
gem 'coveralls', require: false
|
11
|
+
gem 'pry'
|
12
|
+
gem 'pry-byebug', platform: :mri
|
13
|
+
gem 'rspec'
|
14
|
+
gem 'rspec-autotest'
|
15
|
+
gem 'rubocop', '~> 0.30.0'
|
16
|
+
end
|
17
|
+
|
18
|
+
gemspec
|
data/README.md
ADDED
@@ -0,0 +1,285 @@
|
|
1
|
+
# HTTP-2
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/http-2.png)](http://rubygems.org/gems/http-2)
|
4
|
+
[![Build Status](https://travis-ci.org/igrigorik/http-2.png?branch=master)](https://travis-ci.org/igrigorik/http-2)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/igrigorik/http-2/badge.png)](https://coveralls.io/r/igrigorik/http-2)
|
6
|
+
[![Analytics](https://ga-beacon.appspot.com/UA-71196-10/http-2/readme)](https://github.com/igrigorik/ga-beacon)
|
7
|
+
|
8
|
+
Pure Ruby, framework and transport agnostic, implementation of HTTP/2 protocol and HPACK header compression with support for:
|
9
|
+
|
10
|
+
* [Binary framing](http://chimera.labs.oreilly.com/books/1230000000545/ch12.html#_binary_framing_layer) parsing and encoding
|
11
|
+
* [Stream multiplexing](http://chimera.labs.oreilly.com/books/1230000000545/ch12.html#HTTP2_STREAMS_MESSAGES_FRAMES) and [prioritization](http://chimera.labs.oreilly.com/books/1230000000545/ch12.html#HTTP2_PRIORITIZATION)
|
12
|
+
* Connection and stream [flow control](http://chimera.labs.oreilly.com/books/1230000000545/ch12.html#_flow_control)
|
13
|
+
* [Header compression](http://chimera.labs.oreilly.com/books/1230000000545/ch12.html#HTTP2_HEADER_COMPRESSION) and [server push](http://chimera.labs.oreilly.com/books/1230000000545/ch12.html#HTTP2_PUSH)
|
14
|
+
* Connection and stream management
|
15
|
+
* And more... see [API docs](http://www.rubydoc.info/github/igrigorik/http-2/frames)
|
16
|
+
|
17
|
+
Protocol specifications:
|
18
|
+
|
19
|
+
* [Hypertext Transfer Protocol Version 2 (RFC 7540)](https://httpwg.github.io/specs/rfc7540.html)
|
20
|
+
* [HPACK: Header Compression for HTTP/2 (RFC 7541)](https://httpwg.github.io/specs/rfc7541.html)
|
21
|
+
|
22
|
+
|
23
|
+
## Getting started
|
24
|
+
|
25
|
+
```bash
|
26
|
+
$> gem install http-2
|
27
|
+
```
|
28
|
+
|
29
|
+
This implementation makes no assumptions as how the data is delivered: it could be a regular Ruby TCP socket, your custom eventloop, or whatever other transport you wish to use - e.g. ZeroMQ, [avian carriers](http://www.ietf.org/rfc/rfc1149.txt), etc.
|
30
|
+
|
31
|
+
Your code is responsible for feeding data into the parser, which performs all of the necessary HTTP/2 decoding, state management and the rest, and vice versa, the parser will emit bytes (encoded HTTP/2 frames) that you can then route to the destination. Roughly, this works as follows:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
require 'http/2'
|
35
|
+
socket = YourTransport.new
|
36
|
+
|
37
|
+
conn = HTTP2::Client.new
|
38
|
+
conn.on(:frame) {|bytes| socket << bytes }
|
39
|
+
|
40
|
+
while bytes = socket.read
|
41
|
+
conn << bytes
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
Checkout provided [client](https://github.com/igrigorik/http-2/blob/master/example/client.rb) and [server](https://github.com/igrigorik/http-2/blob/master/example/server.rb) implementations for basic examples.
|
46
|
+
|
47
|
+
|
48
|
+
### Connection lifecycle management
|
49
|
+
|
50
|
+
Depending on the role of the endpoint you must initialize either a [Client](http://www.rubydoc.info/github/igrigorik/http-2/HTTP2/Client) or a [Server](http://www.rubydoc.info/github/igrigorik/http-2/HTTP2/Server) object. Doing so picks the appropriate header compression / decompression algorithms and stream management logic. From there, you can subscribe to connection level events, or invoke appropriate APIs to allocate new streams and manage the lifecycle. For example:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
# - Server ---------------
|
54
|
+
server = HTTP2::Server.new
|
55
|
+
|
56
|
+
server.on(:stream) { |stream| ... } # process inbound stream
|
57
|
+
server.on(:frame) { |bytes| ... } # encoded HTTP/2 frames
|
58
|
+
|
59
|
+
server.ping { ... } # run liveness check, process pong response
|
60
|
+
server.goaway # send goaway frame to the client
|
61
|
+
|
62
|
+
# - Client ---------------
|
63
|
+
client = HTTP2::Client.new
|
64
|
+
client.on(:promise) { |stream| ... } # process push promise
|
65
|
+
|
66
|
+
stream = client.new_stream # allocate new stream
|
67
|
+
stream.headers({':method' => 'post', ...}, end_stream: false)
|
68
|
+
stream.data(payload, end_stream: true)
|
69
|
+
```
|
70
|
+
|
71
|
+
Events emitted by the connection object:
|
72
|
+
|
73
|
+
<table>
|
74
|
+
<tr>
|
75
|
+
<td><b>:promise</b></td>
|
76
|
+
<td>client role only, fires once for each new push promise</td>
|
77
|
+
</tr>
|
78
|
+
<tr>
|
79
|
+
<td><b>:stream</b></td>
|
80
|
+
<td>server role only, fires once for each new client stream</td>
|
81
|
+
</tr>
|
82
|
+
<tr>
|
83
|
+
<td><b>:frame</b></td>
|
84
|
+
<td>fires once for every encoded HTTP/2 frame that needs to be sent to the peer</td>
|
85
|
+
</tr>
|
86
|
+
</table>
|
87
|
+
|
88
|
+
|
89
|
+
### Stream lifecycle management
|
90
|
+
|
91
|
+
A single HTTP/2 connection can [multiplex multiple streams](http://chimera.labs.oreilly.com/books/1230000000545/ch12.html#REQUEST_RESPONSE_MULTIPLEXING) in parallel: multiple requests and responses can be in flight simultaneously and stream data can be interleaved and prioritized. Further, the specification provides a well-defined lifecycle for each stream (see below).
|
92
|
+
|
93
|
+
The good news is, all of the stream management, and state transitions, and error checking is handled by the library. All you have to do is subscribe to appropriate events (marked with ":" prefix in diagram below) and provide your application logic to handle request and response processing.
|
94
|
+
|
95
|
+
```
|
96
|
+
+--------+
|
97
|
+
PP | | PP
|
98
|
+
,--------| idle |--------.
|
99
|
+
/ | | \
|
100
|
+
v +--------+ v
|
101
|
+
+----------+ | +----------+
|
102
|
+
| | | H | |
|
103
|
+
,---|:reserved | | |:reserved |---.
|
104
|
+
| | (local) | v | (remote) | |
|
105
|
+
| +----------+ +--------+ +----------+ |
|
106
|
+
| | :active | | :active | |
|
107
|
+
| | ,-------|:active |-------. | |
|
108
|
+
| | H / ES | | ES \ H | |
|
109
|
+
| v v +--------+ v v |
|
110
|
+
| +-----------+ | +-----------+ |
|
111
|
+
| |:half_close| | |:half_close| |
|
112
|
+
| | (remote) | | | (local) | |
|
113
|
+
| +-----------+ | +-----------+ |
|
114
|
+
| | v | |
|
115
|
+
| | ES/R +--------+ ES/R | |
|
116
|
+
| `----------->| |<-----------' |
|
117
|
+
| R | :close | R |
|
118
|
+
`-------------------->| |<--------------------'
|
119
|
+
+--------+
|
120
|
+
```
|
121
|
+
|
122
|
+
For sake of example, let's take a look at a simple server implementation:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
conn = HTTP2::Server.new
|
126
|
+
|
127
|
+
# emits new streams opened by the client
|
128
|
+
conn.on(:stream) do |stream|
|
129
|
+
stream.on(:active) { } # fires when stream transitions to open state
|
130
|
+
stream.on(:close) { } # stream is closed by client and server
|
131
|
+
|
132
|
+
stream.on(:headers) { |head| ... } # header callback
|
133
|
+
stream.on(:data) { |chunk| ... } # body payload callback
|
134
|
+
|
135
|
+
# fires when client terminates its request (i.e. request finished)
|
136
|
+
stream.on(:half_close) do
|
137
|
+
|
138
|
+
# ... generate_response
|
139
|
+
|
140
|
+
# send response
|
141
|
+
stream.headers({
|
142
|
+
":status" => 200,
|
143
|
+
"content-type" => "text/plain"
|
144
|
+
})
|
145
|
+
|
146
|
+
# split response between multiple DATA frames
|
147
|
+
stream.data(response_chunk, end_stream: false)
|
148
|
+
stream.data(last_chunk)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
```
|
152
|
+
|
153
|
+
Events emitted by the [Stream object](http://www.rubydoc.info/github/igrigorik/http-2/HTTP2/Stream):
|
154
|
+
|
155
|
+
<table>
|
156
|
+
<tr>
|
157
|
+
<td><b>:reserved</b></td>
|
158
|
+
<td>fires exactly once when a push stream is initialized</td>
|
159
|
+
</tr>
|
160
|
+
<tr>
|
161
|
+
<td><b>:active</b></td>
|
162
|
+
<td>fires exactly once when the stream become active and is counted towards the open stream limit</td>
|
163
|
+
</tr>
|
164
|
+
<tr>
|
165
|
+
<td><b>:headers</b></td>
|
166
|
+
<td>fires once for each received header block (multi-frame blocks are reassembled before emitting this event)</td>
|
167
|
+
</tr>
|
168
|
+
<tr>
|
169
|
+
<td><b>:data</b></td>
|
170
|
+
<td>fires once for every DATA frame (no buffering)</td>
|
171
|
+
</tr>
|
172
|
+
<tr>
|
173
|
+
<td><b>:half_close</b></td>
|
174
|
+
<td>fires exactly once when the opposing peer closes its end of connection (e.g. client indicating that request is finished, or server indicating that response is finished)</td>
|
175
|
+
</tr>
|
176
|
+
<tr>
|
177
|
+
<td><b>:close</b></td>
|
178
|
+
<td>fires exactly once when both peers close the stream, or if the stream is reset</td>
|
179
|
+
</tr>
|
180
|
+
<tr>
|
181
|
+
<td><b>:priority</b></td>
|
182
|
+
<td>fires once for each received priority update (server only)</td>
|
183
|
+
</tr>
|
184
|
+
</table>
|
185
|
+
|
186
|
+
|
187
|
+
### Prioritization
|
188
|
+
|
189
|
+
Each HTTP/2 [stream has a priority value](http://chimera.labs.oreilly.com/books/1230000000545/ch12.html#HTTP2_PRIORITIZATION) that can be sent when the new stream is initialized, and optionally reprioritized later:
|
190
|
+
|
191
|
+
```ruby
|
192
|
+
client = HTTP2::Client.new
|
193
|
+
|
194
|
+
default_priority_stream = client.new_stream
|
195
|
+
custom_priority_stream = client.new_stream(priority: 42)
|
196
|
+
|
197
|
+
# sometime later: change priority value
|
198
|
+
custom_priority_stream.reprioritize(32000) # emits PRIORITY frame
|
199
|
+
```
|
200
|
+
|
201
|
+
On the opposite side, the server can optimize its stream processing order or resource allocation by accessing the stream priority value (`stream.priority`).
|
202
|
+
|
203
|
+
|
204
|
+
### Flow control
|
205
|
+
|
206
|
+
Multiplexing multiple streams over the same TCP connection introduces contention for shared bandwidth resources. Stream priorities can help determine the relative order of delivery, but priorities alone are insufficient to control how the resource allocation is performed between multiple streams. To address this, HTTP/2 provides a simple mechanism for [stream and connection flow control](http://chimera.labs.oreilly.com/books/1230000000545/ch12.html#_flow_control).
|
207
|
+
|
208
|
+
Connection and stream flow control is handled by the library: all streams are initialized with the default window size (64KB), and send/receive window updates are automatically processed - i.e. window is decremented on outgoing data transfers, and incremented on receipt of window frames. Similarly, if the window is exceeded, then data frames are automatically buffered until window is updated.
|
209
|
+
|
210
|
+
The only thing left is for your application to specify the logic as to when to emit window updates:
|
211
|
+
|
212
|
+
```ruby
|
213
|
+
conn.buffered_amount # check amount of buffered data
|
214
|
+
conn.window # check current window size
|
215
|
+
conn.window_update(1024) # increment connection window by 1024 bytes
|
216
|
+
|
217
|
+
stream.buffered_amount # check amount of buffered data
|
218
|
+
stream.window # check current window size
|
219
|
+
stream.window_update(2048) # increment stream window by 2048 bytes
|
220
|
+
```
|
221
|
+
|
222
|
+
Alternatively, flow control can be disabled by emitting an appropriate settings frame on the connection:
|
223
|
+
|
224
|
+
```ruby
|
225
|
+
# limit number of concurrent streams to 100 and disable flow control
|
226
|
+
conn.settings(streams: 100, window: Float::INFINITY)
|
227
|
+
```
|
228
|
+
|
229
|
+
### Server push
|
230
|
+
|
231
|
+
An HTTP/2 server can [send multiple replies](http://chimera.labs.oreilly.com/books/1230000000545/ch12.html#HTTP2_PUSH) to a single client request. To do so, first it emits a "push promise" frame which contains the headers of the promised resource, followed by the response to the original request, as well as promised resource payloads (which may be interleaved). A simple example is in order:
|
232
|
+
|
233
|
+
```ruby
|
234
|
+
conn = HTTP2::Server.new
|
235
|
+
|
236
|
+
conn.on(:stream) do |stream|
|
237
|
+
stream.on(:headers) { |head| ... }
|
238
|
+
stream.on(:data) { |chunk| ... }
|
239
|
+
|
240
|
+
# fires when client terminates its request (i.e. request finished)
|
241
|
+
stream.on(:half_close) do
|
242
|
+
head = {
|
243
|
+
":status" => 200,
|
244
|
+
":path" => "/other_resource",
|
245
|
+
"content-type" => "text/plain"
|
246
|
+
}
|
247
|
+
|
248
|
+
# initiate server push stream
|
249
|
+
stream.promise(head) do |push|
|
250
|
+
push.headers({ ... })
|
251
|
+
push.data(...)
|
252
|
+
end
|
253
|
+
|
254
|
+
# send response
|
255
|
+
stream.headers({
|
256
|
+
":status" => 200,
|
257
|
+
"content-type" => "text/plain"
|
258
|
+
})
|
259
|
+
|
260
|
+
# split response between multiple DATA frames
|
261
|
+
stream.data(response_chunk, end_stream: false)
|
262
|
+
promise.data(payload)
|
263
|
+
stream.data(last_chunk)
|
264
|
+
end
|
265
|
+
end
|
266
|
+
```
|
267
|
+
|
268
|
+
When a new push promise stream is sent by the server, the client is notified via the `:promise` event:
|
269
|
+
|
270
|
+
```ruby
|
271
|
+
conn = HTTP2::Client.new
|
272
|
+
conn.on(:promise) do |push|
|
273
|
+
# process push stream
|
274
|
+
end
|
275
|
+
```
|
276
|
+
|
277
|
+
The client can cancel any given push stream (via `.close`), or disable server push entirely by sending the appropriate settings frame (note that below setting only impacts server > client direction):
|
278
|
+
|
279
|
+
```ruby
|
280
|
+
client.settings(streams: 0) # setting max limit to 0 disables server push
|
281
|
+
```
|
282
|
+
|
283
|
+
### License
|
284
|
+
|
285
|
+
(MIT License) - Copyright (c) 2013 Ilya Grigorik ![GA](https://www.google-analytics.com/__utm.gif?utmac=UA-71196-9&utmhn=github.com&utmdt=HTTP2&utmp=/http-2/readme)
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
require 'rubocop/rake_task'
|
4
|
+
require 'yard'
|
5
|
+
require_relative 'lib/tasks/generate_huffman_table'
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new
|
8
|
+
RuboCop::RakeTask.new
|
9
|
+
YARD::Rake::YardocTask.new
|
10
|
+
|
11
|
+
task default: [:spec, :rubocop]
|
data/example/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
## Interop
|
2
|
+
|
3
|
+
First, a quick test to ensure that we can talk to ourselves:
|
4
|
+
|
5
|
+
```bash
|
6
|
+
# Direct connection
|
7
|
+
$> ruby server.rb
|
8
|
+
$> ruby client.rb http://localhost:8080/ # GET
|
9
|
+
$> ruby client.rb http://localhost:8080/ -d 'some data' # POST
|
10
|
+
|
11
|
+
# TLS + NPN negotiation
|
12
|
+
$> ruby server.rb --secure
|
13
|
+
$> ruby client.rb https://localhost:8080/ # GET
|
14
|
+
$> ...
|
15
|
+
```
|
16
|
+
|
17
|
+
### [nghttp2](https://github.com/tatsuhiro-t/nghttp2) (HTTP/2.0 C Library)
|
18
|
+
|
19
|
+
Public test server: http://106.186.112.116 (Upgrade + Direct)
|
20
|
+
|
21
|
+
```bash
|
22
|
+
# Direct request (http-2 > nghttp2)
|
23
|
+
$> ruby client.rb http://106.186.112.116/
|
24
|
+
|
25
|
+
# TLS + NPN request (http-2 > nghttp2)
|
26
|
+
$> ruby client.rb https://106.186.112.116/
|
27
|
+
|
28
|
+
# Direct request (nghttp2 > http-2)
|
29
|
+
$> ruby server.rb
|
30
|
+
$> nghttp -vnu http://localhost:8080 # Direct request to Ruby server
|
31
|
+
```
|
32
|
+
|
33
|
+
### Twitter (Java server)
|
34
|
+
|
35
|
+
```bash
|
36
|
+
# NPN + GET request (http-2 > twitter)
|
37
|
+
$> ruby client.rb https://twitter.com/
|
38
|
+
```
|
39
|
+
|
40
|
+
For a complete list of current implementations, see [http2 wiki](https://github.com/http2/http2-spec/wiki/Implementations).
|
data/example/client.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
options = {}
|
4
|
+
OptionParser.new do |opts|
|
5
|
+
opts.banner = 'Usage: client.rb [options]'
|
6
|
+
|
7
|
+
opts.on('-d', '--data [String]', 'HTTP payload') do |v|
|
8
|
+
options[:payload] = v
|
9
|
+
end
|
10
|
+
end.parse!
|
11
|
+
|
12
|
+
uri = URI.parse(ARGV[0] || 'http://localhost:8080/')
|
13
|
+
tcp = TCPSocket.new(uri.host, uri.port)
|
14
|
+
sock = nil
|
15
|
+
|
16
|
+
if uri.scheme == 'https'
|
17
|
+
ctx = OpenSSL::SSL::SSLContext.new
|
18
|
+
ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
19
|
+
|
20
|
+
ctx.npn_protocols = [DRAFT]
|
21
|
+
ctx.npn_select_cb = lambda do |protocols|
|
22
|
+
puts "NPN protocols supported by server: #{protocols}"
|
23
|
+
DRAFT if protocols.include? DRAFT
|
24
|
+
end
|
25
|
+
|
26
|
+
sock = OpenSSL::SSL::SSLSocket.new(tcp, ctx)
|
27
|
+
sock.sync_close = true
|
28
|
+
sock.hostname = uri.hostname
|
29
|
+
sock.connect
|
30
|
+
|
31
|
+
if sock.npn_protocol != DRAFT
|
32
|
+
puts "Failed to negotiate #{DRAFT} via NPN"
|
33
|
+
exit
|
34
|
+
end
|
35
|
+
else
|
36
|
+
sock = tcp
|
37
|
+
end
|
38
|
+
|
39
|
+
conn = HTTP2::Client.new
|
40
|
+
conn.on(:frame) do |bytes|
|
41
|
+
# puts "Sending bytes: #{bytes.unpack("H*").first}"
|
42
|
+
sock.print bytes
|
43
|
+
sock.flush
|
44
|
+
end
|
45
|
+
conn.on(:frame_sent) do |frame|
|
46
|
+
puts "Sent frame: #{frame.inspect}"
|
47
|
+
end
|
48
|
+
conn.on(:frame_received) do |frame|
|
49
|
+
puts "Received frame: #{frame.inspect}"
|
50
|
+
end
|
51
|
+
|
52
|
+
stream = conn.new_stream
|
53
|
+
log = Logger.new(stream.id)
|
54
|
+
|
55
|
+
conn.on(:promise) do |promise|
|
56
|
+
promise.on(:headers) do |h|
|
57
|
+
log.info "promise headers: #{h}"
|
58
|
+
end
|
59
|
+
|
60
|
+
promise.on(:data) do |d|
|
61
|
+
log.info "promise data chunk: <<#{d.size}>>"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
conn.on(:altsvc) do |f|
|
66
|
+
log.info "received ALTSVC #{f}"
|
67
|
+
end
|
68
|
+
|
69
|
+
stream.on(:close) do
|
70
|
+
log.info 'stream closed'
|
71
|
+
sock.close
|
72
|
+
end
|
73
|
+
|
74
|
+
stream.on(:half_close) do
|
75
|
+
log.info 'closing client-end of the stream'
|
76
|
+
end
|
77
|
+
|
78
|
+
stream.on(:headers) do |h|
|
79
|
+
log.info "response headers: #{h}"
|
80
|
+
end
|
81
|
+
|
82
|
+
stream.on(:data) do |d|
|
83
|
+
log.info "response data chunk: <<#{d}>>"
|
84
|
+
end
|
85
|
+
|
86
|
+
stream.on(:altsvc) do |f|
|
87
|
+
log.info "received ALTSVC #{f}"
|
88
|
+
end
|
89
|
+
|
90
|
+
head = {
|
91
|
+
':scheme' => uri.scheme,
|
92
|
+
':method' => (options[:payload].nil? ? 'GET' : 'POST'),
|
93
|
+
':authority' => [uri.host, uri.port].join(':'),
|
94
|
+
':path' => uri.path,
|
95
|
+
'accept' => '*/*',
|
96
|
+
}
|
97
|
+
|
98
|
+
puts 'Sending HTTP 2.0 request'
|
99
|
+
if head[':method'] == 'GET'
|
100
|
+
stream.headers(head, end_stream: true)
|
101
|
+
else
|
102
|
+
stream.headers(head, end_stream: false)
|
103
|
+
stream.data(options[:payload])
|
104
|
+
end
|
105
|
+
|
106
|
+
while !sock.closed? && !sock.eof?
|
107
|
+
data = sock.read_nonblock(1024)
|
108
|
+
# puts "Received bytes: #{data.unpack("H*").first}"
|
109
|
+
|
110
|
+
begin
|
111
|
+
conn << data
|
112
|
+
rescue => e
|
113
|
+
puts "#{e.class} exception: #{e.message} - closing socket."
|
114
|
+
e.backtrace.each { |l| puts "\t" + l }
|
115
|
+
sock.close
|
116
|
+
end
|
117
|
+
end
|
data/example/helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
$LOAD_PATH << 'lib' << '../lib'
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require 'socket'
|
5
|
+
require 'openssl'
|
6
|
+
require 'http/2'
|
7
|
+
require 'uri'
|
8
|
+
|
9
|
+
DRAFT = 'h2'
|
10
|
+
|
11
|
+
class Logger
|
12
|
+
def initialize(id)
|
13
|
+
@id = id
|
14
|
+
end
|
15
|
+
|
16
|
+
def info(msg)
|
17
|
+
puts "[Stream #{@id}]: #{msg}"
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
-----BEGIN CERTIFICATE-----
|
2
|
+
MIID1zCCAr+gAwIBAgIJANjbVITTVqaAMA0GCSqGSIb3DQEBBQUAMFAxCzAJBgNV
|
3
|
+
BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMRgwFgYDVQQKEw9TUERZIFByb3h5
|
4
|
+
IERlbW8xEjAQBgNVBAMTCWxvY2FsaG9zdDAeFw0xNDEwMTQwNDUwMTJaFw0xNTEw
|
5
|
+
MTQwNDUwMTJaMFAxCzAJBgNVBAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMRgw
|
6
|
+
FgYDVQQKEw9TUERZIFByb3h5IERlbW8xEjAQBgNVBAMTCWxvY2FsaG9zdDCCASIw
|
7
|
+
DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMxv7mqzNMVVFoBjqOSUy2cNM9c6
|
8
|
+
6gTgVLr9ssoLU0TC4biY1B/KoD7G9Ive6PwfdpipgGY+tuPHfEzBCCHD7exER1NL
|
9
|
+
npWauo6Lwh3wOjuo5Er6klgBGFuHYx8jJ2jBwCFvTcG2zJRedU/Pby6Fa27X6acw
|
10
|
+
faAtReG5YOHs8YRmg4ErWqfRucoM3zj8vvMWnushMhYQxo1EVLJ2EvvbHEkip4ap
|
11
|
+
pro+2Ql0KY4XT3EoMTRHICbolK/uQYoe0musKnwCGPg2NL6e27uvi47G7GrIpcf3
|
12
|
+
HN4HZMoOzJ8ti7IIEkF0fVTgQEVkluInfned69WCwxecMQZs5sdBuwE3Kh0CAwEA
|
13
|
+
AaOBszCBsDAdBgNVHQ4EFgQU86bqiYciIYDN+KAPlnJL6tSbH6IwgYAGA1UdIwR5
|
14
|
+
MHeAFPOm6omHIiGAzfigD5ZyS+rUmx+ioVSkUjBQMQswCQYDVQQGEwJBVTETMBEG
|
15
|
+
A1UECBMKU29tZS1TdGF0ZTEYMBYGA1UEChMPU1BEWSBQcm94eSBEZW1vMRIwEAYD
|
16
|
+
VQQDEwlsb2NhbGhvc3SCCQDY21SE01amgDAMBgNVHRMEBTADAQH/MA0GCSqGSIb3
|
17
|
+
DQEBBQUAA4IBAQCkcr0DLPCbP5l9G0YI/XKVsUW9fXcTvge6Eko0R8qAkzTcsZQv
|
18
|
+
DbKcIM3z52QguCuJ9k63X4p174FKq7+qmieqaifosGKV03pyyxWLMpRooUUVXEBM
|
19
|
+
gZaRfp9VG2N4zrRaIklOSkAscnwybv2U3LZhKDlc7Yatsr1/TFkbCnzll514UnTz
|
20
|
+
ewjrlzVitUSEkwEGvLhKQuVPM9/3MAm+ztFpx846/GZ2XJSAFQLtHudjMXnFLihA
|
21
|
+
7nGZvE4rudyT70YsKu0BP0KjVZXrxTh81C4kyJu9xo4YuiDCFtvwtjoty0ygbuQN
|
22
|
+
a38i0bxFlYFmbWHooNCPUWVy59MOnW9zxaTV
|
23
|
+
-----END CERTIFICATE-----
|