@@ -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
@@ -78,8 +79,11 @@ def _construct_response_bytes( # pylint: disable=too-many-arguments
78
79
response_headers = {} if headers is None else headers .copy ()
79
80
80
81
response_headers .setdefault ("Content-Type" , content_type )
81
- response_headers .setdefault ("Content-Length" , content_length or len (body ))
82
82
response_headers .setdefault ("Connection" , "close" )
83
+ if chunked :
84
+ response_headers .setdefault ("Transfer-Encoding" , "chunked" )
85
+ else :
86
+ response_headers .setdefault ("Content-Length" , content_length or len (body ))
83
87
84
88
for header , value in response_headers .items ():
85
89
response += f"{ header } : { value } \r \n "
@@ -121,6 +125,33 @@ def send(self, conn: Union["SocketPool.Socket", "socket.socket"]) -> None:
121
125
body = self .body ,
122
126
)
123
127
128
+ def send_chunk_headers (
129
+ self , conn : Union ["SocketPool.Socket" , "socket.socket" ]
130
+ ) -> None :
131
+ """Send Headers for a chunked response over the given socket."""
132
+ self ._send_bytes (
133
+ conn ,
134
+ self ._construct_response_bytes (
135
+ status = self .status ,
136
+ content_type = self .content_type ,
137
+ chunked = True ,
138
+ cache = self .cache ,
139
+ body = "" ,
140
+ ),
141
+ )
142
+
143
+ def send_body_chunk (
144
+ self , conn : Union ["SocketPool.Socket" , "socket.socket" ], chunk : str
145
+ ) -> None :
146
+ """Send chunk of data to the given socket. Send an empty("") chunk to finish the session.
147
+
148
+ :param Union["SocketPool.Socket", "socket.socket"] conn: Current connection.
149
+ :param str chunk: String data to be sent.
150
+ """
151
+ size = "%X\r \n " .encode () % len (chunk )
152
+ self ._send_bytes (conn , size )
153
+ self ._send_bytes (conn , chunk .encode () + b"\r \n " )
154
+
124
155
def _send_response ( # pylint: disable=too-many-arguments
125
156
self ,
126
157
conn : Union ["SocketPool.Socket" , "socket.socket" ],
0 commit comments