Skip to content

Commit a2a2a05

Browse files
committed
rewrite: rewrite rel urls to rel urls, both server and wombat side #123
1 parent 43716f5 commit a2a2a05

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

pywb/rewrite/test/test_url_rewriter.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
2222
# UrlRewriter tests
2323
>>> do_rewrite('other.html', '20131010/http://example.com/path/page.html', 'https://web.archive.org/web/')
24-
'https://web.archive.org/web/20131010/http://example.com/path/other.html'
24+
'/web/20131010/http://example.com/path/other.html'
2525
2626
>>> do_rewrite('file.js', '20131010/http://example.com/path/page.html', 'https://web.archive.org/web/', 'js_')
27-
'https://web.archive.org/web/20131010js_/http://example.com/path/file.js'
27+
'/web/20131010js_/http://example.com/path/file.js'
2828
2929
>>> do_rewrite('file.js', '20131010/http://example.com/', '/coll/')
3030
'/coll/20131010/http://example.com/file.js'
@@ -35,6 +35,9 @@
3535
>>> do_rewrite('file.js', '20131010/http://example.com', '/coll/', '')
3636
'/coll/20131010/http://example.com/file.js'
3737
38+
>>> do_rewrite('/other.html', '20130907*/http://example.com/path/page.html', 'http://localhost:8080/coll/')
39+
'/coll/20130907*/http://example.com/other.html'
40+
3841
>>> do_rewrite('/other.html', '20130907*/http://example.com/path/page.html', '/coll/')
3942
'/coll/20130907*/http://example.com/other.html'
4043

pywb/rewrite/url_rewriter.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def __init__(self, wburl, prefix, full_prefix=None, rel_prefix=None,
3030
self.prefix_scheme = self.full_prefix.split(':')[0]
3131
else:
3232
self.prefix_scheme = None
33+
self.prefix_abs = self.prefix and self.prefix.startswith(self.PROTOCOLS)
3334
self.cookie_scope = cookie_scope
3435
self.rewrite_opts = rewrite_opts
3536

@@ -74,8 +75,15 @@ def rewrite(self, url, mod=None):
7475
mod = wburl.mod
7576

7677
final_url = self.prefix + wburl.to_str(mod=mod, url=new_url)
78+
79+
if not is_abs and self.prefix_abs and not self.rewrite_opts.get('no_match_rel'):
80+
parts = final_url.split('/', 3)
81+
final_url = '/'
82+
if len(parts) == 4:
83+
final_url += parts[3]
84+
7785
# experiment for setting scheme rel url
78-
if scheme_rel and self.prefix.startswith(self.PROTOCOLS):
86+
elif scheme_rel and self.prefix_abs:
7987
final_url = final_url.split(':', 1)[1]
8088

8189
return final_url

pywb/static/wombat.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ var wombat_internal = function($wbwindow) {
143143
var URL_PROPS = ["href", "hash", "pathname", "host", "hostname", "protocol", "origin", "search", "port"];
144144

145145
//============================================
146-
function rewrite_url_(url) {
146+
function rewrite_url_(url, use_rel) {
147147
// If undefined, just return it
148148
if (!url) {
149149
return url;
@@ -215,7 +215,7 @@ var wombat_internal = function($wbwindow) {
215215
return url;
216216
}
217217

218-
return wb_replay_date_prefix + wb_orig_origin + url;
218+
return (use_rel ? wb_coll_prefix : wb_replay_date_prefix) + wb_orig_origin + url;
219219
}
220220

221221
// If full url starting with http://, https:// or //
@@ -235,7 +235,6 @@ var wombat_internal = function($wbwindow) {
235235
}
236236

237237
var curr_scheme = orig_protocol + '//';
238-
var host = orig_host + '/';
239238
var path = url.substring(prefix_host.length);
240239
var rebuild = false;
241240

@@ -251,7 +250,12 @@ var wombat_internal = function($wbwindow) {
251250
}
252251

253252
if (rebuild) {
254-
url = curr_scheme + host + path;
253+
if (!use_rel) {
254+
url = curr_scheme + orig_host;
255+
} else {
256+
url = "";
257+
}
258+
url += "/" + path;
255259
}
256260

257261
return url;
@@ -451,7 +455,8 @@ var wombat_internal = function($wbwindow) {
451455
value = this._parser[prop];
452456
} else {
453457
prop = "href";
454-
value = rewrite_url(this._parser.href);
458+
var rel = (value == this._parser.pathname);
459+
value = rewrite_url(this._parser.href, rel);
455460
}
456461

457462
orig_setter.call(this, prop, value);

0 commit comments

Comments
 (0)