summaryrefslogtreecommitdiffstats
path: root/src/Main/Util.hs
blob: b02f80c91aa067134f1ba4be08d8265348801669 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
-- |
-- Module:      Main.Util
-- Copyright:   (c) 2014 Tomislav Viljetić
-- License:     BSD3
-- Maintainer:  Tomislav Viljetić <tomislav@viljetic.de>
--
-- Grab bag of utilities used by "Main".  This module is used to keep
-- "Main" as application-focused as possible.
--

{-# LANGUAGE OverloadedStrings #-}


module Main.Util
    (
      -- * Data.List utilities
      splitLast,
      -- * Network.Wai utilities
      pathInfoString,
      handleMethod,
      okJSON,
      noContent,
      badRequest,
      badRequest',
      forbidden,
      notFound,
      notAllowed,
      conflict,
      conflict',
      internalServerError,
      internalServerError',
    ) where

import Data.Aeson (encode, ToJSON)
import Data.Monoid
import qualified Data.ByteString.Char8 as BS8
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Text as T
import qualified Data.Text.Lazy.Encoding as LT
import qualified Data.Text.Lazy as LT
import Network.HTTP.Types
import Network.Wai
import Safe



splitLast :: [a] -> ([a], a)
splitLast xs =
    (init xs, last xs)


-- | Like 'pathInfo', but returns 'String's instead.
pathInfoString :: Request -> [String]
pathInfoString = map T.unpack . pathInfo


-- | Route a request based on it's method.
-- If no application is associated with the request's method,
-- then 'notAllowed' is used.
handleMethod :: [(Method, Application)] -> Application
handleMethod apps req respond =
    app req respond
  where
    app = lookupJustDef (notAllowed allow) (requestMethod req) apps
    allow = map fst apps


okJSON :: ToJSON a => a -> Application
okJSON = respondJSON [] ok200


noContent :: Application
noContent = respondEmpty [] noContent204


badRequest :: Application
badRequest = respondEmpty [] badRequest400


badRequest' :: LT.Text -> Application
badRequest' = respondText [] badRequest400


forbidden :: Application
forbidden = respondEmpty [] forbidden403


notFound :: Application
notFound = respondEmpty [] notFound404


notAllowed :: [Method] -> Application
notAllowed allow =
    respondEmpty [(hAllow, BS8.intercalate ", " allow)] methodNotAllowed405


conflict :: Application
conflict =
    respondEmpty [] conflict409


conflict' :: BS.ByteString -> Application
conflict' msg =
    respondEmpty [] status
  where
    status = mkStatus 409 $ "Conflict (" <> msg <> ")"


internalServerError :: Application
internalServerError =
    respondEmpty [] internalServerError500


internalServerError' :: BS.ByteString -> Application
internalServerError' msg =
    respondEmpty [] status
  where
    status = mkStatus 500 $ "Internal Server Error (" <> msg <> ")"



-- XXX currently it's not always possible to send a truly empty response.
-- because 'Network.Wai.Handler.Warp.Response.hasBody' only discriminates
-- by status code and request method.  This means that empty responses may
-- contain superfluous headers like @Transfer-Encoding@.  This behavior
-- should not cause any problems, though.
respondEmpty :: ResponseHeaders -> Status -> Application
respondEmpty extraHeaders status =
    respondLBS status extraHeaders ""


respondJSON :: ToJSON a => ResponseHeaders -> Status -> a -> Application
respondJSON extraHeaders status =
    respondLBS status headers . encode
  where
    headers = (hContentType, "application/json") : extraHeaders


respondText :: ResponseHeaders -> Status -> LT.Text -> Application
respondText extraHeaders status =
    respondLBS status headers . LT.encodeUtf8
  where
    headers = (hContentType, "text/plain; charset=utf-8") : extraHeaders



respondLBS :: Status -> ResponseHeaders -> LBS.ByteString -> Application
respondLBS status headers bs _req respond =
    respond $ responseLBS status headers bs



-- | HTTP Header names, missing from 'Network.HTTP.Types.Header'.

hAllow :: HeaderName
hAllow = "Allow"