]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/ocaml-http/http_parser.ml
- added support for empty bindings like "a=" or simple "a" in query
[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 open Neturl;;
23 open Printf;;
24
25 exception Malformed_query of string
26 exception Malformed_query_part of string * string
27 exception Unsupported_method of string
28 exception Unsupported_HTTP_version of string
29 exception Malformed_request_URI of string
30 exception Malformed_request of string
31
32 (*
33 type url_syntax_option =
34     Url_part_not_recognized
35   | Url_part_allowed
36   | Url_part_required
37
38 * (1) scheme://user:password@host:port/path;params?query#fragment
39 *)
40
41 let request_uri_syntax = {
42   url_enable_scheme    = Url_part_not_recognized;
43   url_enable_user      = Url_part_not_recognized;
44   url_enable_password  = Url_part_not_recognized;
45   url_enable_host      = Url_part_not_recognized;
46   url_enable_port      = Url_part_not_recognized;
47   url_enable_path      = Url_part_required;
48   url_enable_param     = Url_part_not_recognized;
49   url_enable_query     = Url_part_allowed;
50   url_enable_fragment  = Url_part_not_recognized;
51   url_enable_other     = Url_part_not_recognized;
52   url_accepts_8bits    = false;
53   url_is_valid         = (fun _ -> true);
54 }
55
56   (** given an HTTP like query string (e.g. "name1=value1&name2=value2&...")
57   @return a list of pairs [("name1", "value1"); ("name2", "value2")]
58   @raise Malformed_query if the string isn't a valid query string
59   @raise Malformed_query_part if some piece of the query isn't valid
60   *)
61 let split_query_params =
62   let (bindings_sep, binding_sep) = (Pcre.regexp "&", Pcre.regexp "=") in
63   let http_decode url = Netencoding.Url.decode ~plus:false url in
64   fun ~query ->
65     let bindings = Pcre.split ~rex:bindings_sep query in
66     if List.length bindings < 1 then
67       raise (Malformed_query query);
68     List.map
69       (fun binding ->
70         match Pcre.split ~rex:binding_sep binding with
71         | [""; b] -> (* '=b' *) raise (Malformed_query_part (binding, query))
72         | [a; b]  -> (* 'a=b' *) (http_decode a, http_decode b)
73         | [a]     -> (* 'a=' || 'a' *) (http_decode a, "")
74         | _ -> raise (Malformed_query_part (binding, query)))
75       bindings
76
77   (** given an input channel and a separator
78   @return a line read from it (like Pervasives.input_line)
79   line is returned only after reading a separator string; separator string isn't
80   included in the returned value
81   FIXME what about efficiency?, input is performed char-by-char
82   *)
83 let generic_input_line ~sep ~ic =
84   let sep_len = String.length sep in
85   if sep_len < 1 then
86     failwith ("Separator '" ^ sep ^ "' is too short!")
87   else  (* valid separator *)
88     let line = ref "" in
89     let sep_pointer = ref 0 in
90     try
91       while true do
92         if !sep_pointer >= String.length sep then (* line completed *)
93           raise End_of_file
94         else begin (* incomplete line: need to read more *)
95           let ch = input_char ic in
96           if ch = String.get sep !sep_pointer then  (* next piece of sep *)
97             incr sep_pointer
98           else begin  (* useful char *)
99             for i = 0 to !sep_pointer - 1 do
100               line := !line ^ (String.make 1 (String.get sep i))
101             done;
102             sep_pointer := 0;
103             line := !line ^ (String.make 1 ch)
104           end
105         end
106       done;
107       assert false  (* unreacheable statement *)
108     with End_of_file ->
109       if !line = "" then
110         raise End_of_file
111       else
112         !line
113
114   (** given an input channel, reads from it a GET HTTP request and
115   @return a pair <path, query_params> where path is a string representing the
116   requested path and query_params is a list of pairs <name, value> (the GET
117   parameters)
118   *)
119 let parse_request =
120   let patch_empty_path s = (if s = "" then "/" else s) in
121   let pieces_sep = Pcre.regexp " " in
122   fun ic ->
123     let request_line = generic_input_line ~sep:Http_common.crlf ~ic in
124     match Pcre.split ~rex:pieces_sep request_line with
125     | [meth; request_uri_raw; http_version] ->
126         if meth <> "GET" then
127           raise (Unsupported_method meth);
128         (match http_version with
129         | "HTTP/1.0" | "HTTP/1.1" -> ()
130         | _ -> raise (Unsupported_HTTP_version http_version));
131         let request_uri =
132           try
133             url_of_string request_uri_syntax request_uri_raw
134           with Malformed_URL ->
135             raise (Malformed_request_URI request_uri_raw)
136         in
137         let path =
138           patch_empty_path (String.concat "/" (url_path request_uri))
139         in
140         let query_params =
141           try (* act on HTTP encoded URIs *)
142             split_query_params (url_query ~encoded:true request_uri)
143           with Not_found -> []
144         in
145         Http_common.debug_print
146           (sprintf
147             "recevied request; path: %s; params: %s"
148             path
149             (String.concat
150               ", "
151               (List.map (fun (n, v) -> n ^ "=" ^ v) query_params)));
152         (path, query_params)
153     | _ -> raise (Malformed_request request_line)
154