commit 8d1bcde90af6d7b65dccb6f968a7608cf5aa92ab
Author: Luxferre <lux@ferre>
Date: Wed, 20 Sep 2023 20:45:45 +0300
Initial upload
Diffstat:
A | COPYING | | | 24 | ++++++++++++++++++++++++ |
A | Makefile | | | 19 | +++++++++++++++++++ |
A | README.md | | | 41 | +++++++++++++++++++++++++++++++++++++++++ |
A | osmol.ml | | | 67 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 files changed, 151 insertions(+), 0 deletions(-)
diff --git a/COPYING b/COPYING
@@ -0,0 +1,24 @@
+This is free and unencumbered software released into the public domain.
+
+Anyone is free to copy, modify, publish, use, compile, sell, or
+distribute this software, either in source code form or as a compiled
+binary, for any purpose, commercial or non-commercial, and by any
+means.
+
+In jurisdictions that recognize copyright laws, the author or authors
+of this software dedicate any and all copyright interest in the
+software to the public domain. We make this dedication for the benefit
+of the public at large and to the detriment of our heirs and
+successors. We intend this dedication to be an overt act of
+relinquishment in perpetuity of all present and future rights to this
+software under copyright law.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+For more information, please refer to <https://unlicense.org>
diff --git a/Makefile b/Makefile
@@ -0,0 +1,19 @@
+# static POSIX makefile template for OCaml
+# depends on musl-gcc (optionally also zig cc)
+# and opam install ocaml-option-static ocaml-option-flambda
+
+CC = musl-gcc
+# CC = zig cc -target x86_64-linux-musl
+CFLAGS = -Os -static -s -flto
+LDFLAGS = -s -static -flto
+OC = ocamlopt
+OCFLAGS = -O2 -compact -cc "$(CC)" -ccopt "$(CFLAGS)" -cclib "$(LDFLAGS)"
+
+TARGET = osmol
+DEPS = -I +unix unix.cmxa
+
+$(TARGET): $(TARGET).ml
+ $(OC) $(DEPS) $(OCFLAGS) -o $(TARGET) $(TARGET).ml
+
+clean:
+ rm $(TARGET) *.cmi *.cmx *.o
diff --git a/README.md b/README.md
@@ -0,0 +1,40 @@
+# OSmol: the simplest static Gopher server in OCaml
+
+This is a really small (under 45 SLOC) server for serving static content on the small net, namely Gopher. It was written more like as an exercise on writing simple server applications in OCaml, but actually is powering the author's gopherhole at [hoi.st](gopher://hoi.st) as a single set-and-forget static binary with zero host-side dependencies.
+
+## Building
+
+OSmol is expected to be built using the (POSIX-compatible) Makefile supplied in the repo, which, in turn, requires installing [musl-gcc](https://wiki.musl-libc.org/getting-started.html) and two packages from Opam:
+```
+opam install ocaml-option-static ocaml-option-flambda
+```
+
+Of course, if you need other options, you can tweak the Makefile or build OSmol with the toolchain of your choice. The only module dependency here is `unix.cmxa` which comes with any OCaml installation.
+
+## Usage
+
+Run `./osmol --help` to see all command-line options. As of now, they are as follows:
+
+- `-p`: the TCP port to listen on. Defaults to 70.
+- `-d`: the root directory where your content resides. Defaults to `.` (the current working directory).
+- `-i`: the name of the file that must be present in all directories you want to make directly accessible. Without this file, the directory selector cannot be opened. Defaults to `index.map`.
+
+The files whose name is customizable with the `-i` switch usually follow standard Gophermap format.
+
+You can also run Osmol as a daemon, logging its start and other output (not much at the moment), for example, like this:
+```
+nohup ./osmol -d ./content -p 70 </dev/null 2>&1 | logger &
+```
+
+## Limitations
+
+- No CGI or search selectors support (by design).
+- No Gopher+ selector support (by design).
+- No directory autoindexing (all maps must be created with other tools or by hand).
+- No SSL/TLS support (use Traefik or other reverse proxy to enable it).
+
+## Credits
+
+Created by Luxferre in 2023, released into public domain.
+
+Made in Ukraine.
+\ No newline at end of file
diff --git a/osmol.ml b/osmol.ml
@@ -0,0 +1,67 @@
+(*
+ OSmol: simple static Gopher server in OCaml
+ Uses the same logic as Gofor-LX in Python
+ Created by Luxferre in 2023, released into public domain
+*)
+
+let listen_port = ref "70" (* server port *)
+let root_dir = ref "." (* content root directory *)
+let index_map = ref "index.map" (* index map file name *)
+let posargs = ref []
+let usage_msg = "Usage: " ^ Sys.argv.(0) ^ " [-p 70] [-d .] [-i index.map]"
+let speclist = [
+ ("-p", Arg.Set_string listen_port, "Port to listen on (default 70)");
+ ("-d", Arg.Set_string root_dir, "Content root directory (default .)");
+ ("-i", Arg.Set_string index_map, "Index map file name (default index.map)")
+]
+let anon_fun parg = posargs := parg :: !posargs
+let () = Arg.parse speclist anon_fun usage_msg
+
+(* get the resolved root directory with no trailing slashes *)
+
+let real_root_dir = try Unix.realpath !root_dir
+ with _ -> failwith "No root directory!"
+
+(* request processing *)
+
+(* server error message formatter *)
+let errmsg msg =
+ Printf.sprintf "3%s\t\terror.host\t1\r\n.\r\n\r\n" msg |> Bytes.of_string
+
+(* read a file as bytes sequence for Gopher output *)
+let readfile path =
+ if Sys.file_exists path then (* read the file *)
+ let ch = open_in_bin path in
+ let len = in_channel_length ch in
+ let buf = Bytes.create len in
+ try (really_input ch buf 0 len; close_in_noerr ch; buf)
+ with _ -> errmsg "Error reading the selector, try again later!"
+ else errmsg "Selector not found!"
+
+(* find content by selector and return it as bytes sequence *)
+let findsel sel =
+ let selpath = (try Unix.realpath (real_root_dir ^ "/" ^ sel)
+ with _ -> "###") in
+ (* check if selpath starts with rootdir *)
+ if String.starts_with ~prefix:real_root_dir selpath then
+ if Sys.is_directory selpath then (* resolve to index *)
+ readfile (selpath ^ "/" ^ !index_map)
+ else readfile selpath (* normal file *)
+ else errmsg "Selector not found!"
+
+(* inch is line-based, outch is binary *)
+let reqproc inch outch =
+ let reqparts = input_line inch |> String.split_on_char '\t' in
+ output_bytes outch (match reqparts with
+ | sel :: [] -> findsel (String.trim sel) (* valid selector with no tab *)
+ | [] -> findsel "/" (* empty root selector *)
+ | _ :: _ -> errmsg "Search selectors unsupported!"
+ );;
+
+(* start the server *)
+
+let lport = int_of_string(!listen_port) in
+ if lport > 0 && lport < 65536 then (
+ print_endline ("Starting server on port " ^ (string_of_int lport));
+ Unix.establish_server reqproc (Unix.ADDR_INET (Unix.inet_addr_any, lport))
+ ) else failwith "Invalid port!"