This commit is contained in:
swrup 2025-11-02 15:42:43 +01:00
parent f899f5a3a8
commit 5656305924

View file

@ -105,21 +105,24 @@ struct
end end
let decode_uri ~ctx uri = let decode_uri ~ctx uri =
let ( >>= ) = Result.bind in let ( let* ) = Result.bind in
match String.split_on_char '/' uri with match String.split_on_char '/' uri with
| proto :: "" :: user_pass_host_port :: _path -> | proto :: "" :: user_pass_host_port :: _path ->
(if String.equal proto "http:" then let* _scheme, ctx =
Ok ("http", Mimic.add connect_scheme "http" ctx) if String.equal proto "http:" then
else if String.equal proto "https:" then Ok ("http", Mimic.add connect_scheme "http" ctx)
Ok ("https", Mimic.add connect_scheme "https" ctx) else if String.equal proto "https:" then
else Error (`Msg "Couldn't decode user and password")) Ok ("https", Mimic.add connect_scheme "https" ctx)
>>= fun (_scheme, ctx) -> else Error (`Msg "Couldn't decode user and password")
(match String.split_on_char '@' user_pass_host_port with in
let* _user_pass, host_port =
match String.split_on_char '@' user_pass_host_port with
| [ host_port ] -> Ok (None, host_port) | [ host_port ] -> Ok (None, host_port)
| [ _user_pass; host_port ] -> Ok (None, host_port) | [ _user_pass; host_port ] -> Ok (None, host_port)
| _ -> Error (`Msg "Couldn't decode URI")) | _ -> Error (`Msg "Couldn't decode URI")
>>= fun (_user_pass, host_port) -> in
(match String.split_on_char ':' host_port with let* hostname, ctx =
match String.split_on_char ':' host_port with
| [] -> Error (`Msg "Empty host & port") | [] -> Error (`Msg "Empty host & port")
| [ hostname ] -> Ok (hostname, Mimic.add connect_hostname hostname ctx) | [ hostname ] -> Ok (hostname, Mimic.add connect_hostname hostname ctx)
| hd :: tl -> ( | hd :: tl -> (
@ -133,17 +136,18 @@ let decode_uri ~ctx uri =
( hostname, ( hostname,
Mimic.add connect_hostname hostname Mimic.add connect_hostname hostname
(Mimic.add connect_port (int_of_string port) ctx) ) (Mimic.add connect_port (int_of_string port) ctx) )
with Failure _ -> Error (`Msg "Couldn't decode port"))) with Failure _ -> Error (`Msg "Couldn't decode port"))
>>= fun (hostname, ctx) -> Ok (ctx, hostname) in
Ok (ctx, hostname)
| _ -> Error (`Msg "Couldn't decode URI on top") | _ -> Error (`Msg "Couldn't decode URI on top")
let tls_config ?tls_config authenticator = let tls_config ?tls_config authenticator =
let ( let* ) = Result.bind in
lazy lazy
(match tls_config with (match tls_config with
| Some cfg -> Ok (`Custom cfg) | Some cfg -> Ok (`Custom cfg)
| None -> | None ->
let alpn_protocols = [ "h2"; "http/1.1" ] in let alpn_protocols = [ "h2"; "http/1.1" ] in
let ( let* ) = Result.bind in
let* authenticator = authenticator in let* authenticator = authenticator in
let* cfg = Tls.Config.client ~alpn_protocols ~authenticator () in let* cfg = Tls.Config.client ~alpn_protocols ~authenticator () in
Ok (`Default cfg)) Ok (`Default cfg))