]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/ocaml-http/http_parser.ml
- added two TODO comment
[helm.git] / helm / DEVEL / ocaml-http / http_parser.ml
1
2 (*
3   OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
4
5   Copyright (C) <2002> Stefano Zacchiroli <zack@cs.unibo.it>
6
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.
11
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.
16
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
20 *)
21
22 (* TODO some useless function here *)
23 (* TODO remove is_http* from mli? *)
24
25 open Neturl;;
26 open Printf;;
27
28 open Http_types;;
29 open Http_constants;;
30
31 (*
32 type url_syntax_option =
33     Url_part_not_recognized
34   | Url_part_allowed
35   | Url_part_required
36
37 * (1) scheme://user:password@host:port/path;params?query#fragment
38 *)
39
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);
53 }
54
55 module CharSet = Set.Make (Char)
56
57   (** create an "is in" predicate over a character set using an efficient,
58   set-based implementation *)
59 let mk_char_predicate chars =
60   let charset =
61     List.fold_left (fun oldset c -> CharSet.add c oldset) CharSet.empty chars
62   in
63   fun c -> CharSet.mem c charset
64
65 let is_http_separator =
66   mk_char_predicate
67     [ '('; ')'; '<'; '>'; '@'; ','; ';'; ':'; '\\'; '"'; '/'; '['; ']'; '?';
68     '='; '{'; '}'; ' '; '\t' ]
69
70 let is_http_ctl c =
71   match Char.code c with
72   | c when (((c >= 0) && (c <= 31)) || (c = 127)) -> true
73   | _ -> false
74
75   (* internal: used to implement is_* functions *)
76 exception Invalid_char;;
77
78 let is_http_token s =
79   try
80     String.iter
81       (fun c ->
82         if (is_http_ctl c) || (is_http_separator) c then raise Invalid_char)
83       s;
84     true
85   with Invalid_char -> false
86
87 let rec is_http_lws s =
88   (match s.[0] with
89   | ' ' | '\t' -> true
90   | '\r' ->
91       (try
92         (s.[1] = '\n') && ((s.[2] = ' ') || (s.[2] = '\t'))
93       with Invalid_argument "String.get" -> false)
94   | _ -> false)
95
96 let is_http_field_name = is_http_token
97
98 let is_http_field_value s =
99   let rec strip_quoted_string = function
100     | [] -> (false, [])
101     | '"' :: tl -> (true, tl)
102     | '\\' :: '"' :: tl -> strip_quoted_string tl
103     | hd :: tl -> strip_quoted_string tl
104   in
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 *)
110     | '"' :: rest ->
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
114     | [] -> true
115   in is_http_field_value' (Http_misc.string_explode s)
116
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))
120   else
121     ()
122
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
127   *)
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
131   fun ~query ->
132     let bindings = Pcre.split ~rex:bindings_sep query in
133     if List.length bindings < 1 then
134       raise (Malformed_query query);
135     List.map
136       (fun binding ->
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)))
142       bindings
143
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
149   *)
150 let generic_input_line ~sep ~ic =
151   let sep_len = String.length sep in
152   if sep_len < 1 then
153     failwith ("Separator '" ^ sep ^ "' is too short!")
154   else  (* valid separator *)
155     let line = ref "" in
156     let sep_pointer = ref 0 in
157     try
158       while true do
159         if !sep_pointer >= String.length sep then (* line completed *)
160           raise End_of_file
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 *)
164             incr sep_pointer
165           else begin  (* useful char *)
166             for i = 0 to !sep_pointer - 1 do
167               line := !line ^ (String.make 1 (String.get sep i))
168             done;
169             sep_pointer := 0;
170             line := !line ^ (String.make 1 ch)
171           end
172         end
173       done;
174       assert false  (* unreacheable statement *)
175     with End_of_file ->
176       if !line = "" then
177         raise End_of_file
178       else
179         !line
180
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
184   parameters)
185   *)
186 let parse_request =
187   let patch_empty_path s = (if s = "" then "/" else s) in
188   let pieces_sep = Pcre.regexp " " in
189   fun ic ->
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));
198         let request_uri =
199           try
200             url_of_string request_uri_syntax request_uri_raw
201           with Malformed_URL ->
202             raise (Malformed_request_URI request_uri_raw)
203         in
204         let path =
205           patch_empty_path (String.concat "/" (url_path request_uri))
206         in
207         let query_params =
208           try (* act on HTTP encoded URIs *)
209             split_query_params (url_query ~encoded:true request_uri)
210           with Not_found -> []
211         in
212         Http_common.debug_print
213           (sprintf
214             "recevied request; path: %s; params: %s"
215             path
216             (String.concat
217               ", "
218               (List.map (fun (n, v) -> n ^ "=" ^ v) query_params)));
219         (path, query_params)
220     | _ -> raise (Malformed_request request_line)
221