Class: Falcon::Middleware::Proxy

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

Overview

A HTTP middleware for proxying requests to a given set of hosts. Typically used for implementing virtual servers.

Constant Summary collapse

FORWARDED =
"forwarded"
X_FORWARDED_FOR =
"x-forwarded-for"
X_FORWARDED_PROTO =
"x-forwarded-proto"
VIA =
"via"
CONNECTION =
"connection"
HOP_HEADERS =

HTTP hop headers which should not be passed through the proxy.

[
	"connection",
	"keep-alive",
	"public",
	"proxy-authenticate",
	"proxy-authorization",
	"transfer-encoding",
	"upgrade",
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, hosts) ⇒ Proxy

Initialize the proxy middleware.



55
56
57
58
59
60
61
62
63
64
# File 'lib/falcon/middleware/proxy.rb', line 55

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

Instance Attribute Details

#countObject (readonly)

The number of requests that have been proxied.



68
69
70
# File 'lib/falcon/middleware/proxy.rb', line 68

def count
  @count
end

Instance Method Details

#call(request) ⇒ Object

Proxy the request if the authority matches a specific host.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/falcon/middleware/proxy.rb', line 141

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 => error
	Console::Event::Failure.for(error).emit(self)
	return Protocol::HTTP::Response[502, {"content-type" => "text/plain"}, [error.class.name]]
end

#closeObject

Close all the connections to the upstream hosts.



71
72
73
74
75
# File 'lib/falcon/middleware/proxy.rb', line 71

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

#connect(endpoint) ⇒ Object

Establish a connection to the specified upstream endpoint.



79
80
81
# File 'lib/falcon/middleware/proxy.rb', line 79

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

#lookup(request) ⇒ Object

Lookup the appropriate host for the given request.



86
87
88
89
90
91
# File 'lib/falcon/middleware/proxy.rb', line 86

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

Prepare the headers to be sent to an upstream host. In particular, we delete all connection and hop headers.



95
96
97
98
99
100
101
# File 'lib/falcon/middleware/proxy.rb', line 95

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

#prepare_request(request, host) ⇒ Object

Prepare the request to be proxied to the specified host. In particular, we set appropriate VIA, FORWARDED, X_FORWARDED_FOR and X_FORWARDED_PROTO headers.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/falcon/middleware/proxy.rb', line 105

def prepare_request(request, host)
	forwarded = []
	
	Console.debug(self) do |buffer|
		buffer.puts "Request authority: #{request.authority}"
		buffer.puts "Host authority: #{host.authority}"
		buffer.puts "Request: #{request.method} #{request.path} #{request.version}"
		buffer.puts "Request headers: #{request.headers.inspect}"
	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.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