mhdzumair commited on
Commit
b78171a
1 Parent(s): 879f4ac

Only use support request and response header to avoid acl issues

Browse files
mediaflow_proxy/const.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ SUPPORTED_RESPONSE_HEADERS = [
2
+ "accept-ranges",
3
+ "content-type",
4
+ "content-length",
5
+ "content-range",
6
+ "connection",
7
+ "transfer-encoding",
8
+ "last-modified",
9
+ "etag",
10
+ "server",
11
+ "date",
12
+ ]
13
+
14
+ SUPPORTED_REQUEST_HEADERS = [
15
+ "accept",
16
+ "accept-encoding",
17
+ "accept-language",
18
+ "connection",
19
+ "transfer-encoding",
20
+ "range",
21
+ "if-range",
22
+ "user-agent",
23
+ ]
mediaflow_proxy/handlers.py CHANGED
@@ -9,6 +9,7 @@ from pydantic import HttpUrl
9
  from starlette.background import BackgroundTask
10
 
11
  from .configs import settings
 
12
  from .mpd_processor import process_manifest, process_playlist, process_segment
13
  from .utils.cache_utils import get_cached_mpd, get_cached_init_segment
14
  from .utils.http_utils import Streamer, DownloadError, download_file_with_retry, request_with_retry
@@ -47,13 +48,12 @@ async def handle_hls_stream_proxy(request: Request, destination: str, headers: d
47
  return await fetch_and_process_m3u8(streamer, destination, headers, request, key_url)
48
 
49
  headers.update({"accept-ranges": headers.get("range", "bytes=0-")})
50
- # handle the encoding response header, since decompression is handled by the httpx
51
- if "content-encoding" in response.headers:
52
- del response.headers["content-encoding"]
53
 
54
  return StreamingResponse(
55
  streamer.stream_content(destination, headers),
56
- headers=response.headers,
57
  background=BackgroundTask(streamer.close),
58
  )
59
  except httpx.HTTPStatusError as e:
@@ -106,13 +106,16 @@ async def handle_stream_request(method: str, video_url: str, headers: dict):
106
  streamer = Streamer(client)
107
  try:
108
  response = await streamer.head(video_url, headers)
 
 
 
109
  if method == "HEAD":
110
  await streamer.close()
111
- return Response(headers=response.headers, status_code=response.status_code)
112
  else:
113
  return StreamingResponse(
114
  streamer.stream_content(video_url, headers),
115
- headers=response.headers,
116
  background=BackgroundTask(streamer.close),
117
  )
118
  except httpx.HTTPStatusError as e:
 
9
  from starlette.background import BackgroundTask
10
 
11
  from .configs import settings
12
+ from .const import SUPPORTED_RESPONSE_HEADERS
13
  from .mpd_processor import process_manifest, process_playlist, process_segment
14
  from .utils.cache_utils import get_cached_mpd, get_cached_init_segment
15
  from .utils.http_utils import Streamer, DownloadError, download_file_with_retry, request_with_retry
 
48
  return await fetch_and_process_m3u8(streamer, destination, headers, request, key_url)
49
 
50
  headers.update({"accept-ranges": headers.get("range", "bytes=0-")})
51
+ # clean up the headers to only include the necessary headers and remove acl headers
52
+ response_headers = {k: v for k, v in response.headers.items() if k.lower() in SUPPORTED_RESPONSE_HEADERS}
 
53
 
54
  return StreamingResponse(
55
  streamer.stream_content(destination, headers),
56
+ headers=response_headers,
57
  background=BackgroundTask(streamer.close),
58
  )
59
  except httpx.HTTPStatusError as e:
 
106
  streamer = Streamer(client)
107
  try:
108
  response = await streamer.head(video_url, headers)
109
+ # clean up the headers to only include the necessary headers and remove acl headers
110
+ response_headers = {k: v for k, v in response.headers.items() if k.lower() in SUPPORTED_RESPONSE_HEADERS}
111
+
112
  if method == "HEAD":
113
  await streamer.close()
114
+ return Response(headers=response_headers, status_code=response.status_code)
115
  else:
116
  return StreamingResponse(
117
  streamer.stream_content(video_url, headers),
118
+ headers=response_headers,
119
  background=BackgroundTask(streamer.close),
120
  )
121
  except httpx.HTTPStatusError as e:
mediaflow_proxy/utils/http_utils.py CHANGED
@@ -7,20 +7,10 @@ from starlette.requests import Request
7
  from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
8
 
9
  from mediaflow_proxy.configs import settings
 
10
 
11
  logger = logging.getLogger(__name__)
12
 
13
- supported_request_headers = [
14
- "accept",
15
- "accept-encoding",
16
- "accept-language",
17
- "connection",
18
- "transfer-encoding",
19
- "range",
20
- "if-range",
21
- "user-agent",
22
- ]
23
-
24
 
25
  class DownloadError(Exception):
26
  def __init__(self, status_code, message):
@@ -273,6 +263,6 @@ def get_proxy_headers(request: Request) -> dict:
273
  Returns:
274
  dict: A dictionary of proxy headers.
275
  """
276
- request_headers = {k: v for k, v in request.headers.items() if k in supported_request_headers}
277
  request_headers.update({k[2:].lower(): v for k, v in request.query_params.items() if k.startswith("h_")})
278
  return request_headers
 
7
  from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
8
 
9
  from mediaflow_proxy.configs import settings
10
+ from mediaflow_proxy.const import SUPPORTED_REQUEST_HEADERS
11
 
12
  logger = logging.getLogger(__name__)
13
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  class DownloadError(Exception):
16
  def __init__(self, status_code, message):
 
263
  Returns:
264
  dict: A dictionary of proxy headers.
265
  """
266
+ request_headers = {k: v for k, v in request.headers.items() if k in SUPPORTED_REQUEST_HEADERS}
267
  request_headers.update({k[2:].lower(): v for k, v in request.query_params.items() if k.startswith("h_")})
268
  return request_headers