mirror of
https://github.com/gnosygnu/xowa.git
synced 2024-10-27 20:34:16 +00:00
HTTP Server: Generalize file:///root logic even more [#524]
This commit is contained in:
parent
1e254caa79
commit
5ac949e8c0
@ -978,6 +978,43 @@ public class Bry_ {
|
||||
}
|
||||
return bfr.To_bry_and_clear();
|
||||
}
|
||||
public static byte[] Replace_many(byte[] src, byte[] find, byte[] repl) {
|
||||
Bry_bfr bfr = null;
|
||||
int src_len = src.length;
|
||||
int find_len = find.length;
|
||||
|
||||
int pos = 0;
|
||||
while (true) {
|
||||
// find find_bgn
|
||||
int find_bgn = Bry_find_.Find_fwd(src, find, pos, src_len);
|
||||
|
||||
// exit if nothing found
|
||||
if (find_bgn == Bry_find_.Not_found)
|
||||
break;
|
||||
|
||||
// lazy-instantiation
|
||||
if (bfr == null)
|
||||
bfr = Bry_bfr_.New();
|
||||
|
||||
// add everything up to find_bgn
|
||||
bfr.Add_mid(src, pos, find_bgn);
|
||||
|
||||
// add repl
|
||||
bfr.Add(repl);
|
||||
|
||||
// move pos forward
|
||||
pos = find_bgn + find_len;
|
||||
}
|
||||
|
||||
// nothing found; return src
|
||||
if (bfr == null)
|
||||
return src;
|
||||
else {
|
||||
// add rest
|
||||
bfr.Add_mid(src, pos, src_len);
|
||||
return bfr.To_bry_and_clear();
|
||||
}
|
||||
}
|
||||
public static int Trim_end_pos(byte[] src, int end) {
|
||||
for (int i = end - 1; i > -1; i--) {
|
||||
switch (src[i]) {
|
||||
|
@ -143,7 +143,8 @@ public class Http_server_mgr implements Gfo_invk {
|
||||
}
|
||||
else {
|
||||
byte[] page_html = wiki.Html_mgr().Page_wtr_mgr().Gen(page, mode);
|
||||
page_html = Http_server_wkr.Replace_fsys_hack(page_html);
|
||||
|
||||
page_html = Bry_.Replace_many(page_html, app.Fsys_mgr().Root_dir().To_http_file_bry(), Http_server_wkr.Url__fsys);
|
||||
rv = String_.new_u8(page_html); // NOTE: must generate HTML now in order for "wait" and "async_server" to work with text_dbs; DATE:2016-07-10
|
||||
boolean rebuild_html = false;
|
||||
switch (retrieve_mode) {
|
||||
|
@ -145,89 +145,6 @@ public class Http_server_wkr implements Gfo_invk {
|
||||
return page_html;
|
||||
}
|
||||
|
||||
private static final byte[]
|
||||
Bry__file_lhs = Bry_.new_a7("file:")
|
||||
, Bry__file_mid = Bry_.new_a7("/file/")
|
||||
, Bry__file_fsys = Bry_.new_a7("/fsys")
|
||||
;
|
||||
|
||||
public static byte[] Replace_fsys_hack(byte[] html_bry) {
|
||||
// init
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
int len = html_bry.length;
|
||||
int pos = 0;
|
||||
|
||||
// loop while finding "file:.*/file/"
|
||||
while (true) {
|
||||
// find "file:"
|
||||
int lhs_bgn = Bry_find_.Find_fwd(html_bry, Bry__file_lhs, pos);
|
||||
|
||||
// exit if nothing found
|
||||
if (lhs_bgn == Bry_find_.Not_found)
|
||||
break;
|
||||
|
||||
// set lhs_end (after "file:")
|
||||
int lhs_end = lhs_bgn + Bry__file_lhs.length;
|
||||
|
||||
// skip if page literally starts with "file:"
|
||||
if (lhs_bgn == 0) {
|
||||
bfr.Add_mid(html_bry, pos, lhs_end);
|
||||
pos = lhs_end;
|
||||
continue;
|
||||
}
|
||||
|
||||
// get quote char before "file:"
|
||||
int quote_bgn = lhs_bgn - 1;
|
||||
byte quote = html_bry[quote_bgn];
|
||||
|
||||
// skip if no quote found
|
||||
if (quote != Byte_ascii.Apos && quote != Byte_ascii.Quote) {
|
||||
bfr.Add_mid(html_bry, pos, lhs_end);
|
||||
pos = lhs_end;
|
||||
continue;
|
||||
}
|
||||
|
||||
// find end quote
|
||||
int quote_end = Bry_find_.Find_fwd(html_bry, quote, lhs_end);
|
||||
|
||||
// exit if no end quote
|
||||
if (quote_end == Bry_find_.Not_found)
|
||||
break;
|
||||
|
||||
// skip if "'file: ... '" is too long. should be no more than 300
|
||||
if (quote_end - lhs_end > 300) {
|
||||
bfr.Add_mid(html_bry, pos, quote_end);
|
||||
pos = quote_end;
|
||||
continue;
|
||||
}
|
||||
|
||||
// find "/file/"
|
||||
int mid_bgn = Bry_find_.Find_fwd(html_bry, Bry__file_mid, lhs_bgn, quote_end);
|
||||
|
||||
// skip if no "/file/"
|
||||
if (mid_bgn == Bry_find_.Not_found) {
|
||||
bfr.Add_mid(html_bry, pos, quote_end);
|
||||
pos = quote_end;
|
||||
continue;
|
||||
}
|
||||
|
||||
// add everything up to "file:"
|
||||
bfr.Add_mid(html_bry, pos, lhs_bgn);
|
||||
|
||||
// add "/fsys/"
|
||||
bfr.Add(Bry__file_fsys);
|
||||
|
||||
// add everything up to quote
|
||||
bfr.Add_mid(html_bry, mid_bgn, quote_end);
|
||||
|
||||
// move pos forward
|
||||
pos = quote_end;
|
||||
}
|
||||
|
||||
// add rest
|
||||
bfr.Add_mid(html_bry, pos, len);
|
||||
return bfr.To_bry_and_clear();
|
||||
}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk_run)) {this.Run();}
|
||||
else return Gfo_invk_.Rv_unhandled;
|
||||
|
@ -20,32 +20,24 @@ public class Http_server_wkr_tst {
|
||||
@Test public void File_bgn_missing() { // "file:" missing
|
||||
fxt.Test__Replace_fsys_hack("src='file////home/lnxusr/xowa/file/'");
|
||||
}
|
||||
@Test public void File_bgn_at_bos() { // "file:" at start of page
|
||||
fxt.Test__Replace_fsys_hack("file:////home/lnxusr/xowa/file/");
|
||||
}
|
||||
@Test public void Quote_bgn_missing() {
|
||||
fxt.Test__Replace_fsys_hack("(file:////home/lnxusr/xowa/file/)");
|
||||
}
|
||||
@Test public void Quote_end_missing() {
|
||||
fxt.Test__Replace_fsys_hack("a'file:////home/lnxusr/xowa/file/");
|
||||
}
|
||||
@Test public void Too_long() { // skip if too long
|
||||
fxt.Test__Replace_fsys_hack("'file:" + String_.Repeat("a", 301) + "'");
|
||||
}
|
||||
@Test public void File_mid_missing() { // skip if no /file/
|
||||
fxt.Test__Replace_fsys_hack("'file:////home/lnxusr/xowa/file_missing/'");
|
||||
}
|
||||
@Test public void One() {
|
||||
fxt.Test__Replace_fsys_hack("'file:////home/lnxusr/xowa/file/A.png'", "'/fsys/file/A.png'");
|
||||
}
|
||||
@Test public void Many() {
|
||||
fxt.Test__Replace_fsys_hack("a 'file:////home/lnxusr/xowa/file/A.png' b \"file:////home/lnxusr/xowa/file/B.png\" c", "a '/fsys/file/A.png' b \"/fsys/file/B.png\" c");
|
||||
}
|
||||
@Test public void Non_file() {
|
||||
fxt.Test__Replace_fsys_hack("a file:////home/lnxusr/xowa/bin/any/xowa/file/app.window/app_icon.png b", "a /fsys/bin/any/xowa/file/app.window/app_icon.png b");
|
||||
}
|
||||
@Test public void Url() {
|
||||
fxt.Test__Replace_fsys_hack("url(file:////home/lnxusr/xowa/anonymous/wiki/www.wikidata.org/html/logo.png)", "url(/fsys/anonymous/wiki/www.wikidata.org/html/logo.png)");
|
||||
}
|
||||
}
|
||||
class Http_server_wkr_fxt {
|
||||
private static final byte[] root_dir_http = Bry_.new_a7("file:////home/lnxusr/xowa/");
|
||||
public void Test__Replace_fsys_hack(String html) {Test__Replace_fsys_hack(html, html);}
|
||||
public void Test__Replace_fsys_hack(String html, String expd) {
|
||||
byte[] actl = Http_server_wkr.Replace_fsys_hack(Bry_.new_u8(html));
|
||||
byte[] actl = Bry_.Replace_many(Bry_.new_u8(html), root_dir_http, Http_server_wkr.Url__fsys);
|
||||
Gftest.Eq__ary__lines(expd, actl);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user