Skip to content

Commit

Permalink
There is no need of reverse proxy host to be bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dani Möller committed Jul 1, 2018
1 parent d04d30e commit a1dbd23
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/web/examples/reverse-proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
from twisted.internet import reactor
from twisted.web import proxy, server

site = server.Site(proxy.ReverseProxyResource(b'www.twistedmatrix.com', 80, b''))
site = server.Site(proxy.ReverseProxyResource('www.asdf.com', 80, b''))
reactor.listenTCP(8080, site)
reactor.run()
6 changes: 3 additions & 3 deletions src/twisted/web/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class ReverseProxyResource(Resource):
def __init__(self, host, port, path, reactor=reactor):
"""
@param host: the host of the web server to proxy.
@type host: C{bytes}
@type host: C{str}
@param port: the port of the web server to proxy.
@type port: C{port}
Expand Down Expand Up @@ -288,8 +288,8 @@ def render(self, request):
if self.port == 80:
host = self.host
else:
host = self.host + u":" + str(self.port)
request.requestHeaders.setRawHeaders(b"host", [host])
host = u"%s:%d" % (self.host, self.port)
request.requestHeaders.setRawHeaders(b"host", [host.encode('ascii')])
request.content.seek(0, 0)
qs = urllib_parse.urlparse(request.uri)[4]
if qs:
Expand Down
21 changes: 14 additions & 7 deletions src/twisted/web/test/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,15 @@ def _testRender(self, uri, expectedURI):
# Clear the timeout if the tests failed
self.addCleanup(channel.connectionLost, None)

channel.dataReceived(b"GET " +
uri +
b" HTTP/1.1\r\nAccept: text/html\r\n\r\n")
channel.dataReceived(
b"GET %s HTTP/1.1\r\nAccept: text/html\r\n\r\n" % uri)

[(host, port, factory, _timeout, _bind_addr)] = reactor.tcpClients
# Check that one connection has been created, to the good host/port
self.assertEqual(len(reactor.tcpClients), 1)
self.assertEqual(reactor.tcpClients[0][0], u"127.0.0.1")
self.assertEqual(reactor.tcpClients[0][1], 1234)
self.assertEqual(host, u"127.0.0.1")
self.assertEqual(port, 1234)

# Check the factory passed to the connect, and its given path
factory = reactor.tcpClients[0][2]
self.assertIsInstance(factory, ProxyClientFactory)
self.assertEqual(factory.rest, expectedURI)
self.assertEqual(factory.headers[b"host"], b"127.0.0.1:1234")
Expand All @@ -62,6 +60,15 @@ def test_render(self):
return self._testRender(b"/index", b"/path")


def test_render_subpage(self):
"""
Test that L{ReverseProxyResource.render} will instantiate a child
resource that will initiate a connection to the given server
requesting the apropiate url subpath.
"""
return self._testRender(b"/index/page1", b"/path/page1")


def test_renderWithQuery(self):
"""
Test that L{ReverseProxyResource.render} passes query parameters to the
Expand Down

0 comments on commit a1dbd23

Please sign in to comment.