Skip to content

Commit 2175c75

Browse files
committed
Ocsigen_response: Add 'response_string' and 'response_error'
This adds functions from `Cohttp_lwt_unix.Server` that are no longer available with cohttp-eio.
1 parent 9cc1031 commit 2175c75

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/server/ocsigen_cohttp.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,9 @@ let handler ~ssl ~address ~port ~connector (flow, conn) request body =
104104
| `Not_found -> "Not Found"
105105
| _ -> Printexc.to_string exn
106106
in
107-
Cohttp_lwt_unix.Server.respond_error ?headers
107+
Ocsigen_response.respond_error ?headers
108108
~status:(ret_code :> Cohttp.Code.status_code)
109109
~body ()
110-
>>= fun resp -> Lwt.return (Ocsigen_response.of_cohttp resp)
111110
in
112111
(* TODO: equivalent of Ocsigen_range *)
113112
let request =
@@ -137,8 +136,9 @@ let handler ~ssl ~address ~port ~connector (flow, conn) request body =
137136
fun_request request |> Uri.to_string
138137
|> Cohttp.Header.init_with "location"
139138
and status = `Moved_permanently in
140-
Lwt.return (Ocsigen_response.respond ~headers ~status ())
141-
| exn -> handle_error exn)
139+
Lwt.return
140+
(Ocsigen_response.respond_string ~headers ~status ~body:"" ())
141+
| exn -> Lwt.return (handle_error exn))
142142
>>= fun response ->
143143
Lwt.return (Ocsigen_response.to_response_expert response))
144144
(fun () ->

src/server/ocsigen_response.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ let respond ?headers ~status ?(body = Body.empty) () =
3838
let response = Cohttp.Response.make ~status ~encoding ?headers () in
3939
make ~body response
4040

41+
let respond_string ?headers ~status ~body () =
42+
let encoding = Transfer.Fixed (Int64.of_int (String.length body)) in
43+
let response = Response.make ~status ~encoding ?headers () in
44+
let body = Body.of_string body in
45+
make ~body response
46+
47+
let respond_error ?headers ?(status = `Internal_server_error) ~body () =
48+
respond_string ?headers ~status ~body:("Error: " ^ body) ()
49+
4150
let update ?response ?body ?cookies {a_response; a_body; a_cookies} =
4251
let a_response =
4352
match response with Some response -> response | None -> a_response

src/server/ocsigen_response.mli

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ val respond :
3333
(** Like [make] but with an interface similar to
3434
[Cohttp_lwt_unix.Server.respond]. *)
3535

36+
val respond_string :
37+
?headers:Cohttp.Header.t
38+
-> status:Http.Status.t
39+
-> body:string
40+
-> unit
41+
-> t
42+
(** Like [respond] but with a fixed string body. *)
43+
44+
val respond_error :
45+
?headers:Cohttp.Header.t
46+
-> ?status:Http.Status.t
47+
-> body:string
48+
-> unit
49+
-> t
50+
(** Like [respond_string] with ["Error: "] appended to the body. The default
51+
status is [`Internal_server_error].
52+
53+
@deprecated Use [respond_string] with a [~status] argument instead. *)
54+
3655
val update :
3756
?response:Cohttp.Response.t
3857
-> ?body:Body.t

0 commit comments

Comments
 (0)