From b73ff734037c4438874f5a8d8f144178279b7aa1 Mon Sep 17 00:00:00 2001 From: Stefano Zacchiroli Date: Sun, 5 Jan 2003 14:15:13 +0000 Subject: [PATCH] - better test for content-length header existence (now case insensitive) - parse POST query parameters only if content-type is application/x-www-form-urlencoded, otherwise set only request's body POST received data --- helm/DEVEL/ocaml-http/http_request.ml | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/helm/DEVEL/ocaml-http/http_request.ml b/helm/DEVEL/ocaml-http/http_request.ml index e3bc95bc1..cf8601850 100644 --- a/helm/DEVEL/ocaml-http/http_request.ml +++ b/helm/DEVEL/ocaml-http/http_request.ml @@ -38,7 +38,12 @@ class request ic = let uri_str = Neturl.string_of_url uri in let path = Http_parser.parse_path uri in let query_get_params = Http_parser.parse_query_get_params uri in - let headers = Http_parser.parse_headers ic in (* trailing \r\n consumed! *) + let headers = + List.map (* lowercase header names to ease lookups before having a request + object *) + (fun (h,v) -> (String.lowercase h, v)) + (Http_parser.parse_headers ic) (* trailing \r\n consumed! *) + in let body = (* TODO fallback on Transfer-Encoding if Content-Length isn't defined *) if meth = `POST then @@ -46,26 +51,26 @@ class request ic = (try (* read only Content-Length bytes *) let limit_raw = (try - (snd (List.find - (fun (h,v) -> String.lowercase h = "content-length") headers)) + List.assoc "content-length" headers with Not_found -> raise Fallback) in let limit = (try (* TODO supports only a maximum content-length of 1Gb *) int_of_string limit_raw with Failure "int_of_string" -> - raise (Invalid_header ("Content-Length: " ^ limit_raw))) + raise (Invalid_header ("content-length: " ^ limit_raw))) in Http_misc.buf_of_inchan ~limit ic with Fallback -> Http_misc.buf_of_inchan ic) (* read until EOF *) else "" (* TODO empty body for methods other than POST, is what we want? *) in - (* TODO brave assumption: when meth = `POST, Content-Type is - application/x-www-form-urlencoded and is therefore one-liner parsed as a GET - query *) let query_post_params = match meth with - | `POST -> Http_parser.split_query_params body + | `POST -> + let ct = try List.assoc "content-type" headers with Not_found -> "" in + if ct = "application/x-www-form-urlencoded" then + Http_parser.split_query_params body + else [] | _ -> [] in let params = query_post_params @ query_get_params in (* prefers POST params *) -- 2.39.2