commit 38f3851d965c17fc1b6b2ca1b6c478d64914d7bf
parent 9a4358f663fdd156f0ebd9e102e35e8e4166bfb4
Author: Andrew Laack <andrew@laack.co>
Date: Fri, 13 Nov 2026 15:56:48 -0600
Ocaml code for handling submissions, very naive, mostly creating directory structure and handling stdin from cgi
Diffstat:
7 files changed, 160 insertions(+), 8 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,20 +1,34 @@
build-container:
podman build containers -t judge
+build-ocaml:
+ cd ojudge && dune build
+ cp ojudge/_build/default/bin/main.exe cgi-bin/submit-code
+ chmod 755 cgi-bin/submit-code
run-container:
cd example && \
timeout 5 podman run --rm -v ./code:/code:ro -v ./inputs:/inputs:ro -v ./outputs:/outputs:rw --network=none --memory=256MB --cpus 1 judge
-build-cgi:
+build-cgi: build-ocaml
mkdir -p cgi-bin
cp scripts/example.sh cgi-bin/example.sh
cp scripts/home.sh cgi-bin/home.sh
+ cp scripts/submit.sh cgi-bin/submit.sh
+
deploy-server: build-cgi
sudo rm -rf /srv/http/cgi-bin
sudo cp -r cgi-bin /srv/http/
sudo cp lighttpd.conf /etc/lighttpd/lighttpd.conf
+ # state directories
+ sudo mkdir -p /var/lib/ojudge
+ sudo mkdir -p /var/lib/ojudge/submissions
+ sudo mkdir -p /var/lib/ojudge/pending-review
+ sudo mkdir -p /var/lib/ojudge/problems
+ sudo mkdir -p /var/lib/ojudge/contests
+ sudo chown -R http:http /var/lib/ojudge
+
# just in case
sudo chown -R root:root /srv/http
sudo chmod 755 /srv/http/cgi-bin
diff --git a/README b/README
@@ -25,3 +25,62 @@ containers
each submission runs in its own container, started by the dispatching daemon.
see containers/arch.txt for container design
+
+directory structures
+====================
+
+code eval
+---------
+
+a code evaluation has the following directory structure:
+
+├ {submission_id}
+ ├── code
+ │ └── main.ml
+ ├── inputs
+ │ ├── ex1.in
+ │ ├── ex2.in
+ │ └── ex3.in
+ ├── outputs
+ │ ├── ex1.out
+ │ ├── ex2.out
+ │ └── ex3.out
+
+outputs are generated outputs, associated with the corresponding inputs.
+
+when judging a submission the test case answers aren't in the container that compiles and runs the code.
+
+additionally, the code and inputs directories are read only. the code because there's no reason to change it, and build is done in a temp directory, and the inputs to ensure they aren't tampered with. these are bind mounted in with the :ro permissions set.
+
+outputs obviously have to be writable in the container.
+
+from within the container we have the following mount points for the above code (note: the above code is what it looks like on the server's filesystem, not in the container):
+
+/code
+/inputs
+/outputs
+
+these have 1:1 correspondences with the above directories on the server.
+
+server filesystem
+-----------------
+
+we know what a singular code evaluation looks like on the filesystem from above, below is the rest of the server's state (there is no conventional db, only filesystem state):
+
+| /var/lib/ojudge
+
+
+
+ok, so what do we actually need?
+
+submissions/{user_id}/{submission_id}
+ /{data listed above (code, inputs, outputs, and additional metadata about execution)}
+pending-review/ # once a submission is reviewed, the txt file is deleted. these files just serve as a pointer to a submissions/{submission_id} dir
+ /{submission_id}.txt
+problems/{problem_id}
+ /{problem text}
+ /{'hidden' test cases (in + out)}
+contests/{contest_id}
+ /{submission links (user_id/submission_id)}
+ /{current status (user_id and completed problem count)}
+ /{problems}/{problem_id}
diff --git a/cgi-bin/submit-code b/cgi-bin/submit-code
Binary files differ.
diff --git a/cgi-bin/submit.sh b/cgi-bin/submit.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
+cat <<'EOF'
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8">
+ <title>Submit code</title>
+</head>
+<body>
+ <h1>ojudge, submit code</h1>
+ <div class="form-container">
+ <h2>Code</h2>
+ <form id="submit-code" action="submit-code" method="post">
+ <label for="code">Code</label><br>
+
+ <textarea id="username" name="username" rows="20" cols="80"
+ spellcheck="false" required></textarea><br>
+
+ <textarea id="code" name="code" rows="20" cols="80"
+ spellcheck="false" required></textarea><br>
+ <input type="submit" value="Submit">
+ </form>
+ </div>
+</body>
+</html>
+EOF
diff --git a/ojudge/bin/dune b/ojudge/bin/dune
@@ -1,4 +1,5 @@
(executable
(public_name ojudge)
(name main)
- (libraries ojudge))
+ (libraries ojudge threads netcgi2 uuidm)
+ (link_flags (-ccopt -static)))
diff --git a/ojudge/bin/main.ml b/ojudge/bin/main.ml
@@ -1,9 +1,33 @@
+let rec mkdir d =
+ if not (Sys.file_exists d) then (mkdir (Filename.dirname d); Sys.mkdir d 0o755)
-let run =
- "cat ex1.in | ./a.out"
+let submissionReply (cgi : Netcgi.cgi) =
+ cgi#set_header ~content_type:"text/html; charset=utf-8" ();
+ let out = cgi#out_channel in
+ out#output_string (Printf.sprintf "Your code has been submitted. Please await judging.");
+ out#commit_work ()
-let compile =
- "ocamlopt nums.cmxa str.cmxa -pp camlp4o -unsafe main.ml"
+let writeFile filepath toWrite =
+ Out_channel.with_open_text filepath (fun oc -> output_string oc toWrite)
-let command =
- "podman run --rm -it --network=none --memory=256MB --cpus 1 judge test"
+let setupSubmissionDir subId username =
+ let () = mkdir ("/var/lib/ojudge/" ^ username ^ "/" ^ subId) in
+ let () = mkdir ("/var/lib/ojudge/" ^ username ^ "/" ^ subId ^ "/code") in
+ let () = mkdir ("/var/lib/ojudge/" ^ username ^ "/" ^ subId ^ "/inputs") in
+ let () = mkdir ("/var/lib/ojudge/" ^ username ^ "/" ^ subId ^ "/outputs") in
+ mkdir ("/var/lib/ojudge/" ^ username ^ "/" ^ subId ^ "/results")
+
+let uuid () =
+ Uuidm.to_string (Uuidm.v4_gen (Random.State.make_self_init ()) ())
+
+(* TODO: Make sure username can't do traversals, and guard this *)
+
+let handleSubmission (cgi : Netcgi.cgi) =
+ let code = cgi#argument_value "code" in
+ let username = cgi#argument_value "username" in
+ let subId = uuid () in
+ let () = setupSubmissionDir subId username in
+ let () = writeFile ("/var/lib/ojudge/" ^ username ^ "/" ^ subId ^ "/code/main.ml") code in
+ submissionReply cgi
+
+let () = Netcgi_cgi.run handleSubmission
diff --git a/scripts/submit.sh b/scripts/submit.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
+cat <<'EOF'
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8">
+ <title>Submit code</title>
+</head>
+<body>
+ <h1>ojudge, submit code</h1>
+ <div class="form-container">
+ <h2>Code</h2>
+ <form id="submit-code" action="submit-code" method="post">
+ <label for="code">Code</label><br>
+
+ <textarea id="username" name="username" rows="20" cols="80"
+ spellcheck="false" required></textarea><br>
+
+ <textarea id="code" name="code" rows="20" cols="80"
+ spellcheck="false" required></textarea><br>
+ <input type="submit" value="Submit">
+ </form>
+ </div>
+</body>
+</html>
+EOF