File: checker.rb

package info (click to toggle)
ruby-capybara 3.40.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,368 kB
  • sloc: ruby: 23,988; javascript: 752; makefile: 11
file content (44 lines) | stat: -rw-r--r-- 929 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# frozen_string_literal: true

module Capybara
  class Server
    class Checker
      TRY_HTTPS_ERRORS = [EOFError, Net::ReadTimeout, Errno::ECONNRESET].freeze

      def initialize(host, port)
        @host, @port = host, port
        @ssl = false
      end

      def request(&block)
        ssl? ? https_request(&block) : http_request(&block)
      rescue *TRY_HTTPS_ERRORS
        res = https_request(&block)
        @ssl = true
        res
      end

      def ssl?
        @ssl
      end

    private

      def http_request(&block)
        make_request(read_timeout: 2, &block)
      end

      def https_request(&block)
        make_request(**ssl_options, &block)
      end

      def make_request(**options, &block)
        Net::HTTP.start(@host, @port, options.merge(max_retries: 0), &block)
      end

      def ssl_options
        { use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE }
      end
    end
  end
end