Class: AeReverseProxy::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ae_reverse_proxy/client.rb

Constant Summary collapse

CALLBACK_METHODS =
%i[
  on_response
  on_set_cookies
  on_connect
  on_success
  on_redirect
  on_missing
  on_error
  on_complete
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:



28
29
30
31
32
33
# File 'lib/ae_reverse_proxy/client.rb', line 28

def initialize(uri)
  self.uri = uri
  self.callbacks = CALLBACK_METHODS.to_h { |method| [method, proc {}] }

  yield(self) if block_given?
end

Instance Attribute Details

#callbacksObject

Returns the value of attribute callbacks.



8
9
10
# File 'lib/ae_reverse_proxy/client.rb', line 8

def callbacks
  @callbacks
end

#uriObject

Returns the value of attribute uri.



8
9
10
# File 'lib/ae_reverse_proxy/client.rb', line 8

def uri
  @uri
end

Instance Method Details

#forward_request(env, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ae_reverse_proxy/client.rb', line 35

def forward_request(env, options = {})
  # Initialize requests
  source_request = Rack::Request.new(env)
  target_request = Net::HTTP.const_get(source_request.request_method.capitalize).new(source_request.fullpath)

  # Setup headers for forwarding.
  target_request_headers = extract_http_request_headers(source_request.env).merge({
    'ORIGIN' => uri.origin,
    'HOST' => uri.authority,
  })
  target_request.initialize_http_header(target_request_headers)

  # Setup basic auth.
  target_request.basic_auth(options[:username], options[:password]) if options[:username] && options[:password]

  # Setup body.
  if target_request.request_body_permitted? && source_request.body
    source_request.body.rewind
    target_request.body_stream = source_request.body
  end

  # Setup content encoding and type.
  target_request.content_length = source_request.content_length || 0
  target_request.content_type   = source_request.content_type if source_request.content_type

  # Don't encode response/support compression which was
  # causing content length not match the actual content
  # length of the response which ended up causing issues
  # within Varnish (503)
  target_request['Accept-Encoding'] = nil

  # Setup HTTP SSL options.
  http_options = {}
  http_options[:use_ssl] = (uri.scheme == 'https')

  # Make the request.
  target_response = nil
  Net::HTTP.start(uri.hostname, uri.port, http_options) do |http|
    callbacks[:on_connect].call(http)
    target_response = http.request(target_request)
  end

  # Initiate callbacks.
  status_code = target_response.code.to_i
  payload = [status_code, target_response]

  callbacks[:on_response].call(payload)

  if target_response.to_hash['set-cookie']
    set_cookies_hash = {}
    set_cookie_headers = target_response.to_hash['set-cookie']

    set_cookie_headers.each do |set_cookie_header|
      set_cookie_hash = parse_cookie(set_cookie_header)
      name = set_cookie_hash[:name]
      set_cookies_hash[name] = set_cookie_hash
    end

    callbacks[:on_set_cookies].call(payload | [set_cookies_hash])
  end

  case status_code
  when 200..299
    callbacks[:on_success].call(payload)
  when 300..399
    callbacks[:on_redirect].call(payload | [target_response['Location']]) if target_response['Location']
  when 400..499
    callbacks[:on_missing].call(payload)
  when 500..599
    callbacks[:on_error].call(payload)
  end

  callbacks[:on_complete].call(payload)

  payload
end