net-http2 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +33 -0
- data/.rspec +3 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +9 -0
- data/Gemfile +2 -0
- data/LICENSE.md +21 -0
- data/README.md +174 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/net-http2.rb +13 -0
- data/lib/net-http2/client.rb +166 -0
- data/lib/net-http2/request/base.rb +33 -0
- data/lib/net-http2/request/delete.rb +12 -0
- data/lib/net-http2/request/get.rb +12 -0
- data/lib/net-http2/request/post.rb +12 -0
- data/lib/net-http2/request/put.rb +12 -0
- data/lib/net-http2/response.rb +19 -0
- data/lib/net-http2/stream.rb +75 -0
- data/lib/net-http2/version.rb +3 -0
- data/net-http2.gemspec +25 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 10dc650d476227c1a393c90163743402909063f2
|
4
|
+
data.tar.gz: bcbc57dee7a35640a11bc4fd0a237a65a9aa02c3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 95fca293c965b9f40d39e08664dbdfbb5dcb80b003b790c12d955c3913e7909c8f239c407c2368dff05dc08840819cd414c352e0f19f2de554042d474dabd21b
|
7
|
+
data.tar.gz: a66cc7f54b6ae4400753cfbaf5c5af299bc996fdb0e11680900cc2a1046657b87c31b8f8dd1eadd2be5ba1fbdd6aa26b04bcce52866b0b656b8c258158c18094
|
data/.gitignore
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# vim
|
2
|
+
.*.sw[a-z]
|
3
|
+
*.un~
|
4
|
+
Session.vim
|
5
|
+
|
6
|
+
# mine
|
7
|
+
.idea
|
8
|
+
|
9
|
+
# OSX ignores
|
10
|
+
.DS_Store
|
11
|
+
.AppleDouble
|
12
|
+
.LSOverride
|
13
|
+
Icon
|
14
|
+
|
15
|
+
._*
|
16
|
+
.Spotlight-V100
|
17
|
+
.Trashes
|
18
|
+
.AppleDB
|
19
|
+
.AppleDesktop
|
20
|
+
Network Trash Folder
|
21
|
+
Temporary Items
|
22
|
+
.apdisk
|
23
|
+
|
24
|
+
# gem
|
25
|
+
/.bundle/
|
26
|
+
/.yardoc
|
27
|
+
/Gemfile.lock
|
28
|
+
/_yardoc/
|
29
|
+
/coverage/
|
30
|
+
/doc/
|
31
|
+
/pkg/
|
32
|
+
/spec/reports/
|
33
|
+
/tmp/
|
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
net-http2
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.3.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Roberto Ostinelli.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/ostinelli/net-http2.svg?branch=master)](https://travis-ci.org/ostinelli/net-http2)
|
2
|
+
|
3
|
+
# NetHttp2
|
4
|
+
|
5
|
+
NetHttp2 is an HTTP2 client for Ruby.
|
6
|
+
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
Just install the gem:
|
10
|
+
|
11
|
+
```
|
12
|
+
$ gem install net-http2
|
13
|
+
```
|
14
|
+
|
15
|
+
Or add it to your Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'net-http2'
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
With a blocking call:
|
24
|
+
```ruby
|
25
|
+
require 'net-http2'
|
26
|
+
|
27
|
+
# create a client
|
28
|
+
client = NetHttp2::Client.new(uri: "http://106.186.112.116")
|
29
|
+
|
30
|
+
# send request
|
31
|
+
response = client.get('/')
|
32
|
+
|
33
|
+
# read the response
|
34
|
+
response.ok? # => true
|
35
|
+
response.status # => '200'
|
36
|
+
response.headers # => {":status"=>"200"}
|
37
|
+
response.body # => "A body"
|
38
|
+
|
39
|
+
# close the connection
|
40
|
+
client.close
|
41
|
+
```
|
42
|
+
|
43
|
+
With a a non-blocking call:
|
44
|
+
```ruby
|
45
|
+
require 'net-http2'
|
46
|
+
|
47
|
+
# create a client
|
48
|
+
client = NetHttp2::Client.new(uri: "http://106.186.112.116")
|
49
|
+
|
50
|
+
# send request
|
51
|
+
client.async_get('/') do |response|
|
52
|
+
|
53
|
+
# read the response
|
54
|
+
p response.ok? # => true
|
55
|
+
p response.status # => '200'
|
56
|
+
p response.headers # => {":status"=>"200"}
|
57
|
+
p response.body # => "A body"
|
58
|
+
|
59
|
+
# close the connection
|
60
|
+
client.close
|
61
|
+
end
|
62
|
+
|
63
|
+
# quick & dirty fix to wait for the block to be called asynchronously
|
64
|
+
sleep 5
|
65
|
+
```
|
66
|
+
|
67
|
+
|
68
|
+
## Objects
|
69
|
+
|
70
|
+
### `NetHttp2::Client`
|
71
|
+
To create a new client:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
NetHttp2::Client.new(uri)
|
75
|
+
```
|
76
|
+
|
77
|
+
#### Methods
|
78
|
+
|
79
|
+
* **new(uri, options={})** → **`NetHttp2::Client`**
|
80
|
+
Returns w new client. `uri` is a `string` such as https://localhost:443.
|
81
|
+
The only current option is `:ssl_context`, in case the uri has an https scheme and you want your SSL client to use a custom context.
|
82
|
+
|
83
|
+
For instance:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
certificate = File.read("cert.pem")
|
87
|
+
ctx = OpenSSL::SSL::SSLContext.new
|
88
|
+
ctx.key = OpenSSL::PKey::RSA.new(certificate, "cert_password")
|
89
|
+
ctx.cert = OpenSSL::X509::Certificate.new(certificate)
|
90
|
+
|
91
|
+
NetHttp2::Client.new(uri, ssl_context: ctx)
|
92
|
+
```
|
93
|
+
|
94
|
+
* **uri** → **`URI`**
|
95
|
+
Returns the URI of the APNS endpoint.
|
96
|
+
|
97
|
+
* **get(path, headers={}, options={})** → **`NetHttp2::Response` or `nil`**
|
98
|
+
Sends a GET request. This is a blocking call. Options can only specify a `:timeout` (defaults to 60).
|
99
|
+
Returns `nil` in case a timeout occurs.
|
100
|
+
|
101
|
+
For example:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
response_1 = client.get('/path1')
|
105
|
+
response_2 = client.get('/path2', { 'x-custom-header' => 'custom' })
|
106
|
+
response_3 = client.get('/path3', { 'x-custom-header' => 'custom' }, timeout: 1)
|
107
|
+
```
|
108
|
+
|
109
|
+
* **post(path, body, headers={}, options={})** → **`NetHttp2::Response` or `nil`**
|
110
|
+
Sends a POST request. This is a blocking call. Options can only specify a `:timeout` (defaults to 60).
|
111
|
+
Returns `nil` in case a timeout occurs.
|
112
|
+
|
113
|
+
* **put(path, body, headers={}, options={})** → **`NetHttp2::Response` or `nil`**
|
114
|
+
Sends a PUT request. This is a blocking call. Options can only specify a `:timeout` (defaults to 60).
|
115
|
+
Returns `nil` in case a timeout occurs.
|
116
|
+
|
117
|
+
* **delete(path, headers={}, options={})** → **`NetHttp2::Response` or `nil`**
|
118
|
+
Sends a DELETE request. This is a blocking call. Options can only specify a `:timeout` (defaults to 60).
|
119
|
+
Returns `nil` in case a timeout occurs.
|
120
|
+
|
121
|
+
* **async_get(path, headers={}, options={})** → **`NetHttp2::Response` or `nil`**
|
122
|
+
Sends a GET request. This is a non-blocking call. Options can only specify a `:timeout` (defaults to 60).
|
123
|
+
Returns `nil` in case a timeout occurs.
|
124
|
+
|
125
|
+
For example:
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
client.get('/path1') { |response_1| p response_2 }
|
129
|
+
client.get('/path2', { 'x-custom-header' => 'custom' }) { |response_2| p response_2 }
|
130
|
+
client.get('/path3', {}, timeout: 1) { |response_3| p response_3 }
|
131
|
+
```
|
132
|
+
|
133
|
+
* **async_post(path, body, headers={}, options={})** → **`NetHttp2::Response` or `nil`**
|
134
|
+
Sends a POST request. This is a non-blocking call. Options can only specify a `:timeout` (defaults to 60).
|
135
|
+
Returns `nil` in case a timeout occurs.
|
136
|
+
|
137
|
+
* **async_put(path,body, headers={}, options={})** → **`NetHttp2::Response` or `nil`**
|
138
|
+
Sends a PUT request. This is a non-blocking call. Options can only specify a `:timeout` (defaults to 60).
|
139
|
+
Returns `nil` in case a timeout occurs.
|
140
|
+
|
141
|
+
* **async_delete(path, headers={}, options={})** → **`NetHttp2::Response` or `nil`**
|
142
|
+
Sends a DELETE request. This is a non-blocking call. Options can only specify a `:timeout` (defaults to 60).
|
143
|
+
Returns `nil` in case a timeout occurs.
|
144
|
+
|
145
|
+
|
146
|
+
### `NetHttp2::Response`
|
147
|
+
|
148
|
+
#### Methods
|
149
|
+
|
150
|
+
* **ok?** → **`boolean`**
|
151
|
+
Returns if the request was successful.
|
152
|
+
|
153
|
+
* **headers** → **`hash`**
|
154
|
+
Returns a Hash containing the Headers of the response.
|
155
|
+
|
156
|
+
* **status** → **`string`**
|
157
|
+
Returns the status code.
|
158
|
+
|
159
|
+
* **body** → **`string`**
|
160
|
+
Returns the RAW body of the response.
|
161
|
+
|
162
|
+
|
163
|
+
## Contributing
|
164
|
+
So you want to contribute? That's great! Please follow the guidelines below. It will make it easier to get merged in.
|
165
|
+
|
166
|
+
Before implementing a new feature, please submit a ticket to discuss what you intend to do. Your feature might already be in the works, or an alternative implementation might have already been discussed.
|
167
|
+
|
168
|
+
Do not commit to master in your fork. Provide a clean branch without merge commits. Every pull request should have its own topic branch. In this way, every additional adjustments to the original pull request might be done easily, and squashed with `git rebase -i`. The updated branch will be visible in the same pull request, so there will be no need to open new pull requests when there are changes to be applied.
|
169
|
+
|
170
|
+
Ensure to include proper testing. To run tests you simply have to be in the project's root directory and run:
|
171
|
+
|
172
|
+
```bash
|
173
|
+
$ rake
|
174
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "net-http2"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/net-http2.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'net-http2/client'
|
2
|
+
require 'net-http2/response'
|
3
|
+
require 'net-http2/request/base'
|
4
|
+
require 'net-http2/request/delete'
|
5
|
+
require 'net-http2/request/get'
|
6
|
+
require 'net-http2/request/post'
|
7
|
+
require 'net-http2/request/put'
|
8
|
+
require 'net-http2/stream'
|
9
|
+
require 'net-http2/version'
|
10
|
+
|
11
|
+
module NetHttp2
|
12
|
+
raise "Cannot require NetHttp2, unsupported engine '#{RUBY_ENGINE}'" unless RUBY_ENGINE == "ruby"
|
13
|
+
end
|
@@ -0,0 +1,166 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'openssl'
|
3
|
+
require 'uri'
|
4
|
+
require 'http/2'
|
5
|
+
|
6
|
+
module NetHttp2
|
7
|
+
|
8
|
+
class Client
|
9
|
+
attr_reader :uri
|
10
|
+
|
11
|
+
def initialize(uri, options={})
|
12
|
+
@uri = URI.parse(uri)
|
13
|
+
@ssl_context = options[:ssl_context] || OpenSSL::SSL::SSLContext.new
|
14
|
+
|
15
|
+
@is_ssl = (@uri.scheme == 'https')
|
16
|
+
|
17
|
+
@pipe_r, @pipe_w = Socket.pair(:UNIX, :STREAM, 0)
|
18
|
+
@socket_thread = nil
|
19
|
+
@mutex = Mutex.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def get(path, headers={}, options={})
|
23
|
+
request = NetHttp2::Request::Get.new(@uri, path, headers, options)
|
24
|
+
call_with request
|
25
|
+
end
|
26
|
+
|
27
|
+
def post(path, body, headers={}, options={})
|
28
|
+
request = NetHttp2::Request::Post.new(@uri, path, body, headers, options)
|
29
|
+
call_with request
|
30
|
+
end
|
31
|
+
|
32
|
+
def put(path, body, headers={}, options={})
|
33
|
+
request = NetHttp2::Request::Put.new(@uri, path, body, headers, options)
|
34
|
+
call_with request
|
35
|
+
end
|
36
|
+
|
37
|
+
def delete(path, headers={}, options={})
|
38
|
+
request = NetHttp2::Request::Delete.new(@uri, path, headers, options)
|
39
|
+
call_with request
|
40
|
+
end
|
41
|
+
|
42
|
+
def async_get(path, headers={}, options={}, &block)
|
43
|
+
request = NetHttp2::Request::Get.new(@uri, path, headers, options)
|
44
|
+
async_call_with request, &block
|
45
|
+
end
|
46
|
+
|
47
|
+
def async_post(path, body, headers={}, options={}, &block)
|
48
|
+
request = NetHttp2::Request::Post.new(@uri, path, body, headers, options)
|
49
|
+
async_call_with request, &block
|
50
|
+
end
|
51
|
+
|
52
|
+
def async_put(path, body, headers={}, options={}, &block)
|
53
|
+
request = NetHttp2::Request::Put.new(@uri, path, body, headers, options)
|
54
|
+
async_call_with request, &block
|
55
|
+
end
|
56
|
+
|
57
|
+
def async_delete(path, headers={}, options={}, &block)
|
58
|
+
request = NetHttp2::Request::Delete.new(@uri, path, headers, options)
|
59
|
+
async_call_with request, &block
|
60
|
+
end
|
61
|
+
|
62
|
+
def ssl?
|
63
|
+
@is_ssl
|
64
|
+
end
|
65
|
+
|
66
|
+
def close
|
67
|
+
exit_thread(@socket_thread)
|
68
|
+
|
69
|
+
@h2 = nil
|
70
|
+
@pipe_r = nil
|
71
|
+
@pipe_w = nil
|
72
|
+
@socket_thread = nil
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def call_with(request)
|
78
|
+
ensure_open
|
79
|
+
new_stream.call_with request
|
80
|
+
end
|
81
|
+
|
82
|
+
def async_call_with(request, &block)
|
83
|
+
ensure_open
|
84
|
+
new_stream.async_call_with request, &block
|
85
|
+
end
|
86
|
+
|
87
|
+
def new_stream
|
88
|
+
NetHttp2::Stream.new(uri: @uri, h2_stream: h2.new_stream)
|
89
|
+
end
|
90
|
+
|
91
|
+
def ensure_open
|
92
|
+
return if @socket_thread
|
93
|
+
|
94
|
+
socket = new_socket
|
95
|
+
|
96
|
+
@socket_thread = Thread.new do
|
97
|
+
|
98
|
+
begin
|
99
|
+
thread_loop(socket)
|
100
|
+
ensure
|
101
|
+
socket.close unless socket.closed?
|
102
|
+
@socket_thread = nil
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def thread_loop(socket)
|
108
|
+
loop do
|
109
|
+
if ssl?
|
110
|
+
available = socket.pending
|
111
|
+
if available > 0
|
112
|
+
data_received = socket.sysread(available)
|
113
|
+
h2 << data_received
|
114
|
+
break if socket.closed?
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
ready = IO.select([socket, @pipe_r])
|
119
|
+
|
120
|
+
if ready[0].include?(@pipe_r)
|
121
|
+
data_to_send = @pipe_r.read_nonblock(1024)
|
122
|
+
socket.write(data_to_send)
|
123
|
+
end
|
124
|
+
|
125
|
+
if ready[0].include?(socket)
|
126
|
+
data_received = socket.read_nonblock(1024)
|
127
|
+
h2 << data_received
|
128
|
+
break if socket.closed?
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def new_socket
|
134
|
+
tcp = TCPSocket.new(@uri.host, @uri.port)
|
135
|
+
|
136
|
+
if ssl?
|
137
|
+
socket = OpenSSL::SSL::SSLSocket.new(tcp, @ssl_context)
|
138
|
+
socket.sync_close = true
|
139
|
+
socket.hostname = @uri.hostname
|
140
|
+
|
141
|
+
socket.connect
|
142
|
+
|
143
|
+
socket
|
144
|
+
else
|
145
|
+
tcp
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def h2
|
150
|
+
@h2 ||= HTTP2::Client.new.tap do |h2|
|
151
|
+
h2.on(:frame) do |bytes|
|
152
|
+
@mutex.synchronize do
|
153
|
+
@pipe_w.write(bytes)
|
154
|
+
@pipe_w.flush
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def exit_thread(thread)
|
161
|
+
return unless thread && thread.alive?
|
162
|
+
thread.exit
|
163
|
+
thread.join
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module NetHttp2
|
2
|
+
|
3
|
+
module Request
|
4
|
+
|
5
|
+
DEFAULT_TIMEOUT = 60
|
6
|
+
|
7
|
+
class Base
|
8
|
+
attr_reader :uri, :path, :body, :timeout
|
9
|
+
|
10
|
+
def initialize(method, uri, path, body, headers, options={})
|
11
|
+
@method = method
|
12
|
+
@uri = uri
|
13
|
+
@path = path
|
14
|
+
@body = body
|
15
|
+
@headers = headers
|
16
|
+
@timeout = options[:timeout] || DEFAULT_TIMEOUT
|
17
|
+
end
|
18
|
+
|
19
|
+
def headers
|
20
|
+
@headers.merge!({
|
21
|
+
':scheme' => @uri.scheme,
|
22
|
+
':method' => @method,
|
23
|
+
':path' => @path,
|
24
|
+
})
|
25
|
+
|
26
|
+
@headers.merge!('host' => @uri.host) unless @headers['host']
|
27
|
+
@headers.merge!('content-length' => @body.bytesize.to_s) if @body
|
28
|
+
|
29
|
+
@headers
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module NetHttp2
|
2
|
+
|
3
|
+
class Response
|
4
|
+
attr_reader :headers, :body
|
5
|
+
|
6
|
+
def initialize(options={})
|
7
|
+
@headers = options[:headers]
|
8
|
+
@body = options[:body]
|
9
|
+
end
|
10
|
+
|
11
|
+
def status
|
12
|
+
@headers[':status'] if @headers
|
13
|
+
end
|
14
|
+
|
15
|
+
def ok?
|
16
|
+
status == '200'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module NetHttp2
|
2
|
+
|
3
|
+
class Stream
|
4
|
+
|
5
|
+
def initialize(options={})
|
6
|
+
@h2_stream = options[:h2_stream]
|
7
|
+
@uri = options[:uri]
|
8
|
+
@headers = {}
|
9
|
+
@data = ''
|
10
|
+
@completed = false
|
11
|
+
@block = nil
|
12
|
+
|
13
|
+
@h2_stream.on(:headers) do |hs|
|
14
|
+
hs.each { |k, v| @headers[k] = v }
|
15
|
+
end
|
16
|
+
|
17
|
+
@h2_stream.on(:data) { |d| @data << d }
|
18
|
+
@h2_stream.on(:close) { mark_as_completed_and_async_respond }
|
19
|
+
end
|
20
|
+
|
21
|
+
def call_with(request)
|
22
|
+
send_data_of request
|
23
|
+
sync_respond(request.timeout)
|
24
|
+
end
|
25
|
+
|
26
|
+
def async_call_with(request, &block)
|
27
|
+
@block = block
|
28
|
+
send_data_of request
|
29
|
+
|
30
|
+
Thread.new do
|
31
|
+
wait(request.timeout)
|
32
|
+
@block.call(nil) unless @completed
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def send_data_of(request)
|
39
|
+
headers = request.headers
|
40
|
+
body = request.body
|
41
|
+
|
42
|
+
if body
|
43
|
+
@h2_stream.headers(headers, end_stream: false)
|
44
|
+
@h2_stream.data(body, end_stream: true)
|
45
|
+
else
|
46
|
+
@h2_stream.headers(headers, end_stream: true)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def mark_as_completed_and_async_respond
|
51
|
+
@completed = true
|
52
|
+
@block.call(response) if @block
|
53
|
+
end
|
54
|
+
|
55
|
+
def sync_respond(timeout)
|
56
|
+
wait(timeout)
|
57
|
+
response if @completed
|
58
|
+
end
|
59
|
+
|
60
|
+
def response
|
61
|
+
NetHttp2::Response.new(
|
62
|
+
headers: @headers,
|
63
|
+
body: @data
|
64
|
+
)
|
65
|
+
end
|
66
|
+
|
67
|
+
def wait(timeout)
|
68
|
+
cutoff_time = Time.now + timeout
|
69
|
+
|
70
|
+
while !@completed && Time.now < cutoff_time
|
71
|
+
sleep 0.1
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/net-http2.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'net-http2/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "net-http2"
|
8
|
+
spec.version = NetHttp2::VERSION
|
9
|
+
spec.licenses = ['MIT']
|
10
|
+
spec.authors = ["Roberto Ostinelli"]
|
11
|
+
spec.email = ["roberto@ostinelli.net"]
|
12
|
+
spec.summary = %q{NetHttp2 is an HTTP2 client for Ruby.}
|
13
|
+
spec.homepage = "http://github.com/ostinelli/net-http2"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "http-2", "~> 0.8.1"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: net-http2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.7.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Roberto Ostinelli
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: http-2
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.8.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.8.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- roberto@ostinelli.net
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".ruby-gemset"
|
79
|
+
- ".ruby-version"
|
80
|
+
- ".travis.yml"
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.md
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/net-http2.rb
|
88
|
+
- lib/net-http2/client.rb
|
89
|
+
- lib/net-http2/request/base.rb
|
90
|
+
- lib/net-http2/request/delete.rb
|
91
|
+
- lib/net-http2/request/get.rb
|
92
|
+
- lib/net-http2/request/post.rb
|
93
|
+
- lib/net-http2/request/put.rb
|
94
|
+
- lib/net-http2/response.rb
|
95
|
+
- lib/net-http2/stream.rb
|
96
|
+
- lib/net-http2/version.rb
|
97
|
+
- net-http2.gemspec
|
98
|
+
homepage: http://github.com/ostinelli/net-http2
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
metadata: {}
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 2.5.1
|
119
|
+
signing_key:
|
120
|
+
specification_version: 4
|
121
|
+
summary: NetHttp2 is an HTTP2 client for Ruby.
|
122
|
+
test_files: []
|