tornadoweb / tornado

Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed.
http://www.tornadoweb.org/
Apache License 2.0
21.76k stars 5.51k forks source link

Support streaming chunks in HTTP/1.0 read-until-close bodies #3370

Open jadehh opened 7 months ago

bdarnell commented 7 months ago

What exactly are you trying to do here? This looks the same as the chunking in _read_fixed_body. (and remember that all new functionality needs tests, and comments and commit messages should be in english)

jadehh commented 7 months ago

_read_fixed_body function cannot parse like this headers

{
  "Access-Control-Allow-Credentials":true,
  "Access-Control-Allow-Headers": "Content-Type",
  "Access-Control-Allow-Origin": "*",
  "Cache-Control:": "no-cache",
  "Connection": "close",
  "Content-Type": "video/x-flv",
  "Expires": "-1",
  "Pragma": "no-cache"
}

this is a flv live url headers,this looks no Transfer-Encoding key

so,I created _read_chunked_body_byflv function and tested support flv live url

bdarnell commented 7 months ago

Without a Content-Length or Transfer-Encoding header, this is an HTTP/1.0-style read-until-close response, so it is handled by _read_body_until_close. This is discouraged in HTTP/1.1 but allowed for backwards compatibility.

Special-casing the video/x-flv content type is not appropriate, but it would be appropriate to modify _read_body_until_close to read the data in chunks and make multiple data_received calls (similar to _read_fixed_body).

jadehh commented 7 months ago

thanks for reply, modify _read_body_until_close like this

async def _read_body_until_close(self, delegate: httputil.HTTPMessageDelegate) -> None:
    while True:
        chunk = await self.stream.read_bytes(self.params.chunk_size, partial=True)
        if not self._write_finished or self.is_client:
            with _ExceptionLoggingContext(app_log):
                ret = delegate.data_received(chunk)
                if ret is not None:
                    await ret

like this I can use the streaming_callback get real-time chunk, if modify _read_body_until_close, will it have any other impact.

bdarnell commented 4 months ago

This change looks good, but it needs a test and should be rebased into a single commit with comments in english.