1
0
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:
gnosygnu 2019-05-11 23:45:51 -04:00
parent c94090cd09
commit 4d9072830c
3 changed files with 26 additions and 14 deletions

View File

@ -40,30 +40,38 @@ class Http_url_parser {
// Parse urls of form "/wiki_name/wiki/Page_name?action=val" // Parse urls of form "/wiki_name/wiki/Page_name?action=val"
public boolean Parse(byte[] url) { public boolean Parse(byte[] url) {
try { try {
// initial validations // validate
if (url == null) return Fail(null, "invalid url; url is null"); if (url == null) return Fail(null, "invalid url; url is null");
int url_len = url.length; int url_len = url.length;
if (url_len == 0) return Fail(null, "invalid url; url is empty"); 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 '/'"); 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_parser url_parser = new Gfo_url_parser();
Gfo_url url_obj = url_parser.Parse(url); 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; // get wiki
if (segs_len > 1) { if (segs_len == 0) return Fail(url, "invalid url; no wiki");
byte[] x = url_obj.Segs()[2]; this.wiki = segs[0];
if (segs_len > 2) {
Bry_bfr bfr = Bry_bfr_.New(); // get page
for (int i = 2; i < segs_len; i++) { // only page; EX: wiki_name/wiki/Page
if (i != 2) bfr.Add_byte_slash(); if (segs_len == 3) {
bfr.Add(url_obj.Segs()[i]); this.page = segs[2];
} }
x = bfr.To_bry_and_clear(); // 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()); Gfo_qarg_mgr qarg_mgr = new Gfo_qarg_mgr().Init(url_obj.Qargs());
byte[] action_val = qarg_mgr.Read_bry_or("action", Bry_.Empty); byte[] action_val = qarg_mgr.Read_bry_or("action", Bry_.Empty);
if (Bry_.Eq(action_val, Xoa_url_.Qarg__action__read)) if (Bry_.Eq(action_val, Xoa_url_.Qarg__action__read))

View File

@ -23,6 +23,9 @@ public class Http_url_parser_tst {
// wiki-only // wiki-only
fxt.Test__parse("/en.wikipedia.org", fxt.Make().Wiki_("en.wikipedia.org")); 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 // wiki + page
fxt.Test__parse("/en.wikipedia.org/wiki/Page_1", fxt.Make().Wiki_("en.wikipedia.org").Page_("Page_1")); 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 // fail: missing '/' at start
fxt.Test__parse("en.wikipedia.org", fxt.Make().Err_msg_("invalid url; must start with '/'; url=en.wikipedia.org")); fxt.Test__parse("en.wikipedia.org", fxt.Make().Err_msg_("invalid url; must start with '/'; url=en.wikipedia.org"));
/* /*
// fail: missing '/wiki/' // 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")); 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"));

View File

@ -24,7 +24,7 @@ public class Xoh_file_wtr__djvu__tst {
, " <div id=\"xowa_file_div_0\" class=\"thumbinner\" style=\"width:220px;\">" , " <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>" , " <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=\"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>" , " </div>"
, "</div>") , "</div>")