]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/ocaml-http/http_parser_sanity.ml
ocaml 3.09 transition
[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-2005> 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 Library General Public License as
9   published by the Free Software Foundation, version 2.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU Library General Public License for more details.
15
16   You should have received a copy of the GNU Library General Public
17   License along with this program; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
19   USA
20 *)
21
22 open Printf
23
24 open Http_types
25 open Http_constants
26
27 (*
28 type url_syntax_option =
29     Url_part_not_recognized
30   | Url_part_allowed
31   | Url_part_required
32
33 * (1) scheme://user:password@host:port/path;params?query#fragment
34 *)
35
36 let request_uri_syntax =
37 {
38   Neturl.url_enable_scheme  = Neturl.Url_part_not_recognized;
39   url_enable_user           = Neturl.Url_part_not_recognized;
40   url_enable_user_param     = Neturl.Url_part_not_recognized;
41   url_enable_password       = Neturl.Url_part_not_recognized;
42   url_enable_host           = Neturl.Url_part_not_recognized;
43   url_enable_port           = Neturl.Url_part_not_recognized;
44   url_enable_path           = Neturl.Url_part_required;
45   url_enable_param          = Neturl.Url_part_not_recognized;
46   url_enable_query          = Neturl.Url_part_allowed;
47   url_enable_fragment       = Neturl.Url_part_not_recognized;
48   url_enable_other          = Neturl.Url_part_not_recognized;
49   url_accepts_8bits         = false;
50   url_enable_relative       = true;
51   url_is_valid              = (fun _ -> true);
52 }
53
54   (* convention:
55       foo_RE_raw  is the uncompiled regexp matching foo
56       foo_RE      is the compiled regexp matching foo
57       is_foo      is the predicate over string matching foo
58   *)
59
60 let separators_RE_raw = "()<>@,;:\\\\\"/\\[\\]?={} \t"
61 let ctls_RE_raw = "\\x00-\\x1F\\x7F"
62 let token_RE_raw = "[^" ^ separators_RE_raw ^ ctls_RE_raw ^ "]+"
63 let lws_RE_raw = "(\r\n)?[ \t]"
64 let quoted_string_RE_raw = "\"(([^\"])|(\\\\\"))*\""
65 let text_RE_raw = "(([^" ^ ctls_RE_raw ^ "])|(" ^ lws_RE_raw ^ "))+"
66 let field_content_RE_raw =
67   sprintf
68     "^(((%s)|(%s)|(%s))|(%s))*$"
69     token_RE_raw
70     separators_RE_raw
71     quoted_string_RE_raw
72     text_RE_raw
73 (*
74   (* following RFC 2616 specifications *)
75 let field_value_RE_raw = "((" ^ field_content_RE_raw ^ ")|(" ^ lws_RE_raw^ "))*"
76 *)
77   (* smarter implementation: TEXT production is included in the regexp below *)
78 let field_value_RE_raw =
79   sprintf
80     "^((%s)|(%s)|(%s)|(%s))*$"
81     token_RE_raw
82     separators_RE_raw
83     quoted_string_RE_raw
84     lws_RE_raw
85
86 let token_RE = Pcre.regexp ("^" ^ token_RE_raw ^ "$")
87 let field_value_RE = Pcre.regexp ("^" ^ field_value_RE_raw ^ "$")
88 let heading_lws_RE = Pcre.regexp (sprintf "^%s*" lws_RE_raw)
89 let trailing_lws_RE = Pcre.regexp (sprintf "%s*$" lws_RE_raw)
90
91 let is_token s = Pcre.pmatch ~rex:token_RE s
92 let is_field_name = is_token
93 let is_field_value s = Pcre.pmatch ~rex:field_value_RE s
94
95 let heal_header_name s =
96   if not (is_field_name s) then raise (Invalid_header_name s) else ()
97
98 let heal_header_value s =
99   if not (is_field_value s) then raise (Invalid_header_value s) else ()
100
101 let normalize_header_value s =
102   Pcre.replace ~rex:trailing_lws_RE
103     (Pcre.replace ~rex:heading_lws_RE s)
104
105 let heal_header (name, value) =
106   heal_header_name name;
107   heal_header_value name
108  
109 let url_of_string s =
110   try
111     Neturl.url_of_string request_uri_syntax s
112   with Neturl.Malformed_URL -> raise (Malformed_URL s)
113
114 let string_of_url = Neturl.string_of_url
115