]> matita.cs.unibo.it Git - helm.git/commitdiff
ported to daemon_spec
authorStefano Zacchiroli <zack@upsilon.cc>
Thu, 3 Feb 2005 22:12:18 +0000 (22:12 +0000)
committerStefano Zacchiroli <zack@upsilon.cc>
Thu, 3 Feb 2005 22:12:18 +0000 (22:12 +0000)
helm/DEVEL/ocaml-http/examples/always_ok_daemon.ml
helm/DEVEL/ocaml-http/examples/basic_auth.ml
helm/DEVEL/ocaml-http/examples/chdir.ml
helm/DEVEL/ocaml-http/examples/client_address.ml
helm/DEVEL/ocaml-http/examples/damned_recursion.ml
helm/DEVEL/ocaml-http/examples/dump_args.ml
helm/DEVEL/ocaml-http/examples/highlander.ml
helm/DEVEL/ocaml-http/examples/oo_daemon.ml
helm/DEVEL/ocaml-http/examples/timeout.ml
helm/DEVEL/ocaml-http/examples/webfsd.ml

index d5d456d7481d9a22488c4802dcadb2168775ab4f..caa0d4516fe427e8dbd1691e0b2ece1f5ce88186 100644 (file)
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)
 
-open Http_daemon;;
+open Http_types
+
   (* start an http daemon that alway respond with a 200 status code and an empty
   content *)
-start ~port:9999 ~addr:"localhost" (fun _ _ outchan -> respond outchan)
+let spec =
+  { Http_daemon.default_spec with
+      callback = (fun _ outchan -> Http_daemon.respond outchan);
+      port = 9999;
+  }
+
+let _ = Http_daemon.main spec
 
index c3e4e6f9162b55d5cc2fe5d6d5ce25f213b0f564..bdfb2b949d6ef8995817ccfc6bd186598807ee1b 100644 (file)
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)
 
-let callback (req: Http_types.request) outchan =
+open Http_types
+
+(* the easy way: specify authentication requirements within a daemon_spec *)
+let spec =
+  { Http_daemon.default_spec with
+        (* requires basic authentication, username "foo", password "bar" *)
+      auth = Some ("my realm", `Basic ("foo", "bar"));
+      callback = (fun _ outchan -> Http_daemon.respond ~body:"secret" outchan);
+      port = 9999;
+  }
+
+(*
+(* the hard^Wother way: manual handling of authorization *)
+let callback req outchan =
   match req#authorization with
   | Some (`Basic (username, password))
     when username = "foo" && password = "bar" ->
-      Http_daemon.respond ~code:(`Code 200) ~body:"secret page!" outchan
-  | _ -> raise (Http_types.Unauthorized "my secret site")
+      Http_daemon.respond ~code:(`Code 200) ~body:"secret" outchan
+  | _ -> raise (Unauthorized "my secret site")
+
+let spec =
+  { Http_daemon.default_spec with
+      callback = callback;
+      port = 9999;
+  }
+*)
 
-let _ = Http_daemon.start' ~port:9999 callback
+let _ = Http_daemon.main spec
 
index 183b3ca00bd6dfdd96a859dda713221029c86af8..bcba1ebc33947264625c0f801c1262ac0a05f984 100644 (file)
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)
 
-open Http_daemon;;
-open Printf;;
-start
-  ~port:9999
-  ~root:"/etc"
-  (fun _ _ outchan -> respond ~body:(sprintf "%s\n" (Sys.getcwd ())) outchan)
+open Printf
+open Http_types
+
+let spec =
+  { Http_daemon.default_spec with
+      callback = (fun _ outchan ->
+        Http_daemon.respond ~body:(sprintf "%s\n" (Sys.getcwd ())) outchan);
+      port = 9999;
+      root_dir = Some "/etc";
+  }
+
+let _ = Http_daemon.main spec
+
index 0f5c68d2c4ff87798cf7ac5469317290967b87e7..79d4ff836420f5f69c91b302acd8330827cba274 100644 (file)
@@ -19,7 +19,8 @@
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)
 
-open Printf;;
+open Printf
+open Http_types
 
 let callback req outchan =
   let body =
@@ -30,5 +31,12 @@ let callback req outchan =
   in
   let res = new Http_response.response ~body () in
   Http_daemon.respond_with res outchan
