mirror of
https://github.com/gnosygnu/xowa.git
synced 2025-05-30 14:04:56 +00:00
HTTP Server: Redirect xwiki urls to different wikis [#600]
This commit is contained in:
parent
79bdab7946
commit
d908d4f8b5
@ -29,11 +29,10 @@ GPLv3 License: LICENSE-GPLv3.txt
|
||||
Apache License: LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.apps.servers.http; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*; import gplx.xowa.apps.servers.*;
|
||||
import gplx.core.threads.*; import gplx.core.net.*; import gplx.core.primitives.*; import gplx.core.envs.*;
|
||||
import gplx.core.threads.*; import gplx.core.net.*; import gplx.core.primitives.*;
|
||||
import gplx.langs.jsons.*; import gplx.langs.htmls.encoders.*;
|
||||
import gplx.xowa.wikis.pages.*;
|
||||
import gplx.xowa.addons.wikis.searchs.gui.htmlbars.*;
|
||||
import gplx.xowa.specials.*; import gplx.xowa.specials.xowa.errors.*;
|
||||
public class Http_server_mgr implements Gfo_invk {
|
||||
private final Object thread_lock = new Object();
|
||||
private final Gfo_usr_dlg usr_dlg;
|
||||
@ -96,73 +95,9 @@ public class Http_server_mgr implements Gfo_invk {
|
||||
String cmd = url_converter.Decode_str(url_encoded_str);
|
||||
app.Gfs_mgr().Run_str(cmd);
|
||||
}
|
||||
public String Parse_page_to_html(Http_data__client data__client, byte[] wiki_domain, byte[] ttl_bry, byte[] qarg, byte mode, boolean popup_enabled, String popup_mode, String popup_id) {
|
||||
public Http_server_page Parse_page_to_html(Http_data__client data__client, byte[] wiki_domain, byte[] ttl_bry, byte[] qarg, byte mode, boolean popup_enabled, String popup_mode, String popup_id) {
|
||||
synchronized (thread_lock) {
|
||||
// get the wiki
|
||||
Xowe_wiki wiki = (Xowe_wiki)app.Wiki_mgr().Get_by_or_make_init_y(wiki_domain); // assert init for Main_Page; EX:click zh.w on wiki sidebar; DATE:2015-07-19
|
||||
if (Runtime_.Memory_total() > Io_mgr.Len_gb) Xowe_wiki_.Rls_mem(wiki, true); // release memory at 1 GB; DATE:2015-09-11
|
||||
|
||||
// get the url / ttl
|
||||
// empty title returns main page; EX: "" -> "Main_Page"
|
||||
if (Bry_.Len_eq_0(ttl_bry))
|
||||
ttl_bry = wiki.Props().Main_page();
|
||||
// generate ttl of domain/wiki/page; needed for pages with leading slash; EX: "/abcd" -> "en.wikipedia.org/wiki//abcd"; ISSUE#:301; DATE:2018-12-16
|
||||
else {
|
||||
Bry_bfr tmp_bfr = wiki.Utl__bfr_mkr().Get_m001();
|
||||
try {
|
||||
tmp_bfr.Add(wiki.Domain_bry()).Add(gplx.xowa.htmls.hrefs.Xoh_href_.Bry__wiki).Add(ttl_bry).Add_safe(qarg);
|
||||
ttl_bry = tmp_bfr.To_bry_and_clear();
|
||||
} finally {tmp_bfr.Mkr_rls();}
|
||||
}
|
||||
Xoa_url url = wiki.Utl__url_parser().Parse(ttl_bry);
|
||||
Xoa_ttl ttl = wiki.Ttl_parse(url.To_bry_page_w_anch()); // changed from ttl_bry to page_w_anch; DATE:2017-07-24
|
||||
|
||||
// handle invalid titles like "Earth]"; ISSUE#:480; DATE:2019-06-02
|
||||
if (ttl == null) {
|
||||
ttl = wiki.Ttl_parse(Xow_special_meta_.Itm__error.Ttl_bry());
|
||||
url = wiki.Utl__url_parser().Parse(Xoerror_special.Make_url__invalidTitle(ttl_bry));
|
||||
}
|
||||
|
||||
// get the page
|
||||
gplx.xowa.guis.views.Xog_tab_itm tab = Gxw_html_server.Assert_tab2(app, wiki); // HACK: assert tab exists
|
||||
Xoae_page page = wiki.Page_mgr().Load_page(url, ttl, tab);
|
||||
app.Gui_mgr().Browser_win().Active_page_(page); // HACK: init gui_mgr's page for output (which server ordinarily doesn't need)
|
||||
if (page.Db().Page().Exists_n()) { // if page does not exist, replace with message; else null_ref error; DATE:2014-03-08
|
||||
page.Db().Text().Text_bry_(Bry_.new_a7("'''Page not found.'''"));
|
||||
wiki.Parser_mgr().Parse(page, false);
|
||||
}
|
||||
page.Html_data().Head_mgr().Itm__server().Init_by_http(data__client).Enabled_y_();
|
||||
|
||||
// generate html
|
||||
String rv = null;
|
||||
if (popup_enabled) {
|
||||
if (String_.Eq(popup_mode, "more"))
|
||||
rv = wiki.Html_mgr().Head_mgr().Popup_mgr().Show_more(popup_id);
|
||||
else
|
||||
rv = wiki.Html_mgr().Head_mgr().Popup_mgr().Show_init(popup_id, ttl_bry, ttl_bry);
|
||||
}
|
||||
else {
|
||||
byte[] page_html = wiki.Html_mgr().Page_wtr_mgr().Gen(page, mode);
|
||||
|
||||
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) {
|
||||
case File_retrieve_mode.Mode_skip: // noop
|
||||
break;
|
||||
case File_retrieve_mode.Mode_async_server:
|
||||
rebuild_html = true;
|
||||
app.Gui_mgr().Browser_win().Page__async__bgn(tab);
|
||||
break;
|
||||
case File_retrieve_mode.Mode_wait:
|
||||
rebuild_html = true;
|
||||
gplx.xowa.guis.views.Xog_async_wkr.Async(page, tab.Html_itm());
|
||||
page = wiki.Page_mgr().Load_page(url, ttl, tab); // HACK: fetch page again so that HTML will now include img data
|
||||
break;
|
||||
}
|
||||
if (rebuild_html) rv = String_.new_u8(wiki.Html_mgr().Page_wtr_mgr().Gen(page, mode));
|
||||
}
|
||||
return rv;
|
||||
return Http_server_page.Make(app, data__client, wiki_domain, ttl_bry, qarg, retrieve_mode, mode, popup_enabled, popup_mode, popup_id);
|
||||
}
|
||||
}
|
||||
private void Note(String s) {
|
||||
|
@ -32,12 +32,12 @@ public class Http_server_page {
|
||||
public byte[] Redirect() {return redirect;} private byte[] redirect;
|
||||
public static Http_server_page Make(Xoae_app app, Http_data__client data__client, byte[] wiki_domain, byte[] ttl_bry, byte[] qarg, byte retrieve_mode, byte mode, boolean popup_enabled, String popup_mode, String popup_id) {
|
||||
Http_server_page page = new Http_server_page(app);
|
||||
page.Make_url(wiki_domain, ttl_bry, qarg);
|
||||
if (!page.Make_url(wiki_domain, ttl_bry, qarg)) return page; // exit early if xwiki
|
||||
page.Make_page(data__client);
|
||||
page.Make_html(retrieve_mode, mode, popup_enabled, popup_mode, popup_id);
|
||||
return page;
|
||||
}
|
||||
public void Make_url(byte[] wiki_domain, byte[] ttl_bry_arg, byte[] qarg) {
|
||||
public boolean Make_url(byte[] wiki_domain, byte[] ttl_bry_arg, byte[] qarg) {
|
||||
// get wiki
|
||||
wiki = (Xowe_wiki)app.Wiki_mgr().Get_by_or_make_init_y(wiki_domain); // assert init for Main_Page; EX:click zh.w on wiki sidebar; DATE:2015-07-19
|
||||
if (Runtime_.Memory_total() > Io_mgr.Len_gb) Xowe_wiki_.Rls_mem(wiki, true); // release memory at 1 GB; DATE:2015-09-11
|
||||
@ -60,8 +60,8 @@ public class Http_server_page {
|
||||
// get url
|
||||
this.url = wiki.Utl__url_parser().Parse(ttl_bry);
|
||||
if (!Bry_.Eq(url.Wiki_bry(), wiki.Domain_bry())) { // handle xwiki; EX: en.wikipedia.org/wiki/it:Roma; ISSUE#:600; DATE:2019-11-02
|
||||
this.wiki = app.Wiki_mgr().Get_by_or_make(url.Wiki_bry());
|
||||
this.redirect = url.To_bry();
|
||||
return false;
|
||||
}
|
||||
|
||||
// get ttl
|
||||
@ -70,6 +70,7 @@ public class Http_server_page {
|
||||
this.ttl = wiki.Ttl_parse(Xow_special_meta_.Itm__error.Ttl_bry());
|
||||
this.url = wiki.Utl__url_parser().Parse(Xoerror_special.Make_url__invalidTitle(ttl_bry));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public void Make_page(Http_data__client data__client) {
|
||||
// get the page
|
||||
@ -99,7 +100,7 @@ public class Http_server_page {
|
||||
switch (retrieve_mode) {
|
||||
case File_retrieve_mode.Mode_skip: // noop
|
||||
break;
|
||||
case File_retrieve_mode.Mode_async_server:
|
||||
case File_retrieve_mode.Mode_async_server:
|
||||
rebuild_html = true;
|
||||
app.Gui_mgr().Browser_win().Page__async__bgn(tab);
|
||||
break;
|
||||
|
@ -19,9 +19,8 @@ public class Http_server_page__tst {
|
||||
private final Http_server_page__fxt fxt = new Http_server_page__fxt();
|
||||
@Test public void Xwiki() {
|
||||
fxt.Init__xwiki("en.wikipedia.org", "it", "it.wikipedia.org");
|
||||
fxt.Exec__Make_url("en.wikipedia.org", "it:Roma", "");
|
||||
fxt.Test__wiki_domain("it.wikipedia.org");
|
||||
fxt.Test__url("it.wikipedia.org/wiki/Roma");
|
||||
fxt.Test__make_url(false, "en.wikipedia.org", "it:Roma", "");
|
||||
fxt.Test__redirect("it.wikipedia.org/wiki/Roma");
|
||||
}
|
||||
}
|
||||
class Http_server_page__fxt {
|
||||
@ -35,13 +34,11 @@ class Http_server_page__fxt {
|
||||
Xowe_wiki xwiki_wiki = fxt.App().Wiki_mgr().Get_by_or_make(Bry_.new_u8(wiki));
|
||||
xwiki_wiki.Xwiki_mgr().Add_by_atrs(abrv, domain);
|
||||
}
|
||||
public void Exec__Make_url(String wiki_domain, String ttl_bry_arg, String qarg) {
|
||||
page.Make_url(Bry_.new_u8(wiki_domain), Bry_.new_u8(ttl_bry_arg), Bry_.new_u8(qarg));
|
||||
public void Test__make_url(boolean expd, String wiki_domain, String ttl_bry_arg, String qarg) {
|
||||
boolean actl = page.Make_url(Bry_.new_u8(wiki_domain), Bry_.new_u8(ttl_bry_arg), Bry_.new_u8(qarg));
|
||||
Gftest.Eq__bool(expd, actl);
|
||||
}
|
||||
public void Test__url(String expd) {
|
||||
Gftest.Eq__str(expd, page.Url().To_str());
|
||||
}
|
||||
public void Test__wiki_domain(String expd) {
|
||||
Gftest.Eq__str(expd, page.Wiki().Domain_bry());
|
||||
public void Test__redirect(String expd) {
|
||||
Gftest.Eq__str(expd, page.Redirect());
|
||||
}
|
||||
}
|
||||
|
@ -105,8 +105,15 @@ public class Http_server_wkr implements Gfo_invk {
|
||||
page_html = url_parser.Err_msg();
|
||||
}
|
||||
else {
|
||||
page_html = app.Http_server().Parse_page_to_html(data__client, url_parser.Wiki(), url_parser.Page(), url_parser.Qarg(), url_parser.Action(), url_parser.Popup(), url_parser.Popup_mode(), url_parser.Popup_id());
|
||||
page_html = Convert_page(page_html, root_dir_http, String_.new_u8(url_parser.Wiki()));
|
||||
Http_server_page page = app.Http_server().Parse_page_to_html(data__client, url_parser.Wiki(), url_parser.Page(), url_parser.Qarg(), url_parser.Action(), url_parser.Popup(), url_parser.Popup_mode(), url_parser.Popup_id());
|
||||
if (page.Redirect() != null) {
|
||||
Xosrv_http_wkr_.Write_redirect(client_wtr, page.Redirect());
|
||||
return;
|
||||
}
|
||||
else {
|
||||
page_html = page.Html();
|
||||
page_html = Convert_page(page_html, root_dir_http, String_.new_u8(url_parser.Wiki()));
|
||||
}
|
||||
}
|
||||
Xosrv_http_wkr_.Write_response_as_html(client_wtr, Bool_.N, page_html);
|
||||
}
|
||||
@ -175,10 +182,27 @@ class Xosrv_http_wkr_ {
|
||||
} catch (Exception err) {
|
||||
client_wtr.Write_str("Site not found. Check address please, or see console log.\n" + Err_.Message_lang(err));
|
||||
client_wtr.Rls();
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void Write_redirect(Http_client_wtr client_wtr, byte[] redirect) {
|
||||
try{
|
||||
client_wtr.Write_bry
|
||||
( Bry_.Add
|
||||
( Rsp__http_redirect
|
||||
, Rsp__location
|
||||
, redirect
|
||||
, Byte_ascii.Nl_bry
|
||||
)
|
||||
);
|
||||
} catch (Exception err) {
|
||||
client_wtr.Write_str("Redirect failed. Check address please, or see console log.\n" + Err_.Message_lang(err));
|
||||
client_wtr.Rls();
|
||||
}
|
||||
}
|
||||
public static final byte[]
|
||||
Rsp__http_ok = Bry_.new_a7("HTTP/1.1 200 OK:\n")
|
||||
, Rsp__content_type_html = Bry_.new_a7("Content-Type: text/html; charset=utf-8\n")
|
||||
, Rsp__http_redirect = Bry_.new_a7("HTTP/1.1 302 Found:\n")
|
||||
, Rsp__location = Bry_.new_a7("Location: /") // "/" to start from root
|
||||
;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user