Modifying existing gzip read failure recovery mechanism to patch read for broken archives

This commit is contained in:
rootavish 2016-04-06 08:47:06 +05:30 committed by Avishkar Gupta
parent 414857a593
commit d9437fd3d9
4 changed files with 12 additions and 1 deletions

View File

@ -43,7 +43,7 @@ def gunzip(data):
# contains the whole page content
if output or getattr(f, 'extrabuf', None):
try:
output += f.extrabuf
output += f.extrabuf[-f.extrasize:]
finally:
break
else:

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -1,6 +1,8 @@
import unittest
from os.path import join
from w3lib.encoding import html_to_unicode
from scrapy.utils.gz import gunzip, is_gzipped
from scrapy.http import Response, Headers
from tests import tests_datadir
@ -66,3 +68,11 @@ class GunzipTest(unittest.TestCase):
hdrs = Headers({"Content-Type": "application/x-gzip;charset=utf-8"})
r1 = Response("http://www.example.com", headers=hdrs)
self.assertTrue(is_gzipped(r1))
def test_gunzip_illegal_eof(self):
with open(join(SAMPLEDIR, 'unexpected-eof.gz'), 'rb') as f:
text = html_to_unicode('charset=cp1252', gunzip(f.read()))[1]
with open(join(SAMPLEDIR, 'unexpected-eof-output.txt'), 'rb') as o:
expected_text = o.read().decode("utf-8")
self.assertEqual(len(text), len(expected_text))
self.assertEqual(text, expected_text)