Skip to content

Commit 3f8a958

Browse files
author
jczic
committed
Add HttpResponse.OnSent event property
1 parent aaecf5d commit 3f8a958

File tree

3 files changed

+59
-20
lines changed

3 files changed

+59
-20
lines changed

MicroWebSrv2/httpResponse.py

+19
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def __init__(self, microWebSrv2, request) :
122122
self._stream = None
123123
self._sendingBuf = None
124124
self._hdrSent = False
125+
self._onSent = None
125126

126127
# ------------------------------------------------------------------------
127128

@@ -175,6 +176,12 @@ def onLastChunkSent(xasCli, arg) :
175176
self._request._waitForRecvRequest()
176177
else :
177178
self._xasCli.Close()
179+
if self._onSent :
180+
try :
181+
self._onSent(self._mws2, self)
182+
except Exception as ex :
183+
self._mws2.Log( 'Exception raised from "Response.OnSent" handler: %s' % ex,
184+
self._mws2.ERROR )
178185

179186
# ------------------------------------------------------------------------
180187

@@ -509,6 +516,18 @@ def ContentLength(self, value) :
509516
def HeadersSent(self) :
510517
return self._hdrSent
511518

519+
# ------------------------------------------------------------------------
520+
521+
@property
522+
def OnSent(self) :
523+
return self._onSent
524+
525+
@OnSent.setter
526+
def OnSent(self, value) :
527+
if type(value) is not type(lambda x:x) :
528+
raise ValueError('"OnSent" must be a function.')
529+
self._onSent = value
530+
512531
# ============================================================================
513532
# ============================================================================
514533
# ============================================================================

README.md

