1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162# 1 "src/session/ezCookieServer.cohttp.ml"(**************************************************************************)(* *)(* Copyright 2018-2023 OCamlPro *)(* *)(* All rights reserved. This file is distributed under the terms of the *)(* GNU Lesser General Public License version 2.1, with the special *)(* exception on linking described in the file LICENSE. *)(* *)(**************************************************************************)openEzAPIServerUtils(* RFC 2965 has
cookie = "Cookie:" cookie-version 1*((";" | ",") cookie-value)
cookie-value = NAME "=" VALUE [";" path] [";" domain] [";" port]
cookie-version = "$Version" "=" value
NAME = attr
VALUE = value
path = "$Path" "=" value
domain = "$Domain" "=" value
port = "$Port" [ "=" <"> value <"> ]
*)letcookie_re=Re.Str.regexp"[;,][ \t]*"letequals_re=Re.Str.regexp_string"="letday_in_seconds=86400Lletget(req:Req.t)=List.fold_left(funaccheader->letcomps=Re.Str.split_delimcookie_reheaderin(* We don't handle $Path, $Domain, $Port, $Version (or $anything
$else) *)letcookies=List.filter(funs->s.[0]!='$')compsinletsplit_pairaccnvp=matchRe.Str.bounded_splitequals_renvp2with|[]->StringMap.add""""acc|n::[]->StringMap.addn""acc|n::v::_->StringMap.addnvaccinList.fold_leftsplit_pairacccookies)StringMap.empty(StringMap.find"cookie"req.Req.req_headers)(* TODO: Find a proper way to do that, Cohttp lib doesn't provide valid header when trying to clear header *)letset?secure?http_only~expiration~name~value()=ignoresecure;ignorehttp_only;"Set-Cookie",Printf.sprintf"%s=%s; Max-Age=%s"namevalue(Int64.to_stringexpiration)(* Cohttp.Cookie.Set_cookie_hdr.serialize ~version:`HTTP_1_0 @@
Cohttp.Cookie.Set_cookie_hdr.make ~expiration ?secure ?http_only (name, value) *)letclear~name()=set~name~value:""~expiration:0L()letset?secure?http_only?expiration~name~value=letexpiration=matchexpirationwith|Someexp->exp|None->day_in_secondsinset?secure?http_only~name~value~expiration