module RorVsWild module Local class Middleware include ERB::Util attr_reader :app, :config def initialize(app, config) @app, @config = app, config end def call(env) env["REQUEST_URI"] == "/rorvswild" ? standalone_profiler(env) : embed_profiler(env) end def standalone_profiler(env) html = "\n
" [200, {"Content-Type:" => "text/html; charset=utf-8"}, StringIO.new(inject_into(html))] end def embed_profiler(env) status, headers, body = app.call(env) if status >= 200 && status < 300 && headers["Content-Type"] && headers["Content-Type"].include?("text/html") if headers["Content-Encoding"] log_incompatible_middleware_warning else body.each { |string| inject_into(string) } headers["Content-Length"] = body.map(&:bytesize).reduce(0, :+).to_s if headers["Content-Length"] end end [status, headers, body] end def inject_into(html) markup = html_markup(RorVsWild.agent.queue.requests).encode(html.encoding) style = "".encode(html.encoding) if index = html.index("