Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose all cookie directives #82

Merged
merged 2 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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