Skip to content

Commit 258d825

Browse files
committed
Added logic to reconstruct netloc of a forwarded request on server-side when dynamically generating URLs for downloadable config files
1 parent bd96be8 commit 258d825

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/murfey/server/api/bootstrap.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,22 +505,35 @@ def get_cargo_config(request: Request):
505505
and its default path on Windows is %USERPROFILE%\\.cargo\\config.toml.
506506
"""
507507

508-
# Construct URL to our mirror of the Rust sparse index
509-
index_mirror = (
510-
f"{request.url.scheme}://{request.url.netloc}/{rust.prefix.strip('/')}/index/"
508+
# Check if this is a forwarded request from somewhere else and construct netloc
509+
netloc = (
510+
f"{request.headers['X-Forwarded-Host']}:{request.headers['X-Forwarded-Port']}"
511+
if request.headers.get("X-Forwarded-Host")
512+
and request.headers.get("X-Forwarded-Port")
513+
else request.url.netloc
511514
)
512515

516+
# Find path to Rust router using current URL Path
517+
path_to_router = request.url.path.removesuffix("/cargo/config.toml")
518+
519+
# Construct base URL for subsequent use
520+
base_url = f"{request.url.scheme}://{netloc}{path_to_router}"
521+
logger.debug(f"Base URL to Rust sub-router determined to be {base_url}")
522+
523+
# Construct URL to our mirror of the Rust sparse index
524+
index_url = f"{base_url}/index/"
525+
513526
# Construct and return the config.toml file
514527
config_data = "\n".join(
515528
[
516529
"[source.crates-io]",
517530
'replace-with = "murfey-crates"', # Redirect to our mirror
518531
"",
519532
"[source.murfey-crates]",
520-
f'registry = "sparse+{index_mirror}"', # sparse+ to use sparse protocol
533+
f'registry = "sparse+{index_url}"', # sparse+ to use sparse protocol
521534
"",
522535
"[registries.murfey-crates]",
523-
f'index = "sparse+{index_mirror}"', # sparse+ to use sparse protocol
536+
f'index = "sparse+{index_url}"', # sparse+ to use sparse protocol
524537
"",
525538
"[registry]",
526539
'default = "murfey-crates"', # Redirect to our mirror
@@ -568,8 +581,20 @@ def get_index_config(request: Request):
568581
used by Cargo when searching for and downloading packages.
569582
"""
570583

571-
# Construct URL for Rust router
572-
base_url = f"{request.url.scheme}://{request.url.netloc}" + rust.prefix
584+
# Check if this is a forwarded request from somewhere else and construct netloc
585+
netloc = (
586+
f"{request.headers['X-Forwarded-Host']}:{request.headers['X-Forwarded-Port']}"
587+
if request.headers.get("X-Forwarded-Host")
588+
and request.headers.get("X-Forwarded-Port")
589+
else request.url.netloc
590+
)
591+
592+
# Find path to Rust router using current URL Path
593+
path_to_router = request.url.path.removesuffix("/index/config.json")
594+
595+
# Construct base URL for subsequent use
596+
base_url = f"{request.url.scheme}://{netloc}{path_to_router}"
597+
logger.debug(f"Base URL to Rust sub-router determined to be {base_url}")
573598

574599
# Construct config file with the necessary endpoints
575600
config = {

0 commit comments

Comments
 (0)