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
|
2012-07-13 08:57:43 -04:00
|
|
|
class Middleware
|
|
|
|
attr_accessor :error
|
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
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
|
2012-07-13 08:57:43 -04:00
|
|
|
begin
|
|
|
|
@app.call(env)
|
|
|
|
rescue StandardError => e
|
|
|
|
@error = e unless @error
|
|
|
|
raise e
|
|
|
|
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
|
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
attr_reader :app, :port
|
2009-12-13 08:33:14 -05:00
|
|
|
|
2012-07-13 09:19:20 -04:00
|
|
|
def initialize(app, port=Capybara.server_port)
|
2010-07-09 14:31:58 -04:00
|
|
|
@app = app
|
2012-07-13 08:57:43 -04:00
|
|
|
@middleware = Middleware.new(@app)
|
2012-03-06 17:42:47 -05:00
|
|
|
@server_thread = nil # supress warnings
|
2012-07-13 09:19:20 -04:00
|
|
|
@port = port
|
|
|
|
@port ||= Capybara::Server.ports[@app.object_id]
|
|
|
|
@port ||= find_available_port
|
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!
|
|
|
|
@middleware.error = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def error
|
|
|
|
@middleware.error
|
|
|
|
end
|
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
def host
|
2011-10-05 15:09:37 -04:00
|
|
|
Capybara.server_host || "127.0.0.1"
|
2010-07-09 14:31:58 -04:00
|
|
|
end
|
2009-11-04 17:00:05 -05:00
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
def responsive?
|
2013-03-16 14:59:02 -04:00
|
|
|
return false if server_thread && server_thread.join(0)
|
2012-03-06 17:42:47 -05:00
|
|
|
|
2010-07-14 16:54:36 -04:00
|
|
|
res = Net::HTTP.start(host, @port) { |http| http.get('/__identify__') }
|
|
|
|
|
|
|
|
if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
|
|
|
|
return res.body == @app.object_id.to_s
|
|
|
|
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
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
def boot
|
2012-07-13 09:19:20 -04:00
|
|
|
unless responsive?
|
|
|
|
Capybara::Server.ports[@app.object_id] = @port
|
2010-10-08 11:30:08 -04:00
|
|
|
|
2012-07-13 09:19:20 -04:00
|
|
|
@server_thread = Thread.new do
|
|
|
|
Capybara.server.call(@middleware, @port)
|
2010-07-09 14:31:58 -04:00
|
|
|
end
|
2012-07-13 09:19:20 -04:00
|
|
|
|
2013-03-16 14:59:02 -04:00
|
|
|
Timeout.timeout(60) { server_thread.join(0.1) until responsive? }
|
2010-07-09 14:31:58 -04:00
|
|
|
end
|
2012-12-26 08:50:45 -05:00
|
|
|
rescue Timeout::Error
|
2011-10-19 16:26:58 -04:00
|
|
|
raise "Rack application timed out during boot"
|
2010-07-14 16:54:36 -04:00
|
|
|
else
|
|
|
|
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
|
|
|
|
2013-03-16 14:59:02 -04:00
|
|
|
attr_reader :server_thread
|
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
def find_available_port
|
2010-07-14 16:54:36 -04:00
|
|
|
server = TCPServer.new('127.0.0.1', 0)
|
|
|
|
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
|