2 OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
4 Copyright (C) <2002-2007> Stefano Zacchiroli <zack@cs.unibo.it>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation, version 2.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 let debug_dump_request path params =
27 debug_print ("request path = " ^ path);
29 sprintf"request params = %s"
31 (List.map (fun (h,v) -> String.concat "=" [h;v]) params)))
33 let auth_sep_RE = Pcre.regexp ":"
34 let basic_auth_RE = Pcre.regexp "^Basic\\s+"
36 exception Fallback;; (* used internally by request class *)
39 let (meth, uri, version) = Http_parser.parse_request_fst_line ic in
40 let uri_str = Neturl.string_of_url uri in
41 let path = Http_parser.parse_path uri in
42 let query_get_params = Http_parser.parse_query_get_params uri in
45 | None -> [], "" (* No version given, use request's 1st line only *)
46 | Some version -> (* Version specified, parse also headers and body *)
48 List.map (* lowercase header names to ease lookups before having a
50 (fun (h,v) -> (String.lowercase h, v))
51 (Http_parser.parse_headers ic) (* trailing \r\n consumed! *)
54 (* TODO fallback on size defined in Transfer-Encoding if
55 Content-Length isn't defined *)
58 (try (* read only Content-Length bytes *)
61 List.assoc "content-length" headers
62 with Not_found -> raise Fallback)
65 (try (* TODO supports only a maximum content-length of 1Gb *)
66 int_of_string limit_raw
67 with Failure "int_of_string" ->
68 raise (Invalid_header ("content-length: " ^ limit_raw)))
70 Http_misc.buf_of_inchan ~limit ic
71 with Fallback -> Http_misc.buf_of_inchan ic) (* read until EOF *)
72 else (* TODO empty body for methods other than POST, is ok? *)
79 let _hdr, raw_cookies =
81 (fun (hdr, _cookie) -> String.lowercase hdr = "cookie")
84 Some (Http_parser.parse_cookies raw_cookies)
87 | Malformed_cookies _ -> None
89 let query_post_params =
92 let ct = try List.assoc "content-type" headers with Not_found -> "" in
93 if ct = "application/x-www-form-urlencoded" then
94 Http_parser.split_query_params body
98 let params = query_post_params @ query_get_params in (* prefers POST params *)
99 let _ = debug_dump_request path params in
100 let (clisockaddr, srvsockaddr) =
101 (Http_misc.peername_of_in_channel ic, Http_misc.sockname_of_in_channel ic)
107 Http_message.message ~body ~headers ~version ~clisockaddr ~srvsockaddr
110 let tbl = Hashtbl.create (List.length params) in
111 List.iter (fun (n,v) -> Hashtbl.add tbl n v) params;
117 method param ?(meth: meth option) ?(default: string option) name =
120 | None -> Hashtbl.find params_tbl name
121 | Some `GET -> List.assoc name query_get_params
122 | Some `POST -> List.assoc name query_post_params)
125 | None -> raise (Param_not_found name)
126 | Some value -> value)
127 method paramAll ?meth name =
128 (match (meth: meth option) with
129 | None -> List.rev (Hashtbl.find_all params_tbl name)
130 | Some `GET -> Http_misc.list_assoc_all name query_get_params
131 | Some `POST -> Http_misc.list_assoc_all name query_post_params)
132 method params = params
133 method params_GET = query_get_params
134 method params_POST = query_post_params
136 method cookies = cookies
138 method private fstLineToString =
139 let method_string = string_of_method self#meth in
140 match self#version with
142 sprintf "%s %s %s" method_string self#uri (string_of_version version)
143 | None -> sprintf "%s %s" method_string self#uri
145 method authorization: auth_info option =
148 Netencoding.Base64.decode
149 (Pcre.replace ~rex:basic_auth_RE (self#header "authorization"))
151 debug_print ("HTTP Basic auth credentials: " ^ credentials);
152 (match Pcre.split ~rex:auth_sep_RE credentials with
153 | [username; password] -> Some (`Basic (username, password))
155 with Header_not_found _ | Invalid_argument _ | Exit -> None