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
22 (* TODO some useless function here *)
23 (* TODO remove is_http* from mli? *)
32 type url_syntax_option =
33 Url_part_not_recognized
37 * (1) scheme://user:password@host:port/path;params?query#fragment
40 let request_uri_syntax = {
41 url_enable_scheme = Url_part_not_recognized;
42 url_enable_user = Url_part_not_recognized;
43 url_enable_password = Url_part_not_recognized;
44 url_enable_host = Url_part_not_recognized;
45 url_enable_port = Url_part_not_recognized;
46 url_enable_path = Url_part_required;
47 url_enable_param = Url_part_not_recognized;
48 url_enable_query = Url_part_allowed;
49 url_enable_fragment = Url_part_not_recognized;
50 url_enable_other = Url_part_not_recognized;
51 url_accepts_8bits = false;
52 url_is_valid = (fun _ -> true);
55 module CharSet = Set.Make (Char)
57 (** create an "is in" predicate over a character set using an efficient,
58 set-based implementation *)
59 let mk_char_predicate chars =
61 List.fold_left (fun oldset c -> CharSet.add c oldset) CharSet.empty chars
63 fun c -> CharSet.mem c charset
65 let is_http_separator =
67 [ '('; ')'; '<'; '>'; '@'; ','; ';'; ':'; '\\'; '"'; '/'; '['; ']'; '?';
68 '='; '{'; '}'; ' '; '\t' ]
71 match Char.code c with
72 | c when (((c >= 0) && (c <= 31)) || (c = 127)) -> true
75 (* internal: used to implement is_* functions *)
76 exception Invalid_char;;
82 if (is_http_ctl c) || (is_http_separator) c then raise Invalid_char)
85 with Invalid_char -> false
87 let rec is_http_lws s =
92 (s.[1] = '\n') && ((s.[2] = ' ') || (s.[2] = '\t'))
93 with Invalid_argument "String.get" -> false)
96 let is_http_field_name = is_http_token
98 let is_http_field_value s =
99 let rec strip_quoted_string = function
101 | '"' :: tl -> (true, tl)
102 | '\\' :: '"' :: tl -> strip_quoted_string tl
103 | hd :: tl -> strip_quoted_string tl
105 let rec is_http_field_value' = function
106 | '\r' :: '\n' :: sp :: rest when (sp = ' ' || sp = '\t') -> (* strip LWS *)
107 is_http_field_value' rest
108 | c :: rest when (is_http_ctl c && c <> '\t') -> (* \t is in CTL /\ SEP *)
109 false (* CTL aren't allowed *)
111 let (valid, rest) = strip_quoted_string rest in
112 if not valid then false else is_http_field_value' rest
113 | c :: rest -> is_http_field_value' rest
115 in is_http_field_value' (Http_misc.string_explode s)
117 let heal_header (name, value) =
118 if not (is_http_field_name name && is_http_field_value value) then
119 raise (Invalid_header (name ^ ": " ^ value))
123 (** given an HTTP like query string (e.g. "name1=value1&name2=value2&...")
124 @return a list of pairs [("name1", "value1"); ("name2", "value2")]
125 @raise Malformed_query if the string isn't a valid query string
126 @raise Malformed_query_part if some piece of the query isn't valid
128 let split_query_params =
129 let (bindings_sep, binding_sep) = (Pcre.regexp "&", Pcre.regexp "=") in
130 let http_decode url = Netencoding.Url.decode ~plus:false url in
132 let bindings = Pcre.split ~rex:bindings_sep query in
133 if List.length bindings < 1 then
134 raise (Malformed_query query);
137 match Pcre.split ~rex:binding_sep binding with
138 | [""; b] -> (* '=b' *) raise (Malformed_query_part (binding, query))
139 | [a; b] -> (* 'a=b' *) (http_decode a, http_decode b)
140 | [a] -> (* 'a=' || 'a' *) (http_decode a, "")
141 | _ -> raise (Malformed_query_part (binding, query)))
144 (** given an input channel and a separator
145 @return a line read from it (like Pervasives.input_line)
146 line is returned only after reading a separator string; separator string isn't
147 included in the returned value
148 FIXME what about efficiency?, input is performed char-by-char
150 let generic_input_line ~sep ~ic =
151 let sep_len = String.length sep in
153 failwith ("Separator '" ^ sep ^ "' is too short!")
154 else (* valid separator *)
156 let sep_pointer = ref 0 in
159 if !sep_pointer >= String.length sep then (* line completed *)
161 else begin (* incomplete line: need to read more *)
162 let ch = input_char ic in
163 if ch = String.get sep !sep_pointer then (* next piece of sep *)
165 else begin (* useful char *)
166 for i = 0 to !sep_pointer - 1 do
167 line := !line ^ (String.make 1 (String.get sep i))
170 line := !line ^ (String.make 1 ch)
174 assert false (* unreacheable statement *)
181 (** given an input channel, reads from it a GET HTTP request and
182 @return a pair <path, query_params> where path is a string representing the
183 requested path and query_params is a list of pairs <name, value> (the GET
187 let patch_empty_path s = (if s = "" then "/" else s) in
188 let pieces_sep = Pcre.regexp " " in
190 let request_line = generic_input_line ~sep:crlf ~ic in
191 match Pcre.split ~rex:pieces_sep request_line with
192 | [meth; request_uri_raw; http_version] ->
193 if meth <> "GET" then
194 raise (Unsupported_method meth);
195 (match http_version with
196 | "HTTP/1.0" | "HTTP/1.1" -> ()
197 | _ -> raise (Unsupported_HTTP_version http_version));
200 url_of_string request_uri_syntax request_uri_raw
201 with Malformed_URL ->
202 raise (Malformed_request_URI request_uri_raw)
205 patch_empty_path (String.concat "/" (url_path request_uri))
208 try (* act on HTTP encoded URIs *)
209 split_query_params (url_query ~encoded:true request_uri)
212 Http_common.debug_print
214 "recevied request; path: %s; params: %s"
218 (List.map (fun (n, v) -> n ^ "=" ^ v) query_params)));
220 | _ -> raise (Malformed_request request_line)