-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcookie.ml
29 lines (25 loc) · 1 KB
/
cookie.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
type key = string
type value = string
let get_cookie () =
let reg1 = Regexp.regexp "; " in
let list = Regexp.split reg1 (Js.to_string Dom_html.document##cookie) in
let reg2 = Regexp.regexp "=" in
List.map (fun s ->
match Regexp.split reg2 s with
x :: y -> (x, String.concat "=" y)
| [] -> ("", "")
) list
let initial_cookies = get_cookie ()
let set_cookie key value =
let today = jsnew Js.date_now () in
let expire_date = jsnew Js.date_ms
(today##getFullYear () + 1, today##getMonth (), today##getDay (),
today##getHours (), today##getMinutes (), today##getSeconds (),
today##getMilliseconds ()) in
let expire_time = Js.to_string expire_date##toUTCString () in
Dom_html.document##cookie <-
Js.string (Printf.sprintf "%s=%s;expires=%s" key value expire_time)
let set_cookie_with_timeout key value date =
let expire_time = Js.to_string date##toUTCString () in
Dom_html.document##cookie <-
Js.string (Printf.sprintf "%s=%s;expires=%s" key value expire_time)