aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/net/http/response.lux
blob: b49715c4d708bd57a474e8b5cd85208ae04d3a53 (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
(.module:
  [library
   [lux (#- static)
    [control
     [concurrency
      ["." async]
      ["." 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: .public (static response)
  (-> Response Server)
  (function (_ request)
    (async.resolved response)))

(def: .public empty
  (-> Status Response)
  (let [body (frp\in (\ encoding.utf8 encode ""))]
    (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 encode) (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 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]
  )