X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2FDEVEL%2Focaml-http%2Fexamples%2Fdamned_recursion.ml;h=be2e3062983331e92cdf28640d392b16ea816c4f;hb=97c2d258a5c524eb5c4b85208899d80751a2c82f;hp=8c7efd91e07f185098356bffbf15e1a106cb84c6;hpb=2228717a5f41aab3b157713e40753016ad46c2ae;p=helm.git diff --git a/helm/DEVEL/ocaml-http/examples/damned_recursion.ml b/helm/DEVEL/ocaml-http/examples/damned_recursion.ml index 8c7efd91e..be2e30629 100644 --- a/helm/DEVEL/ocaml-http/examples/damned_recursion.ml +++ b/helm/DEVEL/ocaml-http/examples/damned_recursion.ml @@ -19,40 +19,33 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *) -open Http_types;; -open Printf;; +open Printf +open Http_types -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 = +let port = 9999 + +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 -> - let data = - wget "127.0.0.1" 9999 (sprintf "/foo?x=%d" (x-1)) -(* wget "127.0.0.1" 80 "/index.html" *) - in - output_string outchan (sprintf "%s %d" data x) - | _ -> assert false -in -let mode = `Thread in -Http_daemon.start' ~port:9999 ~mode callback + let body = + match i with + | 0 -> "0" + | x when x > 0 -> + let data = + Http_user_agent.get (sprintf "http://127.0.0.1:%d/foo?x=%d" + port (x - 1)) + in + sprintf "%s %d" data x + | _ -> assert false + in + Http_daemon.respond ~code:(`Code 200) ~body outchan; + close_out outchan (* Http_user_agent relies on EOF, not Content-Length *) + +let spec = + { Http_daemon.default_spec with + callback = callback; + port = port; + mode = `Thread; + } + +let _ = Http_daemon.main spec