Skip to content

Commit

Permalink
Add HttpResponse.OnSent event property
Browse files Browse the repository at this point in the history
  • Loading branch information
jczic committed Nov 25, 2019
1 parent aaecf5d commit 3f8a958
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 20 deletions.
19 changes: 19 additions & 0 deletions MicroWebSrv2/httpResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def __init__(self, microWebSrv2, request) :
self._stream = None
self._sendingBuf = None
self._hdrSent = False
self._onSent = None

# ------------------------------------------------------------------------

Expand Down Expand Up @@ -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 )

# ------------------------------------------------------------------------

Expand Down Expand Up @@ -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

# ============================================================================
# ============================================================================
# ============================================================================
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1149,16 +1149,26 @@ except KeyboardInterrupt :
<a name="response-prop"></a>
- ### 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:**
<a name="response-onsent"></a>
```python
def OnSent(microWebSrv2, response)
# <microWebSrv2> is of type MicroWebSrv2
# <response> is of type HttpResponse
```
---
Expand Down
30 changes: 20 additions & 10 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1151,16 +1151,26 @@ except KeyboardInterrupt :
<a name="response-prop"></a>
- ### 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:**
<a name="response-onsent"></a>
```python
def OnSent(microWebSrv2, response)
# <microWebSrv2> is of type MicroWebSrv2
# <response> is of type HttpResponse
```
---
Expand Down

0 comments on commit 3f8a958

Please sign in to comment.