~~
This commit is contained in:
parent
ece827c7ee
commit
84afc29675
4 changed files with 90 additions and 69 deletions
|
|
@ -1,10 +1,10 @@
|
||||||
|
open Syntax
|
||||||
|
|
||||||
module type S = sig
|
module type S = sig
|
||||||
val connect : Mimic.ctx -> Mimic.ctx Lwt.t
|
val connect : Mimic.ctx -> Mimic.ctx Lwt.t
|
||||||
val authenticator : (X509.Authenticator.t, [> `Msg of string ]) result
|
val authenticator : (X509.Authenticator.t, [> `Msg of string ]) result
|
||||||
end
|
end
|
||||||
|
|
||||||
open Lwt.Infix
|
|
||||||
|
|
||||||
let connect_scheme = Mimic.make ~name:"connect-scheme"
|
let connect_scheme = Mimic.make ~name:"connect-scheme"
|
||||||
let connect_port = Mimic.make ~name:"connect-port"
|
let connect_port = Mimic.make ~name:"connect-port"
|
||||||
let connect_hostname = Mimic.make ~name:"connect-hostname"
|
let connect_hostname = Mimic.make ~name:"connect-hostname"
|
||||||
|
|
@ -31,20 +31,16 @@ struct
|
||||||
| `Closed as err -> pp_write_error ppf err
|
| `Closed as err -> pp_write_error ppf err
|
||||||
|
|
||||||
let write flow cs =
|
let write flow cs =
|
||||||
let open Lwt.Infix in
|
write flow cs |> Lwt_result.map_error (fun err -> `Write err)
|
||||||
write flow cs >>= function
|
|
||||||
| Ok _ as v -> Lwt.return v
|
|
||||||
| Error err -> Lwt.return_error (`Write err)
|
|
||||||
|
|
||||||
let writev flow css =
|
let writev flow css =
|
||||||
writev flow css >>= function
|
writev flow css |> Lwt_result.map_error (fun err -> `Write err)
|
||||||
| Ok _ as v -> Lwt.return v
|
|
||||||
| Error err -> Lwt.return_error (`Write err)
|
|
||||||
|
|
||||||
let connect (happy_eyeballs, hostname, port) =
|
let connect (happy_eyeballs, hostname, port) =
|
||||||
Happy_eyeballs.resolve happy_eyeballs hostname [ port ] >>= function
|
let+ res = Happy_eyeballs.resolve happy_eyeballs hostname [ port ] in
|
||||||
| Error (`Msg err) -> Lwt.return_error (`Connect err)
|
match res with
|
||||||
| Ok ((_ipaddr, _port), flow) -> Lwt.return_ok flow
|
| Error (`Msg err) -> Error (`Connect err)
|
||||||
|
| Ok ((_ipaddr, _port), flow) -> Ok flow
|
||||||
end
|
end
|
||||||
|
|
||||||
let tcp_edn, _tcp_protocol = Mimic.register ~name:"tcp" (module TCP)
|
let tcp_edn, _tcp_protocol = Mimic.register ~name:"tcp" (module TCP)
|
||||||
|
|
@ -59,9 +55,10 @@ struct
|
||||||
Result.(
|
Result.(
|
||||||
to_option (bind (Domain_name.of_string hostname) Domain_name.host))
|
to_option (bind (Domain_name.of_string hostname) Domain_name.host))
|
||||||
in
|
in
|
||||||
Happy_eyeballs.resolve happy_eyeballs hostname [ port ] >>= function
|
let* res = Happy_eyeballs.resolve happy_eyeballs hostname [ port ] in
|
||||||
| Ok ((_ipaddr, _port), flow) -> client_of_flow cfg ?host:peer_name flow
|
match res with
|
||||||
| Error (`Msg err) -> Lwt.return_error (`Write (`Connect err))
|
| Error (`Msg err) -> Lwt.return_error (`Write (`Connect err))
|
||||||
|
| Ok ((_ipaddr, _port), flow) -> client_of_flow cfg ?host:peer_name flow
|
||||||
end
|
end
|
||||||
|
|
||||||
let tls_edn, _tls_protocol = Mimic.register ~name:"tls" (module TLS)
|
let tls_edn, _tls_protocol = Mimic.register ~name:"tls" (module TLS)
|
||||||
|
|
@ -154,8 +151,7 @@ let tls_config ?tls_config authenticator =
|
||||||
|
|
||||||
let create_connection ?tls_config:cfg ~ctx ~authenticator uri =
|
let create_connection ?tls_config:cfg ~ctx ~authenticator uri =
|
||||||
let tls_config = tls_config ?tls_config:cfg authenticator in
|
let tls_config = tls_config ?tls_config:cfg authenticator in
|
||||||
let open Lwt_result.Infix in
|
let*? ctx, host = Lwt.return (decode_uri ~ctx uri) in
|
||||||
Lwt.return (decode_uri ~ctx uri) >>= fun (ctx, host) ->
|
|
||||||
let ctx =
|
let ctx =
|
||||||
match Lazy.force tls_config with
|
match Lazy.force tls_config with
|
||||||
| Ok (`Custom cfg) -> Mimic.add connect_tls_config cfg ctx
|
| Ok (`Custom cfg) -> Mimic.add connect_tls_config cfg ctx
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
open Syntax
|
||||||
|
|
||||||
let src = Logs.Src.create "server"
|
let src = Logs.Src.create "server"
|
||||||
|
|
||||||
module Log = (val Logs.src_log src : Logs.LOG)
|
module Log = (val Logs.src_log src : Logs.LOG)
|
||||||
|
|
@ -200,16 +202,18 @@ let hash_of_seed : type response request.
|
||||||
|
|
||||||
let transmit_over_http : to_close:_ -> Mimic.flow -> Mimic.flow -> unit Lwt.t =
|
let transmit_over_http : to_close:_ -> Mimic.flow -> Mimic.flow -> unit Lwt.t =
|
||||||
fun ~to_close src dst ->
|
fun ~to_close src dst ->
|
||||||
let open Lwt.Infix in
|
|
||||||
let closed = Lwt_mvar.create_empty () in
|
let closed = Lwt_mvar.create_empty () in
|
||||||
let rec loop ~src ~dst () =
|
let rec loop ~src ~dst () =
|
||||||
Lwt.pick
|
let* res =
|
||||||
[
|
Lwt.pick
|
||||||
Lwt_result.Infix.(
|
[
|
||||||
Mimic.read src >|= fun v -> (v :> [ `Closed | _ Mirage_flow.or_eof ]));
|
(let+? v = Mimic.read src in
|
||||||
(Lwt_mvar.take closed >|= fun v -> Ok v);
|
(v :> [ `Closed | _ Mirage_flow.or_eof ]));
|
||||||
]
|
(let+ v = Lwt_mvar.take closed in
|
||||||
>>= function
|
Ok v);
|
||||||
|
]
|
||||||
|
in
|
||||||
|
match res with
|
||||||
| Error err ->
|
| Error err ->
|
||||||
Log.err (fun m ->
|
Log.err (fun m ->
|
||||||
m "Got an error while we reading the source (CONNECT): %a."
|
m "Got an error while we reading the source (CONNECT): %a."
|
||||||
|
|
@ -224,8 +228,11 @@ let transmit_over_http : to_close:_ -> Mimic.flow -> Mimic.flow -> unit Lwt.t =
|
||||||
Log.debug (fun m -> m "Transfer over HTTP:");
|
Log.debug (fun m -> m "Transfer over HTTP:");
|
||||||
Log.debug (fun m ->
|
Log.debug (fun m ->
|
||||||
m "@[<hov>%a@]." (Hxd_string.pp Hxd.default) (Cstruct.to_string cs));
|
m "@[<hov>%a@]." (Hxd_string.pp Hxd.default) (Cstruct.to_string cs));
|
||||||
Mimic.write dst cs >>= function
|
let* res = Mimic.write dst cs in
|
||||||
| Ok () -> Lwt.pause () >>= loop ~src ~dst
|
match res with
|
||||||
|
| Ok () ->
|
||||||
|
let* () = Lwt.pause () in
|
||||||
|
loop ~src ~dst ()
|
||||||
| Error err ->
|
| Error err ->
|
||||||
Log.err (fun m ->
|
Log.err (fun m ->
|
||||||
m
|
m
|
||||||
|
|
@ -235,9 +242,9 @@ let transmit_over_http : to_close:_ -> Mimic.flow -> Mimic.flow -> unit Lwt.t =
|
||||||
if Lwt_mvar.is_empty closed then Lwt_mvar.put closed `Closed
|
if Lwt_mvar.is_empty closed then Lwt_mvar.put closed `Closed
|
||||||
else Lwt.return_unit)
|
else Lwt.return_unit)
|
||||||
in
|
in
|
||||||
Lwt.join [ loop ~src ~dst (); loop ~src:dst ~dst:src () ] >>= fun () ->
|
let* () = Lwt.join [ loop ~src ~dst (); loop ~src:dst ~dst:src () ] in
|
||||||
to_close src;
|
to_close src;
|
||||||
Lwt.join [ Mimic.close src; Mimic.close dst ] >>= fun () ->
|
let* () = Lwt.join [ Mimic.close src; Mimic.close dst ] in
|
||||||
Log.debug (fun m -> m "Connection closed properly on both side.");
|
Log.debug (fun m -> m "Connection closed properly on both side.");
|
||||||
Lwt.return_unit
|
Lwt.return_unit
|
||||||
|
|
||||||
|
|
@ -278,9 +285,9 @@ let connect_http_1_1 ~ctx ~authenticator ~to_close flow reqd =
|
||||||
match H1.Headers.get request.H1.Request.headers "host" with
|
match H1.Headers.get request.H1.Request.headers "host" with
|
||||||
| Some uri ->
|
| Some uri ->
|
||||||
let uri = "http://" ^ uri in
|
let uri = "http://" ^ uri in
|
||||||
let open Lwt.Infix in
|
|
||||||
Lwt.async (fun () ->
|
Lwt.async (fun () ->
|
||||||
Connect.create_connection ~ctx ~authenticator uri >>= function
|
let* res = Connect.create_connection ~ctx ~authenticator uri in
|
||||||
|
match res with
|
||||||
| Ok dst ->
|
| Ok dst ->
|
||||||
let headers = H1.Headers.of_list [ ("connection", "close") ] in
|
let headers = H1.Headers.of_list [ ("connection", "close") ] in
|
||||||
let response =
|
let response =
|
||||||
|
|
@ -449,9 +456,9 @@ let connect_http_2_0 ~ctx ~authenticator ~to_close flow reqd =
|
||||||
match H2.Headers.get request.H2.Request.headers "host" with
|
match H2.Headers.get request.H2.Request.headers "host" with
|
||||||
| Some uri ->
|
| Some uri ->
|
||||||
let uri = "http://" ^ uri in
|
let uri = "http://" ^ uri in
|
||||||
let open Lwt.Infix in
|
|
||||||
Lwt.async (fun () ->
|
Lwt.async (fun () ->
|
||||||
Connect.create_connection ~ctx ~authenticator uri >>= function
|
let* res = Connect.create_connection ~ctx ~authenticator uri in
|
||||||
|
match res with
|
||||||
| Ok dst ->
|
| Ok dst ->
|
||||||
let response = H2.Response.create `OK in
|
let response = H2.Response.create `OK in
|
||||||
H2.Reqd.respond_with_string reqd response "";
|
H2.Reqd.respond_with_string reqd response "";
|
||||||
|
|
|
||||||
22
unikernel/syntax.ml
Normal file
22
unikernel/syntax.ml
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
let ( let* ) = Lwt.bind
|
||||||
|
let ( let+ ) x f = Lwt.map f x
|
||||||
|
let ( let*? ) = Lwt_result.bind
|
||||||
|
let ( let+? ) x f = Lwt_result.map f x
|
||||||
|
|
||||||
|
let list_get_ok l =
|
||||||
|
let err = ref None in
|
||||||
|
try
|
||||||
|
Ok
|
||||||
|
(List.map
|
||||||
|
(function
|
||||||
|
| Error _e as e ->
|
||||||
|
err := Some e;
|
||||||
|
raise Exit
|
||||||
|
| Ok v -> v)
|
||||||
|
l)
|
||||||
|
with Exit -> ( match !err with None -> assert false | Some v -> v)
|
||||||
|
|
||||||
|
let lwt_list_get_ok l =
|
||||||
|
let open Lwt.Syntax in
|
||||||
|
let+ l = l in
|
||||||
|
list_get_ok l
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
open Rresult
|
open Rresult
|
||||||
open Cmdliner
|
open Cmdliner
|
||||||
|
open Syntax
|
||||||
|
|
||||||
let port =
|
let port =
|
||||||
let doc = Arg.info ~doc:"Port of HTTP service." [ "p"; "port" ] in
|
let doc = Arg.info ~doc:"Port of HTTP service." [ "p"; "port" ] in
|
||||||
|
|
@ -28,24 +29,6 @@ let alpn =
|
||||||
let map_err_to_string pp_err res =
|
let map_err_to_string pp_err res =
|
||||||
Lwt.map (R.reword_error (R.msgf "%a" pp_err)) res
|
Lwt.map (R.reword_error (R.msgf "%a" pp_err)) res
|
||||||
|
|
||||||
let list_get_ok l =
|
|
||||||
let err = ref None in
|
|
||||||
try
|
|
||||||
Ok
|
|
||||||
(List.map
|
|
||||||
(function
|
|
||||||
| Error _e as e ->
|
|
||||||
err := Some e;
|
|
||||||
raise Exit
|
|
||||||
| Ok v -> v)
|
|
||||||
l)
|
|
||||||
with Exit -> ( match !err with None -> assert false | Some v -> v)
|
|
||||||
|
|
||||||
let lwt_list_get_ok l =
|
|
||||||
let open Lwt.Syntax in
|
|
||||||
let+ l = l in
|
|
||||||
list_get_ok l
|
|
||||||
|
|
||||||
module Make
|
module Make
|
||||||
(Assets_ro : Mirage_kv.RO)
|
(Assets_ro : Mirage_kv.RO)
|
||||||
(Certificates_ro : Mirage_kv.RO)
|
(Certificates_ro : Mirage_kv.RO)
|
||||||
|
|
@ -54,9 +37,6 @@ module Make
|
||||||
(Connect : Connect.S)
|
(Connect : Connect.S)
|
||||||
(HTTP_server : Paf_mirage.S) =
|
(HTTP_server : Paf_mirage.S) =
|
||||||
struct
|
struct
|
||||||
let ( let*? ) = Lwt_result.bind
|
|
||||||
let ( let+? ) x f = Lwt_result.map f x
|
|
||||||
|
|
||||||
module Assets = struct
|
module Assets = struct
|
||||||
let map_err_to_string = map_err_to_string Assets_ro.pp_error
|
let map_err_to_string = map_err_to_string Assets_ro.pp_error
|
||||||
|
|
||||||
|
|
@ -123,26 +103,49 @@ struct
|
||||||
(Fmt.list Fmt.string) l
|
(Fmt.list Fmt.string) l
|
||||||
in
|
in
|
||||||
let* ext_l =
|
let* ext_l =
|
||||||
let ll =
|
let* hd, tl =
|
||||||
l
|
l
|
||||||
|> List.map (fun (_, _, v) -> v)
|
|> List.map (fun (_, _, v) -> v)
|
||||||
|> List.map (List.sort String.compare)
|
|> List.map (List.sort String.compare)
|
||||||
|
|> function
|
||||||
|
| [] ->
|
||||||
|
Fmt.error_msg "directory `%s` is empty"
|
||||||
|
(Mirage_kv.Key.to_string dir)
|
||||||
|
| hd :: tl -> Ok (hd, tl)
|
||||||
in
|
in
|
||||||
match List.sort_uniq Stdlib.compare ll with
|
match List.for_all (( = ) hd) tl with
|
||||||
| [ l ] -> Ok l
|
| false ->
|
||||||
| [] -> assert false
|
|
||||||
| _ll ->
|
|
||||||
Fmt.error_msg
|
Fmt.error_msg
|
||||||
"directory `%s` does not has the same set of file extensions for \
|
"directory `%s` does not has the same set of file extensions for \
|
||||||
each language"
|
each language"
|
||||||
(Mirage_kv.Key.to_string dir)
|
(Mirage_kv.Key.to_string dir)
|
||||||
|
| true -> Ok hd
|
||||||
in
|
in
|
||||||
Ok (etag, lang_l, ext_l)
|
Ok (etag, lang_l, ext_l)
|
||||||
|
|
||||||
let assets ro =
|
let assets ro =
|
||||||
let*? terms_assoc = get ro "terms" in
|
let*? terms_etag, terms_lang_l, terms_ext_l = get ro "terms" in
|
||||||
let*? privacy_assoc = get ro "privacy" in
|
let*? privacy_etag, privacy_lang_l, privacy_ext_l = get ro "privacy" in
|
||||||
Lwt_result.return (terms_assoc, privacy_assoc)
|
Lwt.return
|
||||||
|
@@
|
||||||
|
let open Result.Syntax in
|
||||||
|
let* lang_l =
|
||||||
|
match terms_lang_l = privacy_lang_l with
|
||||||
|
| false ->
|
||||||
|
Fmt.error_msg
|
||||||
|
"terms and privacy directories does not support the same set of \
|
||||||
|
languages"
|
||||||
|
| true -> Ok terms_lang_l
|
||||||
|
in
|
||||||
|
let ext_l =
|
||||||
|
match terms_ext_l = privacy_ext_l with
|
||||||
|
| false ->
|
||||||
|
Fmt.error_msg
|
||||||
|
"terms and privacy directories does not support the same set of \
|
||||||
|
mimetype"
|
||||||
|
| true -> Ok terms_ext_l
|
||||||
|
in
|
||||||
|
Ok (terms_etag, privacy_etag, lang_l, ext_l)
|
||||||
end
|
end
|
||||||
|
|
||||||
let tls certificate_ro key_ro =
|
let tls certificate_ro key_ro =
|
||||||
|
|
@ -255,14 +258,7 @@ struct
|
||||||
let* assets_res = Assets.assets assets_ro in
|
let* assets_res = Assets.assets assets_ro in
|
||||||
match assets_res with
|
match assets_res with
|
||||||
| Error (`Msg m) -> Fmt.failwith "Assets configuration error: %s." m
|
| Error (`Msg m) -> Fmt.failwith "Assets configuration error: %s." m
|
||||||
| Ok (_terms, _privacy) -> (
|
| Ok (_terms_etag, _privacy_etag, _lang_l, _ext_l) -> (
|
||||||
(*
|
|
||||||
let etag, lang_l, ext_l = terms in
|
|
||||||
Fmt.pr "ETAG: %s@\nlanguages: %a@\nextensions: %a@." etag
|
|
||||||
(Fmt.list ~sep:(Fmt.any ", ") Fmt.string)
|
|
||||||
lang_l
|
|
||||||
(Fmt.list ~sep:(Fmt.any ", ") Fmt.string)
|
|
||||||
ext_l; *)
|
|
||||||
let* tls_res = tls certificate_ro key_ro in
|
let* tls_res = tls certificate_ro key_ro in
|
||||||
match use_tls () with
|
match use_tls () with
|
||||||
| false -> run ~ctx ~authenticator http_server
|
| false -> run ~ctx ~authenticator http_server
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue