diff --git a/src/Tiny_httpd.ml b/src/Tiny_httpd.ml index f56a396e..bd1a9828 100644 --- a/src/Tiny_httpd.ml +++ b/src/Tiny_httpd.ml @@ -382,7 +382,9 @@ module Request = struct let get_header_int self h = match get_header self h with | Some x -> (try Some (int_of_string x) with _ -> 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 = Format.fprintf out "[%s]" @@ -584,6 +586,12 @@ module Response = struct 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 = (* add content length to response *) let headers = diff --git a/src/Tiny_httpd.mli b/src/Tiny_httpd.mli index 633f8ea1..18cf6ddd 100644 --- a/src/Tiny_httpd.mli +++ b/src/Tiny_httpd.mli @@ -253,7 +253,16 @@ module Request : sig 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 (** 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, 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}. *) headers: Headers.t; (** Headers of the reply. Some will be set by [Tiny_httpd] automatically. *) body: body; (** Body of the response. Can be empty. *) } (** 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 : ?headers:Headers.t -> code:Response_code.t ->