Class: Service::Proxy::Middleware

Inherits:
Rack::Proxy
  • Object
show all
Defined in:
lib/service/proxy/middleware.rb

Instance Method Summary collapse

Instance Method Details

#perform_request(env) ⇒ Object



8
9
10
11
12
13
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
# File 'lib/service/proxy/middleware.rb', line 8

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

  # use rack proxy for anything hitting our host app at /example_service
  if config && request.path =~ config.proxied_paths_matcher
    service = config.service_matching_path(request.path)

    # most backends required host set properly, but rack-proxy doesn't set this for you automatically
    # even when a backend host is passed in via the options
    env['SERVER_NAME'] = env['HTTP_HOST'] = [service.backend.host, service.backend.port].compact.join(':')
    env['SERVER_PORT'] = service.backend.port.to_s
    env['rack.backend'] = service.backend
    env['rack.url_scheme'] = service.backend.scheme
    env['HTTPS'] = service.backend.scheme.downcase == 'https' ? 'on' : 'off'

    # This is the only path that needs to be set currently on Rails 5 & greater
    env['PATH_INFO'] = service.backend_path_for(request.path)

    # don't send your sites cookies to target service, unless it is a trusted internal service that can parse all your cookies
    env['HTTP_COOKIE'] = '' unless service.send_cookies?

    # other Rack::Proxy opts
    env['rack.ssl_verify_none'] = true unless service.verify_ssl?
    env['http.read_timeout'] = service.read_timeout unless service.read_timeout.nil?

    env['content-length'] = nil

    super(env)
  else
    @app.call(env)
  end
end

#rewrite_response(triplet) ⇒ Object



41
42
43
44
45
# File 'lib/service/proxy/middleware.rb', line 41

def rewrite_response(triplet)
  status, headers, body = triplet
  headers['content-length'] = nil
  triplet
end