-in
-Http_daemon.start' ~port:9999 callback
+
+let spec =
+  { Http_daemon.default_spec with
+      callback = callback;
+      port = 9999
+  }
+
+let _ = Http_daemon.main spec
+
index 8c7efd91e07f185098356bffbf15e1a106cb84c6..bf2cf31ada3faef48ae61a0d6089cecb3fc12180 100644 (file)
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)
 
-open Http_types;;
-open Printf;;
-
-let wget addr port path =
-  let rec wget' inchan buf =
-    Buffer.add_string buf (input_line inchan ^ "\n");
-    wget' inchan buf
-  in
-  prerr_endline (sprintf "DEBUG: wgetting url '%s:%d%s'" addr port path);
-  let sockaddr = Unix.ADDR_INET (Unix.inet_addr_of_string addr, port) in
-  let suck = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
-  Unix.connect suck sockaddr;
-  let outchan = Unix.out_channel_of_descr suck in
-  output_string outchan (sprintf "GET %s HTTP/1.0\r\n\r\n" path);
-  flush outchan;
-  let inchan = Unix.in_channel_of_descr suck in
-  let buf = Buffer.create 1023 in
-  try
-    wget' inchan buf
-  with End_of_file -> Buffer.contents buf
-in
-let callback (req: request) outchan =
+open Printf
+open Http_types
+
+let callback (req: Http_types.request) outchan =
   let i = int_of_string (req#param "x") in
-  prerr_endline (string_of_int i);
   match i with
   | 0 -> output_string outchan "1"
-  | x when x>0 ->
+  | x when x > 0 ->
       let data =
-        wget "127.0.0.1" 9999 (sprintf "/foo?x=%d" (x-1))
-(*         wget "127.0.0.1" 80 "/index.html" *)
+        Http_user_agent.get (sprintf "http://127.0.0.1/foo?x=%d" (x - 1))
       in
       output_string outchan (sprintf "%s %d" data x)
   | _ -> assert false
-in
-let mode = `Thread in
-Http_daemon.start' ~port:9999 ~mode callback
+
+let spec =
+  { Http_daemon.default_spec with
+      callback = callback;
+      port = 9999;
+      mode = `Thread;
+  }
+
+let _ = Http_daemon.main spec
 
index a6dcdda0e09aa9513bab623a6f70e244fafd56d6..b7cf02a86c97f332b1da0b8064512c6200d3adaa 100644 (file)
@@ -19,7 +19,8 @@
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)
 
