aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/net/http/response.lux
blob: eeb67bcde4cb90b517b8ed135576474b8e2ca961 (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
(.using
 [library
  [lux {"-" static}
   [control
    [concurrency
     ["[0]" async]
     ["[0]" frp ("[1]#[0]" monad)]]]
   [data
    ["[0]" text
     ["[0]" encoding]]
    [format
     ["[0]" html]
     ["[0]" css {"+" CSS}]
     ["[0]" context]
     ["[0]" json {"+" JSON} ("[1]#[0]" codec)]]]
   ["[0]" io]
   [world
    ["[0]" binary {"+" Binary}]]]]
 ["[0]" // {"+" Status Body Response Server}
  ["[0]" status]
  ["[0]" mime {"+" MIME}]
  ["[0]" header]
  [// {"+" URL}]])

(def: .public (static response)
  (-> Response Server)
  (function (_ request)
    (async.resolved response)))

(def: .public empty
  (-> Status Response)
  (let [body (frp#in (# encoding.utf8 encoded ""))]
    (function (_ status)
      [status
       [//.#headers (|> context.empty
                        (header.content_length 0)
                        (header.content_type mime.utf_8))
        //.#body body]])))

(def: .public (temporary_redirect to)
  (-> URL Response)
  (let [[status message] (..empty status.temporary_redirect)]
    [status (revised //.#headers (header.location to) message)]))

(def: .public not_found
  Response
  (..empty status.not_found))

(def: .public (content status type data)
  (-> Status MIME Binary Response)
  [status
   [//.#headers (|> context.empty
                    (header.content_length (binary.size data))
                    (header.content_type type))
    //.#body (frp#in data)]])

(def: .public bad_request
  (-> Text Response)
  (|>> (# encoding.utf8 encoded) (content status.bad_request mime.utf_8)))

(def: .public ok
  (-> MIME Binary Response)
  (content status.ok))

(template [<name> <type> <mime> <pre>]
  [(def: .public <name>
     (-> <type> Response)
     (|>> <pre> (# encoding.utf8 encoded) (..ok <mime>)))]

  [text Text          mime.utf_8 (<|)]
  [html html.Document mime.html  html.html]
  [css  CSS           mime.css   css.css]
  [json JSON          mime.json  json#encoded]
  )