@@ -69,6 +69,7 @@ def _construct_response_bytes( # pylint: disable=too-many-arguments
69
69
cache : int = 0 ,
70
70
headers : Dict [str , str ] = None ,
71
71
body : str = "" ,
72
+ chunked : bool = False ,
72
73
) -> bytes :
73
74
"""Constructs the response bytes from the given parameters."""
74
75
@@ -77,7 +78,10 @@ def _construct_response_bytes( # pylint: disable=too-many-arguments
77
78
headers = headers or {}
78
79
79
80
headers .setdefault ("Content-Type" , content_type )
80
- headers .setdefault ("Content-Length" , content_length or len (body ))
81
+ if chunked :
82
+ headers .setdefault ("Transfer-Encoding" , "chunked" )
83
+ else :
84
+ headers .setdefault ("Content-Length" , content_length or len (body ))
81
85
headers .setdefault ("Connection" , "close" )
82
86
83
87
response += f"Cache-Control: max-age={ cache } \r \n "
@@ -120,6 +124,33 @@ def send(self, conn: Union["SocketPool.Socket", "socket.socket"]) -> None:
120
124
body = self .body ,
121
125
)
122
126
127
+ def send_chunk_headers (
128
+ self , conn : Union ["SocketPool.Socket" , "socket.socket" ]
129
+ ) -> None :
130
+ """Send Headers for a chunked response over the given socket."""
131
+ self ._send_bytes (
132
+ conn ,
133
+ self ._construct_response_bytes (
134
+ status = self .status ,
135
+ content_type = self .content_type ,
136
+ chunked = True ,
137
+ cache = self .cache ,
138
+ body = "" ,
139
+ ),
140
+ )
141
+
142
+ def send_body_chunk (
143
+ self , conn : Union ["SocketPool.Socket" , "socket.socket" ], chunk : str
144
+ ) -> None :
145
+ """Send chunk of data to the given socket. Send an empty("") chunk to finish the session.
146
+
147
+ :param Union["SocketPool.Socket", "socket.socket"] conn: Current connection.
148
+ :param str chunk: String data to be sent.
149
+ """
150
+ size = "%X\r \n " .encode () % len (chunk )
151
+ self ._send_bytes (conn , size )
152
+ self ._send_bytes (conn , chunk .encode () + b"\r \n " )
153
+
123
154
def _send_response ( # pylint: disable=too-many-arguments
124
155
self ,
125
156
conn : Union ["SocketPool.Socket" , "socket.socket" ],
0 commit comments