class Ritm::HTTPForwarder

Public Class Methods

new(request_interceptor, response_interceptor, context_config) click to toggle source
# File lib/ritm/interception/http_forwarder.rb, line 34
def initialize(request_interceptor, response_interceptor, context_config)
  @request_interceptor = request_interceptor
  @response_interceptor = response_interceptor
  @config = context_config
  # TODO: make SSL verification a configuration setting
  @client = Faraday.new(
    ssl: { verify: false },
    request: { params_encoder: ParamEncoder.new }
  ) do |conn|
    conn.adapter :net_http
  end
end

Public Instance Methods

forward(request, response) click to toggle source
# File lib/ritm/interception/http_forwarder.rb, line 47
def forward(request, response)
  intercept_request(@request_interceptor, request, @config.intercept.request)
  faraday_response = faraday_forward request
  to_webrick_response faraday_response, response
  intercept_response(@response_interceptor, request, response, @config.intercept.response)
end

Private Instance Methods

faraday_forward(request) click to toggle source
# File lib/ritm/interception/http_forwarder.rb, line 56
def faraday_forward(request)
  req_method = request.request_method.downcase
  @client.send req_method do |req|
    req.options[:proxy] = @config.misc.upstream_proxy
    req.url request.request_uri
    req.body = request.body
    request.header.each { |name, value| req.headers[name] = value }
  end
end
to_webrick_response(faraday_response, webrick_response) click to toggle source
# File lib/ritm/interception/http_forwarder.rb, line 66
def to_webrick_response(faraday_response, webrick_response)
  webrick_response.status = faraday_response.status
  webrick_response.body = faraday_response.body
  faraday_response.headers.each do |name, value|
    case name
    when 'set-cookie'
      WEBrick::Cookie.parse_set_cookies(value).each { |cookie| webrick_response.cookies << cookie }
    else
      webrick_response[name] = value
    end
  end
  webrick_response
end