Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions lib/rack/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def call(env)
end

def _call(env)
@status, @headers, @body = @app.call(env)
return [@status, @headers, @body] unless html?
response = Rack::Response.new([], @status, @headers)
status, headers, body = @app.call(env)
return [status, headers, body] unless headers['Content-Type'] =~ /html/

response = Rack::Response.new([], status, headers)
env[EVENT_TRACKING_KEY] ||= {}

if session = env["rack.session"]
Expand All @@ -56,16 +56,14 @@ def _call(env)
session[EVENT_TRACKING_KEY] = env[EVENT_TRACKING_KEY]
end

@body.each { |fragment| response.write inject(env, fragment) }
@body.close if @body.respond_to?(:close)
body.each { |fragment| response.write inject(env, fragment) }
body.close if body.respond_to?(:close)

response.finish
end

private

def html?; @headers['Content-Type'] =~ /html/; end

def inject(env, response)
duplicated_response = response.dup
@handlers.each(env) do |handler|
Expand Down