]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/ocaml-http/http_request.ml
Initial revision
[helm.git] / helm / DEVEL / ocaml-http / http_request.ml
1
2 (*
3   OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
4
5   Copyright (C) <2002> Stefano Zacchiroli <zack@cs.unibo.it>
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 *)
21
22 open Printf;;
23
24 open Http_common;;
25 open Http_types;;
26
27 let debug_dump_request path params =
28   debug_print ("request path = " ^ path);
29   debug_print (
30     sprintf"request params = %s"
31       (String.concat ";"
32         (List.map (fun (h,v) -> String.concat "=" [h;v]) params)))
33
34 exception Fallback;;  (* used internally by request class *)
35
36 class request ic =
37   let (meth, uri, version) = Http_parser.parse_request_fst_line ic in
38   let uri_str = Neturl.string_of_url uri in
39   let path = Http_parser.parse_path uri in
40   let query_get_params = Http_parser.parse_query_get_params uri in
41   let headers =
42     List.map  (* lowercase header names to ease lookups before having a request
43               object *)
44       (fun (h,v) -> (String.lowercase h, v))
45       (Http_parser.parse_headers ic) (* trailing \r\n consumed! *)
46   in
47   let body =
48     (* TODO fallback on Transfer-Encoding if Content-Length isn't defined *)
49     if meth = `POST then
50       Buffer.contents
51         (try  (* read only Content-Length bytes *)
52           let limit_raw =
53             (try
54               List.assoc "content-length" headers
55             with Not_found -> raise Fallback)
56           in
57           let limit =
58             (try  (* TODO supports only a maximum content-length of 1Gb *)
59               int_of_string limit_raw
60             with Failure "int_of_string" ->
61               raise (Invalid_header ("content-length: " ^ limit_raw)))
62           in
63           Http_misc.buf_of_inchan ~limit ic
64         with Fallback -> Http_misc.buf_of_inchan ic)  (* read until EOF *)
65     else "" (* TODO empty body for methods other than POST, is what we want? *)
66   in
67   let query_post_params =
68     match meth with
69     | `POST ->
70         let ct = try List.assoc "content-type" headers with Not_found -> "" in
71         if ct = "application/x-www-form-urlencoded" then
72           Http_parser.split_query_params body
73         else []
74     | _ -> []
75   in
76   let params = query_post_params @ query_get_params in (* prefers POST params *)
77   let _ = debug_dump_request path params in
78   let (clisockaddr, srvsockaddr) =
79     (Http_misc.peername_of_in_channel ic, Http_misc.sockname_of_in_channel ic)
80   in
81
82   object (self)
83
84     inherit
85       Http_message.message ~body ~headers ~version ~clisockaddr ~srvsockaddr
86
87     val params_tbl =
88       let tbl = Hashtbl.create (List.length params) in
89       List.iter (fun (n,v) -> Hashtbl.add tbl n v) params;
90       tbl
91
92     method meth = meth
93     method uri = uri_str
94     method path = path
95     method param ?meth name =
96       (match (meth: meth option) with
97       | None ->
98           (try
99             Hashtbl.find params_tbl name
100           with Not_found -> raise (Param_not_found name))
101       | Some `GET -> List.assoc name query_get_params
102       | Some `POST -> List.assoc name query_post_params)
103     method paramAll ?meth name =
104       (match (meth: meth option) with
105       | None -> List.rev (Hashtbl.find_all params_tbl name)
106       | Some `GET -> Http_misc.list_assoc_all name query_get_params
107       | Some `POST -> Http_misc.list_assoc_all name query_post_params)
108     method params = params
109     method params_GET = query_get_params
110     method params_POST = query_post_params
111
112     method private fstLineToString =
113       sprintf "%s %s %s"
114         (string_of_method self#meth) self#uri (string_of_version self#version)
115
116   end
117
118 (*    (* OLD IMPLEMENTATION *)
119 class request
120   ~body ~headers ~version ~meth ~uri
121   ~clisockaddr ~srvsockaddr
122   ~path ~params
123   ()
124   =
125   object (self)
126
127     inherit
128       Http_message.message ~body ~headers ~version ~clisockaddr ~srvsockaddr
129
130     val params_tbl =
131       let tbl = Hashtbl.create (List.length params) in
132       List.iter (fun (n,v) -> Hashtbl.add tbl n v) params;
133       tbl
134
135     method meth = meth
136     method uri = uri
137     method path = path
138     method param name =
139       try
140         Hashtbl.find params_tbl name
141       with Not_found ->
142         raise (Param_not_found name)
143     method paramAll name = List.rev (Hashtbl.find_all params_tbl name)
144     method params = params
145
146     method private fstLineToString =
147       sprintf
148         "%s %s %s"
149         (string_of_method self#meth)
150         self#uri
151         (string_of_version self#version)
152
153   end
154 *)
155