Skip to content

Commit dbc92e8

Browse files
authored
Merge pull request #82 from actionshrimp/cookie-directives
Expose all cookie directives
2 parents 854dea6 + 8a5b265 commit dbc92e8

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

Diff for: opium_kernel/cookie.ml

+10-13
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ module Env = struct
1515
end
1616

1717
module Env_resp = struct
18-
type cookie = (string * string * Co.Cookie.expiration) list
18+
type cookie = Co.Cookie.Set_cookie_hdr.t list
1919
let key : cookie Hmap0.key =
2020
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])
2222
end
2323

2424
let current_cookies env record =
@@ -53,30 +53,27 @@ let get req ~key =
5353
|> List.find_map ~f:(fun (k,v) ->
5454
if k = key then Some (decode v) else None)
5555

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 =
5758
let env = Rock.Response.env resp in
5859
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
6062
(* WRONG cookies cannot just be concatenated *)
6163
let all_cookies = current_cookies @ cookies' in
6264
{ resp with Rock.Response.env=(Hmap0.add Env_resp.key all_cookies env) }
6365

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)]
6668

67-
let m = (* TODO: "optimize" *)
69+
let m = (* TODO: "optimize" *)
6870
let filter handler req =
6971
handler req >>| fun response ->
7072
let cookie_headers =
7173
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
7774
response
7875
|> current_cookies_resp (fun r -> r.Rock.Response.env)
79-
|> List.map ~f
76+
|> List.map ~f:Cookie.serialize
8077
in
8178
let old_headers = Rock.Response.headers response in
8279
{ response with Rock.Response.headers=(

Diff for: opium_kernel/cookie.mli

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ val get : Rock.Request.t -> key:string -> string option
99
(** Set the value of a cookie with a certain key in a response *)
1010
val set
1111
: ?expiration:Cohttp.Cookie.expiration
12+
-> ?path:string
13+
-> ?domain:string
14+
-> ?secure:bool
15+
-> ?http_only:bool
1216
-> Rock.Response.t
1317
-> key:string
1418
-> data:string
@@ -17,6 +21,10 @@ val set
1721
(** Like set but will do multiple cookies at once *)
1822
val set_cookies
1923
: ?expiration:Cohttp.Cookie.expiration
24+
-> ?path:string
25+
-> ?domain:string
26+
-> ?secure:bool
27+
-> ?http_only:bool
2028
-> Rock.Response.t
2129
-> (string * string) list
2230
-> Rock.Response.t

0 commit comments

Comments
 (0)