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
28 let (bindings_sep, binding_sep, pieces_sep, header_sep) =
29 (Pcre.regexp "&", Pcre.regexp "=", Pcre.regexp " ", Pcre.regexp ":")
30 let header_RE = Pcre.regexp "([^:]*):(.*)"
32 let url_decode url = Netencoding.Url.decode ~plus:true url
34 (** given an HTTP like query string (e.g. "name1=value1&name2=value2&...")
35 @return a list of pairs [("name1", "value1"); ("name2", "value2")]
36 @raise Malformed_query if the string isn't a valid query string
37 @raise Malformed_query_part if some piece of the query isn't valid
39 let split_query_params query =
40 let bindings = Pcre.split ~rex:bindings_sep query in
42 | [] -> raise (Malformed_query query)
46 match Pcre.split ~rex:binding_sep binding with
47 | [ ""; b ] -> (* '=b' *)
48 raise (Malformed_query_part (binding, query))
49 | [ a; b ] -> (* 'a=b' *) (url_decode a, url_decode b)
50 | [ a ] -> (* 'a=' || 'a' *) (url_decode a, "")
51 | _ -> raise (Malformed_query_part (binding, query)))
54 (** internal, used by generic_input_line *)
55 exception Line_completed;;
57 (** given an input channel and a separator
58 @return a line read from it (like Pervasives.input_line)
59 line is returned only after reading a separator string; separator string isn't
60 included in the returned value
61 TODO what about efficiency?, input is performed char-by-char
63 let generic_input_line ~sep ~ic =
64 let sep_len = String.length sep in
66 failwith ("Separator '" ^ sep ^ "' is too short!")
67 else (* valid separator *)
69 let sep_pointer = ref 0 in
72 if !sep_pointer >= String.length sep then (* line completed *)
74 else begin (* incomplete line: need to read more *)
75 let ch = input_char ic in
76 if ch = String.get sep !sep_pointer then (* next piece of sep *)
78 else begin (* useful char *)
79 for i = 0 to !sep_pointer - 1 do
80 line := !line ^ (String.make 1 (String.get sep i))
83 line := !line ^ (String.make 1 ch)
87 assert false (* unreacheable statement *)
88 with Line_completed -> !line
90 let patch_empty_path = function "" -> "/" | s -> s
91 let debug_dump_request path params =
94 "recevied request; path: %s; params: %s"
96 (String.concat ", " (List.map (fun (n, v) -> n ^ "=" ^ v) params)))
98 let parse_request_fst_line ic =
99 let request_line = generic_input_line ~sep:crlf ~ic in
100 match Pcre.split ~rex:pieces_sep request_line with
101 | [ meth_raw; uri_raw; http_version_raw ] ->
103 (method_of_string meth_raw, (* method *)
104 Http_parser_sanity.url_of_string uri_raw, (* uri *)
105 version_of_string http_version_raw) (* version *)
106 with Neturl.Malformed_URL -> raise (Malformed_request_URI uri_raw))
107 | _ -> raise (Malformed_request request_line)
109 let parse_path uri = patch_empty_path (String.concat "/" (Neturl.url_path uri))
110 let parse_query_get_params uri =
111 try (* act on HTTP encoded URIs *)
112 split_query_params (Neturl.url_query ~encoded:true uri)
115 let parse_headers ic =
116 (* consume also trailing "^\r\n$" line *)
117 let rec parse_headers' headers =
118 match generic_input_line ~sep:crlf ~ic with
119 | "" -> List.rev headers
121 (let subs = Pcre.extract ~rex:header_RE line in
125 with Invalid_argument "Array.get" -> raise (Invalid_header line)
129 Http_parser_sanity.normalize_header_value subs.(2)
130 with Invalid_argument "Array.get" -> ""
132 Http_parser_sanity.heal_header (header, value);
133 parse_headers' ((header, value) :: headers))
137 let parse_request ic =
138 let (meth, uri, version) = parse_request_fst_line ic in
139 let path = parse_path uri in
140 let query_get_params = parse_query_get_params uri in
141 debug_dump_request path query_get_params;
142 (path, query_get_params)