Repository navigation
transparent gzip compression in urllib #43521
Description
Activity
Some webservers support gzipping things before sending
them, this patch adds transparrent support for this in
urllib2 (documentation http://www.http-compression.com/)This patach *requires* hash patch 914340 as a
prerequirement as this enabels stream support in the
gzip libary..- addedextension-modulesC modules in the Modules dirC modules in the Modules dir
on Jun 19, 2006 Looks good.
This needs tests and docs. As a new feature, this could not be released until Python 2.6.
It would be nice to have support for managing content negotiation in general, but that wish isn't an obstacle to this patch.
- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
on Feb 12, 2009 @jakob could you provide an updated patch for py3k that includes unit test and doc changes?
No, I have long since moved on to other things.
Its okay, Jacab, we will take it forward.
15 remaining items
I think, the transparent compression should work at http.client level. I also agree with other points made by Serhiy:
-
transparent decompression should delete headers Content-Encoding and Content-Length (this is as per RFC too)
-
Should not do another compression if the user has a explicit specified intent of using Content-Encoding: gzip and is ready to do decompression himself.
-
This transparent compression/decompression would require the availability gzip module, if not then the feature may be disabled and normal request-response cycle would proceed.
-
I think, having it 'ON' with a flag to switch 'OFF' would be more desirable than having this feature via Handler. The reason being it can help in performance of any requests on servers that support it and browsers have adopted similar approach too.
-
I updated bpo-1508475.diff for Python 3.4 and removed the change in urllib: http_client_gzip.patch. The patch only changes http.client to support server sending gzip data.
For example, the new python.org website serves gzip data even if the Accept-Encoding header is not sent by the client: see the issue bpo-20719.
I have code that already handles an “gzip” encoded response from urlopen(). All three patches leave the Content-Encoding header intact, so I suspect my code would try to decompress the body a second time. Deleting this header (as already suggested) would work for me.
Victor, the patch looks good and would be a welcome enhancement.
There should be an option for turning this on and off (perhaps, I want the zipped content and want to unzip later or in a different thread).
Consider adding support for "deflate" as well.
This is an enhancement, so I am changing the affected version from 3.3 to 3.5.
It is python-only, which works well with the cheeseshop.
That said, the patch is truly short; if that is really sufficient, it could almost go into the documentation as a recipe. But I would prefer some more assurances that it actually does work; a quick skim suggests that it relies on a superclass happening to implement read via readinto.
Needs tests and documentation change.
I think the patch is indeed a bit short, for instannce it looks like calling read() without a size limit could bypass the decoding.
Also, I wonder if Content-Encoding handling is better done at a higher level. What if someone wants to download a *.tar.gz file? They may not expect the tar file to be transparently decompressed. And I suspect this would blow up if you tried a partial range request.
Transfer-Encoding is meant to be the proper way to transparently compress HTTP messages at a low level, but it doesn’t seem to be used as much in the real world.
Related: bpo-1243678, which includes a patch for “httplib” (now known as “http.client”?). That patch looks like it sets Accept-Encoding and decodes according to Content-Encoding. However I suspect it is also trying to be too “transparent” at the wrong level and would have many of the problems already mentioned here.
The Lib/xmlrpc/client.py file appears to already support compression using “Content-Encoding: gzip”. Perhaps it could be leveraged for any work on this issue.
I suggest resolving bpo-15955 first, then the GzipFile API could be used without fear of decompression bombs.
Serhiy, when this came up for bulk-closing you said transparent gzip was a nice-to-have "if anybody provide PRs" — though not easy. Fair enough on that — it never really stalled on code, it stalled on one design call: opt-in handler vs default-on at http.client. That call's yours; here's one concrete answer to it: #154197.
I went opt-in — a HTTPGzipHandler that's not in build_opener()'s defaults, so urlopen() is unchanged unless you ask for it. That sidesteps Martin's 2014 footgun, where default-on would silently decompress a .tar.gz and break Range requests. It decodes only when it added the Accept-Encoding itself, and strips Content-Encoding/Content-Length.
Senthil preferred default-on with a flag; I can flip it if you'd rather — the mechanism's identical. gzip only for now, deflate a follow-up.
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsNo status
- StatusShow more project fieldsTodo
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs