From 2241454988c31521c1f4ff6029399a999396897b Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Sun, 13 Sep 2020 16:41:04 +0200 Subject: ucs: add URLs to the HTML pages, read parameters in the URL Signed-off-by: Alban Gruin --- src/ucs.ml | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/ucs.ml b/src/ucs.ml index eb99d95..cda7bcc 100644 --- a/src/ucs.ml +++ b/src/ucs.ml @@ -43,19 +43,26 @@ let respond ?(mime="text/html; charset=utf-8") ?(status=`OK) body = let server = let callback _conn req _body = let meth = Request.meth req and - uri = Request.uri req - |> Uri.path - |> Astring.String.cuts ~empty:false ~sep:"/" - |> List.map Uri.pct_decode in + uri = Request.uri req in + let path = Uri.path uri + |> Astring.String.cuts ~empty:false ~sep:"/" + |> List.map Uri.pct_decode and + query = Uri.query uri in log req >>= fun () -> - match meth, uri with - | `GET, ["ics"; file] when Astring.String.is_suffix ~affix:".ics" file -> + match meth, path, query with + | `GET, ([] | ["index.html"]), [] -> respond Pages.main + | `GET, ["lnk"], ["group", [group]] -> + let lnk = "/ics/" ^ group ^ ".ics" in + respond @@ Pages.link lnk + | `GET, ["lnk"], _ -> + Server.respond_string ~status:`Bad_request ~body:"Bad request\n" () + | `GET, ["ics"; file], [] when Astring.String.is_suffix ~affix:".ics" file -> let group = String.(sub file 0 (length file - 4)) in fetch group >>= fun body -> Course.decode body |> Ics.to_string |> respond ~mime:"text/calendar; charset=utf-8" - | `GET, _ -> + | `GET, _, _ -> Server.respond_string ~status:`Not_found ~body:"Not found\n" () | _ -> Server.respond_string -- cgit v1.2.1