From 8f5150ad2c1e30b85e5960e07c097b9c8d962d9e Mon Sep 17 00:00:00 2001 From: Paul Marcontell Date: Fri, 25 Oct 2024 11:22:26 -0700 Subject: [PATCH] Add handling for relative paths in Location headers --- jupyter_rsession_proxy/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/jupyter_rsession_proxy/__init__.py b/jupyter_rsession_proxy/__init__.py index 37f7033..d73bb2b 100644 --- a/jupyter_rsession_proxy/__init__.py +++ b/jupyter_rsession_proxy/__init__.py @@ -6,7 +6,7 @@ import tempfile import pwd from textwrap import dedent -from urllib.parse import urlparse, urlunparse +from urllib.parse import urlparse, urlunparse, urljoin def get_rstudio_executable(prog): @@ -40,8 +40,13 @@ def rewrite_netloc(response, request): for header, v in response.headers.get_all(): if header == "Location": u = urlparse(v) + redirect_path = u.path + if u.path.startswith("../"): + # R Help server sometimes responds with relative locations which + # need to be handled if changing the host part of the header. + redirect_path = urljoin(request.path, u.path) if u.netloc != request.host: - response.headers[header] = urlunparse(u._replace(netloc=request.host)) + response.headers[header] = urlunparse(u._replace(netloc=request.host, path=redirect_path)) def get_system_user(): try: