]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/ocaml-http/http_parser_sanity.ml
Initial revision
[helm.git] / helm / DEVEL / ocaml-http / http_parser_sanity.ml
1
2 open Neturl;;
3 open Printf;;
4
5 open Http_types;;
6 open Http_constants;;
7
8 (*
9 type url_syntax_option =
10     Url_part_not_recognized
11   | Url_part_allowed
12   | Url_part_required
13
14 * (1) scheme://user:password@host:port/path;params?query#fragment
15 *)
16
17 let request_uri_syntax = {
18   url_enable_scheme    = Url_part_not_recognized;
19   url_enable_user      = Url_part_not_recognized;
20   url_enable_password  = Url_part_not_recognized;
21   url_enable_host      = Url_part_not_recognized;
22   url_enable_port      = Url_part_not_recognized;
23   url_enable_path      = Url_part_required;
24   url_enable_param     = Url_part_not_recognized;
25   url_enable_query     = Url_part_allowed;
26   url_enable_fragment  = Url_part_not_recognized;
27   url_enable_other     = Url_part_not_recognized;
28   url_accepts_8bits    = false;
29   url_is_valid         = (fun _ -> true);
30 }
31
32   (* convention:
33       foo_RE_raw  is the uncompiled regexp matching foo
34       foo_RE      is the compiled regexp matching foo
35       is_foo      is the predicate over string matching foo
36   *)
37
38 let separators_RE_raw = "()<>@,;:\\\\\"/\\[\\]?={} \t"
39 let ctls_RE_raw = "\\x00-\\x1F\\x7F"
40 let token_RE_raw = "[^" ^ separators_RE_raw ^ ctls_RE_raw ^ "]+"
41 let lws_RE_raw = "(\r\n)?[ \t]"
42 let quoted_string_RE_raw = "\"(([^\"])|(\\\\\"))*\""
43 let text_RE_raw = "(([^" ^ ctls_RE_raw ^ "])|(" ^ lws_RE_raw ^ "))+"
44 let field_content_RE_raw =
45   sprintf
46     "^(((%s)|(%s)|(%s))|(%s))*$"
47     token_RE_raw
48     separators_RE_raw
49     quoted_string_RE_raw
50     text_RE_raw
51 (*
52   (* following RFC 2616 specifications *)
53 let field_value_RE_raw = "((" ^ field_content_RE_raw ^ ")|(" ^ lws_RE_raw^ "))*"
54 *)
55   (* smarter implementation: TEXT production is included in the regexp below *)
56 let field_value_RE_raw =
57   sprintf
58     "^((%s)|(%s)|(%s)|(%s))*$"
59     token_RE_raw
60     separators_RE_raw
61     quoted_string_RE_raw
62     lws_RE_raw
63
64 let token_RE = Pcre.regexp ("^" ^ token_RE_raw ^ "$")
65 let field_value_RE = Pcre.regexp ("^" ^ field_value_RE_raw ^ "$")
66 let heading_lws_RE = Pcre.regexp (sprintf "^%s*" lws_RE_raw)
67 let trailing_lws_RE = Pcre.regexp (sprintf "%s*$" lws_RE_raw)
68
69 let is_token s = Pcre.pmatch ~rex:token_RE s
70 let is_field_name = is_token
71 let is_field_value s = Pcre.pmatch ~rex:field_value_RE s
72
73 let heal_header_name s =
74   if not (is_field_name s) then raise (Invalid_header_name s) else ()
75
76 let heal_header_value s =
77   if not (is_field_value s) then raise (Invalid_header_value s) else ()
78
79 let normalize_header_value s =
80   Pcre.replace ~rex:trailing_lws_RE
81     (Pcre.replace ~rex:heading_lws_RE s)
82
83 let heal_header (name, value) =
84   heal_header_name name;
85   heal_header_value name
86  
87 let url_of_string = url_of_string request_uri_syntax
88 let string_of_url = Neturl.string_of_url
89