Spaces:
Running
Running
Fix streaming header & status issue
Browse files- mediaflow_proxy/handlers.py +12 -2
mediaflow_proxy/handlers.py
CHANGED
@@ -49,10 +49,15 @@ async def handle_hls_stream_proxy(request: Request, destination: str, headers: d
|
|
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 = {
|
|
|
|
|
|
|
|
|
53 |
|
54 |
return StreamingResponse(
|
55 |
streamer.stream_content(destination, headers),
|
|
|
56 |
headers=response_headers,
|
57 |
background=BackgroundTask(streamer.close),
|
58 |
)
|
@@ -107,15 +112,20 @@ async def handle_stream_request(method: str, video_url: str, headers: dict):
|
|
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 = {
|
|
|
|
|
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:
|
|
|
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 = {
|
53 |
+
k.title(): v for k, v in response.headers.items() if k.lower() in SUPPORTED_RESPONSE_HEADERS
|
54 |
+
}
|
55 |
+
# set chunked transfer encoding for streaming
|
56 |
+
response_headers["Transfer-Encoding"] = "chunked"
|
57 |
|
58 |
return StreamingResponse(
|
59 |
streamer.stream_content(destination, headers),
|
60 |
+
status_code=response.status_code,
|
61 |
headers=response_headers,
|
62 |
background=BackgroundTask(streamer.close),
|
63 |
)
|
|
|
112 |
try:
|
113 |
response = await streamer.head(video_url, headers)
|
114 |
# clean up the headers to only include the necessary headers and remove acl headers
|
115 |
+
response_headers = {
|
116 |
+
k.title(): v for k, v in response.headers.items() if k.lower() in SUPPORTED_RESPONSE_HEADERS
|
117 |
+
}
|
118 |
|
119 |
if method == "HEAD":
|
120 |
await streamer.close()
|
121 |
return Response(headers=response_headers, status_code=response.status_code)
|
122 |
else:
|
123 |
+
# set chunked transfer encoding for streaming
|
124 |
+
response_headers["Transfer-Encoding"] = "chunked"
|
125 |
return StreamingResponse(
|
126 |
streamer.stream_content(video_url, headers),
|
127 |
headers=response_headers,
|
128 |
+
status_code=206,
|
129 |
background=BackgroundTask(streamer.close),
|
130 |
)
|
131 |
except httpx.HTTPStatusError as e:
|