shaarli_cgi.ml 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. (*
  2. * https://caml.inria.fr/pub/docs/u3-ocaml/ocaml-steps.html
  3. *
  4. * extract some stuff about the request:
  5. * - request uri
  6. * - cookie(s)
  7. * - POST form data
  8. *
  9. * Response:
  10. * - http status + reason
  11. * - header
  12. * - content-type
  13. * - server
  14. * - body
  15. * - xml+atom (syndic) with xslt and comment prefix,
  16. * - xhtml or
  17. * - text/plain
  18. *
  19. * http://cumulus.github.io/Syndic/syndic/Syndic__/Syndic_atom/#input-and-output
  20. *
  21. * other cgi lib:
  22. * https://gitlab.com/gerdstolpmann/lib-ocamlnet3/blob/master/code/examples/cgi/netcgi2/add.ml
  23. * http://projects.camlcity.org/projects/dl/ocamlnet-4.1.6/doc/html-main/Netcgi.html#TYPEexn_handler
  24. *)
  25. (* https://github.com/rixed/ocaml-cgi/blob/master/cgi.ml#L169 *)
  26. let safe_getenv ?default s =
  27. try
  28. Sys.getenv s
  29. with Not_found ->
  30. match default with
  31. | Some d -> d
  32. | None ->
  33. failwith ("Cgi: the environment variable " ^ s ^ " is not set")
  34. let w s =
  35. print_string s;
  36. print_string "\n";;
  37. let va n =
  38. print_string "<li>";
  39. print_string n;
  40. print_string ": ";
  41. let v = safe_getenv ~default:"-" n in
  42. (* TODO: escape *)
  43. print_string v;
  44. print_string "</li>";
  45. print_string "\n";;
  46. w "Content-type: text/html; charset=utf-8";
  47. w "";
  48. w "<html>
  49. <head><title>Hello, Worλd</title></head>
  50. <body>
  51. <h1>OCaml, where art thou 🐫!</h1>
  52. <ul>";
  53. va "HOME";
  54. va "HTTPS";
  55. va "HTTP_HOST";
  56. va "HTTP_COOKIE";
  57. va "HTTP_ACCEPT";
  58. va "REMOTE_ADDR";
  59. va "REMOTE_USER";
  60. va "REQUEST_METHOD";
  61. va "REQUEST_URI";
  62. va "PATH_INFO";
  63. va "QUERY_STRING";
  64. va "SERVER_NAME";
  65. va "SERVER_PORT";
  66. va "SERVER_SOFTWARE";
  67. w "</ul>
  68. </body>
  69. </html>";
  70. exit 0;;