1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

'v3.6.3.4'

This commit is contained in:
gnosygnu
2016-06-22 15:55:05 -04:00
parent 04af0accdb
commit 8afc115176
27 changed files with 535 additions and 519 deletions

View File

@@ -0,0 +1,50 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.apps.servers.http; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*; import gplx.xowa.apps.servers.*;
import gplx.xowa.files.*;
class Http_file_utl {
public static byte[] To_mime_type_by_path_as_bry(byte[] path_bry) {
int dot_pos = Bry_find_.Find_bwd(path_bry, Byte_ascii.Dot);
return dot_pos == Bry_find_.Not_found ? Mime_octet_stream : To_mime_type_by_ext_as_bry(path_bry, dot_pos, path_bry.length);
}
public static byte[] To_mime_type_by_ext_as_bry(byte[] ext_bry, int bgn, int end) {
Object o = mime_hash.Get_by_mid(ext_bry, bgn, end);
return o == null ? Mime_octet_stream : (byte[])o;
}
private static final byte[]
Mime_octet_stream = Xof_ext_.Mime_type__ary[Xof_ext_.Id_unknown]
, Mime_html = Bry_.new_a7("text/html")
, Mime_css = Bry_.new_a7("text/css")
, Mime_js = Bry_.new_a7("application/javascript")
;
private static final Hash_adp_bry mime_hash = Mime_hash__new();
private static Hash_adp_bry Mime_hash__new() {
Hash_adp_bry rv = Hash_adp_bry.ci_a7();
int len = Xof_ext_.Id__max;
for (int i = 0; i < len; ++i) {
rv.Add_bry_obj
( Bry_.Add(Byte_ascii.Dot, Xof_ext_.Bry__ary[i])
, Xof_ext_.Mime_type__ary[i]);
}
rv.Add_str_obj(".htm" , Mime_html);
rv.Add_str_obj(".html" , Mime_html);
rv.Add_str_obj(".css" , Mime_css);
rv.Add_str_obj(".js" , Mime_js);
return rv;
}
}

View File

