3 OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
5 Copyright (C) <2002-2005> 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 Library General Public License as
9 published by the Free Software Foundation, version 2.
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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
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 let auth_sep_RE = Pcre.regexp ":"
35 let basic_auth_RE = Pcre.regexp "^Basic\\s+"
37 exception Fallback;; (* used internally by request class *)
40 let (meth, uri, version) = Http_parser.parse_request_fst_line ic in
41 let uri_str = Neturl.string_of_url uri in
42 let path = Http_parser.parse_path uri in
43 let query_get_params = Http_parser.parse_query_get_params uri in
46 | None -> [], "" (* No version given, use request's 1st line only *)
47 | Some version -> (* Version specified, parse also headers and body *)
49 List.map (* lowercase header names to ease lookups before having a
51 (fun (h,v) -> (String.lowercase h, v))
52 (Http_parser.parse_headers ic) (* trailing \r\n consumed! *)
55 (* TODO fallback on size defined in Transfer-Encoding if
56 Content-Length isn't defined *)
59 (try (* read only Content-Length bytes *)
62 List.assoc "content-length" headers
63 with Not_found -> raise Fallback)
66 (try (* TODO supports only a maximum content-length of 1Gb *)
67 int_of_string limit_raw
68 with Failure "int_of_string" ->
69 raise (Invalid_header ("content-length: " ^ limit_raw)))
71 Http_misc.buf_of_inchan ~limit ic
72 with Fallback -> Http_misc.buf_of_inchan ic) (* read until EOF *)
73 else (* TODO empty body for methods other than POST, is ok? *)
78 let query_post_params =
81 let ct = try List.assoc "content-type" headers with Not_found -> "" in
82 if ct = "application/x-www-form-urlencoded" then
83 Http_parser.split_query_params body
87 let params = query_post_params @ query_get_params in (* prefers POST params *)
88 let _ = debug_dump_request path params in
89 let (clisockaddr, srvsockaddr) =
90 (Http_misc.peername_of_in_channel ic, Http_misc.sockname_of_in_channel ic)
96 Http_message.message ~body ~headers ~version ~clisockaddr ~srvsockaddr
99 let tbl = Hashtbl.create (List.length params) in
100 List.iter (fun (n,v) -> Hashtbl.add tbl n v) params;
106 method param ?(meth: meth option) ?(default: string option) name =
109 | None -> Hashtbl.find params_tbl name
110 | Some `GET -> List.assoc name query_get_params
111 | Some `POST -> List.assoc name query_post_params)
114 | None -> raise (Param_not_found name)
115 | Some value -> value)
116 method paramAll ?meth name =
117 (match (meth: meth option) with
118 | None -> List.rev (Hashtbl.find_all params_tbl name)
119 | Some `GET -> Http_misc.list_assoc_all name query_get_params
120 | Some `POST -> Http_misc.list_assoc_all name query_post_params)
121 method params = params
122 method params_GET = query_get_params
123 method params_POST = query_post_params
125 method private fstLineToString =
126 let method_string = string_of_method self#meth in
127 match self#version with
129 sprintf "%s %s %s" method_string self#uri (string_of_version version)
130 | None -> sprintf "%s %s" method_string self#uri
132 method authorization: auth_info option =
135 Netencoding.Base64.decode
136 (Pcre.replace ~rex:basic_auth_RE (self#header "authorization"))
138 debug_print ("HTTP Basic auth credentials: " ^ credentials);
139 (match Pcre.split ~rex:auth_sep_RE credentials with
140 | [username; password] -> Some (`Basic (username, password))
142 with Header_not_found _ | Invalid_argument _ | Exit -> None