Skip to content

Commit

Permalink
twisted#12093 Include client port in WSGI request environment (twiste…
Browse files Browse the repository at this point in the history
  • Loading branch information
adiroiban committed Mar 20, 2024
2 parents 50a7feb + edb5eb8 commit c709d3f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/twisted/newsfragments/12096.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
twisted.web.wsgi request environment now contains the peer port number as `REMOTE_PORT`.
20 changes: 19 additions & 1 deletion src/twisted/web/test/test_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,11 @@ def test_remoteAddr(self):
The C{'REMOTE_ADDR'} key of the C{environ} C{dict} passed to the
application contains the address of the client making the request.
"""
d = self.render("GET", "1.1", [], [""])

def channelFactory():
return DummyChannel(peer=IPv4Address("TCP", "192.168.1.1", 12344))

d = self.render("GET", "1.1", [], [""], channelFactory=channelFactory)
d.addCallback(self.environKeyEqual("REMOTE_ADDR", "192.168.1.1"))

return d
Expand All @@ -735,6 +739,20 @@ def channelFactory():

return d

def test_remotePort(self):
"""
The C{'REMOTE_PORT'} key of the C{environ} C{dict} passed to the
application contains the port of the client making the request.
"""

def channelFactory():
return DummyChannel(peer=IPv4Address("TCP", "192.168.1.1", 12344))

d = self.render("GET", "1.1", [], [""], channelFactory=channelFactory)
d.addCallback(self.environKeyEqual("REMOTE_PORT", "12344"))

return d

def test_headers(self):
"""
HTTP request headers are copied into the C{environ} C{dict} passed to
Expand Down
7 changes: 6 additions & 1 deletion src/twisted/web/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,11 @@ def __init__(self, reactor, threadpool, application, request):

# All keys and values need to be native strings, i.e. of type str in
# *both* Python 2 and Python 3, so says PEP-3333.
remotePeer = request.getClientAddress()
self.environ = {
"REQUEST_METHOD": _wsgiString(request.method),
"REMOTE_ADDR": _wsgiString(request.getClientAddress().host),
"REMOTE_ADDR": _wsgiString(remotePeer.host),
"REMOTE_PORT": _wsgiString(str(remotePeer.port)),
"SCRIPT_NAME": _wsgiString(scriptName),
"PATH_INFO": _wsgiString(pathInfo),
"QUERY_STRING": _wsgiString(queryString),
Expand Down Expand Up @@ -535,6 +537,9 @@ class WSGIResource:
An L{IResource} implementation which delegates responsibility for all
resources hierarchically inferior to it to a WSGI application.
The C{environ} argument passed to the application, includes the
C{REMOTE_PORT} key to complement the C{REMOTE_ADDR} key.
@ivar _reactor: An L{IReactorThreads} provider which will be passed on to
L{_WSGIResponse} to schedule calls in the I/O thread.
Expand Down

0 comments on commit c709d3f

Please sign in to comment.