ЛР7: прогон в Docker, исправления по результатам, evidence и отчёт

This commit is contained in:
LOSYASH
2026-12-03 16:54:53 +01:00
parent cbbe525b3e
commit 17baa0dd7e
47 changed files with 1934 additions and 65 deletions
+24 -6
View File
@@ -172,20 +172,38 @@ def raw_http(base: str, method: str, path: str, headers: dict, chunks=(), chunke
раньше (413 до конца тела) — тогда прекращаем отправку и читаем ответ.
Возвращает (status | None, body_bytes)."""
u = urlparse(base)
sock = socket.create_connection((u.hostname, u.port or 80), timeout=timeout)
port = u.port or (443 if u.scheme == "https" else 80)
sock = socket.create_connection((u.hostname, port), timeout=timeout)
if u.scheme == "https": # lab7: тот же TLS с проверкой CA и имени, что у urllib
sock = tls_context().wrap_socket(sock, server_hostname=u.hostname)
try:
lines = [f"{method} {path} HTTP/1.1", f"Host: {u.hostname}:{u.port or 80}", "Connection: close"]
lines = [f"{method} {path} HTTP/1.1", f"Host: {u.hostname}:{port}", "Connection: close"]
lines += [f"{k}: {v}" for k, v in headers.items()]
if chunked:
lines.append("Transfer-Encoding: chunked")
sock.sendall(("\r\n".join(lines) + "\r\n\r\n").encode("latin-1"))
early = b""
for part in chunks:
ready, _, _ = select.select([sock], [], [], 0)
if ready:
break # сервер уже ответил — дальше не шлём
# По TLS 1.3 сокет «читаем» и без ответа: сервер шлёт после handshake служебные
# записи (NewSessionTicket). Пробуем прочитать неблокирующе: нет данных приложения —
# шлём дальше; есть ответ или закрытие — прекращаем отправку.
sock.setblocking(False)
try:
early = sock.recv(65536)
got_reply = True
except (ssl.SSLWantReadError, BlockingIOError):
got_reply = False
except OSError:
got_reply = True
finally:
sock.settimeout(timeout)
if got_reply:
break # сервер уже ответил — дальше не шлём
try:
sock.sendall(f"{len(part):x}\r\n".encode() + part + b"\r\n" if chunked else part)
except (BrokenPipeError, ConnectionResetError):
except OSError: # сервер закрыл приём (в т.ч. ssl.SSLError, WinError 10053)
break
else:
if chunked and not close_after_send:
@@ -195,14 +213,14 @@ def raw_http(base: str, method: str, path: str, headers: dict, chunks=(), chunke
pass
if close_after_send:
return None, b"" # обрыв загрузки: ответ не ждём
data = b""
data = early
try:
while True:
piece = sock.recv(65536)
if not piece:
break
data += piece
except (ConnectionResetError, socket.timeout):
except OSError: # ConnectionReset, timeout, TLS EOF без close_notify
pass
finally:
sock.close()