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
(** 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
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