Skip to content

fix: allow downloading when content-length header is missing#2413

Merged
facelessuser merged 3 commits intofacelessuser:mainfrom
maartenbreddels:fix_download_without_content_length_header
Jul 15, 2024
Merged

fix: allow downloading when content-length header is missing#2413
facelessuser merged 3 commits intofacelessuser:mainfrom
maartenbreddels:fix_download_without_content_length_header

Conversation

@maartenbreddels
Copy link
Copy Markdown
Contributor

This can happen for valid http responses, such as when the server is using chunked encoding.

Fixes #2404.

@gir-bot gir-bot added S: needs-review Needs to be reviewed and/or approved. C: snippets Related to the snippets extension. C: source Related to source code. labels Jul 14, 2024
@maartenbreddels
Copy link
Copy Markdown
Contributor Author

This makes the following work

--8<-- "https://py.cafe/files/maartenbreddels/altair-car-performance-comparison/app.py"

Which gives these headers:

$ curl -I https://py.cafe/files/maartenbreddels/altair-car-performance-comparison/app.py                                                   fix_download_without_content_length_header ◼
HTTP/2 200 
age: 0
cache-control: public, max-age=0, must-revalidate
content-type: text/plain
date: Sun, 14 Jul 2024 19:45:46 GMT
server: Vercel
strict-transport-security: max-age=63072000
vary: RSC, Next-Router-State-Tree, Next-Router-Prefetch
x-matched-path: /files/[username]/[projectname]/[...path]
x-vercel-cache: MISS
x-vercel-execution-region: iad1
x-vercel-id: fra1::iad1::zhtpq-1720986346756-476b90c31ef2

PS: this is used to render the sourcecode for the hosted app at https://py.cafe/maartenbreddels/altair-car-performance-comparison

@facelessuser
Copy link
Copy Markdown
Owner

Seems like a pretty reasonable approach.

We can ignore any lint errors outside of your changes (looks like some new checks are now throwing errors in old code that used to pass).

Tests will have to be updated to pass for new expectations as before we used to always error out for missing content length. I imagine we will likely need at least one new test to cover this new use case as well. If this seems like a little beyond what you are comfortable doing (as it will probably involve doing some mocking and such), I can probably take what you have and finish, but feel free to update if you are comfortable in this area. Either way, I think this is a decent way to move forward on this.

This can happen for valid http responses, such as when the server
is using chunked encoding.

Fixes facelessuser#2404
@maartenbreddels maartenbreddels force-pushed the fix_download_without_content_length_header branch from b3e9c99 to 03933ba Compare July 15, 2024 07:38
@gir-bot gir-bot added the C: tests Related to testing. label Jul 15, 2024
@maartenbreddels
Copy link
Copy Markdown
Contributor Author

Hi,

no problem with adding the test.
I managed to get the unit tests working. Let me know if you prefer to take over and change it to your liking, if so, feel free to force push on my branch.

Hope this is to your liking.

Regards,

Maarten

content_length = int(length)
content = None
if "content-length" not in response.headers:
# we have to read to know if we went over the max, but never more than url_max_size
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wrap url_max_size in backticks (`) so that the spellchecker will ignore them.

for l in f:
length += len(l)
content.append(l)
content = open('tests/test_extensions/_snippets/lines.txt', 'rb').read()
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I'm indifferent as to whether open or with open is used, if with is not used, please explicitly close the file handle. with will take care of this for you. I understand that Cpython (specifically) will likely garbage collect this due to it not having a reference and going out of scope, but that is not the case with all Python implementations and I would like it to be explicitly closed, whether with with or without it.

@facelessuser
Copy link
Copy Markdown
Owner

Overall it looks good, but I'd made a few comments.

@maartenbreddels
Copy link
Copy Markdown
Contributor Author

Good comments 👍 . I implemented them as two separate commits to help review. Feel free to squash merge it when ready.

@facelessuser
Copy link
Copy Markdown
Owner

@maartenbreddels Thanks for your work on this!

@gir-bot lgtm

@gir-bot gir-bot added S: approved The pull request is ready to be merged. and removed S: needs-review Needs to be reviewed and/or approved. labels Jul 15, 2024
@facelessuser facelessuser merged commit ef38568 into facelessuser:main Jul 15, 2024
@maartenbreddels
Copy link
Copy Markdown
Contributor Author

Not to put pressure, but what is your release cadence?

I want to advertise to people to use pycafe + mkdocs and this plugin to have live code examples in their docs, which depends on this fix.

Could you let me know here or in #2404 when this is released, then I can let people know this is possible.

Thanks!

@facelessuser
Copy link
Copy Markdown
Owner

I imagine it will be sometime soon. I can't promise I'll remember to comment. The best way to know when a release is made is to watch the repo, and if all you care about is releases, configure it to only watch releases. You can disable the watch once the release in question is made.

@maartenbreddels
Copy link
Copy Markdown
Contributor Author

Really good idea, will do that! Thanks a ton, also, for these great extensions, and from what I've seen a very nice and high-quality codebase. Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C: snippets Related to the snippets extension. C: source Related to source code. C: tests Related to testing. S: approved The pull request is ready to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Download snippet + server reply without content-length

3 participants