-open Printf;;
+open Printf
+open Http_types
 
 let callback req outchan =
   Http_daemon.send_basic_headers ~code:(`Code 200) outchan;
@@ -37,10 +38,13 @@ let callback req outchan =
         (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params))) ^
     (sprintf "request BODY = '%s'\n" req#body)
   in
-  output_string outchan str;
-  prerr_endline str
-in
-print_endline "Starting custom Http_daemon ...";
-flush stdout;
-Http_daemon.start' ~port:9999 callback
+  output_string outchan str
+
+let spec =
+  { Http_daemon.default_spec with
+      callback = callback;
+      port = 9999;
+  }
+
+let _ = Http_daemon.main spec
 
index 24a4abd84d9d01e211dc3b1689eff213cd48c2f7..d42445481a84ff7220bac0ffbdacff32c3ce4379 100644 (file)
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)
 
-(* almost the same as dump_args.ml, but used to test fast rebinding of the same
-port *)
-
-open Printf;;
-
-let callback req outchan =
-  Http_daemon.send_basic_headers ~code:(`Code 200) outchan;
-  Http_daemon.send_CRLF outchan;
-  let str = 
-    (sprintf "request path = %s\n"  req#path) ^
-    (sprintf "request GET params = %s\n"
-      (String.concat ";"
-        (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_GET))) ^
-    (sprintf "request POST params = %s\n"
-      (String.concat ";"
-        (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_POST))) ^
-    (sprintf "request ALL params = %s\n"
-      (String.concat ";"
-        (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params))) ^
-    (sprintf "request BODY = '%s'\n" req#body)
-  in
-  output_string outchan str;
-  prerr_endline str
-in
-print_endline "Starting custom Http_daemon ...";
-flush stdout;
-(* Sys.catch_break true; *)
-while true do
-(*   try *)
-    Http_daemon.start' ~mode:`Single ~port:9999 callback
-(*   with Sys.Break -> prerr_endline "RESURRECTION!!!!" *)
-done
+(* test for fast rebinding of the tcp port *)
+
+open Printf
+open Http_types
+
+let spec =
+  { Http_daemon.default_spec with
+      callback = (fun _ outchan -> Http_daemon.respond ~body:"foo" outchan);
+      port = 9999;
+      mode = `Single;
+  }
+
+let _ =
+  Sys.catch_break true;
+  while true do
+    try
+      Http_daemon.main spec;
+    with Sys.Break -> prerr_endline "RESURRECTION!!!!"
+  done
 
index 528f2b24688e8cc08c4af5671eca9bc949f8b51b..91197e3f7c53718adf45463f78f95bf1b1608e13 100644 (file)
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)
 
-open Http_daemon;;
-open Http_response;;
+open Http_daemon
+open Http_response
 
   (* the simple way *)
-let d = new daemon ~addr:"127.0.0.1" ~port:9999 () in
-while true do
-  let (req, conn) = d#getRequest in (* wait for valid request *)
-  conn#respond_with (new response ~body:"foo\n" ());
-  conn#close
-done
+let d = new daemon ~addr:"127.0.0.1" ~port:9999 ()
+
+let _ =
+  while true do
+    let (req, conn) = d#getRequest in (* wait for valid request *)
+    conn#respond_with (new response ~body:"foo\n" ());
+    conn#close
+  done
 
 (*
-  (* the hard way *)
+  (* the hard^Wother way *)
 let d = new daemon ~addr:"127.0.0.1" ~port:9999 () in
-while true do
-  let conn = d#accept in (* wait for client connection *)
-  (match conn#getRequest with
-  | None -> ()  (* invalid request received *)
-  | Some req -> conn#respond_with (new response ~body:"foo\n" ()));
-  conn#close (* close socket *)
-done
+let _ =
+  while true do
+    let conn = d#accept in (* wait for client connection *)
+    (match conn#getRequest with
+    | None -> ()  (* invalid request received *)
+    | Some req -> conn#respond_with (new response ~body:"foo\n" ()));
+    conn#close (* close socket *)
+  done
 *)
 
index 84e989b94d22e02b5ffb699cf43889d7596a5cc4..d39f6be7aec86917988ec9885f838d26cd98fbd7 100644 (file)
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)
 
-let callback _ _ outchan =
-  output_string outchan "Here you are!\n";
-  flush outchan
-in
-Http_daemon.start ~port:9999 ~timeout:(Some 10) callback
+open Http_types
+
+let spec =
+  { Http_daemon.default_spec with
+      callback = (fun _ outchan -> Http_daemon.respond ~body:"foo" outchan);
+      timeout = Some 10;
+  }
+
+let _ = Http_daemon.main spec
+
index c41443e87b62ebd65f68c70e5ce6f8aefe4c2462..c7a984b034ff4d269780e62e29a41130b95ce100 100644 (file)
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)
 
-let def_port = 80 in
-let def_addr = "0.0.0.0" in
-let def_root = Sys.getcwd () in
+open Http_types
 
-let port = ref def_port in
-let addr = ref def_addr in
-let root = ref def_root in
+let def_port = 80
+let def_addr = "0.0.0.0"
+let def_root = Sys.getcwd ()
+
+let port = ref def_port
+let addr = ref def_addr
+let root = ref def_root
 let argspec =
   [ "-p", Arg.Int (fun p -> port := p),
       "TCP port on which listen, default: " ^ string_of_int !port;
     "-a", Arg.String (fun a -> addr := a),
       "IP address on which listen, default: " ^ !addr;
     "-r", Arg.String (fun r -> root := r),
-      "DocumentRoot, default: current working directory" ]
-in
-Arg.parse argspec (fun _ -> ()) "";
-Sys.chdir !root;
-Http_daemon.Trivial.start ~addr:!addr ~port:!port ()
+      "DocumentRoot, default: current working directory";
+  ]
+
+let _ =
+  Arg.parse argspec (fun _ -> ()) "";
+  let spec =
+    { Http_daemon.default_spec with
+        address = !addr;
+        port = !port;
+        root_dir = Some !root
+    }
+  in
+  Http_daemon.Trivial.main spec