Skip to content

StreamResponse + HTTP/1.0 = corrupted file  #476

@vbraun

Description

@vbraun

I'm using StreamResponse to serve files. My code is

from aiohttp import web
response = web.StreamResponse()
response.enable_chunked_encoding()
response.content_length = statinfo.st_size
response.content_type = 'application/octet-stream'
response.start(request)
with open(filename, 'rb') as f:
    while True:
        chunk = f.read(CHUNK_SIZE)
        if not chunk:
            return response
        response.write(chunk)
        yield from response.drain()

If I'm using wget to download the file then it works beautifully. But when I'm using Python urllib to download the file, my download is corrupted. The difference seems to be that wget can HTTP/1.1, but urllib only does HTTP/1.0. The network trace is

Host: ...
User-Agent: Python-urllib/1.17

HTTP/1.0 200 OK
CONTENT-LENGTH: 815206
CONTENT-TYPE: application/octet-stream
TRANSFER-ENCODING: chunked
CONNECTION: close
DATE: Thu, 27 Aug 2015 17:31:25 GMT
SERVER: Python/3.4 aiohttp/0.17.2

10000
... binary data ...

and Python saves everything starting at (and including) the 10000 into the file.

I think this is because TRANSFER-ENCODING: chunked is HTTP/1.1 only, and a HTTP/1.0 client is entirely correct in ignoring it. Aiohttp should not send chunk sizes to HTTP/1.0 clients as they almost certainly end up in the data stream.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions