3 OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
5 Copyright (C) <2002> Stefano Zacchiroli <zack@cs.unibo.it>
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.
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.
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
27 let debug_dump_request path params =
28 debug_print ("request path = " ^ path);
30 sprintf"request params = %s"
32 (List.map (fun (h,v) -> String.concat "=" [h;v]) params)))
34 exception Fallback;; (* used internally by request class *)
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
42 List.map (* lowercase header names to ease lookups before having a request
44 (fun (h,v) -> (String.lowercase h, v))
45 (Http_parser.parse_headers ic) (* trailing \r\n consumed! *)
48 (* TODO fallback on Transfer-Encoding if Content-Length isn't defined *)
51 (try (* read only Content-Length bytes *)
54 List.assoc "content-length" headers
55 with Not_found -> raise Fallback)
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)))
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? *)
67 let query_post_params =
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
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)
85 Http_message.message ~body ~headers ~version ~clisockaddr ~srvsockaddr
88 let tbl = Hashtbl.create (List.length params) in
89 List.iter (fun (n,v) -> Hashtbl.add tbl n v) params;
95 method param ?meth name =
96 (match (meth: meth option) with
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
112 method private fstLineToString =
114 (string_of_method self#meth) self#uri (string_of_version self#version)
118 (* (* OLD IMPLEMENTATION *)
120 ~body ~headers ~version ~meth ~uri
121 ~clisockaddr ~srvsockaddr
128 Http_message.message ~body ~headers ~version ~clisockaddr ~srvsockaddr
131 let tbl = Hashtbl.create (List.length params) in
132 List.iter (fun (n,v) -> Hashtbl.add tbl n v) params;
140 Hashtbl.find params_tbl name
142 raise (Param_not_found name)
143 method paramAll name = List.rev (Hashtbl.find_all params_tbl name)
144 method params = params
146 method private fstLineToString =
149 (string_of_method self#meth)
151 (string_of_version self#version)