-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_cookie.ml
103 lines (90 loc) · 3.79 KB
/
http_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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
(*
OCaml HTTP - do it yourself (fully OCaml) HTTP daemon
Copyright (C) <2009> David Sheets <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as
published by the Free Software Foundation, version 2.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*)
type time = [ `Day of int | `Hour of int | `Minute of int | `Second of int ] list
type expiration = [ `Discard | `Session | `Age of time | `Until of float ]
type cookie = { value : string;
expiration : expiration;
domain : string option;
path : string option;
secure : bool }
(* Does not check the contents of name or value for ';', ',', '\s', or name[0]='$' *)
let make ?(expiry=`Session) ?path ?domain ?(secure=false) n v =
(n, { value = v;
expiration = expiry; domain = domain;
path = path; secure = secure })
let duration tml =
let tval = function
| `Day d -> 86400*d
| `Hour h -> 3600*h
| `Minute m -> 60*m
| `Second s -> s
in List.fold_left (fun a t -> a + (tval t)) 0 tml
let serialize_1_1 (n, c) =
let attrs = ["Version=1"] in
let attrs = if c.secure then ("Secure" :: attrs) else attrs in
let attrs = match c.path with None -> attrs
| Some p -> ("Path=" ^ p) :: attrs in
let attrs = match c.expiration with
| `Discard -> "Max-Age=0" :: attrs
| `Session -> "Discard" :: attrs
| `Until stamp ->
let offset = int_of_float (stamp -. (Unix.time ())) in
("Max-Age=" ^ (string_of_int (min 0 offset))) :: attrs
| `Age tml -> ("Max-Age=" ^ (string_of_int (duration tml))) :: attrs in
let attrs = match c.domain with None -> attrs
| Some d -> ("Domain=" ^ d) :: attrs in
("Set-Cookie2", String.concat "; " attrs)
let serialize_1_0 (n, c) =
let fmt_time a =
let days = [| "Sun"; "Mon"; "Tue"; "Wed"; "Thu"; "Fri"; "Sat" |] in
let months = [| "Jan"; "Feb"; "Mar"; "Apr"; "May"; "Jun";
"Jul"; "Aug"; "Sep"; "Oct"; "Nov"; "Dec" |] in
let tm = Unix.gmtime a in
Printf.sprintf "%s, %02d-%s-%d %02d:%02d:%02d GMT"
days.(tm.Unix.tm_wday) tm.Unix.tm_mday months.(tm.Unix.tm_mon)
(1900 + tm.Unix.tm_year) tm.Unix.tm_hour tm.Unix.tm_min tm.Unix.tm_sec
in
let attrs = if c.secure then ["secure"] else [] in
let attrs = match c.path with None -> attrs
| Some p -> ("path=" ^ p) :: attrs in
let attrs = match c.domain with None -> attrs
| Some d -> ("domain=" ^ d) :: attrs in
let attrs = match c.expiration with
| `Discard -> ("expires=" ^ (fmt_time 0.)) :: attrs
| `Session -> attrs
| `Until stamp -> ("expires=" ^ (fmt_time stamp)) :: attrs
| `Age tml ->
let age = float (duration tml) in
("expires=" ^ (fmt_time ((Unix.time ()) +. age))) :: attrs in
let attrs = (n ^ (match c.value with "" -> ""
| v -> "=" ^ v)) :: attrs in
("Set-Cookie", String.concat "; " attrs)
let serialize ?(version=`HTTP_1_0) cp =
match version with
| `HTTP_1_0 -> serialize_1_0 cp
| `HTTP_1_1 -> serialize_1_1 cp
let extract req =
List.fold_left
(fun acc header ->
let comps = Pcre.split ~rex:(Pcre.regexp "(?:;|,)\\s") header in
let cookies = List.filter (fun s -> s.[0] != '$') comps in
let split_pair nvp =
match Pcre.split ~rex:(Pcre.regexp "=") nvp with
| [] -> ("","")
| n :: [] -> (n, "")
| n :: v :: _ -> (n, v)
in (List.map split_pair cookies) @ acc
) [] (Http_request.header req "Cookie")