breaking: more getter/setters for request/response, change signature

some setters now take request last, to favor composition with |>
This commit is contained in:
Simon Cruanes 2021-12-11 10:57:57 -05:00
parent ba31534551
commit 9e26576740
No known key found for this signature in database
GPG key ID: 4AC01D0849AA62B6
2 changed files with 40 additions and 3 deletions

View file

@ -382,7 +382,9 @@ module Request = struct
let get_header_int self h = match get_header self h with let get_header_int self h = match get_header self h with
| Some x -> (try Some (int_of_string x) with _ -> None) | Some x -> (try Some (int_of_string x) with _ -> None)
| None -> None | None -> None
let set_header self k v = {self with headers=Headers.set k v self.headers} let set_header k v self = {self with headers=Headers.set k v self.headers}
let update_headers f self = {self with headers=f self.headers}
let set_body b self = {self with body=b}
let pp_comp_ out comp = let pp_comp_ out comp =
Format.fprintf out "[%s]" Format.fprintf out "[%s]"
@ -584,6 +586,12 @@ module Response = struct
body: body; body: body;
} }
let set_body body self = {self with body}
let set_headers headers self = {self with headers}
let update_headers f self = {self with headers=f self.headers}
let set_header k v self = {self with headers = Headers.set k v self.headers}
let set_code code self = {self with code}
let make_raw ?(headers=[]) ~code body : t = let make_raw ?(headers=[]) ~code body : t =
(* add content length to response *) (* add content length to response *)
let headers = let headers =

View file

@ -253,7 +253,16 @@ module Request : sig
val get_header_int : _ t -> string -> int option val get_header_int : _ t -> string -> int option
val set_header : 'a t -> string -> string -> 'a t val set_header : string -> string -> 'a t -> 'a t
(** [set_header k v req] sets [k: v] in the request [req]'s headers. *)
val update_headers : (Headers.t -> Headers.t) -> 'a t -> 'a t
(** Modify headers
@since 0.11 *)
val set_body : 'a -> _ t -> 'a t
(** [set_body b req] returns a new query whose body is [b].
@since 0.11 *)
val host : _ t -> string val host : _ t -> string
(** Host field of the request. It also appears in the headers. *) (** Host field of the request. It also appears in the headers. *)
@ -320,13 +329,33 @@ module Response : sig
(** Body of a response, either as a simple string, (** Body of a response, either as a simple string,
or a stream of bytes, or nothing (for server-sent events). *) or a stream of bytes, or nothing (for server-sent events). *)
type t = { type t = private {
code: Response_code.t; (** HTTP response code. See {!Response_code}. *) code: Response_code.t; (** HTTP response code. See {!Response_code}. *)
headers: Headers.t; (** Headers of the reply. Some will be set by [Tiny_httpd] automatically. *) headers: Headers.t; (** Headers of the reply. Some will be set by [Tiny_httpd] automatically. *)
body: body; (** Body of the response. Can be empty. *) body: body; (** Body of the response. Can be empty. *)
} }
(** A response to send back to a client. *) (** A response to send back to a client. *)
val set_body : body -> t -> t
(** Set the body of the response.
@since 0.11 *)
val set_header : string -> string -> t -> t
(** Set a header.
@since 0.11 *)
val update_headers : (Headers.t -> Headers.t) -> t -> t
(** Modify headers
@since 0.11 *)
val set_headers : Headers.t -> t -> t
(** Set all headers.
@since 0.11 *)
val set_code : Response_code.t -> t -> t
(** Set the response code.
@since 0.11 *)
val make_raw : val make_raw :
?headers:Headers.t -> ?headers:Headers.t ->
code:Response_code.t -> code:Response_code.t ->