Skip to content

Commit

Permalink
B - handle encodings appropriately and read buffer into new byte stea…
Browse files Browse the repository at this point in the history
…ms as seeking is not available
  • Loading branch information
chapinb committed May 26, 2023
1 parent 43b332d commit 89dfc85
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions libchickadee/parsers/plain_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""

import binascii
import io
from gzip import GzipFile

from libchickadee.parsers import ParserBase, run_parser_from_cli
Expand Down Expand Up @@ -60,19 +61,21 @@ def parse_file(self, file_entry, is_stream=False):
else:
# Encode if needed
two_bytes = file_entry.buffer.read(2)
two_bytes = (
two_bytes.encode() if isinstance(two_bytes, str) else two_bytes.read(2)
)
if isinstance(two_bytes, str):
two_bytes = two_bytes.encode()
stream_data = file_entry.buffer.read().encode()
else:
stream_data = file_entry.buffer.read()

file_entry.seek(0)
byte_stream = io.BytesIO(two_bytes + stream_data)
# Check for gzip stream
file_data = (
GzipFile(fileobj=file_entry)
GzipFile(fileobj=byte_stream)
if binascii.hexlify(two_bytes) == b"1f8b"
else file_entry.buffer
else byte_stream
)

for raw_line in file_data:
for raw_line in file_data.readlines():
line = raw_line if isinstance(raw_line, str) else raw_line.decode()
self.check_ips(line)

Expand Down

0 comments on commit 89dfc85

Please sign in to comment.