@@ -51,9 +51,9 @@ class Http_server_socket implements Gfo_invk {
wkr_pool.Add(wkr_uid);
// server_wtr.Write_str_w_nl("added new worker; uid=" + wkr_uid);
}
Http_server_wkr_v2 wkr = new Http_server_wkr_v2(server_mgr, wkr_uid);
Http_server_wkr wkr = new Http_server_wkr(server_mgr, wkr_uid);
wkr.Init_by_thread(client_socket);
Thread_adp_.Start_by_key("thread:xowa.http_server.client", wkr, Http_server_wkr_v2.Invk_run);
Thread_adp_.Start_by_key("thread:xowa.http_server.client", wkr, Http_server_wkr.Invk_run);
}
}
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {

View File

@@ -20,7 +20,7 @@ import gplx.core.ios.*; import gplx.core.ios.streams.*;
import gplx.core.primitives.*; import gplx.core.net.*; import gplx.langs.htmls.encoders.*;
import gplx.xowa.apps.*;
import gplx.xowa.htmls.js.*;
class Http_server_wkr_v2 implements Gfo_invk {
class Http_server_wkr implements Gfo_invk {
private final int uid;
private final Http_server_mgr server_mgr;
private final Http_server_wtr server_wtr;
@@ -34,7 +34,7 @@ class Http_server_wkr_v2 implements Gfo_invk {
private final Bry_bfr tmp_bfr = Bry_bfr_.New_w_size(64);
private Socket_adp socket;
private Http_data__client data__client;
public Http_server_wkr_v2(Http_server_mgr server_mgr, int uid){
public Http_server_wkr(Http_server_mgr server_mgr, int uid){
this.server_mgr = server_mgr; this.uid = uid;
this.app = server_mgr.App(); this.server_wtr = server_mgr.Server_wtr(); this.url_encoder = server_mgr.Encoder();
this.root_dir_http = app.Fsys_mgr().Root_dir().To_http_file_str();
@@ -84,6 +84,7 @@ class Http_server_wkr_v2 implements Gfo_invk {
int url_end = question_pos == Bry_find_.Not_found ? url.length : question_pos; // ignore files with query params; EX: /file/A.png?key=val
url_encoder.Decode(tmp_bfr, Bool_.N, url, url_bgn, url_end); // decode url to actual chars; note that XOWA stores on fsys in UTF-8 chars; "<EFBFBD>" not "%C3"
byte[] path = tmp_bfr.To_bry_and_clear();
if (gplx.core.envs.Op_sys.Cur().Tid_is_wnt()) path = Bry_.Replace(path, Byte_ascii.Backslash, Byte_ascii.Slash);
client_wtr.Write_bry(Xosrv_http_wkr_.Rsp__http_ok);
// client_wtr.Write_str("Expires: Sun, 17-Jan-2038 19:14:07 GMT\n");
String mime_type = String_.new_u8(Http_file_utl.To_mime_type_by_path_as_bry(path));
@@ -179,33 +180,3 @@ class Xosrv_http_wkr_ {
, Rsp__content_type_html = Bry_.new_a7("Content-Type: text/html; charset=utf-8\n")
;
}
class Http_file_utl {
public static byte[] To_mime_type_by_path_as_bry(byte[] path_bry) {
int dot_pos = Bry_find_.Find_bwd(path_bry, Byte_ascii.Dot);
return dot_pos == Bry_find_.Not_found ? Mime_octet_stream : To_mime_type_by_ext_as_bry(path_bry, dot_pos, path_bry.length);
}
public static byte[] To_mime_type_by_ext_as_bry(byte[] ext_bry, int bgn, int end) {
Object o = mime_hash.Get_by_mid(ext_bry, bgn, end);
return o == null ? Mime_octet_stream : (byte[])o;
}
private static final byte[]
Mime_html = Bry_.new_a7("text/html")
, Mime_jpg = Bry_.new_a7("image/jpeg")
, Mime_png = Bry_.new_a7("image/png")
, Mime_gif = Bry_.new_a7("image/gif")
, Mime_svg = Bry_.new_a7("image/svg+xml")
, Mime_css = Bry_.new_a7("text/css")
, Mime_js = Bry_.new_a7("application/javascript")
, Mime_octet_stream = Bry_.new_a7("application/octet-stream")
;
private static final Hash_adp_bry mime_hash = Hash_adp_bry.ci_a7()
.Add_str_obj(".htm" , Mime_html)
.Add_str_obj(".html" , Mime_html)
.Add_str_obj(".jpg" , Mime_jpg)
.Add_str_obj(".png" , Mime_png)
.Add_str_obj(".gif" , Mime_gif)
.Add_str_obj(".svg" , Mime_svg)
.Add_str_obj(".css" , Mime_css)
.Add_str_obj(".js" , Mime_js)
;
}

View File

@@ -1,174 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.apps.servers.http; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*; import gplx.xowa.apps.servers.*;
import gplx.core.envs.Op_sys;
import gplx.core.net.*; import gplx.core.threads.*; import gplx.langs.htmls.encoders.*;
import java.io.*;
import java.net.*;
import java.nio.charset.Charset;
class Http_server_wkr_v1 implements Runnable{
private static final String CRLF = "\r\n";
private Socket socket;
private Xoae_app app;
private String app_root_dir;
public Http_server_wkr_v1(Socket socket, Xoae_app app){
this.socket = socket;
this.app = app;
this.app_root_dir = app.Fsys_mgr().Root_dir().To_http_file_str();
}
public void run(){
String err_line = "00";
try {
InputStream is = socket.getInputStream(); err_line = "01";
DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); err_line = "02";
BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); err_line = "03";
String request = br.readLine(); err_line = "04";
//System.out.println(request); err_line = "03";
String req = request.substring(4, request.length() - 9).trim(); err_line = "05";
String wiki_domain = "home"; err_line = "06";
String page_name = "Main_Page"; err_line = "07";
if(!req.contains("%file%")){
if(req.equals("/")) { // no page; EX:"localhost:8080" vs "localhost:8080/en.wikipedia.org/wiki/Earth"
String home_url = String_.new_u8(app.Http_server().Home()); err_line = "08";
if (String_.Has_at_bgn(home_url, "file://")) {
Io_url file_url = Io_url_.http_any_(home_url, Op_sys.Cur().Tid_is_wnt()); err_line = "09";
String page_html = Io_mgr.Instance.LoadFilStr(file_url); err_line = "10";
Write_page(dos, page_html, app_root_dir, wiki_domain); err_line = "11";
}
else {
req += app.Http_server().Home(); err_line = "12";
}
}
req = Http_server_wkr_.Assert_main_page(app, req); err_line = "13";
}
if(req.contains("%xowa-cmd%") || req.contains("/xowa-cmd:")){
System.out.println("Command output:"); err_line = "14";
String cmd = ""; err_line = "15";
if (req.contains("%xowa-cmd%"))
cmd = req.substring(req.indexOf("%xowa-cmd%")+20);
else
cmd = req.substring(req.indexOf("/xowa-cmd:")+10);
System.out.println(cmd); err_line = "15";
app.Http_server().Run_xowa_cmd(app, cmd); err_line = "16";
dos.writeBytes("Command sent, see console log for more details."); err_line = "17";
dos.close(); err_line = "18";
}
else if(req.contains("%file%")){
String path = req.replace("/%file%/", app_root_dir); err_line = "19";
path = path.substring(path.indexOf(app_root_dir)+5); err_line = "20";
Gfo_url_encoder url_converter = Gfo_url_encoder_.Http_url; err_line = "21";
path = url_converter.Decode_str(path); err_line = "22";
if(path.contains("?")){
path = path.substring(0, path.indexOf("?")); err_line = "23";
}
FileInputStream fis = new FileInputStream(path); err_line = "24";
dos.writeBytes("HTTP/1.1 200 OK: "); err_line = "25";
dos.writeBytes("Content-Type: " + contentType(path) + CRLF); err_line = "26";
dos.writeBytes(CRLF); err_line = "27";
sendBytes(fis, dos); err_line = "28";
fis.close(); err_line = "28";
dos.close(); err_line = "29";
br.close(); err_line = "30";
socket.close(); err_line = "31";
}else{
String[] req_split = req.split("/"); err_line = "32";
System.out.println("Request: " +request); err_line = "33";
if(req_split.length >= 1){
wiki_domain = req_split[1]; err_line = "34";
}
if(req_split.length >= 4){
page_name = req_split[3]; err_line = "35";
for(int i = 4; i <= req_split.length-1; i++){
page_name += "/"+req_split[i]; err_line = "36";
}
Gfo_url_encoder url_converter = Gfo_url_encoder_.Http_url; err_line = "37";
page_name = url_converter.Decode_str(page_name); err_line = "38";
//page_name = app.Url_converter_url().Decode_str(page_name);
}
try{
// String page_html = app.Http_server().Parse_page_to_html(app, wiki_domain, page_name); err_line = "39";
// Write_page(dos, page_html, app_root_dir, wiki_domain); err_line = "40";
}catch(Exception err) {
dos.writeBytes("Site not found. Check address please, or see console log.\n"+err.getMessage()); err_line = "41";
dos.close(); err_line = "42";
}
}
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
String err_msg = String_.Format("error retrieving page. Please make sure your url is of the form: http://localhost:8080/home/wiki/Main_Page; err_msg={0} err_line={1}", e.toString(), err_line);
System.out.println(err_msg);
e.printStackTrace();
}
}
private static void Write_page(DataOutputStream strm, String page_html, String app_file_dir, String wiki_domain) {
page_html = Convert_page(page_html, app_file_dir, wiki_domain);
Write_to_stream(strm, page_html);
}
private static String Convert_page(String page_html, String app_file_dir, String wiki_domain) {
page_html = page_html.replaceAll(app_file_dir , "%file%/");
page_html = page_html.replaceAll("xowa-cmd" , "%xowa-cmd%/xowa-cmd");
page_html = page_html.replaceAll("<a href=\"/wiki/" , "<a href=\"/"+wiki_domain+"/wiki/");
page_html = page_html.replaceAll("<a href='/wiki/" , "<a href='/"+wiki_domain+"/wiki/");
page_html = page_html.replaceAll("action=\"/wiki/" , "action=\"/"+wiki_domain+"/wiki/");
page_html = page_html.replaceAll("/site" , "");
return page_html;
}
private static void Write_to_stream(DataOutputStream strm, String page_html) {
try{
strm.writeBytes("HTTP/1.1 200 OK: ");
strm.writeBytes("Content-Type: text/html; charset=utf-8" + CRLF);
// strm.writeBytes("Access-Control-Allow-Origin: *" + CRLF); // No 'Access-Control-Allow-Origin' header is present on the requested resource.
strm.writeBytes(CRLF);
strm.write(page_html.getBytes(Charset.forName("UTF-8")));
strm.close();
} catch (Exception err) {
try {
strm.writeBytes("Site not found. Check address please, or see console log.\n"+err.getMessage());
strm.close();
}
catch (Exception io_err) {}
}
}
private void sendBytes(FileInputStream fis, DataOutputStream dos) {
byte[] buffer = new byte[1024];
int bytes = 0;
try {
while((bytes= fis.read(buffer)) != -1){
dos.write(buffer, 0, bytes);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private String contentType(String fileName) {
if(fileName.endsWith(".htm") || fileName.endsWith(".html"))
return "text/html";
if(fileName.endsWith(".jpg"))
return "text/jpg";
if(fileName.endsWith(".gif"))
return "text/gif";
return "application/octet-stream";
}
}