aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/net/http/cookie.lux
blob: bb09b61eeffe23929f65bb91471ce225c4ed4340 (plain)
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
(.using
 [library
  [lux (.except)
   [control
    [monad (.only do)]
    ["[0]" try (.only Try)]
    ["p" parser (.open: "[1]#[0]" monad)
     ["l" text (.only Parser)]]]
   [data
    [number
     ["i" int]]
    [text
     ["%" format (.only format)]]
    [format
     ["[0]" context (.only Context)]]
    [collection
     ["[0]" dictionary]]]
   [time
    ["[0]" duration (.only Duration)]]]]
 ["[0]" // (.only Header)
  ["[0]" header]])

(type: .public Directive
  (-> Text Text))

(def: (directive extension)
  (-> Text Directive)
  (function (_ so_far)
    (format so_far "; " extension)))

(def: .public (set name value)
  (-> Text Text Header)
  (header.has "Set-Cookie" (format name "=" value)))

(def: .public (max_age duration)
  (-> Duration Directive)
  (let [seconds (duration.ticks duration.second duration)]
    (..directive (format "Max-Age=" (if (i.< +0 seconds)
                                      (%.int seconds)
                                      (%.nat (.nat seconds)))))))

(template [<name> <prefix>]
  [(def: .public (<name> value)
     (-> Text Directive)
     (..directive (format <prefix> "=" value)))]

  [domain "Domain"]
  [path "Path"]
  )

(template [<name> <tag>]
  [(def: .public <name>
     Directive
     (..directive <tag>))]

  [secure "Secure"]
  [http_only "HttpOnly"]
  )

(type: .public CSRF_Policy
  (Variant
   {#Strict}
   {#Lax}))

(def: .public (same_site policy)
  (-> CSRF_Policy Directive)
  (..directive (format "SameSite=" (case policy
                                     {#Strict} "Strict"
                                     {#Lax} "Lax"))))

(def: (cookie context)
  (-> Context (Parser Context))
  (do p.monad
    [key (l.slice (l.many! (l.none_of! "=")))
     _ (l.this "=")
     value (l.slice (l.many! (l.none_of! ";")))]
    (in (dictionary.has key value context))))

(def: (cookies context)
  (-> Context (Parser Context))
  (all p.either
       (do p.monad
         [context' (..cookie context)
          _ (l.this "; ")]
         (cookies context'))
       (p#in context)))

(def: .public (get header)
  (-> Text (Try Context))
  (l.result header (..cookies context.empty)))