2

I'm new to the twisted library and I'm trying to write a simple reverse proxy with a particular feature. For most URLs the server should act as a reverse proxy, but for URLs that match a regex it should serve them itself. I have read the twisted documentation on dynamic dispatch and have come up with the following:

local_urls = [
    r'...',
]

class LocalResource(Resource):
    def render(self, request):
        return "Hello World"

class LocalOrRemoteResource(Resource):
    def __init__(self, host, port, path):
        Resource.__init__(self)
        self.host = host
        self.port = port
        self.path = path

    def getChild(self, path, request):
        if any([re.match(url, path) for url in local_urls]):
            return LocalResource()
        else:
            return proxy.ReverseProxyResource(self.host, self.port, path)

root = LocalOrRemoteResource('remote.server', 80, '')
site = server.Site(root)
reactor.listenTCP(8080, site)
reactor.run()

When I run that the server hits a ValueError while trying to parse the Cache-Control header value 'no-cache' as a HTTP status code. So, something's gone very wrong.

When I replace the line:

root = LocalOrRemoteResource('remote.server', 80, '')

with this

root = proxy.ReverseProxyResource('remote.server', 80, '')

it works fine.

Can anyone see what I'm doing wrong?

d4nt
  • 15,475
  • 9
  • 42
  • 51

1 Answers1

0

After looking at this, I tried prepending a '/' to the path that I pass to ReverseProxyResource. In other words, I changed this:

return proxy.ReverseProxyResource(self.host, self.port, path)

to this

return proxy.ReverseProxyResource(self.host, self.port, '/' + path)

That fixed it!

Community
  • 1
  • 1
d4nt
  • 15,475
  • 9
  • 42
  • 51