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

(def: #export (static response)
  (-> Response Server)
  (function (_ request)
    (promise.resolved response)))

(def: #export empty
  (-> Status Response)
  (let [body (frp\wrap (\ encoding.utf8 encode ""))]
    (function (_ status)
      [status
       {#//.headers (|> context.empty
                        (header.content-length 0)
                        (header.content-type mime.utf-8))
        #//.body body}])))

(def: #export (temporary-redirect to)
  (-> URL Response)
  (let [[status message] (..empty status.temporary-redirect)]
    [status (update@ #//.headers (header.location to) message)]))

(def: #export not-found
  Response
  (..empty status.not-found))

(def: #export (content status type data)
  (-> Status MIME Binary Response)
  [status
   {#//.headers (|> context.empty
                    (header.content-length (binary.size data))
                    (header.content-type type))
    #//.body (frp\wrap data)}])

(def: #export bad-request
  (-> Text Response)
  (|>> (\ encoding.utf8 encode) (content status.bad-request mime.utf-8)))

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

(template [<name> <type> <mime> <pre>]
  [(def: #export <name>
     (-> <type> Response)
     (|>> <pre> (\ encoding.utf8 encode) (..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\encode]
  )