From 4b07e51c703a4e3b5b2cc613b196b82e7437470a Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Fri, 23 Oct 2020 20:57:47 +0200 Subject: [PATCH 1/2] Add to_form and of_form functions to body Signed-off-by: Marcello Seri --- cohttp-async/src/body.mli | 1 + cohttp-async/src/body_raw.ml | 3 +++ cohttp-lwt/src/body.ml | 3 +++ cohttp-lwt/src/body.mli | 2 ++ cohttp/src/body.ml | 2 ++ cohttp/src/s.ml | 2 ++ 6 files changed, 13 insertions(+) diff --git a/cohttp-async/src/body.mli b/cohttp-async/src/body.mli index a88e53c456..ff6f3d1c6c 100644 --- a/cohttp-async/src/body.mli +++ b/cohttp-async/src/body.mli @@ -16,3 +16,4 @@ val to_pipe : t -> string Pipe.Reader.t val of_pipe : string Pipe.Reader.t -> t val map : t -> f:(string -> string) -> t val as_pipe : t -> f:(string Pipe.Reader.t -> string Pipe.Reader.t) -> t +val to_form : t -> (string * string list) list Deferred.t \ No newline at end of file diff --git a/cohttp-async/src/body_raw.ml b/cohttp-async/src/body_raw.ml index 61337d0fc0..638cd7b625 100644 --- a/cohttp-async/src/body_raw.ml +++ b/cohttp-async/src/body_raw.ml @@ -71,6 +71,9 @@ let map t ~f = let as_pipe t ~f = `Pipe (t |> to_pipe |> f) +let to_form t = to_string t >>| Uri.query_of_encoded +let of_form ?scheme f = Uri.encoded_of_query ?scheme f |> of_string + let write_body write_body (body : t) writer = match body with | `Empty -> return () diff --git a/cohttp-lwt/src/body.ml b/cohttp-lwt/src/body.ml index cfc8d28f60..0c901b8d35 100644 --- a/cohttp-lwt/src/body.ml +++ b/cohttp-lwt/src/body.ml @@ -104,3 +104,6 @@ let map f t = match t with | #Body.t as t -> (Body.map f t :> t) | `Stream s -> `Stream (Lwt_stream.map f s) + + let to_form (body:t) = to_string body >|= Uri.query_of_encoded + let of_form ?scheme f = Uri.encoded_of_query ?scheme f |> of_string diff --git a/cohttp-lwt/src/body.mli b/cohttp-lwt/src/body.mli index f68cd34411..ebf5db27c4 100644 --- a/cohttp-lwt/src/body.mli +++ b/cohttp-lwt/src/body.mli @@ -29,6 +29,8 @@ val to_string_list : t -> string list Lwt.t val to_stream : t -> string Lwt_stream.t val of_stream : string Lwt_stream.t -> t +val to_form : t -> (string * string list) list Lwt.t + val create_stream : ('a -> Cohttp.Transfer.chunk Lwt.t) -> 'a -> string Lwt_stream.t val length : t -> (int64 * t) Lwt.t diff --git a/cohttp/src/body.ml b/cohttp/src/body.ml index 948f600ee1..62bf4550db 100644 --- a/cohttp/src/body.ml +++ b/cohttp/src/body.ml @@ -64,4 +64,6 @@ let map f = function | `String s -> `String (f s) | `Strings sl -> `Strings (List.map f sl) +let to_form t = Uri.query_of_encoded (to_string t) +let of_form ?scheme f = Uri.encoded_of_query ?scheme f |> of_string (* TODO: maybe add a functor here that uses IO.S *) diff --git a/cohttp/src/s.ml b/cohttp/src/s.ml index ed6f93d714..885cefea05 100644 --- a/cohttp/src/s.ml +++ b/cohttp/src/s.ml @@ -133,10 +133,12 @@ module type Body = sig type t val to_string : t -> string val to_string_list : t -> string list + val to_form : t -> (string * string list) list val empty : t val is_empty : t -> bool val of_string : string -> t val of_string_list : string list -> t + val of_form : ?scheme:string -> (string * string list) list -> t val map : (string -> string) -> t -> t val transfer_encoding : t -> Transfer.encoding end From 714fd1f63c5875e031de8b4e1fdb1ad82bfaae0e Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Fri, 23 Oct 2020 21:06:34 +0200 Subject: [PATCH 2/2] Update changes Signed-off-by: Marcello Seri --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index f46fc06e41..6b8f608ebb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,7 @@ - cohttp: fix transfer-encoding ordering in headers (@mseri #721) - Lower-level support for long-running cohttp-async connections (@brendanlong #704) - fix deadlock in logging (@dinosaure #722) +- add of_form and to_form functions to body (@seliopou #440, @mseri #723) ## v2.5.4 (2020-07-21)