Class: Http2::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/http2/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(http2) ⇒ Connection

Returns a new instance of Connection.



2
3
4
5
6
7
8
# File 'lib/http2/connection.rb', line 2

def initialize(http2)
  @http2 = http2
  @debug = http2.debug
  @args = http2.args
  @nl = http2.nl
  reconnect
end

Instance Method Details

#apply_sslObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/http2/connection.rb', line 151

def apply_ssl
  puts "Http2: Initializing SSL." if @debug
  require "openssl" unless ::Kernel.const_defined?(:OpenSSL)

  ssl_context = OpenSSL::SSL::SSLContext.new

  if @args[:ssl_skip_verify]
    ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
  else
    ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
  end

  @sock_ssl = OpenSSL::SSL::SSLSocket.new(@sock_plain, ssl_context)
  @sock_ssl.hostname = @http2.host
  @sock_ssl.sync_close = true
  @sock_ssl.connect

  @sock = @sock_ssl
end

#closeObject

Closes the current connection if any.



104
105
106
107
108
# File 'lib/http2/connection.rb', line 104

def close
  @sock.close if @sock && !@sock.closed?
  @sock_ssl.close if @sock_ssl && !@sock_ssl.closed?
  @sock_plain.close if @sock_plain && !@sock_plain.closed?
end

#closed?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/http2/connection.rb', line 29

def closed?
  @sock.closed?
end

#connect_proxyObject



146
147
148
149
# File 'lib/http2/connection.rb', line 146

def connect_proxy
  puts "Http2: Opening socket connection to '#{@args[:host]}:#{@args[:port]}' through proxy '#{@args[:proxy][:host]}:#{@args[:proxy][:port]}'." if @debug
  @sock_plain = TCPSocket.new(@args[:proxy][:host], @args[:proxy][:port].to_i)
end

#connect_proxy_connectObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/http2/connection.rb', line 110

def connect_proxy_connect
  puts "Http2: Initializing proxy connect to '#{@args[:host]}:#{@args[:port]}' through proxy '#{@args[:proxy][:host]}:#{@args[:proxy][:port]}'." if @debug
  @sock_plain = TCPSocket.new(@args[:proxy][:host], @args[:proxy][:port])

  connect = "CONNECT #{@args[:host]}:#{@args[:port]} HTTP/1.1#{@nl}"
  puts "Http2: Sending connect: #{connect}" if @debug
  @sock_plain.write(connect)

  headers = {
    "Host" => "#{@args[:host]}:#{@args[:port]}"
  }

  if @args[:proxy][:user] && @args[:proxy][:passwd]
    headers["Proxy-Authorization"] = "Basic #{["#{@args[:proxy][:user]}:#{@args[:proxy][:passwd]}"].pack("m").chomp}"
  end

  headers.each do |key, value|
    header = "#{key}: #{value}"
    puts "Http2: Sending header to proxy: #{header}" if @debug
    @sock_plain.write("#{header}#{@nl}")
  end

  @sock_plain.write(@nl)

  res = @sock_plain.gets.to_s
  raise "Couldn't connect through proxy: #{res}" unless res.match(/^http\/1\.(0|1)\s+200/i)

  @sock_plain.gets

  @proxy_connect = true
end

#destroyObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/http2/connection.rb', line 10

def destroy
  @sock.close if @sock && !@sock.closed?
  @sock = nil

  @sock_plain.close if @sock_plain && !@sock_plain.closed?
  @sock_plain = nil

  @sock_ssl.close if @sock_ssl && !@sock_ssl.closed?
  @sock_ssl = nil
end

#getsObject



21
22
23
# File 'lib/http2/connection.rb', line 21

def gets
  @sock.gets
end

#proxy_connect?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/http2/connection.rb', line 142

def proxy_connect?
  @proxy_connect
end

#read(length) ⇒ Object



25
26
27
# File 'lib/http2/connection.rb', line 25

def read(length)
  @sock.read(length)
end

#reconnectObject

Reconnects to the host.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/http2/connection.rb', line 64

def reconnect
  puts "Http2: Reconnect." if @debug

  # Open connection.
  if @args[:proxy]
    if @args[:proxy][:connect]
      connect_proxy_connect
    else
      connect_proxy
    end
  else
    puts "Http2: Opening socket connection to '#{@http2.host}:#{@http2.port}'." if @debug
    @sock_plain = TCPSocket.new(@http2.host, @http2.port)
  end

  if @args[:ssl]
    apply_ssl
  else
    @sock = @sock_plain
  end
end

#sock_puts(str) ⇒ Object



41
42
43
# File 'lib/http2/connection.rb', line 41

def sock_puts(str)
  sock_write("#{str}#{@nl}")
end

#sock_write(str) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/http2/connection.rb', line 33

def sock_write(str)
  str = str.to_s
  return if str.empty?

  count = @sock.write(str)
  raise "Couldnt write to socket: '#{count}', '#{str}'." if count <= 0
end

#socket_working?Boolean

Returns boolean based on the if the object is connected and the socket is working.

Examples

puts “Socket is working.” if http.socket_working?

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/http2/connection.rb', line 89

def socket_working?
  return false if !@sock || @sock.closed?

  if @keepalive_timeout && @request_last
    between = Time.now.to_i - @request_last.to_i
    if between >= @keepalive_timeout
      puts "Http2: We are over the keepalive-wait - returning false for socket_working?." if @debug
      return false
    end
  end

  true
end

#write(str) ⇒ Object

Tries to write a string to the socket. If it fails it reconnects and tries again.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/http2/connection.rb', line 46

def write(str)
  reconnect unless socket_working?

  puts "Http2: Writing: #{str}" if @debug

  begin
    raise Errno::EPIPE, "The socket is closed." if !@sock || @sock.closed?

    sock_write(str)
  rescue Errno::EPIPE # this can also be thrown by puts.
    reconnect
    sock_write(str)
  end

  @request_last = Time.now
end