micropython / micropython/micropython-lib
urequests.get ignores content-length header
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2.9k
- Forks
- 1.1k
- Avg merge
- 7d 6h
- Merged PRs (30d)
- 3
Description
When connecting a server running this php sample
<?php
set_time_limit(0);
ob_start();
header('Content-type: text/plain');
header("Content-Encoding: none");
echo "OK";
header('Content-Length: '.ob_get_length());
header('Connection: close');
ob_end_flush();
@ob_flush();
flush();
if(session_id()) session_write_close();
sleep(10);
die("Client should never see this");
?>
if you view this page in a web browser or in curl you will only get OK, however urequests.get ignores the content length and waits for sleep to finish and get the die message before returning content, urequests.head has the same issue except you can't get the status_code
i managed to fix this, but my fix does nothing when i import it even though it works if i copy/paste the library inside my script
this was how i fixed it
@property
def content(self):
if self._cached is None:
try:
if(self.headers["Content-Length"]):
self._cached = self.raw.read(int(self.headers["Content-Length"]))
else:
self._cached = self.raw.read()
finally:
self.raw.close()
self.raw = None
return self._cached
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the urequests content property and the get/head entry points, then reproduce the behavior against the PHP sample in the issue. Verify that the response stops at the declared Content-Length instead of waiting for later output, while preserving the returned content and status information.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100