2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2018-01-08 15:23:54 -05:00
|
|
|
|
2009-12-09 10:39:03 -05:00
|
|
|
require 'uri'
|
2009-11-05 11:35:45 -05:00
|
|
|
require 'net/http'
|
2009-11-05 11:39:57 -05:00
|
|
|
require 'rack'
|
2009-11-05 11:35:45 -05:00
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
module Capybara
|
|
|
|
class Server
|
2016-01-18 17:01:12 -05:00
|
|
|
class Middleware
|
|
|
|
class Counter
|
|
|
|
attr_reader :value
|
2015-07-06 19:19:47 -04:00
|
|
|
|
2016-01-18 17:01:12 -05:00
|
|
|
def initialize
|
|
|
|
@value = 0
|
|
|
|
@mutex = Mutex.new
|
|
|
|
end
|
2015-07-06 19:19:47 -04:00
|
|
|
|
2016-01-18 17:01:12 -05:00
|
|
|
def increment
|
|
|
|
@mutex.synchronize { @value += 1 }
|
|
|
|
end
|
2015-07-06 19:19:47 -04:00
|
|
|
|
2016-01-18 17:01:12 -05:00
|
|
|
def decrement
|
|
|
|
@mutex.synchronize { @value -= 1 }
|
|
|
|
end
|
2015-07-06 19:19:47 -04:00
|
|
|
end
|
|
|
|
|
2012-07-13 08:57:43 -04:00
|
|
|
attr_accessor :error
|
|
|
|
|
2016-12-15 12:04:01 -05:00
|
|
|
def initialize(app, server_errors)
|
2010-07-09 14:31:58 -04:00
|
|
|
@app = app
|
2015-07-06 19:19:47 -04:00
|
|
|
@counter = Counter.new
|
2016-12-15 12:04:01 -05:00
|
|
|
@server_errors = server_errors
|
2015-07-06 19:19:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def pending_requests?
|
|
|
|
@counter.value > 0
|
2010-07-09 14:31:58 -04:00
|
|
|
end
|
2009-12-13 08:33:14 -05:00
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
def call(env)
|
|
|
|
if env["PATH_INFO"] == "/__identify__"
|
2011-04-26 06:41:43 -04:00
|
|
|
[200, {}, [@app.object_id.to_s]]
|
2010-07-09 14:31:58 -04:00
|
|
|
else
|
2015-07-06 19:19:47 -04:00
|
|
|
@counter.increment
|
2012-07-13 08:57:43 -04:00
|
|
|
begin
|
|
|
|
@app.call(env)
|
2016-12-15 12:04:01 -05:00
|
|
|
rescue *@server_errors => e
|
2018-01-08 15:23:54 -05:00
|
|
|
@error ||= e
|
2012-07-13 08:57:43 -04:00
|
|
|
raise e
|
2015-07-06 19:19:47 -04:00
|
|
|
ensure
|
|
|
|
@counter.decrement
|
2012-07-13 08:57:43 -04:00
|
|
|
end
|
2010-07-09 14:31:58 -04:00
|
|
|
end
|
2009-12-13 08:33:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-14 16:54:36 -04:00
|
|
|
class << self
|
|
|
|
def ports
|
|
|
|
@ports ||= {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-11 17:26:37 -04:00
|
|
|
attr_reader :app, :port, :host
|
2009-12-13 08:33:14 -05:00
|
|
|
|
2018-05-01 16:36:42 -04:00
|
|
|
def initialize(app, *deprecated_options, port: Capybara.server_port, host: Capybara.server_host, reportable_errors: Capybara.server_errors)
|
|
|
|
warn "Positional arguments, other than the application, to Server#new are deprecated, please use keyword arguments" unless deprecated_options.empty?
|
2010-07-09 14:31:58 -04:00
|
|
|
@app = app
|
2014-03-31 18:10:27 -04:00
|
|
|
@server_thread = nil # suppress warnings
|
2018-05-01 16:36:42 -04:00
|
|
|
@host = deprecated_options[1] || host
|
|
|
|
@reportable_errors = deprecated_options[2] || reportable_errors
|
|
|
|
@using_ssl = false
|
|
|
|
@port = deprecated_options[0] || port
|
2016-07-25 13:04:50 -04:00
|
|
|
@port ||= Capybara::Server.ports[port_key]
|
2016-03-18 17:34:58 -04:00
|
|
|
@port ||= find_available_port(host)
|
2010-07-09 14:31:58 -04:00
|
|
|
end
|
2009-11-04 17:00:05 -05:00
|
|
|
|
2012-07-13 08:57:43 -04:00
|
|
|
def reset_error!
|
2016-07-25 15:12:59 -04:00
|
|
|
middleware.error = nil
|
2012-07-13 08:57:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def error
|
2016-07-25 15:12:59 -04:00
|
|
|
middleware.error
|
2012-07-13 08:57:43 -04:00
|
|
|
end
|
|
|
|
|
2018-05-01 16:36:42 -04:00
|
|
|
def using_ssl?
|
|
|
|
@using_ssl
|
|
|
|
end
|
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
def responsive?
|
2013-03-17 19:35:48 -04:00
|
|
|
return false if @server_thread && @server_thread.join(0)
|
2012-03-06 17:42:47 -05:00
|
|
|
|
2018-05-01 16:36:42 -04:00
|
|
|
begin
|
|
|
|
res = if !@using_ssl
|
|
|
|
http_connect
|
|
|
|
else
|
|
|
|
https_connect
|
|
|
|
end
|
|
|
|
rescue EOFError, Net::ReadTimeout
|
|
|
|
res = https_connect
|
|
|
|
@using_ssl = true
|
|
|
|
end
|
2010-07-14 16:54:36 -04:00
|
|
|
|
|
|
|
if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
|
2016-07-25 15:12:59 -04:00
|
|
|
return res.body == app.object_id.to_s
|
2010-07-14 16:54:36 -04:00
|
|
|
end
|
2013-03-16 14:59:02 -04:00
|
|
|
rescue SystemCallError
|
2010-07-14 16:54:36 -04:00
|
|
|
return false
|
2010-07-09 14:31:58 -04:00
|
|
|
end
|
2009-12-13 08:33:14 -05:00
|
|
|
|
2015-07-06 19:19:47 -04:00
|
|
|
def wait_for_pending_requests
|
2018-04-18 10:52:37 -04:00
|
|
|
start_time = Capybara::Helpers.monotonic_time
|
|
|
|
while pending_requests?
|
|
|
|
if (Capybara::Helpers.monotonic_time - start_time) > 60
|
|
|
|
raise "Requests did not finish in 60 seconds"
|
|
|
|
end
|
|
|
|
sleep 0.01
|
|
|
|
end
|
2015-07-06 19:19:47 -04:00
|
|
|
end
|
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
def boot
|
2012-07-13 09:19:20 -04:00
|
|
|
unless responsive?
|
2016-07-25 15:12:59 -04:00
|
|
|
Capybara::Server.ports[port_key] = port
|
2010-10-08 11:30:08 -04:00
|
|
|
|
2012-07-13 09:19:20 -04:00
|
|
|
@server_thread = Thread.new do
|
2016-07-25 15:12:59 -04:00
|
|
|
Capybara.server.call(middleware, port, host)
|
2010-07-09 14:31:58 -04:00
|
|
|
end
|
2012-07-13 09:19:20 -04:00
|
|
|
|
2018-04-18 10:52:37 -04:00
|
|
|
start_time = Capybara::Helpers.monotonic_time
|
|
|
|
until responsive?
|
|
|
|
if (Capybara::Helpers.monotonic_time - start_time) > 60
|
|
|
|
raise "Rack application timed out during boot"
|
|
|
|
end
|
|
|
|
@server_thread.join(0.1)
|
|
|
|
end
|
2010-07-09 14:31:58 -04:00
|
|
|
end
|
2018-04-18 10:52:37 -04:00
|
|
|
|
2010-07-14 16:54:36 -04:00
|
|
|
self
|
2009-11-04 17:00:05 -05:00
|
|
|
end
|
2009-11-07 09:35:47 -05:00
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
private
|
2010-01-17 11:40:26 -05:00
|
|
|
|
2018-05-01 16:36:42 -04:00
|
|
|
def http_connect
|
|
|
|
Net::HTTP.start(host, port, read_timeout: 2) { |http| http.get('/__identify__') }
|
|
|
|
end
|
|
|
|
|
|
|
|
def https_connect
|
|
|
|
Net::HTTP.start(host, port, use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) { |http| http.get('/__identify__') }
|
|
|
|
end
|
|
|
|
|
2016-07-25 15:12:59 -04:00
|
|
|
def middleware
|
2018-05-01 16:36:42 -04:00
|
|
|
@middleware ||= Middleware.new(app, @reportable_errors)
|
2016-07-25 15:12:59 -04:00
|
|
|
end
|
|
|
|
|
2016-07-25 13:04:50 -04:00
|
|
|
def port_key
|
2016-07-25 15:12:59 -04:00
|
|
|
Capybara.reuse_server ? app.object_id : middleware.object_id
|
2016-07-25 13:04:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def pending_requests?
|
2016-07-25 15:12:59 -04:00
|
|
|
middleware.pending_requests?
|
2016-07-25 13:04:50 -04:00
|
|
|
end
|
|
|
|
|
2016-03-18 17:34:58 -04:00
|
|
|
def find_available_port(host)
|
|
|
|
server = TCPServer.new(host, 0)
|
2010-07-14 16:54:36 -04:00
|
|
|
server.addr[1]
|
|
|
|
ensure
|
|
|
|
server.close if server
|
2010-07-09 04:05:30 -04:00
|
|
|
end
|
2010-07-09 14:31:58 -04:00
|
|
|
end
|
2009-11-07 09:35:47 -05:00
|
|
|
end
|