ojudge

A self-hosted competitive programming site written in OCaml
git clone git://git.laack.co/ojudge.git
Log | Files | Refs | README

commit 0625449dbdad33d75ea06bfc810f6acfef8aa1a1
parent 92b9c51ee0877e2b96207e39bf8db351ad8effb5
Author: Andrew Laack <andrew@laack.co>
Date:   Sun, 15 Nov 2026 15:14:50 -0600

Add the ability to submit code + username, which is basically just parsing at present

Diffstat:
MMakefile | 1+
Mojudge/bin/dune | 5+++++
Aojudge/bin/submit.ml | 109+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 115 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -3,6 +3,7 @@ ocaml: cd ojudge && dune build cp ./ojudge/_build/install/default/bin/home cgi/ cp ./ojudge/_build/install/default/bin/problem cgi/ + cp ./ojudge/_build/install/default/bin/submit cgi/ ensure-config: id http # required for lighttpd diff --git a/ojudge/bin/dune b/ojudge/bin/dune @@ -7,3 +7,8 @@ (public_name problem) (name problem) (libraries ojudge unix)) + +(executable + (public_name submit) + (name submit) + (libraries ojudge unix)) diff --git a/ojudge/bin/submit.ml b/ojudge/bin/submit.ml @@ -0,0 +1,109 @@ +let isAlpha ch = + let num = int_of_char ch in + num >= (int_of_char 'a') && num <= (int_of_char 'z') + +let hexa_val conf = + match conf with + | '0'..'9' -> Char.code conf - Char.code '0' + | 'a'..'f' -> Char.code conf - Char.code 'a' + 10 + | 'A'..'F' -> Char.code conf - Char.code 'A' + 10 + | _ -> 0 + +let raw_decode s = + let rec need_decode i = + if i < Bytes.length s then + match Bytes.get s i with + | '%' | '+' -> true + | _ -> need_decode (succ i) + else false + in + let rec compute_len i i1 = + if i < Bytes.length s then + let i = + match Bytes.get s i with + | '%' when i + 2 < Bytes.length s -> i + 3 + | _ -> succ i + in + compute_len i (succ i1) + else i1 + in + let rec copy_decode_in s1 i i1 = + if i < Bytes.length s then + let i = + match Bytes.get s i with + | '%' when i + 2 < Bytes.length s -> + let v = hexa_val (Bytes.get s (i + 1)) * 16 + hexa_val (Bytes.get s (i + 2)) in + Bytes.set s1 i1 (Char.chr v); i + 3 + | '+' -> Bytes.set s1 i1 ' '; succ i + | x -> Bytes.set s1 i1 x; succ i + in + copy_decode_in s1 i (succ i1) + else s1 + in + if need_decode 0 then + let len = compute_len 0 0 in + let s1 = Bytes.create len in + copy_decode_in s1 0 0 + else + s + + +let printHTMLPreamble () = + print_endline "Content-type: text/html\n\n"; + print_endline "<html>" + +let printHTMLEnding () = + print_endline "</html>" + +let printBody () = + print_endline "<p>Submit your code here.</p>"; + print_endline "<form action=\"\" method=\"POST\">"; + print_endline " <label for=\"username\">username:</label><br>"; + print_endline " <input type=\"text\" id=\"username\" name=\"username\" value=\"\"><br>"; + print_endline " <label for=\"code\">code</label><br>"; + print_endline " <textarea type=\"text\" id=\"code\" name=\"code\"></textarea><br><br>"; + print_endline " <input type=\"submit\" value=\"Submit\">"; + print_endline "</form> " + +let readString () = + try + Scanf.scanf " %s" (fun x -> x) + with End_of_file -> "" + +let matchPrefix input prefix = + if (String.length input) < (String.length prefix) then false + else + if String.sub input 0 (String.length prefix) <> prefix then false else true + +let getUsernameAndCode st = + if not (matchPrefix st "username=") then + None + else + let noUsernameStr = String.sub st (String.length "username=") ((String.length st) - (String.length "username=")) in + let idxOfAmp = String.index noUsernameStr '&' in + let username = String.sub noUsernameStr 0 idxOfAmp in + let afterUsername = String.sub noUsernameStr (idxOfAmp+1) (String.length noUsernameStr - (idxOfAmp+1)) in + Some (afterUsername, username) +let isAlphaSt input = + (String.fold_left (fun valid ch -> (if ((isAlpha ch ) && valid) then true else false)) true input) + + +(* TODO: This is a very brittle way to pull out data from these requests. I think this'd struggle if there's a username with '&' *) + +let printReply () = + let st = String.of_bytes (raw_decode (Bytes.of_string (readString ()))) in + if st = "" then + (printHTMLPreamble (); printBody (); printHTMLEnding ()) + else + (printHTMLPreamble (); print_endline "<pre>"; + match (getUsernameAndCode st) with + | None -> () + | Some (code,username) -> ( + if isAlphaSt username then + (print_endline ("Username: " ^ username); print_endline ("Code: " ^ code)) + else + print_endline "Username may only be lowercase alpha characters." + ); + print_endline "</pre>"; printHTMLEnding ()) + +let () = printReply ()