Class: Ritm::HTTPForwarder

Inherits:
Object
  • Object
show all
Includes:
InterceptUtils
Defined in:
lib/ritm/interception/http_forwarder.rb

Overview

Forwarder that acts as a WEBrick <-> Faraday adaptor: Works this way:

1. A WEBrick request objects is received
2. The WEBrick request object is sent to the request interceptor
3. The (maybe modified) WEBrick request object is transformed into a Faraday request and sent to destination server
4. The Faraday response obtained from the server is transformed into a WEBrick response
5. The WEBrick response object is sent to the response interceptor
6. The (maybe modified) WEBrick response object is sent back to the client

Besides the possible modifications to be done by interceptors there might be automated globally configured transformations like header stripping/adding.

Instance Method Summary collapse

Methods included from InterceptUtils

#intercept_request, #intercept_response

Constructor Details

#initialize(request_interceptor, response_interceptor, context_config) ⇒ HTTPForwarder

Returns a new instance of HTTPForwarder.



19
20
21
22
23
24
25
26
27
28
# File 'lib/ritm/interception/http_forwarder.rb', line 19

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 }) do |conn|
    conn.adapter :net_http
    conn.proxy = @config.misc.upstream_proxy unless @config.misc.upstream_proxy.nil?
  end
end

Instance Method Details

#forward(request, response) ⇒ Object



30
31
32
33
34
35
# File 'lib/ritm/interception/http_forwarder.rb', line 30

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