Skip to content

Commit

Permalink
More consistent env var setting / static path fix (#688)
Browse files Browse the repository at this point in the history
* template/custom env var fix:
- ensure pywb.host_prefix, pywb.app_prefix and pywb.static_prefix set for all requests via prepare_env()
- ensure X-Forwarded-Proto is accounted for in pywb.host_prefix
- call prepare_env() in handle_request(), and also in rewriterapp (in case using a different front-end app).

* update wombat to 3.3.6 (includes partial fix for #684)
* bump version to 2.6.3
  • Loading branch information
ikreymer authored Dec 23, 2021
1 parent 5c35a43 commit c97a667
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pywb/apps/frontendapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,8 @@ def handle_request(self, environ, start_response):
urls = self.url_map.bind_to_environ(environ)
try:
endpoint, args = urls.match()
# store original script_name (original prefix) before modifications are made
environ['pywb.app_prefix'] = environ.get('SCRIPT_NAME', '')

self.rewriterapp.prepare_env(environ)

# store original script_name (original prefix) before modifications are made
environ['ORIG_SCRIPT_NAME'] = environ.get('SCRIPT_NAME')
Expand Down
25 changes: 17 additions & 8 deletions pywb/apps/rewriterapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,24 @@ def send_redirect(self, new_path, url_parts, urlrewriter):

return resp

def render_content(self, wb_url, kwargs, environ):
wb_url = wb_url.replace('#', '%23')
wb_url = WbUrl(wb_url)
def prepare_env(self, environ):
""" setup environ path prefixes and scheme """
if 'pywb.host_prefix' in environ:
return

proto = environ.get('HTTP_X_FORWARDED_PROTO', self.force_scheme)

if proto:
environ['wsgi.url_scheme'] = proto

environ['pywb.host_prefix'] = self.get_host_prefix(environ)
environ['pywb.app_prefix'] = environ.get('SCRIPT_NAME', '')
environ['pywb.static_prefix'] = environ['pywb.host_prefix'] + environ['pywb.app_prefix'] + '/' + self.static_prefix

def render_content(self, wb_url, kwargs, environ):
wb_url = wb_url.replace('#', '%23')
wb_url = WbUrl(wb_url)

history_page = environ.pop('HTTP_X_WOMBAT_HISTORY_PAGE', '')
if history_page:
wb_url.url = history_page
Expand All @@ -321,13 +330,13 @@ def render_content(self, wb_url, kwargs, environ):

is_timegate = self._check_accept_dt(wb_url, environ)

host_prefix = self.get_host_prefix(environ)
self.prepare_env(environ)

host_prefix = environ['pywb.host_prefix']
rel_prefix = self.get_rel_prefix(environ)
full_prefix = host_prefix + rel_prefix
environ['pywb.host_prefix'] = host_prefix
pywb_static_prefix = host_prefix + environ.get('pywb.app_prefix', '') + '/' + self.static_prefix
environ['pywb.static_prefix'] = pywb_static_prefix
pywb_static_prefix += '/'

pywb_static_prefix = environ['pywb.static_prefix'] + '/'
is_proxy = ('wsgiprox.proxy_host' in environ)

# if OPTIONS in proxy mode, just generate the proxy responss
Expand Down
2 changes: 1 addition & 1 deletion pywb/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '2.6.2'
__version__ = '2.6.3'

if __name__ == '__main__':
print(__version__)
15 changes: 14 additions & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,25 @@ def setup_class(cls):
def test_home(self):
resp = self.testapp.get('/')
self._assert_basic_html(resp)
assert '<link rel="stylesheet" href="http://localhost:80/static/css/base.css"' in resp.text
assert '/pywb' in resp.text

def test_home_custom_prefix(self):
resp = self.testapp.get('/', extra_environ={'SCRIPT_NAME': '/wayback'})
self._assert_basic_html(resp)
assert '<link rel="stylesheet" href="http://localhost:80/wayback/static/css/base.css"' in resp.text
assert '/pywb' in resp.text

def test_pywb_root(self):
resp = self.testapp.get('/pywb/')
self._assert_basic_html(resp)
assert '<link rel="stylesheet" href="/static/css/base.css"' in resp.text
assert '<link rel="stylesheet" href="http://localhost:80/static/css/base.css"' in resp.text
assert 'Search' in resp.text

def test_pywb_root_custom_prefix(self):
resp = self.testapp.get('/pywb/', extra_environ={'SCRIPT_NAME': '/wayback'})
self._assert_basic_html(resp)
assert '<link rel="stylesheet" href="http://localhost:80/wayback/static/css/base.css"' in resp.text
assert 'Search' in resp.text

def test_pywb_root_head(self):
Expand Down
2 changes: 1 addition & 1 deletion wombat

0 comments on commit c97a667

Please sign in to comment.