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

WARC Fixes: Payload hash and CDX records #360

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion wpull/warc/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def get_http_header(self) -> Response:
with wpull.util.reset_file_offset(self.block_file):
data = self.block_file.read(4096)

match = re.match(br'(.*?\r?\n\r?\n)', data)
match = re.match(br'(.*?\r?\n\r?\n)', data, re.DOTALL)

if not match:
return
Expand Down
10 changes: 9 additions & 1 deletion wpull/warc/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,15 @@ def response_data(self, data: bytes):
self._response_temp_file.write(data)

def end_response(self, response: HTTPResponse):
payload_offset = len(response.to_bytes())
self._response_record.block_file.seek(0)
while True:
data = self._response_record.block_file.readline()
if data in (b'\r\n', b'\n'):
payload_offset = self._response_record.block_file.tell()
break
if not data:
payload_offset = 0
break

self._response_record.block_file.seek(0)
self._recorder.set_length_and_maybe_checksums(
Expand Down
10 changes: 8 additions & 2 deletions wpull/warc/recorder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ def test_warc_recorder(self):
warc_filename = 'asdf.warc'
cdx_filename = 'asdf.cdx'

response_header_bytes = b"HTTP/1.1 200 OK\r\n"
response_header_bytes += b"Content-Type: text/plain\r\n"
response_header_bytes += b"X-Empty-Field: \r\n"
response_header_bytes += b"\r\n"

warc_recorder = WARCRecorder(
file_prefix,
params=WARCRecorderParams(
Expand All @@ -65,6 +70,7 @@ def test_warc_recorder(self):
request.address = ('0.0.0.0', 80)
request.prepare_for_send()
response = HTTPResponse(200, 'OK')
response.parse(response_header_bytes)
response.body = Body()

with wpull.util.reset_file_offset(response.body):
Expand All @@ -75,7 +81,7 @@ def test_warc_recorder(self):
session.request_data(request.to_bytes())
session.end_request(request)
session.begin_response(response)
session.response_data(response.to_bytes())
session.response_data(response_header_bytes)
session.response_data(response.body.content())
session.end_response(response)
session.close()
Expand Down Expand Up @@ -135,7 +141,7 @@ def test_warc_recorder(self):
self.assertTrue(cdx_lines[0].startswith(b' CDX'))

self.assertEqual(b'http://example.com/', cdx_fields[0])
self.assertEqual(b'-', cdx_fields[2])
self.assertEqual(b'text/plain', cdx_fields[2])
self.assertEqual(b'200', cdx_fields[3])
self.assertNotEqual(b'-', cdx_fields[4])
self.assertNotEqual(b'0', cdx_fields[5])
Expand Down