Class: Falcon::Proxy
- Inherits:
-
Async::HTTP::Middleware
- Object
- Async::HTTP::Middleware
- Falcon::Proxy
- Defined in:
- lib/falcon/proxy.rb
Constant Summary collapse
- X_FORWARDED_FOR =
'x-forwarded-for'.freeze
- VIA =
'via'.freeze
- CONNECTION =
::HTTP::Protocol::CONNECTION
- HOP_HEADERS =
[ 'connection', 'keep-alive', 'public', 'proxy-authenticate', 'transfer-encoding', 'upgrade', ]
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
Instance Method Summary collapse
- #call(request) ⇒ Object
- #close ⇒ Object
- #connect(endpoint) ⇒ Object
-
#initialize(app, hosts) ⇒ Proxy
constructor
A new instance of Proxy.
- #lookup(request) ⇒ Object
- #prepare_headers(headers) ⇒ Object
Constructor Details
#initialize(app, hosts) ⇒ Proxy
Returns a new instance of Proxy.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/falcon/proxy.rb', line 48 def initialize(app, hosts) super(app) @server_context = nil @hosts = hosts @clients = {} @count = 0 end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
59 60 61 |
# File 'lib/falcon/proxy.rb', line 59 def count @count end |
Instance Method Details
#call(request) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/falcon/proxy.rb', line 86 def call(request) if endpoint = lookup(request) @count += 1 if address = request.remote_address request.headers.add(X_FORWARDED_FOR, address.ip_address) end request.headers.add(VIA, "#{request.version} #{self.class}") client = connect(endpoint) prepare_headers(request.headers) client.call(request) else super end rescue Async.logger.error(self) {$!} return Async::HTTP::Response[502, {'content-type' => 'text/plain'}, ["#{$!.inspect}: #{$!.backtrace.join("\n")}"]] end |
#close ⇒ Object
61 62 63 64 65 |
# File 'lib/falcon/proxy.rb', line 61 def close @clients.each_value(&:close) super end |
#connect(endpoint) ⇒ Object
67 68 69 |
# File 'lib/falcon/proxy.rb', line 67 def connect(endpoint) @clients[endpoint] ||= Async::HTTP::Client.new(endpoint) end |
#lookup(request) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/falcon/proxy.rb', line 71 def lookup(request) # Trailing dot and port is ignored/normalized. if = request..sub(/(\.)?(:\d+)?$/, '') return @hosts[] end end |
#prepare_headers(headers) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/falcon/proxy.rb', line 78 def prepare_headers(headers) if connection = headers[CONNECTION] headers.slice!(connection) end headers.slice!(HOP_HEADERS) end |