@@ -90,6 +90,9 @@ def route_func(request):
9090 Common MIME types are defined in `adafruit_httpserver.mime_types`.
9191 """
9292
93+ size : int = 0
94+ """Size of the response in bytes."""
95+
9396 def __init__ ( # pylint: disable=too-many-arguments
9497 self ,
9598 request : Request ,
@@ -192,6 +195,9 @@ def send(
192195 self ._send_bytes (self .request .connection , encoded_response_message_body )
193196 self ._response_already_sent = True
194197
198+ if self .request .server .debug :
199+ _debug_response_sent (self )
200+
195201 @staticmethod
196202 def _check_file_path_is_valid (file_path : str ) -> bool :
197203 """
@@ -270,6 +276,9 @@ def send_file( # pylint: disable=too-many-arguments
270276 self ._send_bytes (self .request .connection , bytes_read )
271277 self ._response_already_sent = True
272278
279+ if self .request .server .debug :
280+ _debug_response_sent (self )
281+
273282 def send_chunk (self , chunk : str = "" ) -> None :
274283 """
275284 Sends chunk of response.
@@ -279,6 +288,7 @@ def send_chunk(self, chunk: str = "") -> None:
279288
280289 :param str chunk: String data to be sent.
281290 """
291+ self ._check_if_not_already_sent ()
282292 self ._check_chunked (True )
283293
284294 if getattr (chunk , "encode" , None ):
@@ -299,14 +309,19 @@ def __exit__(self, exception_type, exception_value, exception_traceback):
299309
300310 if self .chunked :
301311 self .send_chunk ("" )
312+ self ._response_already_sent = True
313+
314+ if self .chunked and self .request .server .debug :
315+ _debug_response_sent (self )
316+
302317 return True
303318
304- @staticmethod
305319 def _send_bytes (
320+ self ,
306321 conn : Union ["SocketPool.Socket" , "socket.socket" ],
307322 buffer : Union [bytes , bytearray , memoryview ],
308323 ):
309- bytes_sent = 0
324+ bytes_sent : int = 0
310325 bytes_to_send = len (buffer )
311326 view = memoryview (buffer )
312327 while bytes_sent < bytes_to_send :
@@ -318,3 +333,16 @@ def _send_bytes(
318333 if exc .errno == ECONNRESET :
319334 return
320335 raise
336+ self .size += bytes_sent
337+
338+
339+ def _debug_response_sent (response : "Response" ):
340+ """Prints a message when after a response is sent."""
341+ client_ip = response .request .client_address [0 ]
342+ method = response .request .method
343+ path = response .request .path
344+ req_size = len (response .request .raw_request )
345+ status = response .status
346+ res_size = response .size
347+
348+ print (f'{ client_ip } -- "{ method } { path } " { req_size } -- "{ status } " { res_size } ' )
0 commit comments