]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/ocaml-http/http_parser.ml
- split http_parser module (all code that parse http requests and
[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
24 exception Malformed_query of string
25 exception Malformed_query_binding of string * string
26 exception Unsupported_method of string
27 exception Unsupported_HTTP_version of string
28 exception Malformed_request_URI of string
29 exception Malformed_request of string
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   (** given a list of length 2
56   @return a pair formed by the elements of the list
57   @raise Assert_failure if the list length isn't 2
58   *)
59 let pair_of_2_sized_list = function
60   | [a;b] -> (a,b)
61   | _ -> assert false
62
63   (** given an HTTP like query string (e.g. "name1=value1&name2=value2&...")
64   @return a list of pairs [("name1", "value1"); ("name2", "value2")]
65   @raise Malformed_query if the string isn't a valid query string
66   @raise Malformed_query_binding if some piece of the query isn't valid
67   *)
68 let split_query_params =
69   let (bindings_sep, binding_sep) = (Pcre.regexp "&", Pcre.regexp "=") in
70   fun ~query ->
71     let bindings = Pcre.split ~rex:bindings_sep query in
72     if List.length bindings < 1 then
73       raise (Malformed_query query);
74     List.map
75       (fun binding ->
76         let pieces = Pcre.split ~rex:binding_sep binding in
77         if List.length pieces <> 2 then
78           raise (Malformed_query_binding (binding, query));
79         pair_of_2_sized_list pieces)
80       bindings
81
82   (** given an input channel and a separator
83   @return a line read from it (like Pervasives.input_line)
84   line is returned only after reading a separator string; separator string isn't
85   included in the returned value
86   FIXME what about efficiency?, input is performed char-by-char
87   *)
88 let generic_input_line ~sep ~ic =
89   let sep_len = String.length sep in
90   if sep_len < 1 then
91     failwith ("Separator '" ^ sep ^ "' is too short!")
92   else  (* valid separator *)
93     let line = ref "" in
94     let sep_pointer = ref 0 in
95     try
96       while true do
97         if !sep_pointer >= String.length sep then (* line completed *)
98           raise End_of_file
99         else begin (* incomplete line: need to read more *)
100           let ch = input_char ic in
101           if ch = String.get sep !sep_pointer then  (* next piece of sep *)
102             incr sep_pointer
103           else begin  (* useful char *)
104             for i = 0 to !sep_pointer - 1 do
105               line := !line ^ (String.make 1 (String.get sep i))
106             done;
107             sep_pointer := 0;
108             line := !line ^ (String.make 1 ch)
109           end
110         end
111       done;
112       assert false  (* unreacheable statement *)
113     with End_of_file ->
114       if !line = "" then
115         raise End_of_file
116       else
117         !line
118
119   (** given an input channel, reads from it a GET HTTP request and
120   @return a pair <path, query_params> where path is a string representing the
121   requested path and query_params is a list of pairs <name, value> (the GET
122   parameters)
123   *)
124 let parse_request =
125   let patch_empty_path s = (if s = "" then "/" else s) in
126   let pieces_sep = Pcre.regexp " " in
127   fun ic ->
128     let request_line = generic_input_line ~sep:Http_common.crlf ~ic in
129     match Pcre.split ~rex:pieces_sep request_line with
130     | [meth; request_uri_raw; http_version] ->
131         if meth <> "GET" then
132           raise (Unsupported_method meth);
133         (match http_version with
134         | "HTTP/1.0" | "HTTP/1.1" -> ()
135         | _ -> raise (Unsupported_HTTP_version http_version));
136         let request_uri =
137           try
138             url_of_string request_uri_syntax request_uri_raw
139           with Malformed_URL ->
140             raise (Malformed_request_URI request_uri_raw)
141         in
142         let path =
143           patch_empty_path (String.concat "/" (url_path request_uri))
144         in
145         let query_params =
146           try split_query_params (url_query request_uri) with Not_found -> []
147         in
148         (path, query_params)
149     | _ -> raise (Malformed_request request_line)
150