Skip to content

Commit

Permalink
Merge pull request #82 from actionshrimp/cookie-directives
Browse files Browse the repository at this point in the history
Expose all cookie directives
  • Loading branch information
rgrinberg committed Sep 26, 2018
2 parents 854dea6 + 8a5b265 commit dbc92e8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
23 changes: 10 additions & 13 deletions opium_kernel/cookie.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ module Env = struct
end

module Env_resp = struct
type cookie = (string * string * Co.Cookie.expiration) list
type cookie = Co.Cookie.Set_cookie_hdr.t list
let key : cookie Hmap0.key =
Hmap0.Key.create
("cookie_res",[%sexp_of: (string * string * Co.Cookie.expiration) list])
("cookie_res",[%sexp_of: Co.Cookie.Set_cookie_hdr.t list])
end

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

let set_cookies ?(expiration = `Session) resp cookies =
(* Path defaulted to "/" as otherwise the default is the path of the request's URI *)
let set_cookies ?expiration ?(path = "/") ?domain ?secure ?http_only resp cookies =
let env = Rock.Response.env resp in
let current_cookies = current_cookies_resp (fun r->r.Rock.Response.env) resp in
let cookies' = List.map cookies ~f:(fun (key, data) -> (key, data, expiration)) in
let cookies' = List.map cookies ~f:(fun (key, data) ->
Co.Cookie.Set_cookie_hdr.make ~path ?domain ?expiration ?secure ?http_only (key, encode data)) in
(* WRONG cookies cannot just be concatenated *)
let all_cookies = current_cookies @ cookies' in
{ resp with Rock.Response.env=(Hmap0.add Env_resp.key all_cookies env) }

let set ?expiration resp ~key ~data =
set_cookies ?expiration resp [(key, data)]
let set ?expiration ?path ?domain ?secure ?http_only resp ~key ~data =
set_cookies ?expiration ?path ?domain ?secure ?http_only resp [(key, data)]

let m = (* TODO: "optimize" *)
let m = (* TODO: "optimize" *)
let filter handler req =
handler req >>| fun response ->
let cookie_headers =
let module Cookie = Co.Cookie.Set_cookie_hdr in
let f (k, v, expiration) =
(k, encode v)
|> Cookie.make ~path:"/" ~expiration
|> Cookie.serialize
in
response
|> current_cookies_resp (fun r -> r.Rock.Response.env)
|> List.map ~f
|> List.map ~f:Cookie.serialize
in
let old_headers = Rock.Response.headers response in
{ response with Rock.Response.headers=(
Expand Down
8 changes: 8 additions & 0 deletions opium_kernel/cookie.mli
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ val get : Rock.Request.t -> key:string -> string option
(** Set the value of a cookie with a certain key in a response *)
val set
: ?expiration:Cohttp.Cookie.expiration
-> ?path:string
-> ?domain:string
-> ?secure:bool
-> ?http_only:bool
-> Rock.Response.t
-> key:string
-> data:string
Expand All @@ -17,6 +21,10 @@ val set
(** Like set but will do multiple cookies at once *)
val set_cookies
: ?expiration:Cohttp.Cookie.expiration
-> ?path:string
-> ?domain:string
-> ?secure:bool
-> ?http_only:bool
-> Rock.Response.t
-> (string * string) list
-> Rock.Response.t
Expand Down

0 comments on commit dbc92e8

Please sign in to comment.