Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cheroot/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,12 @@ def _serve_unservicable(self):
conn = self._unservicable_conns.get()
if conn is _STOPPING_FOR_INTERRUPT:
return
request = HTTPRequest(self, conn)
request = HTTPRequest(
self,
conn,
proxy_mode=self.proxy_mode,
strict_mode=self.strict_mode,
)
try:
request.simple_response('503 Service Unavailable')
except (OSError, errors.FatalSSLAlert):
Expand Down
61 changes: 61 additions & 0 deletions cheroot/test/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,67 @@ def test_stop_interrupts_serve():
assert not serve_thread.is_alive()


def test_unservicable_conn_uses_server_proxy_and_strict_mode(monkeypatch):
"""Check that 503 requests inherit proxy/strict mode from the server."""
httpserver = HTTPServer(
bind_addr=(ANY_INTERFACE_IPV4, EPHEMERAL_PORT),
gateway=Gateway,
)
close_calls = []
request_init_args = []

class CapturingHTTPRequest:
"""Capture request constructor args used for 503 responses."""

def __init__(
self,
server,
conn,
proxy_mode=False,
strict_mode=True,
):
request_init_args.append(
{
'server': server,
'conn': conn,
'proxy_mode': proxy_mode,
'strict_mode': strict_mode,
},
)

def simple_response(self, _status):
return None

def close_conn():
close_calls.append(None)

conn = types.SimpleNamespace(linger=False, close=close_conn)
stopping_sentinel = object()
httpserver.proxy_mode = True
httpserver.strict_mode = False
httpserver.ready = True
httpserver._unservicable_conns.put(conn)
httpserver._unservicable_conns.put(stopping_sentinel)
monkeypatch.setattr(
'cheroot.server._STOPPING_FOR_INTERRUPT',
stopping_sentinel,
)
monkeypatch.setattr('cheroot.server.HTTPRequest', CapturingHTTPRequest)

httpserver._serve_unservicable()

assert request_init_args == [
{
'server': httpserver,
'conn': conn,
'proxy_mode': True,
'strict_mode': False,
},
]
assert conn.linger is True
assert close_calls == [None]


@pytest.mark.parametrize(
'exc_cls',
(
Expand Down
Loading