2011-11-17 20:44:48 +00:00
|
|
|
require 'erb'
|
|
|
|
|
2011-11-04 15:51:22 +00:00
|
|
|
module Rack
|
|
|
|
class LiveReload
|
|
|
|
LIVERELOAD_JS_PATH = '/__rack/livereload.js'
|
2011-11-08 14:48:34 +00:00
|
|
|
LIVERELOAD_LOCAL_URI = 'http://localhost:35729/livereload.js'
|
2011-11-04 15:51:22 +00:00
|
|
|
|
2012-01-03 13:58:51 +00:00
|
|
|
BAD_USER_AGENTS = [ %r{MSIE} ]
|
|
|
|
|
2011-11-04 15:51:22 +00:00
|
|
|
attr_reader :app
|
|
|
|
|
2011-11-07 14:06:40 +00:00
|
|
|
def initialize(app, options = {})
|
2012-01-03 13:58:51 +00:00
|
|
|
@app, @options = app, options
|
2011-11-04 15:51:22 +00:00
|
|
|
end
|
|
|
|
|
2011-11-08 14:48:34 +00:00
|
|
|
def use_vendored?
|
|
|
|
return @use_vendored if @use_vendored
|
|
|
|
|
|
|
|
if @options[:source]
|
|
|
|
@use_vendored = (@options[:source] == :vendored)
|
|
|
|
else
|
|
|
|
require 'net/http'
|
|
|
|
require 'uri'
|
|
|
|
|
|
|
|
uri = URI.parse(LIVERELOAD_LOCAL_URI)
|
|
|
|
|
|
|
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
|
|
http.read_timeout = 1
|
|
|
|
|
|
|
|
begin
|
|
|
|
http.send_request('GET', uri.path)
|
|
|
|
@use_vendored = false
|
2011-11-14 14:10:40 +00:00
|
|
|
rescue Timeout::Error, Errno::ECONNREFUSED, EOFError
|
2011-11-08 14:48:34 +00:00
|
|
|
@use_vendored = true
|
2011-11-14 14:10:40 +00:00
|
|
|
rescue => e
|
|
|
|
$stderr.puts e.inspect
|
|
|
|
raise e
|
2011-11-08 14:48:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@use_vendored
|
|
|
|
end
|
|
|
|
|
2011-11-04 15:51:22 +00:00
|
|
|
def call(env)
|
2012-03-04 18:25:45 +00:00
|
|
|
dup._call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def _call(env)
|
2011-11-17 20:44:48 +00:00
|
|
|
_, path, file = (env['PATH_INFO'] || '').split('/')
|
|
|
|
|
|
|
|
if path == '__rack' && ::File.file?(target = ::File.expand_path("../../../js/#{file}", __FILE__))
|
|
|
|
deliver_file(target)
|
2011-11-04 15:51:22 +00:00
|
|
|
else
|
|
|
|
status, headers, body = @app.call(env)
|
2012-03-05 19:14:41 +00:00
|
|
|
body.close if body.respond_to?(:close)
|
2012-03-02 14:25:48 +00:00
|
|
|
|
2012-09-18 21:22:19 +00:00
|
|
|
new_body = [] ; body.each { |line| new_body << line.to_s }
|
|
|
|
|
|
|
|
if !ignored?(env['PATH_INFO']) and !bad_browser?(env['HTTP_USER_AGENT'])
|
|
|
|
if headers['Content-Type'] and status == 200 and headers['Content-Type'][%r{text/html}]
|
2011-12-01 12:25:02 +00:00
|
|
|
content_length = 0
|
|
|
|
|
2012-03-05 19:14:41 +00:00
|
|
|
new_body.each do |line|
|
2012-12-04 19:12:53 +00:00
|
|
|
if !headers['X-Rack-LiveReload'] && line['<head']
|
2011-12-01 12:25:02 +00:00
|
|
|
host_to_use = (@options[:host] || env['HTTP_HOST'] || 'localhost').gsub(%r{:.*}, '')
|
|
|
|
|
|
|
|
if use_vendored?
|
|
|
|
src = LIVERELOAD_JS_PATH.dup + "?host=#{host_to_use}"
|
|
|
|
else
|
|
|
|
src = LIVERELOAD_LOCAL_URI.dup.gsub('localhost', host_to_use) + '?'
|
|
|
|
end
|
2011-11-08 14:48:34 +00:00
|
|
|
|
2011-12-01 12:25:02 +00:00
|
|
|
src << "&mindelay=#{@options[:min_delay]}" if @options[:min_delay]
|
|
|
|
src << "&maxdelay=#{@options[:max_delay]}" if @options[:max_delay]
|
|
|
|
src << "&port=#{@options[:port]}" if @options[:port]
|
2011-11-04 15:51:22 +00:00
|
|
|
|
2011-12-01 12:25:02 +00:00
|
|
|
template = ERB.new(::File.read(::File.expand_path('../../../skel/livereload.html.erb', __FILE__)))
|
2011-11-17 20:44:48 +00:00
|
|
|
|
2012-12-04 19:12:53 +00:00
|
|
|
if line['<head']
|
|
|
|
line.gsub!(/<head[^>]*>/) { |match| %{#{match}#{template.result(binding)}} }
|
2012-12-03 19:00:45 +00:00
|
|
|
end
|
2011-11-07 13:41:51 +00:00
|
|
|
|
2011-12-01 12:25:02 +00:00
|
|
|
headers["X-Rack-LiveReload"] = '1'
|
|
|
|
end
|
|
|
|
|
2012-01-23 18:42:25 +00:00
|
|
|
content_length += line.bytesize
|
2011-11-04 15:51:22 +00:00
|
|
|
end
|
2011-11-07 13:49:42 +00:00
|
|
|
|
2011-12-01 12:25:02 +00:00
|
|
|
headers['Content-Length'] = content_length.to_s
|
2011-11-04 15:51:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-02 14:25:48 +00:00
|
|
|
[ status, headers, new_body ]
|
2011-11-04 15:51:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-03 13:58:51 +00:00
|
|
|
def ignored?(path_info)
|
|
|
|
@options[:ignore] and @options[:ignore].any? { |filter| path_info[filter] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def bad_browser?(user_agent)
|
|
|
|
BAD_USER_AGENTS.any? { |pattern| (user_agent || '')[pattern] }
|
|
|
|
end
|
|
|
|
|
2011-11-04 15:51:22 +00:00
|
|
|
private
|
|
|
|
def deliver_file(file)
|
2011-11-17 20:44:48 +00:00
|
|
|
type = case ::File.extname(file)
|
|
|
|
when '.js'
|
|
|
|
'text/javascript'
|
|
|
|
when '.swf'
|
|
|
|
'application/swf'
|
|
|
|
end
|
|
|
|
|
|
|
|
[ 200, { 'Content-Type' => type, 'Content-Length' => ::File.size(file).to_s }, [ ::File.read(file) ] ]
|
|
|
|
end
|
|
|
|
|
|
|
|
def force_swf?
|
|
|
|
@options[:force_swf]
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_swf?
|
|
|
|
!@options[:no_swf]
|
2011-11-04 15:51:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|