diff --git a/.gitignore b/.gitignore index b2db012..837b757 100644 --- a/.gitignore +++ b/.gitignore @@ -4,34 +4,11 @@ Makefile *.cmo *.cmx *.o +*.byte +*.opt config.cache config.log config.status -opam-version: "2.0" -version: "0.10" -maintainer: "rixed-opam@happyleptic.org" -authors: [ - "Daniel de Rauglaudre" - "Jean-Christophe FILLIATRE" - "Cedric Cellier" ] -homepage: "https://www.lri.fr/~filliatr/ftp/ocaml/cgi/" -bug-reports: "https://github.com/rixed/ocaml-cgi/issues" -dev-repo: "git+https://github.com/rixed/ocaml-cgi.git" -build: [ - ["./configure"] - [make] -] -install: [make "install"] -remove: [["ocamlfind" "remove" "cgi"]] -depends: [ - "ocaml" {> "4.01.0"} - "ocamlfind" {build} -] -synopsis: "Library for writing CGIs" -flags: light-uninstall -url { - src: "https://github.com/rixed/ocaml-cgi/archive/v0.10.tar.gz" - checksum: "md5=1e1b9e4de0ba12688b4b66523f42125d" -} +runcgi configure.lineno autom4te.cache diff --git a/Makefile.in b/Makefile.in index 7b3eb52..7db0e5c 100644 --- a/Makefile.in +++ b/Makefile.in @@ -18,9 +18,11 @@ MLI = $(CMO:.cmo=.mli) all: @OCAMLBEST@ -byte: $(CMO) +byte: $(CMO) runcgi.byte + cp -l -f runcgi.byte runcgi -opt: $(CMO) $(CMX) +opt: $(CMO) $(CMX) runcgi.opt + cp -l -f runcgi.opt runcgi ################################################################### # Installation and export @@ -33,10 +35,10 @@ OPTFILES = cgi.cmx cgi.o install: install-@OCAMLBEST@ install-byte: - ocamlfind install cgi $(COMMFILES) $(BYTEFILES) + ocamlfind install cgi $(COMMFILES) $(BYTEFILES) runcgi install-opt: - ocamlfind install cgi $(COMMFILES) $(BYTEFILES) $(OPTFILES) + ocamlfind install cgi $(COMMFILES) $(BYTEFILES) $(OPTFILES) runcgi MAJORVN=0 MINORVN=9 @@ -63,7 +65,7 @@ source: $(FILES) # Generic rules ################################################################### -.SUFFIXES: .mli .ml .cmi .cmo .cmx +.SUFFIXES: .mli .ml .cmi .cmo .cmx .byte .opt FLAGS := -w -3 @@ -79,15 +81,21 @@ FLAGS := -w -3 .ml.cmx: $(CAMLOPT) -c $(FLAGS) $< +.cmo.byte: + $(CAMLC) unix.cma $(FLAGS) $< -o $@ + +.cmx.opt: + $(CAMLOPT) unix.cmxa $(FLAGS) $< -o $@ + ################################################################### # backup, clean and depend : ################################################################### clean:: - rm -f *.cm[iox] *.o *~ + rm -f *.cm[iox] *.o *.opt *.byte *~ distclean dist-clean:: clean - rm -f config.cache config.status config.log Makefile + rm -f config.cache config.status config.log Makefile runcgi .depend: rm -f .depend diff --git a/runcgi.ml b/runcgi.ml new file mode 100644 index 0000000..9389ded --- /dev/null +++ b/runcgi.ml @@ -0,0 +1,40 @@ +(* Runs a command with CGI related envvars set. *) + +let () = + let meth = ref "GET" + and typ = ref "text/plain" + and body = ref "" + and query = ref "" + and cmd = ref [] in + let default d h = h ^" (default: "^ String.escaped d ^")" in + Arg.(parse + [ "-method", Set_string meth, default !meth "HTTP method (likely GET or POST)" ; + "-type", Set_string typ, default !typ "Content type" ; + "-body", Set_string body, default !body "Content body" ; + "-query", Set_string query, default !query "Query string (after the \"?\" in the URL, ex: \"x=1&y=2\")" ] + (fun s -> cmd := s :: !cmd) + "runcgi -method -type -body -query command ...args...") ; + let cmd = List.rev !cmd in + if cmd = [] then ( + Printf.eprintf "Missing command\n" ; + exit 1 + ) ; + let cmd = Array.of_list cmd in + let env = + [ "CONTENT_LENGTH", string_of_int (String.length !body) ; + "REQUEST_METHOD", !meth ; + "CONTENT_TYPE", !typ ; + "QUERY_STRING", !query ; + "SERVER_PORT", "80" ; + "SCRIPT_NAME", cmd.(0) ; + "SERVER_NAME", "runcgi" ] in + let add_env n env = + match Sys.getenv n with + | exception Not_found -> env + | s -> (n, s) :: env in + let env = add_env "HOME" env in + let env = add_env "PATH" env in + let env = + List.map (fun (n, v) -> n ^"="^ v) env |> + Array.of_list in + Unix.execvpe cmd.(0) cmd env