Skip to content

Commit 70e3d38

Browse files
committed
Manual changes
Cohttp_eio substitutes Cohttp_lwt_unix. Uncaught exceptions are now logged with `Cohttp_eio`'s `~on_error` callback. The active switch and the environment are passed using Fiber variables. Ocsigen_stream: Read loop adapted to Eio Ocsigen_response: Adapted to Eio.File and use unbuffered Eio.Flow read Ocsigen_response: Cohttp_eio doesn't expose the internal Response module. Ocsigen_server: Server no longer listen on IPv6 when started with `` ~ports:[ `All ] ``. Revproxy: Implement `get_inet_addr` with Eio Revproxy: HTTPS is not supported out of the box in Cohttp_eio Async exceptions are now handled with a result type: `Fiber.fork_promise` returns a `('a, exn) result Promise.t` and no longer use a callback for exceptions. Add dependency on magic-mime. It was a transitive dependency through cohttp-lwt-unix. Command pipe: Fork daemon Use Eio.Exn.pp instead of Printexc.to_string in log messages. Eio exceptions are printed in a better way with regard to the margin.
1 parent d1b0c8f commit 70e3d38

22 files changed

+177
-189
lines changed

dune-project

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
(depends
1919
(ocaml (>= 4.08.1))
2020
(camlzip (>= 1.04))
21-
(cohttp-lwt-unix (>= 6.0))
22-
conduit-lwt-unix
21+
cohttp-eio
2322
http
2423
cryptokit
2524
(ipaddr (>= 2.1))
26-
(lwt (>= 3.0))
25+
eio
26+
eio_main
2727
lwt_react
2828
lwt_ssl
2929
ocamlfind

ocsigenserver.opam

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ depends: [
1313
"dune" {>= "3.19"}
1414
"ocaml" {>= "4.08.1"}
1515
"camlzip" {>= "1.04"}
16-
"cohttp-lwt-unix" {>= "6.0"}
17-
"conduit-lwt-unix"
16+
"cohttp-eio"
1817
"http"
1918
"cryptokit"
2019
"ipaddr" {>= "2.1"}
21-
"lwt" {>= "3.0"}
20+
"eio"
21+
"eio_main"
2222
"lwt_react"
2323
"lwt_ssl"
2424
"ocamlfind"

src/baselib/dune

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
(flags
55
(:standard -no-keep-locs))
66
(modules ocsigen_lib_base)
7-
(libraries lwt))
7+
(libraries eio eio.unix))
88

99
(library
1010
(name baselib)
@@ -22,11 +22,12 @@
2222
(libraries
2323
str
2424
findlib
25-
lwt.unix
25+
eio
2626
cryptokit
2727
re
2828
ocsigen_lib_base
29-
cohttp-lwt
29+
cohttp
30+
cohttp-eio
3031
logs
3132
(select
3233
dynlink_wrapper.ml

src/baselib/ocsigen_cache.ml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ end = struct
120120
; mutable finaliser_after : 'a node -> unit
121121
; time_bound : time_bound option }
122122

123-
and time_bound = {timer : float; mutable collector : unit Promise.t option}
123+
and time_bound =
124+
{timer : float; mutable collector : (unit, exn) result Promise.t option}
124125

125126
(* Checks (by BY):
126127
@@ -227,10 +228,13 @@ end = struct
227228
| Some n ->
228229
t.collector <-
229230
Some
230-
(sleep_until n.collection;
231-
collect r n;
232-
t.collector <- None;
233-
update_collector r))
231+
(Fiber.fork_promise
232+
~sw:(Option.get (Fiber.get Ocsigen_lib.current_switch))
233+
(fun () ->
234+
sleep_until n.collection;
235+
collect r n;
236+
t.collector <- None;
237+
update_collector r)))
234238

235239
(* Add a node that do not belong to any list to a list.
236240
The fields [succ] and [prev] are overridden.

src/baselib/ocsigen_lib.ml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717
*)
1818

19+
open Eio.Std
1920
include Ocsigen_lib_base
2021
module String = String_base
2122

23+
let current_switch = Eio.Fiber.create_key ()
24+
let env = Eio.Fiber.create_key ()
25+
2226
(*****************************************************************************)
2327

2428
module Ip_address = struct
@@ -27,17 +31,15 @@ module Ip_address = struct
2731
let get_inet_addr ?(v6 = false) host =
2832
let rec aux = function
2933
| [] -> raise No_such_host
30-
| {Unix.ai_addr = Unix.ADDR_INET (inet_addr, _); _} :: _ -> inet_addr
34+
| `Tcp (ipv4v6, _port) :: tl ->
35+
Eio.Net.Ipaddr.fold
36+
~v4:(fun ip -> if v6 then aux tl else ip)
37+
~v6:(fun ip -> if v6 then ip else aux tl)
38+
ipv4v6
3139
| _ :: l -> aux l
3240
in
33-
let options =
34-
[ (if v6 then Unix.AI_FAMILY Unix.PF_INET6 else Unix.AI_FAMILY Unix.PF_INET)
35-
]
36-
in
37-
aux
38-
(Unix.getaddrinfo
39-
(* TODO: lwt-to-direct-style: This call to [Unix.getaddrinfo] was [Lwt_unix.getaddrinfo] before the rewrite. *)
40-
host "" options)
41+
let env = Stdlib.Option.get (Fiber.get env) in
42+
(aux (Eio.Net.getaddrinfo env#net host) : _ Eio.Net.Ipaddr.t :> string)
4143
end
4244

4345
(*****************************************************************************)

src/baselib/ocsigen_lib.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ include
2929
and type 'a Clist.t = 'a Ocsigen_lib_base.Clist.t
3030
and type 'a Clist.node = 'a Ocsigen_lib_base.Clist.node
3131

32+
val current_switch : Eio.Switch.t Eio.Fiber.key
33+
val env : Eio_unix.Stdenv.base Eio.Fiber.key
34+
3235
val make_cryptographic_safe_string : unit -> string
3336
(** Generate an unique and cryptographically safe random string.
3437
It is impossible to guess for other people and
@@ -39,7 +42,7 @@ module String : module type of String_base
3942
module Ip_address : sig
4043
exception No_such_host
4144

42-
val get_inet_addr : ?v6:bool -> string -> Unix.inet_addr
45+
val get_inet_addr : ?v6:bool -> string -> string
4346
end
4447

4548
module Filename : sig

src/baselib/ocsigen_stream.ml

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ exception Cancelled
2525
exception Already_read
2626
exception Finalized
2727

28-
type 'a stream = 'a step Promise.t Lazy.t
28+
type 'a stream = 'a step Lazy.t
29+
(** Forcing a [stream] performs effects. *)
2930

3031
and 'a step =
3132
| Finished of 'a stream option
@@ -215,43 +216,35 @@ let substream delim s =
215216
let of_file filename =
216217
let fd = Unix.openfile filename [Unix.O_RDONLY; Unix.O_NONBLOCK] 0o666 in
217218
let ch =
218-
(fun x1 ->
219-
Eio.Buf_read.of_flow ~max_size:1_000_000
220-
(Eio_unix.Net.import_socket_stream
221-
~sw:(Stdlib.Option.get (Fiber.get Ocsigen_lib.current_switch))
222-
~close_unix:true x1
223-
: [`R | `Flow | `Close] r))
224-
fd
219+
(Eio_unix.Net.import_socket_stream
220+
~sw:(Stdlib.Option.get (Fiber.get Ocsigen_lib.current_switch))
221+
~close_unix:true fd
222+
: [`R | `Flow | `Close] r)
225223
in
226-
let buf = Bytes.create 1024 in
224+
let buf = Cstruct.create 1024 in
227225
let rec aux () =
228-
let n =
229-
Eio.Flow.single_read
230-
(* TODO: lwt-to-direct-style: [buf] should be a [Cstruct.t]. *)
231-
(* TODO: lwt-to-direct-style: [Eio.Flow.single_read] operates on a [Flow.source] but [ch] is likely of type [Eio.Buf_read.t]. Rewrite this code to use [Buf_read] (which contains an internal buffer) or change the call to [Eio.Buf_read.of_flow] used to create the buffer. *)
232-
(* TODO: lwt-to-direct-style: Dropped expression (buffer offset): [0]. *)
233-
(* TODO: lwt-to-direct-style: Dropped expression (buffer length): [1024]. *)
234-
ch buf
235-
in
226+
let n = Eio.Flow.single_read ch buf in
236227
if n = 0
237228
then empty None
238229
else
239230
(* Streams should be immutable, thus we always make a copy
240231
of the buffer *)
241-
cont (Bytes.sub_string buf 0 n) aux
232+
cont (Cstruct.to_string ~len:n buf) aux
242233
in
243234
make ~finalize:(fun _ -> Unix.close fd) aux
244235

245236
let of_string s = make (fun () -> cont s (fun () -> empty None))
246237

247-
(** Convert a {!Lwt_stream.t} to an {!Ocsigen_stream.t}. *)
248-
let of_lwt_stream stream =
238+
let of_eio_flow body =
239+
let buf = Cstruct.create !net_buffer_size in
249240
let rec aux () =
250-
match Lwt_stream.get stream with Some e -> cont e aux | None -> empty None
241+
match Eio.Flow.single_read body buf with
242+
| exception End_of_file -> empty None
243+
| len -> cont (Cstruct.to_string ~len buf) aux
251244
in
252245
make aux
253246

254-
let of_cohttp_body body = Cohttp_lwt.Body.to_stream body |> of_lwt_stream
247+
let of_cohttp_body = of_eio_flow
255248

256249
module StringStream = struct
257250
type out = string t

src/baselib/ocsigen_stream.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ val of_file : string -> string t
106106
val of_string : string -> string t
107107
(** returns a stream containing a string. *)
108108

109-
val of_cohttp_body : Cohttp_lwt.Body.t -> string t
110-
(** Convert a {!Lwt_stream.t} to an {!Ocsigen_stream.t}. *)
109+
val of_cohttp_body : Cohttp_eio.Body.t -> string t
110+
(** Convert the body of a request to a {!Ocsigen_stream}. *)
111111

112112
module StringStream : sig
113113
type out = string t

src/extensions/revproxy.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
2323
The reverse proxy is still experimental. *)
2424

25+
open Eio.Std
2526
module Pcre = Re.Pcre
2627

2728
let section = Logs.Src.create "ocsigen:ext:revproxy"
@@ -128,7 +129,13 @@ let gen dir = function
128129
Uri.make ~scheme ~host ~port ~path ()
129130
and body = Ocsigen_request.body request_info
130131
and meth = Ocsigen_request.meth request_info in
131-
Cohttp_lwt_unix.Client.call ~headers ~body meth uri
132+
let sw = Stdlib.Option.get (Fiber.get Ocsigen_lib.current_switch) in
133+
let env = Stdlib.Option.get (Fiber.get Ocsigen_lib.env) in
134+
let client =
135+
(* TODO: Https not supported out of the box in [Cohttp_eio]. *)
136+
Cohttp_eio.Client.make ~https:None env#net
137+
in
138+
Cohttp_eio.Client.call client ~sw ~headers ~body meth uri
132139
in
133140
Ocsigen_extensions.Ext_found
134141
(fun () -> Ocsigen_response.of_cohttp (do_request ()))

src/files/ocsigenserver.conf/gen.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ let deps () =
8484
; "ocsigenserver" ]
8585
in
8686
let packages =
87-
"lwt_ssl,bytes,lwt.unix,logs,logs-syslog.unix,syslog-message,ipaddr,findlib,cryptokit,re,str,xml-light,dynlink,cohttp-lwt-unix,http"
87+
"lwt_ssl,bytes,lwt.unix,logs,logs-syslog.unix,syslog-message,ipaddr,findlib,cryptokit,re,str,xml-light,dynlink,cohttp-eio,http"
8888
in
8989
let deps = ref [] in
9090
let cmd = "ocamlfind query -p-format -recursive " ^ packages in

0 commit comments

Comments
 (0)