123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107(*********************************************************************************)(* 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 *)(* *)(*********************************************************************************)typecookie_domain=string*bool(* host * with-subdomains *)typecookie_path=stringlistmoduleMake()=structletis_suffix~s~suf=letlen=String.lengthsinletlen_suf=String.lengthsufinlen>=len_suf&&String.subs(len-len_suf)len_suf=sufletis_prefix~s~pref=letlen_s=String.lengthsinletlen_pref=String.lengthprefin(len_pref<=len_s)&&(String.subs0len_pref)=prefletprint_cookiesdbg(str,cookie)=let(a,b)=Cohttp.Cookie.Set_cookie_hdr.serializecookieinletstr=Printf.sprintf"cookie: %s => (%s, %s)"strabindbgstrletcookies=ref[]letclear()=cookies:=[]letremove_expired_cookies()=cookies:=List.filter(fun(_,_,exp,_)->matchexpwithNone->true|Somet->t>=Unix.time())!cookiesletadd_cookieiric=letopenCohttp.Cookie.Set_cookie_hdrin(*let (k, v) = c.cookie in
prerr_endline (Printf.sprintf "Set-cookie: %s -> %s" k v);*)letdomain=matchc.domainwith|None->((matchIri.hostiriwithNone->""|Somes->s),false)|Somes->(s,true)inletpath=matchc.pathwith|None->Iri.path_stringiri|Somep->pinletexp=matchc.expirationwith`Session->None|`Max_agen->Some(Unix.time()+.(Int64.to_floatn))inletc=c.cookieincookies:=(domain,path,exp,c)::!cookiesletuse_cookiehostpath(c_domain,sub)c_pathexp=letb=(matchexpwithNone->true|Somet->t>=Unix.time())&&(sub&&is_suffix~s:host~suf:c_domain)||c_domain=host&&is_prefix~s:path~pref:c_pathin(*prerr_endline (Printf.sprintf
"use_cookie %s %s (%s, %b) %s _ = %b" host path c_domain sub c_path b
*)bletcookies_by_iriiri=matchIri.hostiriwithNone->[]|Somehost->letpath=Iri.path_stringiriinList.fold_left(funacc(dom,p,exp,c)->ifuse_cookiehostpathdompexpthenc::accelseacc)[]!cookiesend