+20-10
Original file line numberDiff line numberDiff line change
@@ -1149,16 +1149,26 @@ except KeyboardInterrupt :
11491149
<a name="response-prop"></a>
11501150
- ### HttpResponse properties
11511151
1152-
| Name | Type | Get | Set | Description |
1153-
|------------------|:-----------------------------:|:-----------------------:|:-----------------------:|---------------------------------------------------------------------------------------------|
1154-
| `Request` | [HttpRequest](#request-class) | :ballot_box_with_check: | - | *Http request related to this response.* |
1155-
| `UserAddress` | tuple | :ballot_box_with_check: | - | *User remote address of the http connection such as a tuple of `(str_ip_addr, int_port)`.* |
1156-
| `IsSSL` | bool | :ballot_box_with_check: | - | *Indicates that the http connection is secured by SSL/TLS security layer with certificate.* |
1157-
| `AllowCaching` | bool | :ballot_box_with_check: | :ballot_box_with_check: | *Indicates to the user the possible caching of this response.* |
1158-
| `ContentType` | str or None | :ballot_box_with_check: | :ballot_box_with_check: | *Type of the content of this response.* |
1159-
| `ContentCharset` | str or None | :ballot_box_with_check: | :ballot_box_with_check: | *Encoding charset used for the content of this response.* |
1160-
| `ContentLength` | int | :ballot_box_with_check: | :ballot_box_with_check: | *Length of the content of this response.* |
1161-
| `HeadersSent` | bool | :ballot_box_with_check: | - | *Indicates that response http headers was already sent.* |
1152+
| Name | Type | Get | Set | Description |
1153+
|------------------|:------------------------------------:|:-----------------------:|:-----------------------:|---------------------------------------------------------------------------------------------|
1154+
| `Request` | [HttpRequest](#request-class) | :ballot_box_with_check: | - | *Http request related to this response.* |
1155+
| `UserAddress` | tuple | :ballot_box_with_check: | - | *User remote address of the http connection such as a tuple of `(str_ip_addr, int_port)`.* |
1156+
| `IsSSL` | bool | :ballot_box_with_check: | - | *Indicates that the http connection is secured by SSL/TLS security layer with certificate.* |
1157+
| `AllowCaching` | bool | :ballot_box_with_check: | :ballot_box_with_check: | *Indicates to the user the possible caching of this response.* |
1158+
| `ContentType` | str or None | :ballot_box_with_check: | :ballot_box_with_check: | *Type of the content of this response.* |
1159+
| `ContentCharset` | str or None | :ballot_box_with_check: | :ballot_box_with_check: | *Encoding charset used for the content of this response.* |
1160+
| `ContentLength` | int | :ballot_box_with_check: | :ballot_box_with_check: | *Length of the content of this response.* |
1161+
| `HeadersSent` | bool | :ballot_box_with_check: | - | *Indicates that response http headers was already sent.* |
1162+
| `OnSent` | [callback](#response-onsent) or None | :ballot_box_with_check: | :ballot_box_with_check: | *Callback function when response is fully sent.* |
1163+
1164+
> **Definition of the above callback functions:**
1165+
1166+
<a name="response-onsent"></a>
1167+
```python
1168+
def OnSent(microWebSrv2, response)
1169+
# <microWebSrv2> is of type MicroWebSrv2
1170+
# <response> is of type HttpResponse
1171+
```
11621172
11631173
---
11641174

docs/index.md

+20-10
Original file line numberDiff line numberDiff line change
@@ -1151,16 +1151,26 @@ except KeyboardInterrupt :
11511151
<a name="response-prop"></a>
11521152
- ### HttpResponse properties
11531153
1154-
| Name | Type | Get | Set | Description |
1155-
|------------------|:-----------------------------:|:-----------------------:|:-----------------------:|---------------------------------------------------------------------------------------------|
1156-
| `Request` | [HttpRequest](#request-class) | Yes | - | *Http request related to this response.* |
1157-
| `UserAddress` | tuple | Yes | - | *User remote address of the http connection such as a tuple of `(str_ip_addr, int_port)`.* |
1158-
| `IsSSL` | bool | Yes | - | *Indicates that the http connection is secured by SSL/TLS security layer with certificate.* |
1159-
| `AllowCaching` | bool | Yes | Yes | *Indicates to the user the possible caching of this response.* |
1160-
| `ContentType` | str or None | Yes | Yes | *Type of the content of this response.* |
1161-
| `ContentCharset` | str or None | Yes | Yes | *Encoding charset used for the content of this response.* |
1162-
| `ContentLength` | int | Yes | Yes | *Length of the content of this response.* |
1163-
| `HeadersSent` | bool | Yes | - | *Indicates that response http headers was already sent.* |
1154+
| Name | Type | Get | Set | Description |
1155+
|------------------|:------------------------------------:|:-----------------------:|:-----------------------:|---------------------------------------------------------------------------------------------|
1156+
| `Request` | [HttpRequest](#request-class) | :ballot_box_with_check: | - | *Http request related to this response.* |
1157+
| `UserAddress` | tuple | :ballot_box_with_check: | - | *User remote address of the http connection such as a tuple of `(str_ip_addr, int_port)`.* |
1158+
| `IsSSL` | bool | :ballot_box_with_check: | - | *Indicates that the http connection is secured by SSL/TLS security layer with certificate.* |
1159+
| `AllowCaching` | bool | :ballot_box_with_check: | :ballot_box_with_check: | *Indicates to the user the possible caching of this response.* |
1160+
| `ContentType` | str or None | :ballot_box_with_check: | :ballot_box_with_check: | *Type of the content of this response.* |
1161+
| `ContentCharset` | str or None | :ballot_box_with_check: | :ballot_box_with_check: | *Encoding charset used for the content of this response.* |
1162+
| `ContentLength` | int | :ballot_box_with_check: | :ballot_box_with_check: | *Length of the content of this response.* |
1163+
| `HeadersSent` | bool | :ballot_box_with_check: | - | *Indicates that response http headers was already sent.* |
1164+
| `OnSent` | [callback](#response-onsent) or None | :ballot_box_with_check: | :ballot_box_with_check: | *Callback function when response is fully sent.* |
1165+
1166+
> **Definition of the above callback functions:**
1167+
1168+
<a name="response-onsent"></a>
1169+
```python
1170+
def OnSent(microWebSrv2, response)
1171+
# <microWebSrv2> is of type MicroWebSrv2
1172+
# <response> is of type HttpResponse
1173+
```
11641174
11651175
---
11661176

0 commit comments

Comments
 (0)