Class: Falcon::Proxy

Inherits:
Async::HTTP::Middleware
  • Object
show all
Defined in:
lib/falcon/proxy.rb

Constant Summary collapse

FORWARDED =
'forwarded'.freeze
X_FORWARDED_FOR =
'x-forwarded-for'.freeze
X_FORWARDED_PROTO =
'x-forwarded-proto'.freeze
VIA =
'via'.freeze
CONNECTION =
'connection'.freeze
HOP_HEADERS =
[
	'connection',
	'keep-alive',
	'public',
	'proxy-authenticate',
	'transfer-encoding',
	'upgrade',
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, hosts) ⇒ Proxy

Returns a new instance of Proxy.



51
52
53
54
55
56
57
58
59
60
# File 'lib/falcon/proxy.rb', line 51

def initialize(app, hosts)
	super(app)
	
	@server_context = nil
	
	@hosts = hosts
	@clients = {}
	
	@count = 0
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



62
63
64
# File 'lib/falcon/proxy.rb', line 62

def count
  @count
end

Instance Method Details

#call(request) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/falcon/proxy.rb', line 122

def call(request)
	if host = lookup(request)
		@count += 1
		
		request = self.prepare_request(request, host)
		
		client = connect(host.endpoint)
		
		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

#closeObject



64
65
66
67
68
# File 'lib/falcon/proxy.rb', line 64

def close
	@clients.each_value(&:close)
	
	super
end

#connect(endpoint) ⇒ Object



70
71
72
# File 'lib/falcon/proxy.rb', line 70

def connect(endpoint)
	@clients[endpoint] ||= Async::HTTP::Client.new(endpoint)
end

#lookup(request) ⇒ Object



74
75
76
77
78
79
# File 'lib/falcon/proxy.rb', line 74

def lookup(request)
	# Trailing dot and port is ignored/normalized.
	if authority = request.authority.sub(/(\.)?(:\d+)?$/, '')
		return @hosts[authority]
	end
end

#prepare_headers(headers) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/falcon/proxy.rb', line 81

def prepare_headers(headers)
	if connection = headers[CONNECTION]
		headers.extract(connection)
	end
	
	headers.extract(HOP_HEADERS)
end

#prepare_request(request, host) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/falcon/proxy.rb', line 89

def prepare_request(request, host)
	forwarded = []
	
	# Async.logger.info(self) do |buffer|
	# 	buffer.puts "Request authority: #{request.authority}"
	# 	buffer.puts "Host authority: #{host.authority}"
	# 	buffer.puts "Endpoint authority: #{host.endpoint.authority}"
	# end
	
	# The authority of the request must match the authority of the endpoint we are proxying to, otherwise SNI and other things won't work correctly.
	request.authority = host.endpoint.authority
	
	if address = request.remote_address
		request.headers.add(X_FORWARDED_FOR, address.ip_address)
		forwarded << "for=#{address.ip_address}"
	end
	
	if scheme = request.scheme
		request.headers.add(X_FORWARDED_PROTO, scheme)
		forwarded << "proto=#{scheme}"
	end
	
	unless forwarded.empty?
		request.headers.add(FORWARDED, forwarded.join(';'))
	end
	
	request.headers.add(VIA, "#{request.version} #{self.class}")
	
	self.prepare_headers(request.headers)
	
	return request
end