FastCGI implementation in OCaml
1(* Echo server that shows request information *)
2
3open Fastcgi
4
5let echo_handler request response =
6 let http_req = Responder.request_of_fastcgi request in
7 let http_resp = Responder.response_of_fastcgi response in
8
9 http_resp.write_status 200;
10 http_resp.write_header "Content-Type" "text/html";
11
12 http_resp.write_body "<h1>FastCGI Echo Server</h1>";
13 http_resp.write_body ("<p><strong>Method:</strong> " ^ http_req.method_ ^ "</p>");
14 http_resp.write_body ("<p><strong>URI:</strong> " ^ http_req.uri ^ "</p>");
15 http_resp.write_body ("<p><strong>Query String:</strong> " ^ http_req.query_string ^ "</p>");
16
17 (match http_req.content_type with
18 | Some ct -> http_resp.write_body ("<p><strong>Content-Type:</strong> " ^ ct ^ "</p>")
19 | None -> ());
20
21 (match http_req.content_length with
22 | Some cl -> http_resp.write_body ("<p><strong>Content-Length:</strong> " ^ string_of_int cl ^ "</p>")
23 | None -> ());
24
25 http_resp.write_body "<h2>Headers:</h2><ul>";
26 List.iter (fun (name, value) ->
27 http_resp.write_body ("<li><strong>" ^ name ^ ":</strong> " ^ value ^ "</li>")
28 ) http_req.headers;
29 http_resp.write_body "</ul>";
30
31 http_resp.finish ();
32
33 { app_status = 0; protocol_status = Request_complete }
34
35let () = Eio_main.run @@ fun env ->
36 let net = Eio.Stdenv.net env in
37 Eio.Switch.run @@ fun sw ->
38 Server.run_default ~sw ~net
39 ~handler:(Handler.Responder echo_handler)
40 ~listen_address:(`Tcp ("127.0.0.1", 9001))