require 'rack/livereload' module Rack class LiveReload class BodyProcessor LIVERELOAD_JS_PATH = '/__rack/livereload.js' HEAD_TAG_REGEX = /|/ LIVERELOAD_PORT = 35729 attr_reader :content_length, :new_body, :livereload_added def livereload_local_uri "http://localhost:#{@options[:live_reload_port]}/livereload.js" end def initialize(body, options) @body, @options = body, options @options[:live_reload_port] ||= LIVERELOAD_PORT @processed = false end def force_swf? @options[:force_swf] end def with_swf? !@options[:no_swf] end 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 rescue Timeout::Error, Errno::ECONNREFUSED, EOFError @use_vendored = true rescue => e $stderr.puts e.inspect raise e end end @use_vendored end def processed? @processed end def process!(env) @env = env @body.close if @body.respond_to?(:close) @new_body = [] ; @body.each { |line| @new_body << line.to_s } @content_length = 0 @livereload_added = false @new_body.each do |line| if !@livereload_added && line['