aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlban Gruin2020-09-13 16:38:43 +0200
committerAlban Gruin2020-09-13 16:42:20 +0200
commit9da797daf063aa3234dbdfb6a1105f391c4c2bae (patch)
treec9e67acef8e29ea64ae3a421809b163eac5b83b0
parent69170ef4009cb7145f3a50999b898b9d2e8d7ee7 (diff)
ucs: add a helper to send quickly a 200 OK response
Signed-off-by: Alban Gruin <alban at pa1ch dot fr>
-rw-r--r--src/ucs.ml10
1 files changed, 7 insertions, 3 deletions
diff --git a/src/ucs.ml b/src/ucs.ml
index c9fcf95..eb99d95 100644
--- a/src/ucs.ml
+++ b/src/ucs.ml
@@ -36,6 +36,10 @@ let log req =
Printer.Calendar.sprint "%Y-%m-%d %H:%M:%S" @@ Calendar.now ()) in
Lwt_io.printlf "[%s] %s" datetime (Uri.path uri)
+let respond ?(mime="text/html; charset=utf-8") ?(status=`OK) body =
+ let headers = Header.init_with "Content-Type" mime in
+ Server.respond_string ~status ~body ~headers ()
+
let server =
let callback _conn req _body =
let meth = Request.meth req and
@@ -48,9 +52,9 @@ let server =
| `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 ->
- let body = Ics.to_string @@ Course.decode body and
- headers = Header.init_with "Content-Type" "text/calendar" in
- Server.respond_string ~status:`OK ~body ~headers ()
+ Course.decode body
+ |> Ics.to_string
+ |> respond ~mime:"text/calendar; charset=utf-8"
| `GET, _ ->
Server.respond_string ~status:`Not_found ~body:"Not found\n" ()
| _ ->