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'
|
2010-07-09 21:06:44 -04:00
|
|
|
require 'capybara/util/timeout'
|
2009-11-05 11:35:45 -05:00
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
module Capybara
|
|
|
|
class Server
|
|
|
|
class Identify
|
|
|
|
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__"
|
|
|
|
[200, {}, @app.object_id.to_s]
|
|
|
|
else
|
|
|
|
@app.call(env)
|
|
|
|
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
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
end
|
2009-11-04 17:00:05 -05:00
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
def host
|
|
|
|
"localhost"
|
|
|
|
end
|
2009-11-04 17:00:05 -05:00
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
def url(path)
|
|
|
|
if path =~ /^http/
|
|
|
|
path
|
|
|
|
else
|
|
|
|
(Capybara.app_host || "http://#{host}:#{port}") + path.to_s
|
|
|
|
end
|
2010-01-17 11:40:26 -05:00
|
|
|
end
|
2009-12-13 08:33:14 -05:00
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
def responsive?
|
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
|
|
|
|
rescue Errno::ECONNREFUSED, Errno::EBADF
|
|
|
|
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 handler
|
2010-02-19 12:13:03 -05:00
|
|
|
begin
|
2010-07-09 14:31:58 -04:00
|
|
|
require 'rack/handler/thin'
|
|
|
|
Rack::Handler::Thin
|
2010-02-19 12:13:03 -05:00
|
|
|
rescue LoadError
|
2010-07-09 14:31:58 -04:00
|
|
|
begin
|
|
|
|
require 'rack/handler/mongrel'
|
|
|
|
Rack::Handler::Mongrel
|
|
|
|
rescue LoadError
|
|
|
|
require 'rack/handler/webrick'
|
|
|
|
Rack::Handler::WEBrick
|
|
|
|
end
|
2010-02-19 12:13:03 -05:00
|
|
|
end
|
|
|
|
end
|
2009-12-13 08:33:14 -05:00
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
def boot
|
2010-07-14 16:54:36 -04:00
|
|
|
if @app
|
|
|
|
@port = Capybara::Server.ports[@app.object_id]
|
|
|
|
|
|
|
|
if not @port or not responsive?
|
|
|
|
@port = find_available_port
|
|
|
|
Capybara::Server.ports[@app.object_id] = @port
|
2009-12-13 08:33:14 -05:00
|
|
|
|
2010-07-14 16:54:36 -04:00
|
|
|
Thread.new { handler.run(Identify.new(@app), :Port => @port, :AccessLog => []) }
|
2010-07-09 14:31:58 -04:00
|
|
|
|
2010-07-14 16:54:36 -04:00
|
|
|
Capybara.timeout(10) { if responsive? then true else sleep(0.5) and false end }
|
2010-07-09 14:31:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Timeout::Error
|
2010-07-11 07:11:30 -04:00
|
|
|
puts "Rack application timed out during boot"
|
2010-07-09 14:31:58 -04:00
|
|
|
exit
|
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
|
|
|
|
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
|