commit 7063c47ab9a8d983282d14a63431c927c026969d
parent 0625449dbdad33d75ea06bfc810f6acfef8aa1a1
Author: Andrew Laack <andrew@laack.co>
Date: Sun, 15 Nov 2026 19:09:18 -0600
Uploaded code seems to work at a baseline level
Diffstat:
5 files changed, 66 insertions(+), 4 deletions(-)
diff --git a/Makefile b/Makefile
@@ -16,6 +16,8 @@ run-server: ocaml
sudo rm -rf /srv/http/ojudge
sudo mkdir -p /srv/http/ojudge
sudo mkdir -p /var/lib/ojudge/problems/
+ sudo mkdir -p /var/lib/ojudge/submissions/
+ sudo chown -R http:http /var/lib/ojudge/submissions/ # allow cgi scripts to create submissions
sudo cp -r assets/problems /var/lib/ojudge/
sudo cp -r cgi /srv/http/ojudge/
sudo cp config/lighttpd.conf /etc/lighttpd/lighttpd.conf
diff --git a/assets/problems/sum-of-matches/a.out b/assets/problems/sum-of-matches/a.out
Binary files differ.
diff --git a/ojudge/bin/dune b/ojudge/bin/dune
@@ -11,4 +11,4 @@
(executable
(public_name submit)
(name submit)
- (libraries ojudge unix))
+ (libraries ojudge unix uuidm))
diff --git a/ojudge/bin/problem.ml b/ojudge/bin/problem.ml
@@ -1,3 +1,4 @@
+
let printHTMLPreamble () =
print_endline "Content-type: text/html\n\n";
print_endline "<html>"
diff --git a/ojudge/bin/submit.ml b/ojudge/bin/submit.ml
@@ -1,6 +1,17 @@
+open Uuidm
+
+let getEnv varName =
+ try Some (Unix.getenv varName)
+ with Not_found -> None
+
let isAlpha ch =
let num = int_of_char ch in
num >= (int_of_char 'a') && num <= (int_of_char 'z')
+let isAlphaAndSlash input =
+ (String.fold_left (fun valid ch -> (if ((isAlpha ch || ch = '-') && valid) then true else false)) true input)
+
+let isAlphaStr input =
+ (String.fold_left (fun valid ch -> (if ((isAlpha ch) && valid) then true else false)) true input)
let hexa_val conf =
match conf with
@@ -83,10 +94,58 @@ let getUsernameAndCode st =
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)
+ if (isAlphaStr username) then
+ Some ((String.sub afterUsername (String.length "code=") ((String.length afterUsername) - (String.length "code="))), username)
+ else
+ None
let isAlphaSt input =
(String.fold_left (fun valid ch -> (if ((isAlpha ch ) && valid) then true else false)) true input)
+let createSubmissionDirectory username submission =
+ let () =
+ if not (Sys.file_exists ("/var/lib/ojudge/submissions/" ^ username ^ "/" ^ submission)) then
+ (Sys.mkdir ("/var/lib/ojudge/submissions/" ^ username ^ "/" ^ submission) 0o755)
+ else ()
+ in
+ if not (Sys.file_exists ("/var/lib/ojudge/submissions/" ^ username ^ "/" ^ submission ^ "/code")) then
+ (Sys.mkdir ("/var/lib/ojudge/submissions/" ^ username ^ "/" ^ submission ^ "/code") 0o755)
+ else ()
+
+let createSubmissionFile username cuid code =
+ let filePath = ("/var/lib/ojudge/submissions/" ^ username ^ "/" ^ cuid ^ "/code/" ^ "main.ml") in
+ let oc = open_out filePath in
+ Printf.fprintf oc "%s" code
+
+
+let createUserDir username =
+ if not (Sys.file_exists ("/var/lib/ojudge/submissions/" ^ username)) then
+ (Sys.mkdir ("/var/lib/ojudge/submissions/" ^ username) 0o755)
+ else ()
+
+let createSubmission username code problem =
+ (* Technically, we should probably do a file exists and is dir check but this is probably fine. *)
+ createUserDir username;
+ let uuid = Uuidm.v4_gen (Random.State.make_self_init ()) in
+ let cuid = Uuidm.to_string (uuid ()) in
+ createSubmissionDirectory username cuid;
+ createSubmissionFile username cuid code;
+ cuid
+
+let getProblemName () =
+ let path = (match (getEnv "REQUEST_URI") with
+ | Some x -> x
+ | None -> ""
+ ) in
+ let problemSize = (String.length "/problem/") in
+ if matchPrefix path "/problem/"
+ then let problemNameUnsafe = (String.sub path problemSize ((String.length path)- problemSize))
+ in
+ if isAlphaAndSlash problemNameUnsafe
+ then problemNameUnsafe
+ else ""
+ else
+ ""
+
(* 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 '&' *)
@@ -97,10 +156,10 @@ let printReply () =
else
(printHTMLPreamble (); print_endline "<pre>";
match (getUsernameAndCode st) with
- | None -> ()
+ | None -> print_endline "Invalid username, only lowercase alpha characters are supported."
| Some (code,username) -> (
if isAlphaSt username then
- (print_endline ("Username: " ^ username); print_endline ("Code: " ^ code))
+ (print_endline "This is a test"; let submissionId = createSubmission username code (getProblemName ()) in print_endline ("Submission created! " ^ submissionId))
else
print_endline "Username may only be lowercase alpha characters."
);