forked from mightymercado/hls-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
23 lines (16 loc) · 932 Bytes
/
proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from http import HTTPStatus
from handlers import ProxyHandlerFactory, ProxyResponse
from http_helpers import http_client
def hls_proxy(url: bytes, extension: str) -> ProxyResponse:
response_to_proxy = http_client.get(url=url,
stream=True,
allow_redirects=False,
timeout=3)
if not response_to_proxy.ok:
return ProxyResponse(body=response_to_proxy.content, status=response_to_proxy.status_code)
if response_to_proxy.is_redirect:
response_to_proxy = http_client.get(response_to_proxy.headers['location'], stream=True, timeout=3)
if (handler := ProxyHandlerFactory.get_handler(extension)):
return handler.handle(response_to_proxy)
return ProxyResponse(body=f'Bad Request, Unsupported Extension: {extension}',
status=HTTPStatus.BAD_REQUEST)