Customized Reverse Proxy to send along host header from client.

This commit is contained in:
Kelketek 2013-07-20 19:36:32 -05:00
parent d861cc5e8b
commit 699f86273b
2 changed files with 37 additions and 2 deletions

View file

@ -11,10 +11,13 @@ application.
a great example/aid on how to do this.)
"""
import urlparse
from urllib import quote as urlquote
from twisted.web import resource, http
from twisted.python import threadpool
from twisted.internet import reactor
from twisted.application import service, internet
from twisted.web.proxy import ReverseProxyResource
from twisted.web.server import NOT_DONE_YET
from twisted.web.wsgi import WSGIResource
from django.core.handlers.wsgi import WSGIHandler
@ -44,6 +47,36 @@ class HTTPChannelWithXForwardedFor(http.HTTPChannel):
http.HTTPFactory.protocol = HTTPChannelWithXForwardedFor
class EvenniaReverseProxyResource(ReverseProxyResource):
def getChild(self, path, request):
"""
Create and return a proxy resource with the same proxy configuration
as this one, except that its path also contains the segment given by
C{path} at the end.
"""
return EvenniaReverseProxyResource(
self.host, self.port, self.path + '/' + urlquote(path, safe=""),
self.reactor)
def render(self, request):
"""
Render a request by forwarding it to the proxied server.
"""
# RFC 2616 tells us that we can omit the port if it's the default port,
# but we have to provide it otherwise
request.content.seek(0, 0)
qs = urlparse.urlparse(request.uri)[4]
if qs:
rest = self.path + '?' + qs
else:
rest = self.path
clientFactory = self.proxyClientFactoryClass(
request.method, rest, request.clientproto,
request.getAllHeaders(), request.content.read(), request)
self.reactor.connectTCP(self.host, self.port, clientFactory)
return NOT_DONE_YET
#
# Website server resource
#