1
0
mirror of https://github.com/webrecorder/pywb.git synced 2025-03-15 00:03:28 +01:00

rewrite: disable refer-redirect in case of POST, handle request w/o redirect

(can't use 307 because of FF)
This commit is contained in:
Ilya Kreymer 2014-06-13 16:23:11 -07:00
parent dfef05a74d
commit 0d3f663ef1
2 changed files with 30 additions and 4 deletions

View File

@ -2,6 +2,7 @@ import urlparse
import re
from pywb.rewrite.url_rewriter import UrlRewriter
from pywb.rewrite.wburl import WbUrl
from wbrequestresponse import WbRequest, WbResponse
@ -161,9 +162,13 @@ class ReferRedirect:
path = path[len(app_path):]
ref_route = None
ref_request = None
for route in routes:
ref_request = route.parse_request(env, False, request_uri=path)
if ref_request:
ref_route = route
break
# must have matched one of the routes
@ -186,10 +191,23 @@ class ReferRedirect:
# 2013/path.html -> /path.html
rel_request_uri = rel_request_uri[len(timestamp_path) - 1:]
rewritten_url = rewriter.rewrite(rel_request_uri)
# if post, can't redirect as that would lost the post data
# (can't use 307 because FF will show confirmation warning)
if ref_request.method == 'POST':
new_wb_url = WbUrl(rewritten_url[len(rewriter.prefix):])
ref_request.wb_url.url = new_wb_url.url
print 'REL URI ', ref_request.wb_url.url
return ref_route.handler(ref_request)
final_url = urlparse.urlunsplit((ref_split.scheme,
ref_split.netloc,
rewriter.rewrite(rel_request_uri),
rewritten_url,
'',
''))
return WbResponse.redir_response(final_url, status='307 Temp Redirect')
return WbResponse.redir_response(final_url, status='302 Temp Redirect')

View File

@ -188,12 +188,12 @@ class TestWb:
# without timestamp
resp = self.testapp.get('/_css/2013.1/screen.css', headers = [('Referer', 'http://localhost:8080/pywb/2014/http://iana.org/')])
assert resp.status_int == 307
assert resp.status_int == 302
assert resp.headers['Location'] == target, resp.headers['Location']
# with timestamp
resp = self.testapp.get('/2014/_css/2013.1/screen.css', headers = [('Referer', 'http://localhost:8080/pywb/2014/http://iana.org/')])
assert resp.status_int == 307
assert resp.status_int == 302
assert resp.headers['Location'] == target, resp.headers['Location']
@ -240,6 +240,14 @@ class TestWb:
assert resp.status_int == 200
assert '"data": "^"' in resp.body
def test_post_redirect(self):
# post handled without redirect (since 307 not allowed)
resp = self.testapp.post('/post', {'foo': 'bar', 'test': 'abc'}, headers=[('Referer', 'http://localhost:8080/pywb/2014/http://httpbin.org/post')])
assert resp.status_int == 200
assert '"foo": "bar"' in resp.body
assert '"test": "abc"' in resp.body
def test_excluded_content(self):
resp = self.testapp.get('/pywb/http://www.iana.org/_img/bookmark_icon.ico', status = 403)
assert resp.status_int == 403