diff --git a/MicroWebSrv2/httpResponse.py b/MicroWebSrv2/httpResponse.py index 39c4741..c47bac5 100644 --- a/MicroWebSrv2/httpResponse.py +++ b/MicroWebSrv2/httpResponse.py @@ -122,6 +122,7 @@ def __init__(self, microWebSrv2, request) : self._stream = None self._sendingBuf = None self._hdrSent = False + self._onSent = None # ------------------------------------------------------------------------ @@ -175,6 +176,12 @@ def onLastChunkSent(xasCli, arg) : self._request._waitForRecvRequest() else : self._xasCli.Close() + if self._onSent : + try : + self._onSent(self._mws2, self) + except Exception as ex : + self._mws2.Log( 'Exception raised from "Response.OnSent" handler: %s' % ex, + self._mws2.ERROR ) # ------------------------------------------------------------------------ @@ -509,6 +516,18 @@ def ContentLength(self, value) : def HeadersSent(self) : return self._hdrSent + # ------------------------------------------------------------------------ + + @property + def OnSent(self) : + return self._onSent + + @OnSent.setter + def OnSent(self, value) : + if type(value) is not type(lambda x:x) : + raise ValueError('"OnSent" must be a function.') + self._onSent = value + # ============================================================================ # ============================================================================ # ============================================================================ diff --git a/README.md b/README.md index 7537291..54ad452 100644 --- a/README.md +++ b/README.md @@ -1149,16 +1149,26 @@ except KeyboardInterrupt : - ### HttpResponse properties - | Name | Type | Get | Set | Description | - |------------------|:-----------------------------:|:-----------------------:|:-----------------------:|---------------------------------------------------------------------------------------------| - | `Request` | [HttpRequest](#request-class) | :ballot_box_with_check: | - | *Http request related to this response.* | - | `UserAddress` | tuple | :ballot_box_with_check: | - | *User remote address of the http connection such as a tuple of `(str_ip_addr, int_port)`.* | - | `IsSSL` | bool | :ballot_box_with_check: | - | *Indicates that the http connection is secured by SSL/TLS security layer with certificate.* | - | `AllowCaching` | bool | :ballot_box_with_check: | :ballot_box_with_check: | *Indicates to the user the possible caching of this response.* | - | `ContentType` | str or None | :ballot_box_with_check: | :ballot_box_with_check: | *Type of the content of this response.* | - | `ContentCharset` | str or None | :ballot_box_with_check: | :ballot_box_with_check: | *Encoding charset used for the content of this response.* | - | `ContentLength` | int | :ballot_box_with_check: | :ballot_box_with_check: | *Length of the content of this response.* | - | `HeadersSent` | bool | :ballot_box_with_check: | - | *Indicates that response http headers was already sent.* | + | Name | Type | Get | Set | Description | + |------------------|:------------------------------------:|:-----------------------:|:-----------------------:|---------------------------------------------------------------------------------------------| + | `Request` | [HttpRequest](#request-class) | :ballot_box_with_check: | - | *Http request related to this response.* | + | `UserAddress` | tuple | :ballot_box_with_check: | - | *User remote address of the http connection such as a tuple of `(str_ip_addr, int_port)`.* | + | `IsSSL` | bool | :ballot_box_with_check: | - | *Indicates that the http connection is secured by SSL/TLS security layer with certificate.* | + | `AllowCaching` | bool | :ballot_box_with_check: | :ballot_box_with_check: | *Indicates to the user the possible caching of this response.* | + | `ContentType` | str or None | :ballot_box_with_check: | :ballot_box_with_check: | *Type of the content of this response.* | + | `ContentCharset` | str or None | :ballot_box_with_check: | :ballot_box_with_check: | *Encoding charset used for the content of this response.* | + | `ContentLength` | int | :ballot_box_with_check: | :ballot_box_with_check: | *Length of the content of this response.* | + | `HeadersSent` | bool | :ballot_box_with_check: | - | *Indicates that response http headers was already sent.* | + | `OnSent` | [callback](#response-onsent) or None | :ballot_box_with_check: | :ballot_box_with_check: | *Callback function when response is fully sent.* | + + > **Definition of the above callback functions:** + + + ```python + def OnSent(microWebSrv2, response) + # is of type MicroWebSrv2 + # is of type HttpResponse + ``` --- diff --git a/docs/index.md b/docs/index.md index 2c5c949..bd3ea80 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1151,16 +1151,26 @@ except KeyboardInterrupt : - ### HttpResponse properties - | Name | Type | Get | Set | Description | - |------------------|:-----------------------------:|:-----------------------:|:-----------------------:|---------------------------------------------------------------------------------------------| - | `Request` | [HttpRequest](#request-class) | Yes | - | *Http request related to this response.* | - | `UserAddress` | tuple | Yes | - | *User remote address of the http connection such as a tuple of `(str_ip_addr, int_port)`.* | - | `IsSSL` | bool | Yes | - | *Indicates that the http connection is secured by SSL/TLS security layer with certificate.* | - | `AllowCaching` | bool | Yes | Yes | *Indicates to the user the possible caching of this response.* | - | `ContentType` | str or None | Yes | Yes | *Type of the content of this response.* | - | `ContentCharset` | str or None | Yes | Yes | *Encoding charset used for the content of this response.* | - | `ContentLength` | int | Yes | Yes | *Length of the content of this response.* | - | `HeadersSent` | bool | Yes | - | *Indicates that response http headers was already sent.* | + | Name | Type | Get | Set | Description | + |------------------|:------------------------------------:|:-----------------------:|:-----------------------:|---------------------------------------------------------------------------------------------| + | `Request` | [HttpRequest](#request-class) | :ballot_box_with_check: | - | *Http request related to this response.* | + | `UserAddress` | tuple | :ballot_box_with_check: | - | *User remote address of the http connection such as a tuple of `(str_ip_addr, int_port)`.* | + | `IsSSL` | bool | :ballot_box_with_check: | - | *Indicates that the http connection is secured by SSL/TLS security layer with certificate.* | + | `AllowCaching` | bool | :ballot_box_with_check: | :ballot_box_with_check: | *Indicates to the user the possible caching of this response.* | + | `ContentType` | str or None | :ballot_box_with_check: | :ballot_box_with_check: | *Type of the content of this response.* | + | `ContentCharset` | str or None | :ballot_box_with_check: | :ballot_box_with_check: | *Encoding charset used for the content of this response.* | + | `ContentLength` | int | :ballot_box_with_check: | :ballot_box_with_check: | *Length of the content of this response.* | + | `HeadersSent` | bool | :ballot_box_with_check: | - | *Indicates that response http headers was already sent.* | + | `OnSent` | [callback](#response-onsent) or None | :ballot_box_with_check: | :ballot_box_with_check: | *Callback function when response is fully sent.* | + + > **Definition of the above callback functions:** + + + ```python + def OnSent(microWebSrv2, response) + # is of type MicroWebSrv2 + # is of type HttpResponse + ``` ---