Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG+2] [HttpCompressionMW] Do not decompress gzip binary/octet-stream responses #2065

Merged
merged 3 commits into from
Jul 11, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion scrapy/utils/gz.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ def gunzip(data):
return output

_is_gzipped_re = re.compile(br'^application/(x-)?gzip\b', re.I)
_is_octetstream_re = re.compile(br'^(application|binary)/octet-stream\b', re.I)
Copy link
Member

@kmike kmike Jul 8, 2016

Choose a reason for hiding this comment

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

A small trick (not sure if everyone will like it): it can be written as

_is_octetstream = re.compile(br'^(application|binary)/octet-stream\b', re.I).search

and then used like or (_is_octetstream(ctype) and cenc in ...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe @Tethik can comment too

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice trick. It would make the final boolean expression a tiny bit easier to read.

return (_is_gzipped(ctype) is not None or
            (_is_octetstream(ctype) is not None and
             cenc in (b'gzip', b'x-gzip')))

👍

Copy link
Member

Choose a reason for hiding this comment

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

Ah right, _is_gzipped can also be written this way. I'd even drop 'is not None' checks. From https://docs.python.org/3.5/library/re.html#match-objects:

Match objects always have a boolean value of True

There is an example in stdlib docs:

match = re.search(pattern, string)
if match:
    process(match)

Copy link
Contributor

Choose a reason for hiding this comment

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

Doing is not None explicitly makes it a bool though. If we drop the is not None then we're going to return None or True instead of True or False. I doubt it makes a difference though.

return _is_gzipped(ctype) or
            (_is_octetstream(ctype) and
             cenc in (b'gzip', b'x-gzip')))

If we want to be really pedantic we could probably also drop the parantheses too.

Copy link
Member

Choose a reason for hiding this comment

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

@redapple The PR looks fine 👍. Could you please clean up and merge it?

2016-07-09 4:25 GMT+05:00 Joakim Uddholm notifications@github.com:

In scrapy/utils/gz.py
#2065 (comment):

@@ -51,8 +51,12 @@ def gunzip(data):
return output

_is_gzipped_re = re.compile(br'^application/(x-)?gzip\b', re.I)
+_is_octetstream_re = re.compile(br'^(application|binary)/octet-stream\b', re.I)

Doing is not None explicitly makes it a bool though. If we drop the is
not None then we're going to return None or True instead of True or
False. I doubt it makes a difference though.

return _is_gzipped(ctype) or
(_is_octetstream(ctype) and
cenc in (b'gzip', b'x-gzip')))

If we want to be really pedantic we could probably also drop the
parantheses too.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/scrapy/scrapy/pull/2065/files/778f1cf84cfc2047fdd43b45f908bf0078a160b6#r70154620,
or mute the thread
https://github.com/notifications/unsubscribe/AAGldbtIqFZwferWdBSBy2TG31Tv_0zEks5qTtxngaJpZM4I5xtb
.


def is_gzipped(response):
"""Return True if the response is gzipped, or False otherwise"""
ctype = response.headers.get('Content-Type', b'')
return _is_gzipped_re.search(ctype) is not None
cenc = response.headers.get('Content-Encoding', b'').lower()
return (_is_gzipped_re.search(ctype) is not None or
(_is_octetstream_re.search(ctype) is not None and
cenc in (b'gzip', b'x-gzip')))
20 changes: 20 additions & 0 deletions tests/test_downloadermiddleware_httpcompression.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,26 @@ def test_process_response_gzipped_contenttype(self):
self.assertEqual(response.headers['Content-Encoding'], b'gzip')
self.assertEqual(response.headers['Content-Type'], b'application/gzip')

def test_process_response_gzip_app_octetstream_contenttype(self):
response = self._getresponse('gzip')
response.headers['Content-Type'] = 'application/octet-stream'
request = response.request

newresponse = self.mw.process_response(request, response, self.spider)
self.assertIs(newresponse, response)
self.assertEqual(response.headers['Content-Encoding'], b'gzip')
self.assertEqual(response.headers['Content-Type'], b'application/octet-stream')

def test_process_response_gzip_binary_octetstream_contenttype(self):
response = self._getresponse('x-gzip')
response.headers['Content-Type'] = 'binary/octet-stream'
request = response.request

newresponse = self.mw.process_response(request, response, self.spider)
self.assertIs(newresponse, response)
self.assertEqual(response.headers['Content-Encoding'], b'gzip')
self.assertEqual(response.headers['Content-Type'], b'binary/octet-stream')

def test_process_response_head_request_no_decode_required(self):
response = self._getresponse('gzip')
response.headers['Content-Type'] = 'application/gzip'
Expand Down