net-http2 0.9.2 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/README.md +16 -14
- data/lib/net-http2/client.rb +33 -44
- data/lib/net-http2/stream.rb +0 -1
- data/lib/net-http2/version.rb +1 -1
- data/net-http2.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53d47a5179357c9eebc6e0c23e6eb0ba724d4ccc
|
4
|
+
data.tar.gz: d82ec20911a804eef2183564580fbde8d03483e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 784a337baecb984bffb5b1594c40a46a4aab7bce5cc570d651953a49af0687658ccfeef4026cbbfa2e7b7bdf709e7990f8ffa13563de00fcde180ae9bbb6d7c0
|
7
|
+
data.tar.gz: 784009bbae1c958ad17fb0efbb227c7291f4ec59d270e8c5aea4fbe902e39a141ebacb15abf52dd7a9dc1ef14b0308b034669cf8466bc881f7ecc81f87256c63
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
[](https://travis-ci.org/ostinelli/net-http2)
|
2
2
|
[](https://codeclimate.com/github/ostinelli/net-http2)
|
3
|
+
[](https://badge.fury.io/rb/net-http2)
|
3
4
|
|
4
5
|
# NetHttp2
|
5
6
|
|
@@ -26,7 +27,7 @@ To perform a sync call:
|
|
26
27
|
require 'net-http2'
|
27
28
|
|
28
29
|
# create a client
|
29
|
-
client = NetHttp2::Client.new("http://
|
30
|
+
client = NetHttp2::Client.new("http://nghttp2.org")
|
30
31
|
|
31
32
|
# send request
|
32
33
|
response = client.call(:get, '/')
|
@@ -46,7 +47,7 @@ To perform an async call:
|
|
46
47
|
require 'net-http2'
|
47
48
|
|
48
49
|
# create a client
|
49
|
-
client = NetHttp2::Client.new("http://
|
50
|
+
client = NetHttp2::Client.new("http://nghttp2.org")
|
50
51
|
|
51
52
|
# prepare request
|
52
53
|
request = client.prepare_request(:get, '/')
|
@@ -58,7 +59,8 @@ request.on(:close) { puts "request completed!" }
|
|
58
59
|
client.call_async(request)
|
59
60
|
|
60
61
|
# We need to wait for the callbacks to be triggered, so here's a quick and dirty fix for this example.
|
61
|
-
# In real life, if you are using async requests
|
62
|
+
# In real life, if you are using async requests you probably have a running loop that keeps
|
63
|
+
# your program alive.
|
62
64
|
sleep 5
|
63
65
|
|
64
66
|
# close the connection
|
@@ -73,12 +75,12 @@ client.close
|
|
73
75
|
|
74
76
|
* **new(url, options={})** → **`NetHttp2::Client`**
|
75
77
|
|
76
|
-
Returns a new client. `url` is a `string` such as
|
78
|
+
Returns a new client. `url` is a `string` such as `http://nghttp2.org`.
|
77
79
|
The only current option is `:ssl_context`, in case the url has an https scheme and you want your SSL client to use a custom context.
|
78
80
|
|
79
81
|
To create a new client:
|
80
82
|
```ruby
|
81
|
-
NetHttp2::Client.new("http://
|
83
|
+
NetHttp2::Client.new("http://nghttp2.org")
|
82
84
|
```
|
83
85
|
|
84
86
|
To create a new client with a custom SSL context:
|
@@ -88,7 +90,7 @@ client.close
|
|
88
90
|
ctx.key = OpenSSL::PKey::RSA.new(certificate, "cert_password")
|
89
91
|
ctx.cert = OpenSSL::X509::Certificate.new(certificate)
|
90
92
|
|
91
|
-
NetHttp2::Client.new("
|
93
|
+
NetHttp2::Client.new("https://nghttp2.org", ssl_context: ctx)
|
92
94
|
```
|
93
95
|
|
94
96
|
* **uri** → **`URI`**
|
@@ -107,7 +109,7 @@ These behave similarly to HTTP/1 calls.
|
|
107
109
|
```ruby
|
108
110
|
response_1 = client.call(:get, '/path1')
|
109
111
|
response_2 = client.call(:get, '/path2', headers: { 'x-custom' => 'custom' })
|
110
|
-
response_3 = client.call(:post '/path3', body: "the request body", timeout: 1)
|
112
|
+
response_3 = client.call(:post, '/path3', body: "the request body", timeout: 1)
|
111
113
|
```
|
112
114
|
|
113
115
|
|
@@ -122,10 +124,10 @@ The real benefit of HTTP/2 is being able to receive body and header streams. Ins
|
|
122
124
|
request = client.prepare_request(:get, '/path', headers: { 'x-custom-header' => 'custom' })
|
123
125
|
```
|
124
126
|
|
125
|
-
* **on(event, &block)**
|
127
|
+
* **on(event, &block)**
|
126
128
|
|
127
129
|
Allows to set a callback for the request. Available events are:
|
128
|
-
|
130
|
+
|
129
131
|
* `:headers`: triggered when headers are received (called once).
|
130
132
|
* `:body_chunk`: triggered when body chunks are received (may be called multiple times).
|
131
133
|
* `:close`: triggered when the request has been completed (called once).
|
@@ -148,23 +150,23 @@ The real benefit of HTTP/2 is being able to receive body and header streams. Ins
|
|
148
150
|
#### Methods
|
149
151
|
|
150
152
|
* **method** → **`symbol`**
|
151
|
-
|
153
|
+
|
152
154
|
The request's method.
|
153
155
|
|
154
156
|
* **uri** → **`URI`**
|
155
|
-
|
157
|
+
|
156
158
|
The request's URI.
|
157
159
|
|
158
160
|
* **path** → **`string`**
|
159
|
-
|
161
|
+
|
160
162
|
The request's path.
|
161
163
|
|
162
164
|
* **body** → **`string`**
|
163
|
-
|
165
|
+
|
164
166
|
The request's body.
|
165
167
|
|
166
168
|
* **timeout** → **`integer`**
|
167
|
-
|
169
|
+
|
168
170
|
The request's timeout.
|
169
171
|
|
170
172
|
|
data/lib/net-http2/client.rb
CHANGED
@@ -16,9 +16,7 @@ module NetHttp2
|
|
16
16
|
|
17
17
|
@is_ssl = (@uri.scheme == 'https')
|
18
18
|
|
19
|
-
|
20
|
-
@socket_thread = nil
|
21
|
-
@mutex = Mutex.new
|
19
|
+
init_vars
|
22
20
|
end
|
23
21
|
|
24
22
|
def call(method, path, options={})
|
@@ -42,15 +40,19 @@ module NetHttp2
|
|
42
40
|
|
43
41
|
def close
|
44
42
|
exit_thread(@socket_thread)
|
45
|
-
|
46
|
-
@h2 = nil
|
47
|
-
@pipe_r = nil
|
48
|
-
@pipe_w = nil
|
49
|
-
@socket_thread = nil
|
43
|
+
init_vars
|
50
44
|
end
|
51
45
|
|
52
46
|
private
|
53
47
|
|
48
|
+
def init_vars
|
49
|
+
@h2 = nil
|
50
|
+
@socket = nil
|
51
|
+
@socket_thread = nil
|
52
|
+
@first_data_sent = false
|
53
|
+
@mutex = Mutex.new
|
54
|
+
end
|
55
|
+
|
54
56
|
def new_stream
|
55
57
|
NetHttp2::Stream.new(uri: @uri, h2_stream: h2.new_stream)
|
56
58
|
end
|
@@ -60,38 +62,38 @@ module NetHttp2
|
|
60
62
|
|
61
63
|
return if @socket_thread
|
62
64
|
|
63
|
-
socket = new_socket
|
65
|
+
@socket = new_socket
|
64
66
|
|
65
67
|
@socket_thread = Thread.new do
|
66
68
|
|
67
69
|
begin
|
68
|
-
|
70
|
+
socket_loop
|
71
|
+
rescue EOFError
|
72
|
+
# socket closed
|
69
73
|
ensure
|
70
|
-
socket.close unless socket.closed?
|
74
|
+
@socket.close unless @socket.closed?
|
75
|
+
@socket = nil
|
71
76
|
@socket_thread = nil
|
72
77
|
end
|
73
78
|
end.tap { |t| t.abort_on_exception = true }
|
74
79
|
end
|
75
80
|
end
|
76
81
|
|
77
|
-
def
|
82
|
+
def socket_loop
|
78
83
|
|
79
|
-
|
84
|
+
ensure_sent_before_receiving
|
80
85
|
|
81
86
|
loop do
|
82
87
|
|
83
|
-
|
84
|
-
|
85
|
-
ready = IO.select([socket, @pipe_r])
|
86
|
-
|
87
|
-
if ready[0].include?(socket)
|
88
|
-
data_received = socket.readpartial(1024)
|
88
|
+
begin
|
89
|
+
data_received = @socket.read_nonblock(1024)
|
89
90
|
h2 << data_received
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
91
|
+
rescue IO::WaitReadable
|
92
|
+
IO.select([@socket])
|
93
|
+
retry
|
94
|
+
rescue IO::WaitWritable
|
95
|
+
IO.select(nil, [@socket])
|
96
|
+
retry
|
95
97
|
end
|
96
98
|
end
|
97
99
|
end
|
@@ -112,24 +114,9 @@ module NetHttp2
|
|
112
114
|
end
|
113
115
|
end
|
114
116
|
|
115
|
-
def
|
116
|
-
|
117
|
-
|
118
|
-
rescue IO::WaitReadable
|
119
|
-
IO.select([@pipe_r])
|
120
|
-
retry
|
121
|
-
end
|
122
|
-
|
123
|
-
def read_if_pending(socket)
|
124
|
-
if ssl?
|
125
|
-
available = socket.pending
|
126
|
-
|
127
|
-
if available > 0
|
128
|
-
data_received = socket.sysread(available)
|
129
|
-
h2 << data_received
|
130
|
-
|
131
|
-
true
|
132
|
-
end
|
117
|
+
def ensure_sent_before_receiving
|
118
|
+
while !@first_data_sent
|
119
|
+
sleep 0.1
|
133
120
|
end
|
134
121
|
end
|
135
122
|
|
@@ -137,8 +124,10 @@ module NetHttp2
|
|
137
124
|
@h2 ||= HTTP2::Client.new.tap do |h2|
|
138
125
|
h2.on(:frame) do |bytes|
|
139
126
|
@mutex.synchronize do
|
140
|
-
@
|
141
|
-
@
|
127
|
+
@socket.write(bytes)
|
128
|
+
@socket.flush
|
129
|
+
|
130
|
+
@first_data_sent = true
|
142
131
|
end
|
143
132
|
end
|
144
133
|
end
|
data/lib/net-http2/stream.rb
CHANGED
data/lib/net-http2/version.rb
CHANGED
data/net-http2.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
spec.add_dependency "http-2", "~> 0.8.
|
22
|
+
spec.add_dependency "http-2", "~> 0.8.2"
|
23
23
|
|
24
24
|
spec.add_development_dependency "bundler", "~> 1.3"
|
25
25
|
spec.add_development_dependency "rake", "~> 10.0"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: net-http2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roberto Ostinelli
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http-2
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.8.
|
19
|
+
version: 0.8.2
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.8.
|
26
|
+
version: 0.8.2
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|