Class: Net::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/MrMurano/http.rb

Instance Method Summary collapse

Instance Method Details

#connectObject



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/MrMurano/http.rb', line 280

def connect
  if proxy?
    conn_address = proxy_address
    conn_port    = proxy_port
  else
    conn_address = address
    conn_port    = port
  end

  D "opening connection to #{conn_address}:#{conn_port}..."
  s = Timeout.timeout(@open_timeout, Net::OpenTimeout) do
    TCPSocket.open(conn_address, conn_port, @local_host, @local_port)
  end
  s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
  D 'opened'
  if use_ssl?
    ssl_parameters = {}
    iv_list = instance_variables
    SSL_IVNAMES.each_with_index do |ivname, i|
      if iv_list.include?(ivname)
        value = instance_variable_get(ivname)
        ssl_parameters[SSL_ATTRIBUTES[i]] = value if value
      end
    end
    @ssl_context = OpenSSL::SSL::SSLContext.new
    @ssl_context.set_params(ssl_parameters)
    D "starting SSL for #{conn_address}:#{conn_port}..."
    s = OpenSSL::SSL::SSLSocket.new(s, @ssl_context)
    s.sync_close = true
    D 'SSL established'
  end
  @socket = BufferedIO.new(s)
  @socket.read_timeout = @read_timeout
  @socket.continue_timeout = @continue_timeout
  @socket.debug_output = @debug_output
  if use_ssl?
    begin
      if proxy?
        # 2017-07-02: Changing shovel operator << to +=
        # to support Ruby 3.0 frozen string literals.
        buf = "CONNECT #{@address}:#{@port} HTTP/#{HTTPVersion}\r\n"
        buf += "Host: #{@address}:#{@port}\r\n"
        if proxy_user
          credential = ["#{proxy_user}:#{proxy_pass}"].pack('m')
          credential.delete!("\r\n")
          buf += "Proxy-Authorization: Basic #{credential}\r\n"
        end
        buf += "\r\n"
        @socket.write(buf)
        HTTPResponse.read_new(@socket).value
      end
      # Server Name Indication (SNI) RFC 3546
      s.hostname = @address if s.respond_to? :hostname=
      if @ssl_session &&
         Time.now < @ssl_session.time + @ssl_session.timeout
        s.session = @ssl_session if @ssl_session
      end
      Timeout.timeout(@open_timeout, Net::OpenTimeout) { s.connect }
      if @ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE
        s.post_connection_check(@address)
      end
      @ssl_session = s.session
    rescue StandardError => exception
      D "Conn close because of connect error #{exception}"
      @socket.close if @socket && !@socket.closed?
      raise exception
    end
  end
  on_connect
end