Spaces:
Running
Running
Fix proxy stream request headers not passing for seeking the content
Browse files- mediaflow_proxy/routes.py +1 -13
- mediaflow_proxy/utils/http_utils.py +28 -1
mediaflow_proxy/routes.py
CHANGED
@@ -2,23 +2,11 @@ from fastapi import Request, Depends, APIRouter
|
|
2 |
from pydantic import HttpUrl
|
3 |
|
4 |
from .handlers import handle_hls_stream_proxy, proxy_stream, get_manifest, get_playlist, get_segment, get_public_ip
|
|
|
5 |
|
6 |
proxy_router = APIRouter()
|
7 |
|
8 |
|
9 |
-
def get_proxy_headers(request: Request) -> dict:
|
10 |
-
"""
|
11 |
-
Extracts proxy headers from the request query parameters.
|
12 |
-
|
13 |
-
Args:
|
14 |
-
request (Request): The incoming HTTP request.
|
15 |
-
|
16 |
-
Returns:
|
17 |
-
dict: A dictionary of proxy headers.
|
18 |
-
"""
|
19 |
-
return {k[2:]: v for k, v in request.query_params.items() if k.startswith("h_")}
|
20 |
-
|
21 |
-
|
22 |
@proxy_router.head("/hls")
|
23 |
@proxy_router.get("/hls")
|
24 |
async def hls_stream_proxy(
|
|
|
2 |
from pydantic import HttpUrl
|
3 |
|
4 |
from .handlers import handle_hls_stream_proxy, proxy_stream, get_manifest, get_playlist, get_segment, get_public_ip
|
5 |
+
from .utils.http_utils import get_proxy_headers
|
6 |
|
7 |
proxy_router = APIRouter()
|
8 |
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
@proxy_router.head("/hls")
|
11 |
@proxy_router.get("/hls")
|
12 |
async def hls_stream_proxy(
|
mediaflow_proxy/utils/http_utils.py
CHANGED
@@ -3,12 +3,24 @@ from urllib import parse
|
|
3 |
|
4 |
import httpx
|
5 |
import tenacity
|
|
|
6 |
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
|
7 |
|
8 |
from mediaflow_proxy.configs import settings
|
9 |
|
10 |
logger = logging.getLogger(__name__)
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
class DownloadError(Exception):
|
14 |
def __init__(self, status_code, message):
|
@@ -220,7 +232,7 @@ def encode_mediaflow_proxy_url(
|
|
220 |
return f"{base_url}?{encoded_params}"
|
221 |
|
222 |
|
223 |
-
def get_original_scheme(request) -> str:
|
224 |
"""
|
225 |
Determines the original scheme (http or https) of the request.
|
226 |
|
@@ -249,3 +261,18 @@ def get_original_scheme(request) -> str:
|
|
249 |
|
250 |
# Default to http if no indicators of https are found
|
251 |
return "http"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
import httpx
|
5 |
import tenacity
|
6 |
+
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):
|
|
|
232 |
return f"{base_url}?{encoded_params}"
|
233 |
|
234 |
|
235 |
+
def get_original_scheme(request: Request) -> str:
|
236 |
"""
|
237 |
Determines the original scheme (http or https) of the request.
|
238 |
|
|
|
261 |
|
262 |
# Default to http if no indicators of https are found
|
263 |
return "http"
|
264 |
+
|
265 |
+
|
266 |
+
def get_proxy_headers(request: Request) -> dict:
|
267 |
+
"""
|
268 |
+
Extracts proxy headers from the request query parameters.
|
269 |
+
|
270 |
+
Args:
|
271 |
+
request (Request): The incoming HTTP request.
|
272 |
+
|
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
|