]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/ocaml-http/http_parser_sanity.ml
first moogle template checkin
[helm.git] / helm / DEVEL / ocaml-http / http_parser_sanity.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 open Http_types;;
26 open Http_constants;;
27
28 (*
29 type url_syntax_option =
30     Url_part_not_recognized
31   | Url_part_allowed
32   | Url_part_required
33
34 * (1) scheme://user:password@host:port/path;params?query#fragment
35 *)
36
37 let request_uri_syntax = {
38   url_enable_scheme    = Url_part_not_recognized;
39   url_enable_user      = Url_part_not_recognized;
40   url_enable_password  = Url_part_not_recognized;
41   url_enable_host      = Url_part_not_recognized;
42   url_enable_port      = Url_part_not_recognized;
43   url_enable_path      = Url_part_required;
44   url_enable_param     = Url_part_not_recognized;
45   url_enable_query     = Url_part_allowed;
46   url_enable_fragment  = Url_part_not_recognized;
47   url_enable_other     = Url_part_not_recognized;
48   url_accepts_8bits    = false;
49   url_is_valid         = (fun _ -> true);
50 }
51
52   (* convention:
53       foo_RE_raw  is the uncompiled regexp matching foo
54       foo_RE      is the compiled regexp matching foo
55       is_foo      is the predicate over string matching foo
56   *)
57
58 let separators_RE_raw = "()<>@,;:\\\\\"/\\[\\]?={} \t"
59 let ctls_RE_raw = "\\x00-\\x1F\\x7F"
60 let token_RE_raw = "[^" ^ separators_RE_raw ^ ctls_RE_raw ^ "]+"
61 let lws_RE_raw = "(\r\n)?[ \t]"
62 let quoted_string_RE_raw = "\"(([^\"])|(\\\\\"))*\""
63 let text_RE_raw = "(([^" ^ ctls_RE_raw ^ "])|(" ^ lws_RE_raw ^ "))+"
64 let field_content_RE_raw =
65   sprintf
66     "^(((%s)|(%s)|(%s))|(%s))*$"
67     token_RE_raw
68     separators_RE_raw
69     quoted_string_RE_raw
70     text_RE_raw
71 (*
72   (* following RFC 2616 specifications *)
73 let field_value_RE_raw = "((" ^ field_content_RE_raw ^ ")|(" ^ lws_RE_raw^ "))*"
74 *)
75   (* smarter implementation: TEXT production is included in the regexp below *)
76 let field_value_RE_raw =
77   sprintf
78     "^((%s)|(%s)|(%s)|(%s))*$"
79     token_RE_raw
80     separators_RE_raw
81     quoted_string_RE_raw
82     lws_RE_raw
83
84 let token_RE = Pcre.regexp ("^" ^ token_RE_raw ^ "$")
85 let field_value_RE = Pcre.regexp ("^" ^ field_value_RE_raw ^ "$")
86 let heading_lws_RE = Pcre.regexp (sprintf "^%s*" lws_RE_raw)
87 let trailing_lws_RE = Pcre.regexp (sprintf "%s*$" lws_RE_raw)
88
89 let is_token s = Pcre.pmatch ~rex:token_RE s
90 let is_field_name = is_token
91 let is_field_value s = Pcre.pmatch ~rex:field_value_RE s
92
93 let heal_header_name s =
94   if not (is_field_name s) then raise (Invalid_header_name s) else ()
95
96 let heal_header_value s =
97   if not (is_field_value s) then raise (Invalid_header_value s) else ()
98
99 let normalize_header_value s =
100   Pcre.replace ~rex:trailing_lws_RE
101     (Pcre.replace ~rex:heading_lws_RE s)
102
103 let heal_header (name, value) =
104   heal_header_name name;
105   heal_header_value name
106  
107 let url_of_string s =
108   try
109     url_of_string request_uri_syntax s
110   with Neturl.Malformed_URL -> raise (Malformed_URL s)
111 let string_of_url = Neturl.string_of_url
112