]> matita.cs.unibo.it Git - helm.git/blob - DEVEL/ocaml-http/0.1.4-3/examples/dump_args.ml
tagging
[helm.git] / DEVEL / ocaml-http / 0.1.4-3 / examples / dump_args.ml
1 (*
2   OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
3
4   Copyright (C) <2002-2007> Stefano Zacchiroli <zack@cs.unibo.it>
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2 of the License, or
9   (at your option) any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 *)
20
21 open Printf
22 open Http_types
23
24 let callback req outchan =
25   let str = 
26     (sprintf "request path = %s\n"  req#path) ^
27     (sprintf "request GET params = %s\n"
28       (String.concat ";"
29         (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_GET))) ^
30     (sprintf "request POST params = %s\n"
31       (String.concat ";"
32         (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_POST))) ^
33     (sprintf "request ALL params = %s\n"
34       (String.concat ";"
35         (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params))) ^
36     (sprintf "cookies = %s\n"
37       (match req#cookies with
38       | None ->
39           "NO COOKIES "
40           ^ (if req#hasHeader ~name:"cookie"
41              then "('Cookie:' header was '" ^ req#header ~name:"cookie" ^ "')"
42              else "(No 'Cookie:' header received)")
43       | Some cookies ->
44           (String.concat ";"
45             (List.map (fun (n,v) -> String.concat "=" [n;v]) cookies)))) ^
46     (sprintf "request BODY = '%s'\n\n" req#body)
47   in
48   Http_daemon.respond ~code:(`Code 200) ~body: str outchan
49
50 let spec =
51   { Http_daemon.default_spec with
52       callback = callback;
53       port = 9999;
54   }
55
56 let _ = Http_daemon.main spec
57