Skip to content

Commit

Permalink
Add a simple tool to help run cgi scripts from the command line
Browse files Browse the repository at this point in the history
Also fix garbage in .gitignore
  • Loading branch information
rixed committed May 11, 2023
1 parent 2e3d48f commit 30ebcfb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 33 deletions.
29 changes: 3 additions & 26 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,11 @@ Makefile
*.cmo
*.cmx
*.o
*.byte
*.opt
config.cache
config.log
config.status
opam-version: "2.0"
version: "0.10"
maintainer: "[email protected]"
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
22 changes: 15 additions & 7 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
40 changes: 40 additions & 0 deletions runcgi.ml
Original file line number Diff line number Diff line change
@@ -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 <meth> -type <type> -body <txt> -query <qry> 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

0 comments on commit 30ebcfb

Please sign in to comment.