1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00
This commit is contained in:
gnosygnu
2015-08-17 02:09:16 -04:00
parent 34c34f227c
commit df10db140c
421 changed files with 4867 additions and 2429 deletions

View File

@@ -21,4 +21,9 @@ public class Json_itm_ {
public static final byte Tid_unknown = 0, Tid_null = 1, Tid_bool = 2, Tid_int = 3, Tid_decimal = 4, Tid_string = 5, Tid_kv = 6, Tid_array = 7, Tid_nde = 8;
public static final byte[][] Names = Bry_.Ary("unknown", "null", "boolean", "int", "decimal", "string", "keyval", "array", "nde");
public static final byte[] Const_true = Bry_.new_a7("true"), Const_false = Bry_.new_a7("false"), Const_null = Bry_.new_a7("null");
public static byte[] To_bry(Bry_bfr bfr, Json_itm itm) {
if (itm == null) return Bry_.Empty;
itm.Print_as_json(bfr, 0);
return bfr.Xto_bry_and_clear();
}
}

View File

@@ -21,6 +21,8 @@ public class Json_kv extends Json_itm_base {
@Override public byte Tid() {return Json_itm_.Tid_kv;}
public Json_itm Key() {return key;} Json_itm key;
public Json_itm Val() {return val;} Json_itm val;
public Json_nde Val_as_nde() {return Json_nde.cast_(val);}
public Json_ary Val_as_ary() {return Json_ary.cast(val);}
public byte[] Key_as_bry() {return key.Data_bry();}
public String Key_as_str() {return (String)key.Data();}
public byte[] Val_as_bry() {return val.Data_bry();}

View File

@@ -31,6 +31,7 @@ public class Json_nde extends Json_itm_base implements Json_grp {
return rv;
}
public Json_itm Get_at(int i) {return subs[i];}
public Json_kv Get_kv(byte[] key) {return Json_kv.cast_(Get_itm(key));}
public Json_nde Get(String key) {return Get(Bry_.new_u8(key));}
public Json_nde Get(byte[] key) {
Json_kv kv = Json_kv.cast_(this.Get_itm(key)); if (kv == null) throw Err_.new_("json", "kv not found", "key", key);
@@ -48,6 +49,7 @@ public class Json_nde extends Json_itm_base implements Json_grp {
}
return null;
}
public boolean Has(byte[] key) {return Get_bry(key, null) != null;}
public byte[] Get_bry(byte[] key) {
byte[] rv = Get_bry(key, null); if (rv == null) throw Err_.new_("json", "key missing", "key", key);
return rv;
@@ -60,8 +62,7 @@ public class Json_nde extends Json_itm_base implements Json_grp {
if (kv_obj.Tid() != Json_itm_.Tid_kv) return or; // key is not a key_val
Json_kv kv = (Json_kv)kv_obj;
Json_itm val = kv.Val();
if (val == null) return or;
return val.Data_bry();
return (val == null) ? or : val.Data_bry();
}
public Json_nde Add_many(Json_itm... ary) {
int len = ary.length;

View File

@@ -71,7 +71,7 @@ public class Json_parser {
case Byte_ascii.Brack_bgn: return Make_ary(doc);
case Byte_ascii.Curly_bgn: return Make_nde(doc);
}
throw Err_.new_unhandled(Char_.XtoStr(b));
throw Err_.new_unhandled(Char_.To_str(b));
}
throw Err_.new_wo_type("eos reached in val");
}
@@ -157,7 +157,7 @@ public class Json_parser {
if (src[pos] == expd)
++pos;
else
throw err_(src, pos, "expected '{0}' but got '{1}'", Char_.XtoStr(expd), Char_.XtoStr(src[pos]));
throw err_(src, pos, "expected '{0}' but got '{1}'", Char_.To_str(expd), Char_.To_str(src[pos]));
}
Err err_(byte[] src, int bgn, String fmt, Object... args) {return err_(src, bgn, src.length, fmt, args);}
Err err_(byte[] src, int bgn, int src_len, String fmt, Object... args) {

View File

@@ -0,0 +1,55 @@
/*
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.core.logs; import gplx.*; import gplx.core.*;
public class Gfo_log_fil {
private final Bry_bfr fil_bfr = Bry_bfr.new_(), msg_bfr = Bry_bfr.new_();
private final String key;
private final Io_url dir;
private final long size_max;
private int file_idx;
private Io_url fil_cur;
private final Gfo_log_fmtr fmtr = new Gfo_log_fmtr();
private final Gfo_log_fil session;
public Gfo_log_fil(Gfo_log_fil session, String key, Io_url dir, long size_max) {
this.session = session;
this.key = key;
this.dir = dir;
this.size_max = size_max;
this.fil_cur = Fil_new();
}
public void Add(String msg, Object... vals) {
fmtr.Add(msg_bfr, msg, vals);
Add_by_bfr(msg_bfr);
msg_bfr.Clear();
}
public void Add_by_bfr(Bry_bfr msg_bfr) {
if (msg_bfr.Len() + fil_bfr.Len() > size_max) {
this.Flush();
fil_cur = Fil_new();
}
fil_bfr.Add_bfr_and_preserve(msg_bfr);
if (session != null) session.Add_by_bfr(msg_bfr);
}
public void Flush() {
Io_mgr.I.AppendFilBfr(fil_cur, fil_bfr);
}
private Io_url Fil_new() {
String part = size_max == -1 ? "" : "-" + Int_.Xto_str(++file_idx);
return dir.OwnerDir().GenSubFil_ary(key, part, ".log");
}
}

View File

@@ -0,0 +1,58 @@
/*
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.core.logs; import gplx.*; import gplx.core.*;
import gplx.core.btries.*;
class Gfo_log_fmtr {
private final Btrie_fast_mgr trie = Btrie_fast_mgr.cs()
.Add("~|" , Bry_.new_a7("~|<"))
.Add("|" , Bry_.new_a7("~||"))
.Add("\n" , Bry_.new_a7("~|n"))
.Add("\t" , Bry_.new_a7("~|t"))
;
public void Add(Bry_bfr bfr, String msg, Object... vals) {
Add_bry(bfr, Bry_.new_u8(msg));
int len = vals.length;
for (int i = 0; i < len; ++i) {
bfr.Add_byte(Byte_ascii.Pipe);
byte[] val_bry = Bry_.new_u8(Object_.Xto_str_strict_or_empty(vals[i]));
Add_bry(bfr, val_bry);
}
bfr.Add_byte_nl();
}
private void Add_bry(Bry_bfr bfr, byte[] src) {
if (src == null) return;
int len = src.length; if (len == 0) return;
int pos = 0;
int add_bgn = -1;
while (true) {
if (pos == len) break;
byte b = src[pos];
Object o = trie.Match_bgn_w_byte(b, src, pos, len);
if (o == null) {
if (add_bgn == -1) add_bgn = pos;
}
else {
if (add_bgn != -1) bfr.Add_mid(src, add_bgn, pos);
byte[] repl = (byte[])o;
bfr.Add(repl);
pos = trie.Match_pos();
}
}
if (add_bgn != -1) bfr.Add_mid(src, add_bgn, len);
}
}

View File

@@ -0,0 +1,55 @@
/*
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.core.logs; import gplx.*; import gplx.core.*;
public class Gfo_log_mgr {
private final Ordered_hash fil_list = Ordered_hash_.new_();
private final Gfo_log_fil session_fil;
private final Io_url dir;
private final long size_dflt = Io_mgr.Len_mb * 2;
public Gfo_log_mgr(Io_url dir) {
this.dir = dir;
this.session_fil = new Gfo_log_fil(null, "session", dir, -1);
}
public Gfo_log_fil Fils__get_or_new(String key) {
Gfo_log_fil rv = (Gfo_log_fil)fil_list.Get_by(key);
if (rv == null) {
rv = new Gfo_log_fil(session_fil, key, dir, size_dflt);
fil_list.Add(key, rv);
}
return rv;
}
public void Msgs__add(String fil_key, String msg, Object... vals) {
Gfo_log_fil fil = (Gfo_log_fil)Fils__get_or_new(fil_key);
fil.Add(msg, vals);
}
}
// class Some_log_cls {
// private final Gfo_log_mgr log_mgr = new Gfo_log_mgr();
// public void Init() {
// }
// private Gfo_log_fil dedicated;
// public void Init_dedicated() {
// dedicated = log_mgr.Fil_get_or_new("parse");
// }
// public void Proc_w_dedicate() {
// dedicated.Add("file download failed", "url", "msg");
// }
// public void Proc_dynamic() {
// log_mgr.Msg_add("parse", "file download failed", "url", "msg");
// }
// }

View File

@@ -57,7 +57,7 @@ public class Http_request_parser {
if (Bry_.Has_at_end(line, Tkn_content_type_boundary_end)) break; // last form_data pair will end with "--"; stop
line = Parse_content_type_boundary(rdr);
}
break; // assume form_data ends POST request
break; // assume form_data sends POST request
}
Object o = trie.Match_bgn(line, 0, line_len);
if (o == null) {
@@ -101,7 +101,8 @@ public class Http_request_parser {
this.protocol = Bry_.Mid(line, url_end + 1, line_len);
}
private void Parse_content_type(int val_bgn, byte[] line, int line_len) { // EX: Content-Type: multipart/form-data; boundary=---------------------------72432484930026
int boundary_bgn = Bry_finder.Find_fwd(line, Tkn_boundary, val_bgn, line_len); if (boundary_bgn == Bry_finder.Not_found) throw Err_.new_wo_type("invalid content_type", "line", line, "request", To_str());
// handle wolfram and other clients; DATE:2015-08-03
int boundary_bgn = Bry_finder.Find_fwd(line, Tkn_boundary, val_bgn, line_len); if (boundary_bgn == Bry_finder.Not_found) return; // PURPOSE: ignore content-type for GET calls like by Mathematica server; DATE:2015-08-04 // throw Err_.new_wo_type("invalid content_type", "line", line, "request", To_str());
int content_type_end = Bry_finder.Find_bwd(line, Byte_ascii.Semic, boundary_bgn);
this.content_type = Bry_.Mid(line, val_bgn, content_type_end);
this.content_type_boundary = Bry_.Add(Tkn_content_type_boundary_end, Bry_.Mid(line, boundary_bgn += Tkn_boundary.length, line_len));

View File

@@ -25,6 +25,9 @@ public class Http_request_parser_tst {
@Test public void Type_content_type() {
fxt.Test_content_type("Content-Type: multipart/form-data; boundary=---------------------------72432484930026", "multipart/form-data", "-----------------------------72432484930026");
}
@Test public void Type_content_type__x_www_form_url_encoded() { // PURPOSE: ignore content-type for GET calls like by Mathematica server; DATE:2015-08-04
fxt.Test_content_type("Content-Type: application/x-www-form-urlencoded", null, null);
}
@Test public void Type_form_data() {
fxt.Test_form_data(String_.Ary
( "POST /url HTTP/1.1"