net-http2 0.9.2 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ebde069e39e0e49475a138be06a9727b75d13f84
4
- data.tar.gz: d037c98b2682a951bf7c3b6dd50e07d864d9ddff
3
+ metadata.gz: 53d47a5179357c9eebc6e0c23e6eb0ba724d4ccc
4
+ data.tar.gz: d82ec20911a804eef2183564580fbde8d03483e0
5
5
  SHA512:
6
- metadata.gz: 5f9d0e50b1657cb4771c4fb21fc2864c80a7e424817e33eb38840418a48901b22d9ed91f24555396cf092dd596c918872fe648eaa61d7d3318c0daff20d55a65
7
- data.tar.gz: ab354b9ad23b47e34b08cf9d47e5822e9fcebde205663bbac3fb2a1da0f768ad34743d3d69aed4e132ca3f6b087f3796a50a11d06c12e97b50697689d9fdd981
6
+ metadata.gz: 784a337baecb984bffb5b1594c40a46a4aab7bce5cc570d651953a49af0687658ccfeef4026cbbfa2e7b7bdf709e7990f8ffa13563de00fcde180ae9bbb6d7c0
7
+ data.tar.gz: 784009bbae1c958ad17fb0efbb227c7291f4ec59d270e8c5aea4fbe902e39a141ebacb15abf52dd7a9dc1ef14b0308b034669cf8466bc881f7ecc81f87256c63
data/.travis.yml CHANGED
@@ -3,6 +3,7 @@ rvm:
3
3
  - 2.1
4
4
  - 2.2
5
5
  - 2.3.0
6
+ - 2.3.1
6
7
 
7
8
  branches:
8
9
  only:
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  [![Build Status](https://travis-ci.org/ostinelli/net-http2.svg?branch=master)](https://travis-ci.org/ostinelli/net-http2)
2
2
  [![Code Climate](https://codeclimate.com/github/ostinelli/net-http2/badges/gpa.svg)](https://codeclimate.com/github/ostinelli/net-http2)
3
+ [![Gem Version](https://badge.fury.io/rb/net-http2.svg)](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://106.186.112.116")
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://106.186.112.116")
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 so you probably have a running loop that keeps your program alive.
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 https://localhost:443.
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://106.186.112.116")
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("http://106.186.112.116", ssl_context: ctx)
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
 
@@ -16,9 +16,7 @@ module NetHttp2
16
16
 
17
17
  @is_ssl = (@uri.scheme == 'https')
18
18
 
19
- @pipe_r, @pipe_w = Socket.pair(:UNIX, :STREAM, 0)
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
- thread_loop(socket)
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 thread_loop(socket)
82
+ def socket_loop
78
83
 
79
- send_before_receiving(socket)
84
+ ensure_sent_before_receiving
80
85
 
81
86
  loop do
82
87
 
83
- next if read_if_pending(socket)
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
- end
91
-
92
- if ready[0].include?(@pipe_r)
93
- data_to_send = @pipe_r.readpartial(1024)
94
- socket.write(data_to_send)
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 send_before_receiving(socket)
116
- data_to_send = @pipe_r.read_nonblock(1024)
117
- socket.write(data_to_send)
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
- @pipe_w.write(bytes)
141
- @pipe_w.flush
127
+ @socket.write(bytes)
128
+ @socket.flush
129
+
130
+ @first_data_sent = true
142
131
  end
143
132
  end
144
133
  end
@@ -62,7 +62,6 @@ module NetHttp2
62
62
  def listen_for_close
63
63
  @h2_stream.on(:close) do |data|
64
64
  @completed = true
65
-
66
65
  @request.emit(:close, data) if async?
67
66
  end
68
67
  end
@@ -1,3 +1,3 @@
1
1
  module NetHttp2
2
- VERSION = "0.9.2"
2
+ VERSION = "0.10.0"
3
3
  end
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.1"
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.9.2
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-01 00:00:00.000000000 Z
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.1
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.1
26
+ version: 0.8.2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement