123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171(*********************************************************************************)(* OCaml-LDP *)(* *)(* Copyright (C) 2016-2023 Institut National de Recherche en Informatique *)(* et en Automatique. All rights reserved. *)(* *)(* This program is free software; you can redistribute it and/or modify *)(* it under the terms of the GNU Lesser General Public License version *)(* 3 as published by the Free Software Foundation. *)(* *)(* This program is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU General Public License for more details. *)(* *)(* You should have received a copy of the GNU General Public License *)(* along with this program; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA *)(* 02111-1307 USA *)(* *)(* Contact: Maxence.Guesdon@inria.fr *)(* *)(*********************************************************************************)openLwt.InfixmoduleIO=structtype'at='aLwt.tlet(>>=)=Lwt.bindletreturn=Lwt.returntypeic=Lwt_io.input_channeltypeoc=Lwt_io.output_channeltypeconn=ic*ocletread_lineic=Lwt_io.read_line_opticletreadiccount=Lwt_io.read~counticletwriteocbuf=Lwt_io.writeocbufletflushoc=Lwt_io.flushoctypeerror=exnletcatchf=match%lwtf()with|exceptione->return(Result.errore)|v->return(Result.okv)letpp_errorfe=letmsg=Printexc.to_stringeinFormat.pp_print_stringfmsgendmoduleTls_net=structmoduleIO=IOtypectx=Tls.Config.clientletdefault_ctx=letauthenticator?ip~host:__=OkNonein(*let authenticator = X509.Authenticator.chain_of_trust [] in*)Tls.Config.(client~authenticator())letconnect_uri~ctxuri=lethost=matchUri.hosturiwithNone->""|Somes->sinletport=matchUri.porturiwithNone->443|Somen->ninTls_lwt.connect_ext(*~trace:eprint_sexp*)ctx(host,port)>>=fun(ic,oc)->Lwt.return((ic,oc),ic,oc)letclosec=Lwt.catch(fun()->Lwt_io.closec)(fun_->Lwt.return_unit)letclose_inic=Lwt.ignore_result(closeic)letclose_outoc=Lwt.ignore_result(closeoc)letcloseicoc=Lwt.ignore_result(closeic>>=fun()->closeoc)letsexp_of_ctx_=failwith"sexp_of_ctx not implemented"endmoduleClient=Cohttp_lwt.Make_client(IO)(Tls_net)letclient_call=Client.callmoduletypeP=sigvaldbg:string->unitLwt.tvalauthenticator:X509.Authenticator.tvalcertificates:Tls.Config.own_certendmoduleMake(P:P):Ldp.Http.Requests=structletdbg=P.dbgincludeLdp.Cookies.Make()letcall?body?(headers=Cohttp.Header.init())methiri=letheaders=Cohttp.Header.prepend_user_agentheaders!Ldp.Http.user_agentinletheaders=matchcookies_by_iriiriwith|[]->headers|cookies->(*List.iter
(fun (k, v) -> prerr_endline (Printf.sprintf "setting cookie: %s => %s" k v))
cookies;*)let(k,v)=Cohttp.Cookie.Cookie_hdr.serializecookiesinCohttp.Header.addheaderskvinletctx=Tls.Config.client~authenticator:P.authenticator~certificates:P.certificates(*~version:(`TLS_1_2,`TLS_1_3)*)()in(*let%lwt dbgbody = match body with
| None -> Lwt.return "no body"
| Some b -> Cohttp_lwt.Body.to_string b
in
Ldp.Log.debug (fun m -> m "%s %s\n%sbody=%s"
(Cohttp.Code.string_of_method meth)
(Iri.to_string iri)
(match headers with None -> "<no headers>\n"
| Some h -> Cohttp.Header.to_string h)
dbgbody
);*)match%lwtClient.call~ctx?body~headersmeth(Uri.of_string(Iri.to_uriiri))with|exceptione->Ldp.Types.(fail(Request_error(iri,Printexc.to_stringe)))|(resp,body)->let()=letcookies=Cohttp.Cookie.Set_cookie_hdr.extractresp.Cohttp.Response.headersinmatchcookieswith|[]->()|_->remove_expired_cookies();List.iter(add_cookieiri)(List.mapsndcookies);inLwt.return(resp,body)endlet(dummy_authenticator:X509.Authenticator.t)=fun?ip~hostcerts->OkNoneletmake?cache_impl?cache_dir?(authenticator=dummy_authenticator)?cert~dbg()=(*let%lwt authenticator = X509_lwt.authenticator `No_authentication_I'M_STUPID in*)(* let%lwt authenticator = X509_lwt.authenticator (`Ca_dir cert_dir) in*)let%lwtcertificates=matchcertwith|None->Lwt.return`None|Some(cert,priv_key)->X509_lwt.private_of_pems~cert~priv_key>>=func->Lwt.return(`Singlec)inletmoduleP=structletdbg=dbgletauthenticator=authenticatorletcertificates=certificatesendinlet%lwtcache=matchcache_impl,cache_dirwith|None,None->Lwt.return(moduleLdp.Http.No_cache:Ldp.Http.Cache)|Somec,_->Lwt.returnc|_,Somedir->Ldp.Cache.of_dirdirinletmoduleC=(valcache:Ldp.Http.Cache)inletmoduleH=Ldp.Http.Cached_http(C)(Make(P))inLwt.return(moduleH:Ldp.Http.Http)