Class: Bugsnag::Middleware::RackRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/bugsnag/middleware/rack_request.rb

Overview

Extracts and attaches rack data to an error report

Constant Summary collapse

SPOOF =
"[SPOOF]".freeze
"Cookie".freeze

Instance Method Summary collapse

Constructor Details

#initialize(bugsnag) ⇒ RackRequest

Returns a new instance of RackRequest.


10
11
12
# File 'lib/bugsnag/middleware/rack_request.rb', line 10

def initialize(bugsnag)
  @bugsnag = bugsnag
end

Instance Method Details

#call(report) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bugsnag/middleware/rack_request.rb', line 14

def call(report)
  if report.request_data[:rack_env]
    env = report.request_data[:rack_env]

    request = ::Rack::Request.new(env)

    params = request.params rescue {}
    client_ip = request.ip.to_s rescue SPOOF
    session = env["rack.session"]

    # Set the automatic context
    report.automatic_context = "#{request.request_method} #{request.path}"

    # Set a sensible default for user_id
    report.user["id"] = request.ip

    # Build the clean url (hide the port if it is obvious)
    url = "#{request.scheme}://#{request.host}"
    url << ":#{request.port}" unless [80, 443].include?(request.port)

    # If app is passed a bad URL, this code will crash attempting to clean it
    begin
      url << Bugsnag.cleaner.clean_url(request.fullpath)
    rescue StandardError => stde
      Bugsnag.configuration.warn "RackRequest - Rescued error while cleaning request.fullpath: #{stde}"
    end

    referer = nil
    begin
      referer = Bugsnag.cleaner.clean_url(request.referer) if request.referer
    rescue StandardError => stde
      Bugsnag.configuration.warn "RackRequest - Rescued error while cleaning request.referer: #{stde}"
    end

    # Add a request tab
    report.add_tab(:request, {
      :url => url,
      :httpMethod => request.request_method,
      :params => params.to_hash,
      :referer => referer,
      :clientIp => client_ip,
      :headers => format_headers(env, referer)
    })

    # add the HTTP version if present
    if env["SERVER_PROTOCOL"]
      report.(:request, :httpVersion, env["SERVER_PROTOCOL"])
    end

    add_request_body(report, request, env)
    add_cookies(report, request)

    # Add an environment tab
    if report.configuration.send_environment
      report.add_tab(:environment, env)
    end

    # Add a session tab
    if session
      if session.is_a?(Hash)
        # Rails 3
        report.add_tab(:session, session)
      elsif session.respond_to?(:to_hash)
        # Rails 4
        report.add_tab(:session, session.to_hash)
      end
    end
  end

  @bugsnag.call(report)
end