redesign
This commit is contained in:
1 parent
ad620c976a
commit
0973129c17
26 files changed
+2255
-2245
No files matched your search
+18
-14
@@ -6,6 +6,7 @@ sent to the browser or reverse proxy.
|
||||
"""
|
||||
|
||||
import codecs
|
||||
import hashlib
|
||||
import re
|
||||
import tempfile
|
||||
from dataclasses import dataclass
|
||||
@@ -26,6 +27,7 @@ class ConversionResult:
|
||||
lines_read: int
|
||||
lines_emitted: int
|
||||
output_size: int
|
||||
content_sha256: str
|
||||
|
||||
|
||||
def _get_encoding(content_type: str) -> str:
|
||||
@@ -37,11 +39,11 @@ def _get_encoding(content_type: str) -> str:
|
||||
try:
|
||||
encoding = codecs.lookup(encoding).name
|
||||
except (LookupError, ValueError) as exc:
|
||||
raise InvalidSourceError("Nieznane kodowanie tekstu z serwera zrodlowego.") from exc
|
||||
raise InvalidSourceError("Unsupported source text encoding.") from exc
|
||||
# The reader splits on ASCII newline octets. Multi-byte encodings need a
|
||||
# different framing protocol and must not be interpreted as hosts text.
|
||||
if encoding in {"utf-16", "utf-16-le", "utf-16-be", "utf-32", "utf-32-le", "utf-32-be"}:
|
||||
raise InvalidSourceError("Nieobslugiwane kodowanie pliku listy.")
|
||||
raise InvalidSourceError("Unsupported source list encoding.")
|
||||
return encoding
|
||||
|
||||
|
||||
@@ -55,10 +57,10 @@ def _bounded_lines(response, *, chunk_size: int, line_limit: int, source_limit:
|
||||
if not chunk:
|
||||
continue
|
||||
if not isinstance(chunk, bytes):
|
||||
raise InvalidSourceError("Zrodlo nie zwrocilo poprawnych danych binarnych.")
|
||||
raise InvalidSourceError("Source returned invalid binary data.")
|
||||
total += len(chunk)
|
||||
if total > source_limit:
|
||||
raise SourceTooLargeError("Plik zrodlowy przekracza dopuszczalny rozmiar.")
|
||||
raise SourceTooLargeError("Source list exceeds the maximum allowed size.")
|
||||
pieces = chunk.split(b"\n")
|
||||
for part in pieces[:-1]:
|
||||
if skipping or len(pending) + len(part) > line_limit:
|
||||
@@ -104,7 +106,7 @@ def convert_source(
|
||||
if length:
|
||||
try:
|
||||
if int(length) > source_limit:
|
||||
raise SourceTooLargeError("Plik zrodlowy przekracza dopuszczalny rozmiar.")
|
||||
raise SourceTooLargeError("Source list exceeds the maximum allowed size.")
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
@@ -112,13 +114,15 @@ def convert_source(
|
||||
lines_read = 0
|
||||
lines_emitted = 0
|
||||
written = 0
|
||||
content_hasher = hashlib.sha256()
|
||||
|
||||
def append(data: bytes):
|
||||
nonlocal written
|
||||
if written + len(data) > output_limit:
|
||||
raise SourceTooLargeError("Wynik konwersji przekracza dopuszczalny rozmiar.")
|
||||
raise SourceTooLargeError("Converted output exceeds the maximum allowed size.")
|
||||
output.write(data)
|
||||
written += len(data)
|
||||
content_hasher.update(data)
|
||||
|
||||
try:
|
||||
if debug_lines is not None and debug_lines:
|
||||
@@ -133,37 +137,37 @@ def convert_source(
|
||||
lines_read += 1
|
||||
if raw is None:
|
||||
if debug_lines is not None and lines_read <= 5:
|
||||
append(b"# [DEBUG] pominieto zbyt dlugi wiersz\n")
|
||||
append(b"# [DEBUG] skipped an overlong line\n")
|
||||
continue
|
||||
if b"\x00" in raw:
|
||||
raise InvalidSourceError("Zrodlo zawiera dane binarne zamiast listy tekstowej.")
|
||||
raise InvalidSourceError("Source contains binary data instead of a text list.")
|
||||
try:
|
||||
line = raw.decode(encoding, errors="strict")
|
||||
if lines_read == 1:
|
||||
line = line.lstrip("\ufeff")
|
||||
except UnicodeError as exc:
|
||||
raise InvalidSourceError("Nie mozna zdekodowac tekstu listy (kodowanie).") from exc
|
||||
raise InvalidSourceError("Unable to decode source list text (character encoding).") from exc
|
||||
try:
|
||||
converted = convert_line(line, target_ip)
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise InvalidSourceError("Nie mozna przetworzyc formatu listy.") from exc
|
||||
raise InvalidSourceError("Unable to process this list format.") from exc
|
||||
if converted:
|
||||
lines_emitted += 1
|
||||
append((converted + "\n").encode("utf-8"))
|
||||
if debug_lines is not None and lines_read <= 5:
|
||||
preview = line[:200].replace("\r", "\\r").replace("\n", "\\n")
|
||||
append(f"# [DEBUG] podglad {lines_read}: {preview}\n".encode("utf-8"))
|
||||
append(f"# [DEBUG] preview {lines_read}: {preview}\n".encode("utf-8"))
|
||||
|
||||
if lines_emitted == 0:
|
||||
raise InvalidSourceError(
|
||||
"Nie znaleziono domen w obslugiwanym formacie. Sprawdz adres i zawartosc listy."
|
||||
"No domains found in a supported format. Check the URL and list contents."
|
||||
)
|
||||
if debug_lines is not None:
|
||||
append(
|
||||
f"# [DEBUG] podsumowanie: przeczytano={lines_read}, wyemitowano={lines_emitted}\n".encode("utf-8")
|
||||
f"# [DEBUG] summary: read={lines_read}, emitted={lines_emitted}\n".encode("utf-8")
|
||||
)
|
||||
output.seek(0)
|
||||
return ConversionResult(output, lines_read, lines_emitted, written)
|
||||
return ConversionResult(output, lines_read, lines_emitted, written, content_hasher.hexdigest())
|
||||
except BaseException:
|
||||
output.close()
|
||||
raise
|
||||
|
||||
Reference in new issue
Block a user