1
0
mirror of https://github.com/webrecorder/pywb.git synced 2025-03-24 06:59:52 +01:00

rewrite: better parametrize live rewrite timestamp cookie, restore LiveResourceException

This commit is contained in:
Ilya Kreymer 2014-08-04 18:41:33 -07:00
parent 86bc2f17ba
commit 72fe274e06
2 changed files with 21 additions and 10 deletions

View File

@ -1,5 +1,8 @@
var update_wb_url = push_state; var update_wb_url = push_state;
var LIVE_COOKIE_REGEX = /pywb.timestamp=([\d]{1,14})/;
function make_outer_url(url, ts) function make_outer_url(url, ts)
{ {
if (ts) { if (ts) {
@ -88,8 +91,7 @@ window.onpopstate = function(event) {
} }
function extract_ts_cookie(value) { function extract_ts_cookie(value) {
var regex = /pywb.timestamp=([\d]{1,14})/; var result = value.match(LIVE_COOKIE_REGEX);
var result = value.match(regex);
if (result) { if (result) {
return result[1]; return result[1];
} else { } else {
@ -114,10 +116,11 @@ function iframe_loaded(event) {
ts = iframe.wbinfo.timestamp; ts = iframe.wbinfo.timestamp;
is_live = iframe.wbinfo.is_live; is_live = iframe.wbinfo.is_live;
} else { } else {
ts = extract_ts(iframe.location.href); ts = extract_ts_cookie(iframe.document.cookie);
if (!ts) { if (ts) {
is_live = true; is_live = true;
ts = extract_ts_cookie(iframe.document.cookie); } else {
ts = extract_ts(iframe.location.href);
} }
} }
capture_str = _wb_js.ts_to_date(ts, true); capture_str = _wb_js.ts_to_date(ts, true);

View File

@ -19,6 +19,9 @@ class LiveResourceException(WbException):
#================================================================= #=================================================================
class RewriteHandler(SearchPageWbUrlHandler): class RewriteHandler(SearchPageWbUrlHandler):
LIVE_COOKIE = 'pywb.timestamp={0}; max-age=60';
def __init__(self, config): def __init__(self, config):
super(RewriteHandler, self).__init__(config) super(RewriteHandler, self).__init__(config)
@ -28,6 +31,8 @@ class RewriteHandler(SearchPageWbUrlHandler):
self.head_insert_view = HeadInsertView.init_from_config(config) self.head_insert_view = HeadInsertView.init_from_config(config)
self.live_cookie = config.get('live-cookie', self.LIVE_COOKIE)
def handle_request(self, wbrequest): def handle_request(self, wbrequest):
try: try:
return self.render_content(wbrequest) return self.render_content(wbrequest)
@ -35,8 +40,7 @@ class RewriteHandler(SearchPageWbUrlHandler):
except Exception as exc: except Exception as exc:
url = wbrequest.wb_url.url url = wbrequest.wb_url.url
msg = 'Could not load the url from the live web: ' + url msg = 'Could not load the url from the live web: ' + url
#raise LiveResourceException(msg=msg, url=url) raise LiveResourceException(msg=msg, url=url)
raise
def _live_request_headers(self, wbrequest): def _live_request_headers(self, wbrequest):
return {} return {}
@ -58,9 +62,13 @@ class RewriteHandler(SearchPageWbUrlHandler):
return self._make_response(wbrequest, *result) return self._make_response(wbrequest, *result)
def _make_response(self, wbrequest, status_headers, gen, is_rewritten): def _make_response(self, wbrequest, status_headers, gen, is_rewritten):
cdx = wbrequest.env['pywb.cdx'] # if cookie set, pass recorded timestamp info via cookie
cookie = 'pywb.timestamp=' + cdx['timestamp'] + '; max-age=60' # so that client side may be able to access it
status_headers.headers.append(('Set-Cookie', cookie)) # used by framed mode to update frame banner
if self.live_cookie:
cdx = wbrequest.env['pywb.cdx']
value = self.live_cookie.format(cdx['timestamp'])
status_headers.headers.append(('Set-Cookie', value))
return WbResponse(status_headers, gen) return WbResponse(status_headers, gen)