Class: Logtail::Util::Request

Inherits:
Rack::Request
  • Object
show all
Defined in:
lib/logtail-rack/util/request.rb

Constant Summary collapse

HTTP_HEADER_ORIGINAL_DELIMITER =

We store strings as constants since they are reused on a per request basis. This avoids string allocations.

'_'.freeze
HTTP_HEADER_NEW_DELIMITER =
'_'.freeze
HTTP_PREFIX =
'HTTP_'.freeze
REMOTE_IP_KEY_NAME =
'action_dispatch.remote_ip'.freeze
REQUEST_ID_KEY_NAME1 =
'action_dispatch.request_id'.freeze
REQUEST_ID_KEY_NAME2 =
'HTTP_X_REQUEST_ID'.freeze

Instance Method Summary collapse

Instance Method Details

#body_contentObject



15
16
17
18
19
# File 'lib/logtail-rack/util/request.rb', line 15

def body_content
  content = body.read
  body.rewind
  content
end

#headersObject

Returns a list of request headers. The rack env contains a lot of data, this function identifies those that were the actual request headers.

This was extracted from: github.com/ruby-grape/grape/blob/91c6c78ae3d3f3ffabaf57ffc4dc35ab7cfc7b5f/lib/grape/request.rb#L30



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/logtail-rack/util/request.rb', line 25

def headers
  @headers ||= begin
    headers = {}

    @env.each_pair do |k, v|
      next unless k.is_a?(String) && k.to_s.start_with?(HTTP_PREFIX)

      k = k[5..-1].
        split(HTTP_HEADER_ORIGINAL_DELIMITER).
        each(&:capitalize!).
        join(HTTP_HEADER_NEW_DELIMITER)

      headers[k] = v
    end

    headers
  end
end

#ipObject



44
45
46
47
48
49
50
# File 'lib/logtail-rack/util/request.rb', line 44

def ip
  @ip ||= if @env[REMOTE_IP_KEY_NAME]
    @env[REMOTE_IP_KEY_NAME].to_s || super
  else
    super
  end
end

#refererObject



52
53
54
55
# File 'lib/logtail-rack/util/request.rb', line 52

def referer
  # Rails 3.X returns "/" for some reason
  @referer ||= super == "/" ? nil : super
end

#request_idObject



57
58
59
60
# File 'lib/logtail-rack/util/request.rb', line 57

def request_id
  @request_id ||= @env[REQUEST_ID_KEY_NAME1] ||
    @env[REQUEST_ID_KEY_NAME2]
end