Module: TShield::Controllers::Requests::Helpers

Included in:
Server
Defined in:
lib/tshield/controllers/requests.rb

Instance Method Summary collapse

Instance Method Details

#add_headers(headers, path) ⇒ Object



99
100
101
102
103
# File 'lib/tshield/controllers/requests.rb', line 99

def add_headers(headers, path)
  configuration.get_headers(domain(path)).each do |source, destiny| 
    headers[destiny] = request.env[source] unless request.env[source].nil?
  end
end

#configurationObject



105
106
107
# File 'lib/tshield/controllers/requests.rb', line 105

def configuration
  @configuration ||= TShield::Configuration.singleton
end

#current_session_name(request) ⇒ Object



94
95
96
97
# File 'lib/tshield/controllers/requests.rb', line 94

def current_session_name(request)
  session = TShield::Sessions.current(request.ip)
  session ? session[:name] : 'no-session'
end

#domain(path) ⇒ Object



109
110
111
# File 'lib/tshield/controllers/requests.rb', line 109

def domain(path)
  @domain ||= configuration.get_domain_for(path)
end

#set_content_type(request_content_type) ⇒ Object



90
91
92
# File 'lib/tshield/controllers/requests.rb', line 90

def set_content_type(request_content_type)
  content_type :json
end

#treat(params, request, response) ⇒ Object



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
85
86
87
88
# File 'lib/tshield/controllers/requests.rb', line 48

def treat(params, request, response)
  path = params.fetch('captures', [])[0]

  debugger if TShield::Options.instance.break?(path: path, moment: :before)

  method = request.request_method
  request_content_type = request.content_type

  headers = {
    'Content-Type' => request.content_type || 'application/json'
  }

  add_headers(headers, path)

  options = {
    method: method,
    headers: headers,
    raw_query: request.env['QUERY_STRING'],
    ip: request.ip
  }

  if ['POST', 'PUT', 'PATCH'].include? method
    result = request.body.read.encode('UTF-8', {
      :invalid => :replace,
      :undef   => :replace,
      :replace => ''
    })
    options[:body] = result
  end

  set_content_type content_type

  api_response = TShield::Request.new(path, options).response

  logger.info(
    "original=#{api_response.original} method=#{method} path=#{path} content-type=#{request_content_type} session=#{current_session_name(request)}")

  status api_response.status
  headers api_response.headers.reject { |k,v| configuration.get_excluded_headers(domain(path)).include?(k) }
  body api_response.body
end