Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy mode: replay improvements for content not captured via proxy mode #520

Merged
merged 2 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions pywb/apps/rewriterapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ def render_content(self, wb_url, kwargs, environ):
'pywb.static_prefix', '/static/')
is_proxy = ('wsgiprox.proxy_host' in environ)

# if OPTIONS in proxy mode, just generate the proxy responss
if is_proxy and self.is_preflight(environ):
return WbResponse.options_response(environ)

environ['pywb.host_prefix'] = host_prefix

if self.use_js_obj_proxy:
Expand Down Expand Up @@ -551,6 +555,9 @@ def render_content(self, wb_url, kwargs, environ):

response = WbResponse(status_headers, gen)

if is_proxy and environ.get('HTTP_ORIGIN'):
response.add_access_control_headers(environ)

return response

def format_response(self, response, wb_url, full_prefix, is_timegate, is_proxy):
Expand Down Expand Up @@ -817,6 +824,19 @@ def is_ajax(self, environ):

return False

def is_preflight(self, environ):
if environ.get('REQUEST_METHOD') != 'OPTIONS':
return False

if not environ.get('HTTP_ORIGIN'):
return False

if not environ.get('HTTP_ACCESS_CONTROL_REQUEST_METHOD') and not environ.get('HTTP_ACCESS_CONTROL_REQUEST_HEADERS'):
return False

return True


def get_base_url(self, wb_url, kwargs):
type_ = kwargs.get('type')
return self.paths[type_].format(**kwargs)
Expand Down
8 changes: 4 additions & 4 deletions pywb/apps/wbrequestresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@ def add_access_control_headers(self, env=None):
allowed_methods = allowed_methods + ', ' + r_method
acr_headers = env.get('HTTP_ACCESS_CONTROL_REQUEST_HEADERS')
if acr_headers is not None:
self.status_headers.add_header('Access-Control-Allow-Headers', acr_headers)
self.status_headers.replace_header('Access-Control-Allow-Headers', acr_headers)
allowed_origin = env.get('HTTP_ORIGIN', env.get('HTTP_REFERER', allowed_origin))
if allowed_origin is None:
allowed_origin = '*'
self.status_headers.replace_header('Access-Control-Allow-Origin', allowed_origin)
self.status_headers.add_header('Access-Control-Allow-Methods', allowed_methods)
self.status_headers.add_header('Access-Control-Allow-Credentials', 'true')
self.status_headers.add_header('Access-Control-Max-Age', '1800')
self.status_headers.replace_header('Access-Control-Allow-Methods', allowed_methods)
self.status_headers.replace_header('Access-Control-Allow-Credentials', 'true')
self.status_headers.replace_header('Access-Control-Max-Age', '1800')
return self

def __repr__(self):
Expand Down
2 changes: 1 addition & 1 deletion pywb/static/wombatProxyMode.js

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions tests/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ def test_proxy_replay(self, scheme):
assert res.headers['Memento-Datetime'] == 'Mon, 27 Jan 2014 17:12:51 GMT'
assert 'Content-Location' not in res.headers

def test_proxy_replay_cors(self, scheme):
res = requests.get('{0}://example.com/'.format(scheme),
proxies=self.proxies,
verify=self.root_ca_file,
headers={'Origin': '{0}://api.example.com/'.format(scheme)})

assert 'Example Domain' in res.text

assert res.headers['Access-Control-Allow-Methods'] == 'GET, POST, PUT, OPTIONS, DELETE, PATCH, HEAD, TRACE, CONNECT'
assert res.headers['Access-Control-Allow-Credentials'] == 'true'
assert res.headers['Access-Control-Allow-Origin'] == '{0}://api.example.com/'.format(scheme)

assert 'Content-Location' not in res.headers

def test_proxy_replay_change_dt(self, scheme):
headers = {'Accept-Datetime': 'Mon, 26 Dec 2011 17:12:51 GMT'}
res = requests.get('{0}://example.com/'.format(scheme),
Expand Down
2 changes: 1 addition & 1 deletion wombat
Submodule wombat updated 1 files
+16 −0 src/wombatLite.js