mirror of
https://github.com/gnosygnu/xowa.git
synced 2024-10-27 20:34:16 +00:00
HTTP Server: Parse links with trailing slash [#459]
This commit is contained in:
parent
c94090cd09
commit
4d9072830c
@ -40,30 +40,38 @@ class Http_url_parser {
|
||||
// Parse urls of form "/wiki_name/wiki/Page_name?action=val"
|
||||
public boolean Parse(byte[] url) {
|
||||
try {
|
||||
// initial validations
|
||||
// validate
|
||||
if (url == null) return Fail(null, "invalid url; url is null");
|
||||
int url_len = url.length;
|
||||
if (url_len == 0) return Fail(null, "invalid url; url is empty");
|
||||
if (url[0] != Byte_ascii.Slash) return Fail(url, "invalid url; must start with '/'");
|
||||
|
||||
// parse
|
||||
Gfo_url_parser url_parser = new Gfo_url_parser();
|
||||
Gfo_url url_obj = url_parser.Parse(url);
|
||||
this.wiki = url_obj.Segs()[0];
|
||||
byte[][] segs = url_obj.Segs();
|
||||
int segs_len = segs.length;
|
||||
|
||||
int segs_len = url_obj.Segs().length;
|
||||
if (segs_len > 1) {
|
||||
byte[] x = url_obj.Segs()[2];
|
||||
if (segs_len > 2) {
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
for (int i = 2; i < segs_len; i++) {
|
||||
if (i != 2) bfr.Add_byte_slash();
|
||||
bfr.Add(url_obj.Segs()[i]);
|
||||
}
|
||||
x = bfr.To_bry_and_clear();
|
||||
// get wiki
|
||||
if (segs_len == 0) return Fail(url, "invalid url; no wiki");
|
||||
this.wiki = segs[0];
|
||||
|
||||
// get page
|
||||
// only page; EX: wiki_name/wiki/Page
|
||||
if (segs_len == 3) {
|
||||
this.page = segs[2];
|
||||
}
|
||||
// page and subs; EX: wiki_name/wiki/Page/A/B/C
|
||||
else if (segs_len > 3) {
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
for (int i = 2; i < segs_len; i++) {
|
||||
if (i != 2) bfr.Add_byte_slash();
|
||||
bfr.Add(segs[i]);
|
||||
}
|
||||
this.page = x;
|
||||
this.page = bfr.To_bry_and_clear();
|
||||
}
|
||||
|
||||
// get qargs
|
||||
Gfo_qarg_mgr qarg_mgr = new Gfo_qarg_mgr().Init(url_obj.Qargs());
|
||||
byte[] action_val = qarg_mgr.Read_bry_or("action", Bry_.Empty);
|
||||
if (Bry_.Eq(action_val, Xoa_url_.Qarg__action__read))
|
||||
|
@ -23,6 +23,9 @@ public class Http_url_parser_tst {
|
||||
// wiki-only
|
||||
fxt.Test__parse("/en.wikipedia.org", fxt.Make().Wiki_("en.wikipedia.org"));
|
||||
|
||||
// wiki w/ trailing slash; ISSUE#:459 DATE:2019-05-11
|
||||
fxt.Test__parse("/en.wikipedia.org/", fxt.Make().Wiki_("en.wikipedia.org"));
|
||||
|
||||
// wiki + page
|
||||
fxt.Test__parse("/en.wikipedia.org/wiki/Page_1", fxt.Make().Wiki_("en.wikipedia.org").Page_("Page_1"));
|
||||
|
||||
@ -52,6 +55,7 @@ public class Http_url_parser_tst {
|
||||
|
||||
// fail: missing '/' at start
|
||||
fxt.Test__parse("en.wikipedia.org", fxt.Make().Err_msg_("invalid url; must start with '/'; url=en.wikipedia.org"));
|
||||
|
||||
/*
|
||||
// fail: missing '/wiki/'
|
||||
fxt.Test__parse("/en.wikipedia.org/Page_1", fxt.Make().Err_msg_("invalid url; must have '/wiki/' after wiki_domain; url=/en.wikipedia.org/Page_1"));
|
||||
|
@ -24,7 +24,7 @@ public class Xoh_file_wtr__djvu__tst {
|
||||
, " <div id=\"xowa_file_div_0\" class=\"thumbinner\" style=\"width:220px;\">"
|
||||
, " <a href=\"/wiki/File:A.djvu\" class=\"image\" xowa_title=\"A.djvu\"><img id=\"xoimg_0\" alt=\"\" src=\"file:///mem/wiki/repo/trg/thumb/7/6/A.djvu/220px-1.jpg\" width=\"0\" height=\"0\" /></a>"
|
||||
, " <div class=\"thumbcaption\">"
|
||||
, "<div class=\"magnify\"><a href=\"/wiki/File:A.djvu\" class=\"@gplx.Internal protected\" title=\"Enlarge\"></a></div>"
|
||||
, "<div class=\"magnify\"><a href=\"/wiki/File:A.djvu\" class=\"internal\" title=\"Enlarge\"></a></div>"
|
||||
, " </div>"
|
||||
, " </div>"
|
||||
, "</div>")
|
||||
|
Loading…
Reference in New Issue
Block a user