]> matita.cs.unibo.it Git - helm.git/blobdiff - 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
index 87b910010e5f9cbd8d3cf6b44a26fda85857df48..8a16398e88c97c39807b9a6862f258afee99619c 100644 (file)
@@ -23,7 +23,7 @@ open Neturl;;
 open Printf;;
 
 exception Malformed_query of string
-exception Malformed_query_binding of string * string
+exception Malformed_query_part of string * string
 exception Unsupported_method of string
 exception Unsupported_HTTP_version of string
 exception Malformed_request_URI of string
@@ -56,7 +56,7 @@ let request_uri_syntax = {
   (** given an HTTP like query string (e.g. "name1=value1&name2=value2&...")
   @return a list of pairs [("name1", "value1"); ("name2", "value2")]
   @raise Malformed_query if the string isn't a valid query string
-  @raise Malformed_query_binding if some piece of the query isn't valid
+  @raise Malformed_query_part if some piece of the query isn't valid
   *)
 let split_query_params =
   let (bindings_sep, binding_sep) = (Pcre.regexp "&", Pcre.regexp "=") in
@@ -67,12 +67,11 @@ let split_query_params =
       raise (Malformed_query query);
     List.map
       (fun binding ->
-        let pieces = Pcre.split ~rex:binding_sep binding in
-        if List.length pieces <> 2 then
-          raise (Malformed_query_binding (binding, query));
-        (match pieces with
-        | [a; b] -> (http_decode a, http_decode b)
-        | _ -> assert false))
+        match Pcre.split ~rex:binding_sep binding with
+        | [""; b] -> (* '=b' *) raise (Malformed_query_part (binding, query))
+        | [a; b]  -> (* 'a=b' *) (http_decode a, http_decode b)
+        | [a]     -> (* 'a=' || 'a' *) (http_decode a, "")
+        | _ -> raise (Malformed_query_part (binding, query)))
       bindings
 
   (** given an input channel and a separator