Skip to content

bpo-37363: Add audit events to the http.client module #21321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 23, 2021
Merged
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
4 changes: 4 additions & 0 deletions Doc/library/http.client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ HTTPConnection Objects
this is called automatically when making a request if the client does not
already have a connection.

.. audit-event:: http.client.connect self,host,port http.client.HTTPConnection.connect


.. method:: HTTPConnection.close()

Expand Down Expand Up @@ -437,6 +439,8 @@ also send your request step by step, by using the four functions below.
:meth:`endheaders` method has been called and before :meth:`getresponse` is
called.

.. audit-event:: http.client.send self,data http.client.HTTPConnection.send


.. _httpresponse-objects:

Expand Down
4 changes: 4 additions & 0 deletions Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import io
import re
import socket
import sys
import collections.abc
from urllib.parse import urlsplit

Expand Down Expand Up @@ -931,6 +932,7 @@ def _tunnel(self):

def connect(self):
"""Connect to the host and port specified in __init__."""
sys.audit("http.client.connect", self, self.host, self.port)
self.sock = self._create_connection(
(self.host,self.port), self.timeout, self.source_address)
self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
Expand Down Expand Up @@ -978,8 +980,10 @@ def send(self, data):
break
if encode:
datablock = datablock.encode("iso-8859-1")
sys.audit("http.client.send", self, datablock)
self.sock.sendall(datablock)
return
sys.audit("http.client.send", self, data)
try:
self.sock.sendall(data)
except TypeError:
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/audit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,24 @@ def hook(event, args):
gc.get_referents(y)


def test_http_client():
import http.client

def hook(event, args):
if event.startswith("http.client."):
print(event, *args[1:])

sys.addaudithook(hook)

conn = http.client.HTTPConnection('www.python.org')
try:
conn.request('GET', '/')
except OSError:
print('http.client.send', '[cannot send]')
finally:
conn.close()


if __name__ == "__main__":
from test.support import suppress_msvcrt_asserts

Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ def test_gc(self):
["gc.get_objects", "gc.get_referrers", "gc.get_referents"]
)

def test_http(self):
import_helper.import_module("http.client")
returncode, events, stderr = self.run_python("test_http_client")
if returncode:
self.fail(stderr)

if support.verbose:
print(*events, sep='\n')
self.assertEqual(events[0][0], "http.client.connect")
self.assertEqual(events[0][2], "www.python.org 80")
self.assertEqual(events[1][0], "http.client.send")
if events[1][2] != '[cannot send]':
self.assertIn('HTTP', events[1][2])


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add audit events to the :mod:`http.client` module.