@@ -15,10 +15,10 @@ module Env = struct
15
15
end
16
16
17
17
module Env_resp = struct
18
- type cookie = ( string * string * Co.Cookie .expiration ) list
18
+ type cookie = Co.Cookie.Set_cookie_hdr .t list
19
19
let key : cookie Hmap0.key =
20
20
Hmap0.Key. create
21
- (" cookie_res" ,[% sexp_of: ( string * string * Co.Cookie. expiration) list ])
21
+ (" cookie_res" ,[% sexp_of: Co.Cookie.Set_cookie_hdr. t list ])
22
22
end
23
23
24
24
let current_cookies env record =
@@ -53,30 +53,27 @@ let get req ~key =
53
53
|> List. find_map ~f: (fun (k ,v ) ->
54
54
if k = key then Some (decode v) else None )
55
55
56
- let set_cookies ?(expiration = `Session ) resp cookies =
56
+ (* Path defaulted to "/" as otherwise the default is the path of the request's URI *)
57
+ let set_cookies ?expiration ?(path = " /" ) ?domain ?secure ?http_only resp cookies =
57
58
let env = Rock.Response. env resp in
58
59
let current_cookies = current_cookies_resp (fun r ->r.Rock.Response. env) resp in
59
- let cookies' = List. map cookies ~f: (fun (key , data ) -> (key, data, expiration)) in
60
+ let cookies' = List. map cookies ~f: (fun (key , data ) ->
61
+ Co.Cookie.Set_cookie_hdr. make ~path ?domain ?expiration ?secure ?http_only (key, encode data)) in
60
62
(* WRONG cookies cannot just be concatenated *)
61
63
let all_cookies = current_cookies @ cookies' in
62
64
{ resp with Rock.Response. env= (Hmap0. add Env_resp. key all_cookies env) }
63
65
64
- let set ?expiration resp ~key ~data =
65
- set_cookies ?expiration resp [(key, data)]
66
+ let set ?expiration ? path ? domain ? secure ? http_only resp ~key ~data =
67
+ set_cookies ?expiration ?path ?domain ?secure ?http_only resp [(key, data)]
66
68
67
- let m = (* TODO: "optimize" *)
69
+ let m = (* TODO: "optimize" *)
68
70
let filter handler req =
69
71
handler req >> | fun response ->
70
72
let cookie_headers =
71
73
let module Cookie = Co.Cookie. Set_cookie_hdr in
72
- let f (k , v , expiration ) =
73
- (k, encode v)
74
- |> Cookie. make ~path: " /" ~expiration
75
- |> Cookie. serialize
76
- in
77
74
response
78
75
|> current_cookies_resp (fun r -> r.Rock.Response. env)
79
- |> List. map ~f
76
+ |> List. map ~f: Cookie. serialize
80
77
in
81
78
let old_headers = Rock.Response. headers response in
82
79
{ response with Rock.Response. headers= (
0 commit comments