Skip to content

Use content length to avoid http hang#602

Closed
bmidgley wants to merge 1 commit into
micropython:masterfrom
bmidgley:use-header-length
Closed

Use content length to avoid http hang#602
bmidgley wants to merge 1 commit into
micropython:masterfrom
bmidgley:use-header-length

Conversation

@bmidgley

@bmidgley bmidgley commented Jan 30, 2023

Copy link
Copy Markdown

response.content() is hanging on responses that don't end with a final newline.

Issue is in #546

Here's my actual workaround while waiting for the real fix... reach into the impl and read one character at a time. Stop when reaching "}"

    response = urequests.get(url)
    response._cached = response.raw.read(1)
    while True:
        response._cached += response.raw.read(1)
        if response._cached[-1] == 125: break
    json = response.json()

@bmidgley bmidgley changed the title Use content length to avoid hang Use content length to avoid http hang Jan 30, 2023
@bmidgley

Copy link
Copy Markdown
Author

Is there another way to avoid this hang?

@andrewleech

Copy link
Copy Markdown
Contributor

I don't think the .read() function should be waiting for a newline, only .readline() is supposed to work this way.
.read() should just provide all data currently available.
The existing implementation can work for downloading binary files too, which definitely doesn't depend on a trailing newline.

That being said it does seem sensible to me to honour the content length header if it's provided.

@dpgeorge

Copy link
Copy Markdown
Member

Closing in favour of #1124, which implements HTTP/1.1.

@dpgeorge dpgeorge closed this Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants