Files
Athena-Deck/video_proxy.py
T

31 lines
1.7 KiB
Python

"""HTTP transport only: preserve the selected backend's API paths and payloads."""
import http.client
import json
import os
import socket
MAX_BODY=256*1024*1024
REQUEST_HEADERS={'content-type','accept','range','if-range','if-none-match','if-modified-since'}
HOP_HEADERS={'connection','keep-alive','proxy-authenticate','proxy-authorization','te','trailer','transfer-encoding','upgrade'}
def relay(handler,service):
if handler.headers.get('Transfer-Encoding'):raise ValueError('Content-Length erforderlich; Chunked-Uploads nicht unterstützt.')
length=int(handler.headers.get('Content-Length','0'))
if not 0<=length<=MAX_BODY:raise ValueError('Anfrage größer als 256 MiB oder ungültige Länge.')
data=dict(action='video-http',service=service,method=handler.command,path=handler.path,length=length,headers={k:v for k,v in handler.headers.items() if k.lower() in REQUEST_HEADERS})
path=os.environ.get('DECK_DOCKER_HELPER_SOCKET','/run/athena-deck-docker/control.sock')
with socket.socket(socket.AF_UNIX) as sock:
sock.settimeout(3600);sock.connect(path);sock.sendall(json.dumps(data).encode()+b'\n')
remaining=length
while remaining:
chunk=handler.rfile.read(min(1024*1024,remaining))
if not chunk:raise ValueError('Unvollständige Anfrage.')
sock.sendall(chunk);remaining-=len(chunk)
response=http.client.HTTPResponse(sock);response.begin()
handler.sent=True;handler.send_response(response.status)
for key,value in response.getheaders():
if key.lower() not in HOP_HEADERS|{'server','date'}:handler.send_header(key,value)
handler.send_header('Connection','close');handler.end_headers()
if handler.command!='HEAD':
while chunk:=response.read(1024*1024):handler.wfile.write(chunk)