mirror of
https://github.com/webrecorder/pywb.git
synced 2025-03-24 06:59:52 +01:00
config: add support for forcing a scheme for url rewriting, eg: 'force_scheme: https', fixes #314
This commit is contained in:
parent
4f58111875
commit
3101e567f3
@ -103,6 +103,8 @@ class RewriterApp(object):
|
|||||||
else:
|
else:
|
||||||
self.csp_header = None
|
self.csp_header = None
|
||||||
|
|
||||||
|
self.force_scheme = config.get('force_scheme')
|
||||||
|
|
||||||
def add_csp_header(self, wb_url, status_headers):
|
def add_csp_header(self, wb_url, status_headers):
|
||||||
if self.csp_header and wb_url.mod == self.replay_mod:
|
if self.csp_header and wb_url.mod == self.replay_mod:
|
||||||
status_headers.headers.append(self.csp_header)
|
status_headers.headers.append(self.csp_header)
|
||||||
@ -202,6 +204,10 @@ class RewriterApp(object):
|
|||||||
def render_content(self, wb_url, kwargs, environ):
|
def render_content(self, wb_url, kwargs, environ):
|
||||||
wb_url = wb_url.replace('#', '%23')
|
wb_url = wb_url.replace('#', '%23')
|
||||||
wb_url = WbUrl(wb_url)
|
wb_url = WbUrl(wb_url)
|
||||||
|
|
||||||
|
if self.force_scheme:
|
||||||
|
environ['wsgi.url_scheme'] = self.force_scheme
|
||||||
|
|
||||||
is_timegate = self._check_accept_dt(wb_url, environ)
|
is_timegate = self._check_accept_dt(wb_url, environ)
|
||||||
|
|
||||||
host_prefix = self.get_host_prefix(environ)
|
host_prefix = self.get_host_prefix(environ)
|
||||||
|
48
tests/test_force_https.py
Normal file
48
tests/test_force_https.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
from .base_config_test import BaseConfigTest, fmod
|
||||||
|
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
class TestForceHttps(BaseConfigTest):
|
||||||
|
@classmethod
|
||||||
|
def setup_class(cls):
|
||||||
|
super(TestForceHttps, cls).setup_class('config_test.yaml',
|
||||||
|
custom_config={'force_scheme': 'https'})
|
||||||
|
|
||||||
|
def test_force_https_replay_1(self, fmod):
|
||||||
|
resp = self.get('/pywb/20140128051539{0}/http://example.com/', fmod)
|
||||||
|
|
||||||
|
assert '"https://localhost:80/pywb/20140128051539{0}/http://www.iana.org/domains/example"'.format(fmod) in resp.text, resp.text
|
||||||
|
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
class TestForceHttpsRedirect(BaseConfigTest):
|
||||||
|
@classmethod
|
||||||
|
def setup_class(cls):
|
||||||
|
super(TestForceHttpsRedirect, cls).setup_class('config_test_redirect_classic.yaml',
|
||||||
|
custom_config={'force_scheme': 'https'})
|
||||||
|
|
||||||
|
def test_force_https_redirect_replay_1(self, fmod):
|
||||||
|
resp = self.get('/pywb/20140128051539{0}/http://example.com/', fmod)
|
||||||
|
|
||||||
|
assert resp.headers['Location'] == 'https://localhost:80/pywb/20140127171251{0}/http://example.com'.format(fmod)
|
||||||
|
resp = resp.follow()
|
||||||
|
|
||||||
|
assert resp.headers['Location'] == 'https://localhost:80/pywb/20140127171251{0}/http://example.com/'.format(fmod)
|
||||||
|
resp = resp.follow()
|
||||||
|
|
||||||
|
assert '"https://localhost:80/pywb/20140127171251{0}/http://www.iana.org/domains/example"'.format(fmod) in resp.text, resp.text
|
||||||
|
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
class TestForceHttpsRoot(BaseConfigTest):
|
||||||
|
@classmethod
|
||||||
|
def setup_class(cls):
|
||||||
|
super(TestForceHttpsRoot, cls).setup_class('config_test_root_coll.yaml',
|
||||||
|
custom_config={'force_scheme': 'https'})
|
||||||
|
|
||||||
|
def test_force_https_root_replay_1(self, fmod):
|
||||||
|
resp = self.get('/20140128051539{0}/http://www.iana.org/domains/example', fmod)
|
||||||
|
|
||||||
|
assert resp.headers['Location'] == 'https://localhost:80/20140128051539{0}/https://www.iana.org/domains/reserved'.format(fmod)
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user