@@ -10,13 +10,8 @@ module Seahorse
10
10
module Client
11
11
# @api private
12
12
module H2
13
-
14
13
# H2 Connection build on top of `http/2` gem
15
- # (requires Ruby >= 2.1)
16
- # with TLS layer plus ALPN, requires:
17
- # Ruby >= 2.3 and OpenSSL >= 1.0.2
18
14
class Connection
19
-
20
15
OPTIONS = {
21
16
max_concurrent_streams : 100 ,
22
17
connection_timeout : 60 ,
@@ -27,7 +22,7 @@ class Connection
27
22
ssl_ca_bundle : nil ,
28
23
ssl_ca_directory : nil ,
29
24
ssl_ca_store : nil ,
30
- enable_alpn : false
25
+ enable_alpn : true
31
26
}
32
27
33
28
# chunk read size at socket
@@ -41,25 +36,23 @@ def initialize(options = {})
41
36
instance_variable_set ( "@#{ opt_name } " , value )
42
37
end
43
38
@h2_client = HTTP2 ::Client . new (
44
- settings_max_concurrent_streams : max_concurrent_streams
39
+ settings_max_concurrent_streams : @ max_concurrent_streams
45
40
)
46
- @logger = if @http_wire_trace
47
- options [ :logger ] || Logger . new ( $stdout)
48
- end
41
+ @logger ||= Logger . new ( $stdout) if @http_wire_trace
49
42
@chunk_size = options [ :read_chunk_size ] || CHUNKSIZE
43
+
50
44
@errors = [ ]
51
45
@status = :ready
46
+
52
47
@mutex = Mutex . new # connection can be shared across requests
53
48
@socket = nil
54
49
@socket_thread = nil
55
50
end
56
51
57
52
OPTIONS . keys . each do |attr_name |
58
- attr_reader ( attr_name )
53
+ attr_reader attr_name
59
54
end
60
55
61
- alias ssl_verify_peer? ssl_verify_peer
62
-
63
56
attr_reader :errors
64
57
65
58
attr_accessor :input_signal_thread
@@ -112,7 +105,7 @@ def start(stream)
112
105
@h2_client << data
113
106
rescue IO ::WaitReadable
114
107
begin
115
- unless IO . select ( [ @socket ] , nil , nil , connection_read_timeout )
108
+ unless IO . select ( [ @socket ] , nil , nil , @ connection_read_timeout)
116
109
self . debug_output ( 'socket connection read time out' )
117
110
self . close!
118
111
else
@@ -154,11 +147,11 @@ def closed?
154
147
end
155
148
156
149
def debug_output ( msg , type = nil )
157
- prefix = case type
150
+ prefix =
151
+ case type
158
152
when :send then '-> '
159
153
when :receive then '<- '
160
- else
161
- ''
154
+ else ''
162
155
end
163
156
return unless @logger
164
157
_debug_entry ( prefix + msg )
@@ -206,7 +199,7 @@ def _nonblocking_connect(tcp, addr)
206
199
begin
207
200
tcp . connect_nonblock ( addr )
208
201
rescue IO ::WaitWritable
209
- unless IO . select ( nil , [ tcp ] , nil , connection_timeout )
202
+ unless IO . select ( nil , [ tcp ] , nil , @ connection_timeout)
210
203
tcp . close
211
204
raise
212
205
end
@@ -220,31 +213,28 @@ def _nonblocking_connect(tcp, addr)
220
213
221
214
def _tls_context
222
215
ssl_ctx = OpenSSL ::SSL ::SSLContext . new ( :TLSv1_2 )
223
- if ssl_verify_peer?
216
+ if @ ssl_verify_peer
224
217
ssl_ctx . verify_mode = OpenSSL ::SSL ::VERIFY_PEER
225
- ssl_ctx . ca_file = ssl_ca_bundle ? ssl_ca_bundle : _default_ca_bundle
226
- ssl_ctx . ca_path = ssl_ca_directory ? ssl_ca_directory : _default_ca_directory
227
- ssl_ctx . cert_store = ssl_ca_store if ssl_ca_store
218
+ ssl_ctx . ca_file = @ ssl_ca_bundle || _default_ca_bundle
219
+ ssl_ctx . ca_path = @ ssl_ca_directory || _defalt_ca_directory
220
+ ssl_ctx . cert_store = @ ssl_ca_store if @ ssl_ca_store
228
221
else
229
222
ssl_ctx . verify_mode = OpenSSL ::SSL ::VERIFY_NONE
230
223
end
231
- if enable_alpn
224
+ if @ enable_alpn
232
225
debug_output ( 'enabling ALPN for TLS ...' )
233
226
ssl_ctx . alpn_protocols = [ 'h2' ]
234
227
end
235
228
ssl_ctx
236
229
end
237
230
238
231
def _default_ca_bundle
239
- File . exist? ( OpenSSL ::X509 ::DEFAULT_CERT_FILE ) ?
240
- OpenSSL ::X509 ::DEFAULT_CERT_FILE : nil
232
+ OpenSSL ::X509 ::DEFAULT_CERT_FILE if File . exist? ( OpenSSL ::X509 ::DEFAULT_CERT_FILE )
241
233
end
242
234
243
- def _default_ca_directory
244
- Dir . exist? ( OpenSSL ::X509 ::DEFAULT_CERT_DIR ) ?
245
- OpenSSL ::X509 ::DEFAULT_CERT_DIR : nil
235
+ def _defalt_ca_directory
236
+ OpenSSL ::X509 ::DEFAULT_CERT_DIR if Dir . exist? ( OpenSSL ::X509 ::DEFAULT_CERT_DIR )
246
237
end
247
-
248
238
end
249
239
end
250
240
end
0 commit comments