Class: Rack::Padlock::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/padlock/server.rb

Constant Summary collapse

PORT =
9988

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Server

Returns a new instance of Server.



10
11
12
13
14
# File 'lib/rack/padlock/server.rb', line 10

def initialize(app)
  @app = app
  @middleware = Rack::Padlock.new(@app)
  @server_thread = nil
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



8
9
10
# File 'lib/rack/padlock/server.rb', line 8

def app
  @app
end

Instance Method Details

#base_uriObject



45
46
47
# File 'lib/rack/padlock/server.rb', line 45

def base_uri
  "https://localhost:#{PORT}"
end

#bootObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rack/padlock/server.rb', line 16

def boot
  @server_thread = Thread.new do
    webserver = Rack::Padlock::Webrick.run(@middleware, PORT)
  end

  Timeout.timeout(60) { @server_thread.join(0.1) until responsive? }
rescue Timeout::Error
  raise "Rack application timed out during boot"
else
  self
end

#responsive?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rack/padlock/server.rb', line 28

def responsive?
  return false if @server_thread && @server_thread.join(0)

  require "net/https"
  require 'uri'
  uri = URI.parse("#{base_uri}/padlock_middleware/poke")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)

  return response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPRedirection)
rescue Errno::ECONNREFUSED, Errno::EBADF
  return false
end