Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to_form and of_form functions to body #723

Merged
merged 2 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions cohttp-async/src/body.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions cohttp-async/src/body_raw.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down
3 changes: 3 additions & 0 deletions cohttp-lwt/src/body.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions cohttp-lwt/src/body.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions cohttp/src/body.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 *)
2 changes: 2 additions & 0 deletions cohttp/src/s.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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