From a97d106d293d35a51dd27ed8c8ce621fb67e187c Mon Sep 17 00:00:00 2001 From: Stefano Zacchiroli Date: Sun, 1 Dec 2002 17:20:56 +0000 Subject: [PATCH] - added damned_recursion example --- helm/DEVEL/ocaml-http/examples/Makefile | 7 ++- .../ocaml-http/examples/damned_recursion.ml | 63 +++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 helm/DEVEL/ocaml-http/examples/damned_recursion.ml diff --git a/helm/DEVEL/ocaml-http/examples/Makefile b/helm/DEVEL/ocaml-http/examples/Makefile index 825a27567..ed9c3b028 100644 --- a/helm/DEVEL/ocaml-http/examples/Makefile +++ b/helm/DEVEL/ocaml-http/examples/Makefile @@ -6,7 +6,8 @@ OBJS_MT_OPT = ../http_mt.cmxa EXAMPLES_FLAGS = -I .. -linkpkg EXAMPLES = \ - always_ok_daemon webfsd obj_foo dump_args timeout dont_fork threads chdir threads_foo + always_ok_daemon webfsd obj_foo dump_args timeout dont_fork \ + threads chdir damned_recursion all: $(EXAMPLES) opt: $(patsubst %,%.opt,$(EXAMPLES)) @@ -20,9 +21,9 @@ threads: threads.ml threads.opt: threads.ml $(OCAMLOPT) $(EXAMPLES_FLAGS) $(OBJS_MT_OPT) $(THREADS_FLAGS) -o $@ $< -threads_foo: threads_foo.ml +damned_recursion: damned_recursion.ml $(OCAMLC) $(EXAMPLES_FLAGS) $(OBJS_MT) $(THREADS_FLAGS) -package netclient -o $@ $< -threads_foo.opt: threads_foo.ml +damned_recursion.opt: damned_recursion.ml $(OCAMLOPT) $(EXAMPLES_FLAGS) $(OBJS_MT_OPT) $(THREADS_FLAGS) -package netclient -o $@ $< distclean: clean diff --git a/helm/DEVEL/ocaml-http/examples/damned_recursion.ml b/helm/DEVEL/ocaml-http/examples/damned_recursion.ml new file mode 100644 index 000000000..32faa0137 --- /dev/null +++ b/helm/DEVEL/ocaml-http/examples/damned_recursion.ml @@ -0,0 +1,63 @@ + +(* + OCaml HTTP - do it yourself (fully OCaml) HTTP daemon + + Copyright (C) <2002> Stefano Zacchiroli + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*) + +open Printf;; + +(* +let wget url = + prerr_endline (sprintf "DEBUG: wgetting url '%s'" url); + Http_client.Convenience.http_get url +in +*) +let wget addr port path = + let rec wget' inchan data = + try + wget' inchan (data ^ input_line inchan) + with End_of_file -> + data + 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" path); + flush outchan; + let inchan = Unix.in_channel_of_descr suck in + wget' inchan "" +in +let callback req outchan = + let i = int_of_string (req#param "x") in + prerr_endline (string_of_int i); + match i with + | 0 -> output_string outchan "0" + | x when x>0 -> + let data = + wget "127.0.0.1" 9999 (sprintf "/foo?x=%d" (x-1)) +(* wget (sprintf "http://localhost:9999/foo?x=%d" (x-1)) *) +(* wget "http://localhost:80/index.html" *) + in + output_string outchan (sprintf "%s %d" data x) + | _ -> assert false +in +Http_client.Convenience.http_trials := 1; +(* Http_daemon.start' ~port:9999 ~mode:`Thread callback *) +Http_daemon.start' ~port:9999 ~mode:`Fork callback -- 2.39.2