Skip to content

Commit 927b841

Browse files
bpo-37363: Add audit events to the http.client module (pythonGH-21321)
Add audit events to the `http.client` module Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 32980fb commit 927b841

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

Doc/library/http.client.rst

+4
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ HTTPConnection Objects
368368
this is called automatically when making a request if the client does not
369369
already have a connection.
370370

371+
.. audit-event:: http.client.connect self,host,port http.client.HTTPConnection.connect
372+
371373

372374
.. method:: HTTPConnection.close()
373375

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

442+
.. audit-event:: http.client.send self,data http.client.HTTPConnection.send
443+
440444

441445
.. _httpresponse-objects:
442446

Lib/http/client.py

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import io
7575
import re
7676
import socket
77+
import sys
7778
import collections.abc
7879
from urllib.parse import urlsplit
7980

@@ -931,6 +932,7 @@ def _tunnel(self):
931932

932933
def connect(self):
933934
"""Connect to the host and port specified in __init__."""
935+
sys.audit("http.client.connect", self, self.host, self.port)
934936
self.sock = self._create_connection(
935937
(self.host,self.port), self.timeout, self.source_address)
936938
self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
@@ -978,8 +980,10 @@ def send(self, data):
978980
break
979981
if encode:
980982
datablock = datablock.encode("iso-8859-1")
983+
sys.audit("http.client.send", self, datablock)
981984
self.sock.sendall(datablock)
982985
return
986+
sys.audit("http.client.send", self, data)
983987
try:
984988
self.sock.sendall(data)
985989
except TypeError:

Lib/test/audit-tests.py

+18
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,24 @@ def hook(event, args):
341341
gc.get_referents(y)
342342

343343

344+
def test_http_client():
345+
import http.client
346+
347+
def hook(event, args):
348+
if event.startswith("http.client."):
349+
print(event, *args[1:])
350+
351+
sys.addaudithook(hook)
352+
353+
conn = http.client.HTTPConnection('www.python.org')
354+
try:
355+
conn.request('GET', '/')
356+
except OSError:
357+
print('http.client.send', '[cannot send]')
358+
finally:
359+
conn.close()
360+
361+
344362
if __name__ == "__main__":
345363
from test.support import suppress_msvcrt_asserts
346364

Lib/test/test_audit.py

+14
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ def test_gc(self):
130130
["gc.get_objects", "gc.get_referrers", "gc.get_referents"]
131131
)
132132

133+
def test_http(self):
134+
import_helper.import_module("http.client")
135+
returncode, events, stderr = self.run_python("test_http_client")
136+
if returncode:
137+
self.fail(stderr)
138+
139+
if support.verbose:
140+
print(*events, sep='\n')
141+
self.assertEqual(events[0][0], "http.client.connect")
142+
self.assertEqual(events[0][2], "www.python.org 80")
143+
self.assertEqual(events[1][0], "http.client.send")
144+
if events[1][2] != '[cannot send]':
145+
self.assertIn('HTTP', events[1][2])
146+
133147

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

0 commit comments

Comments
 (0)