|
4 | 4 | I currently think that they should be synchronized since they should not do IO
|
5 | 5 | Where as endpoint module is designed for IO
|
6 | 6 | """
|
| 7 | +import mimetypes |
7 | 8 | from collections import OrderedDict
|
8 | 9 |
|
9 | 10 | from curio import spawn, Event, aopen
|
@@ -169,40 +170,37 @@ async def send(self, stream_id: int, headers, data: bytes=None):
|
169 | 170 | :param data: HTTP response body. Has to be bytes(binary data).
|
170 | 171 | It's users' responsibility to encode any kinds of data to binary.
|
171 | 172 | """
|
172 |
| - # print('HTTP.send') |
| 173 | + |
173 | 174 | # not sure if check for None or Falsy(empty containers)
|
174 | 175 | if data is None:
|
175 | 176 | self.connection.send_headers(stream_id, headers, end_stream=True)
|
176 | 177 | await self.sock.sendall(self.connection.data_to_send())
|
177 | 178 |
|
178 | 179 | else:
|
| 180 | + # todo: change this to logger in the future. |
179 | 181 | print('HTTP.send ', headers)
|
180 | 182 |
|
181 | 183 | self.connection.send_headers(stream_id, headers, end_stream=False)
|
182 |
| - # print('HTTP.send headers') |
| 184 | + |
183 | 185 | await self.sock.sendall(self.connection.data_to_send())
|
184 |
| - # print('HTTP.send before body') |
| 186 | + |
185 | 187 | # body
|
186 | 188 | i = 0
|
187 | 189 | while True:
|
188 |
| - # print('HTTP.send in loop') |
| 190 | + |
189 | 191 | while not self.connection.local_flow_control_window(stream_id):
|
190 | 192 | await self.wait_for_flow_control(stream_id)
|
191 |
| - # print('HTTP.send 1') |
| 193 | + |
192 | 194 | chunk_size = min(self.connection.local_flow_control_window(stream_id), READ_CHUNK_SIZE)
|
193 |
| - # print('HTTP.send 2') |
194 |
| - # this line is sync |
| 195 | + |
195 | 196 | data_to_send = data[i:i+chunk_size]
|
196 | 197 | end_stream = (len(data_to_send) != chunk_size)
|
197 |
| - # print('HTTP.send 3') |
198 |
| - # print(stream_id, len(data_to_send), end_stream) |
| 198 | + |
199 | 199 | try:
|
200 | 200 | self.connection.send_data(stream_id, data_to_send, end_stream=end_stream)
|
201 | 201 | except BaseException as e:
|
202 | 202 | print(e)
|
203 |
| - # print(i, len(data_to_send), chunk_size) |
204 |
| - # print(data_to_send) |
205 |
| - # print(self.connection.data_to_send()) |
| 203 | + |
206 | 204 | await self.sock.sendall(self.connection.data_to_send())
|
207 | 205 |
|
208 | 206 | if end_stream:
|
@@ -239,7 +237,15 @@ async def send_file(self, file_path):
|
239 | 237 | # 不知道这个 context manager 是否处理文件没找到
|
240 | 238 | async with aopen(file_path, mode='rb') as f:
|
241 | 239 | data = await f.read()
|
| 240 | + |
242 | 241 | self.headers['content-length'] = str(len(data))
|
| 242 | + |
| 243 | + content_type, content_encoding = mimetypes.guess_type(file_path) |
| 244 | + if content_type: |
| 245 | + self.headers['content-type'] = content_type |
| 246 | + if content_encoding: |
| 247 | + self.headers['content-encoding'] = content_encoding |
| 248 | + |
243 | 249 | await self.send(data)
|
244 | 250 |
|
245 | 251 | async def send_status_code(self, status_code):
|
|
0 commit comments