-
Notifications
You must be signed in to change notification settings - Fork 5
/
Request.template.hs
315 lines (290 loc) · 10.1 KB
/
Request.template.hs
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
{-# LANGUAGE OverloadedStrings #-}
module Tie.Template.Request_
( -- * Parameters
Style (..),
pathVariable,
requiredQueryParameter,
requiredQueryParameters,
optionalQueryParameter,
optionalQueryParameters,
requiredHeader,
optionalHeader,
-- * Request body
parseRequestBody,
jsonBodyParser,
formBodyParser,
)
where
import qualified Data.Aeson
import qualified Data.Aeson.Types
import Data.Attoparsec.ByteString (eitherResult, parseWith)
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Builder as Builder
import qualified Data.ByteString.Lazy as LBS
import Data.Coerce (coerce)
import qualified Data.HashMap.Strict as HashMap
import qualified Data.List as List
import qualified Data.List.NonEmpty as NonEmpty
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import qualified Network.HTTP.Media
import Network.HTTP.Types (HeaderName, hContentType)
import qualified Network.Wai as Wai
import System.IO.Unsafe (unsafeInterleaveIO)
import Web.FormUrlEncoded
( FromForm,
parseAll,
urlDecodeAsForm,
urlDecodeForm,
)
import Web.HttpApiData
( FromHttpApiData,
parseHeader,
parseQueryParam,
parseUrlPiece,
parseUrlPieces,
)
pathVariable ::
(FromHttpApiData a) =>
-- | Path variable value
Text ->
(a -> Wai.Application) ->
Wai.Application
pathVariable value withVariable = \request respond ->
case parseUrlPiece value of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Right x ->
withVariable x request respond
{-# INLINEABLE pathVariable #-}
data Style
= FormStyle
| CommaDelimitedStyle
| SpaceDelimitedStyle
| PipeDelimitedStyle
newtype CommaDelimitedValue a = CommaDelimitedValue {unCommaDelimitedValue :: [a]}
instance (FromHttpApiData a) => FromHttpApiData (CommaDelimitedValue a) where
parseUrlPiece input = do
xs <- parseUrlPieces (Text.splitOn "," input)
pure (CommaDelimitedValue xs)
newtype SpaceDelimitedValue a = SpaceDelimitedValue {unSpaceDelimitedValue :: [a]}
instance (FromHttpApiData a) => FromHttpApiData (SpaceDelimitedValue a) where
parseUrlPiece input = do
xs <- parseUrlPieces (Text.splitOn " " input)
pure (SpaceDelimitedValue xs)
newtype PipeDelimitedValue a = PipeDelimitedValue {unPipeDelimitedValue :: [a]}
instance (FromHttpApiData a) => FromHttpApiData (PipeDelimitedValue a) where
parseUrlPiece input = do
xs <- parseUrlPieces (Text.splitOn "|" input)
pure (PipeDelimitedValue xs)
requiredQueryParameters ::
(FromHttpApiData a) =>
Style ->
ByteString ->
(NonEmpty.NonEmpty a -> Wai.Application) ->
Wai.Application
requiredQueryParameters style name withParam =
case style of
FormStyle -> \request respond ->
case urlDecodeForm (LBS.fromStrict (ByteString.drop 1 (Wai.rawQueryString request))) of
Left error ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Right form ->
case parseAll (Text.decodeUtf8 name) form of
Left _ ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Right [] ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Right (x : xs) ->
withParam (x NonEmpty.:| xs) request respond
SpaceDelimitedStyle ->
requiredQueryParameter
name
( \xs -> \request respond ->
case NonEmpty.nonEmpty (unSpaceDelimitedValue xs) of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Just xs ->
withParam xs request respond
)
PipeDelimitedStyle ->
requiredQueryParameter
name
( \xs -> \request respond ->
case NonEmpty.nonEmpty (unPipeDelimitedValue xs) of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Just xs ->
withParam xs request respond
)
CommaDelimitedStyle ->
requiredQueryParameter
name
( \xs -> \request respond ->
case NonEmpty.nonEmpty (unCommaDelimitedValue xs) of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Just xs ->
withParam xs request respond
)
optionalQueryParameters ::
(FromHttpApiData a) =>
Style ->
ByteString ->
(Maybe (NonEmpty.NonEmpty a) -> Wai.Application) ->
Wai.Application
optionalQueryParameters style name withParam =
case style of
FormStyle -> \request respond ->
case urlDecodeForm (LBS.fromStrict (ByteString.drop 1 (Wai.rawQueryString request))) of
Left error ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Right form ->
case parseAll (Text.decodeUtf8 name) form of
Left _ ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Right [] ->
withParam Nothing request respond
Right (x : xs) ->
withParam (Just (x NonEmpty.:| xs)) request respond
SpaceDelimitedStyle ->
optionalQueryParameter
name
False
( \xs ->
withParam (xs >>= NonEmpty.nonEmpty . unSpaceDelimitedValue)
)
PipeDelimitedStyle ->
optionalQueryParameter
name
False
( \xs ->
withParam (xs >>= NonEmpty.nonEmpty . unPipeDelimitedValue)
)
CommaDelimitedStyle ->
optionalQueryParameter
name
False
( \xs ->
withParam (xs >>= NonEmpty.nonEmpty . unCommaDelimitedValue)
)
requiredQueryParameter ::
(FromHttpApiData a) =>
ByteString ->
(a -> Wai.Application) ->
Wai.Application
requiredQueryParameter name withParam = \request respond ->
case List.lookup name (Wai.queryString request) of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just (Just value) ->
case parseQueryParam (Text.decodeUtf8 value) of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> Builder.byteString name))
Right x ->
withParam x request respond
{-# INLINEABLE requiredQueryParameter #-}
optionalQueryParameter ::
(FromHttpApiData a) =>
ByteString ->
-- | Allow empty, e.g. "x="
Bool ->
(Maybe a -> Wai.Application) ->
Wai.Application
optionalQueryParameter name allowEmpty withParam = \request respond ->
case List.lookup name (Wai.queryString request) of
Nothing ->
withParam Nothing request respond
Just Nothing
| allowEmpty ->
withParam Nothing request respond
| otherwise ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just (Just value) ->
case parseQueryParam (Text.decodeUtf8 value) of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> Builder.byteString name))
Right x ->
withParam (Just x) request respond
{-# INLINEABLE optionalQueryParameter #-}
optionalHeader ::
(FromHttpApiData a) =>
HeaderName ->
(Maybe a -> Wai.Application) ->
Wai.Application
optionalHeader name withHeader = \request respond ->
case List.lookup name (Wai.requestHeaders request) of
Nothing ->
withHeader Nothing request respond
Just value ->
case parseHeader value of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Right x ->
withHeader (Just x) request respond
{-# INLINEABLE optionalHeader #-}
requiredHeader ::
(FromHttpApiData a) =>
HeaderName ->
(a -> Wai.Application) ->
Wai.Application
requiredHeader name withHeader = \request respond ->
case List.lookup name (Wai.requestHeaders request) of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Just value ->
case parseHeader value of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Right x ->
withHeader x request respond
{-# INLINEABLE requiredHeader #-}
data BodyParser a
= BodyParser
Network.HTTP.Media.MediaType
((a -> Wai.Application) -> Wai.Application)
jsonBodyParser :: (Data.Aeson.FromJSON a) => BodyParser a
jsonBodyParser = BodyParser "application/json" parseRequestBodyJSON
{-# INLINE jsonBodyParser #-}
formBodyParser :: (FromForm a) => BodyParser a
formBodyParser = BodyParser "application/xxx-form-urlencoded" parseRequestBodyForm
{-# INLINE formBodyParser #-}
parseRequestBody :: [BodyParser a] -> (a -> Wai.Application) -> Wai.Application
parseRequestBody parsers withBody = \request respond -> do
let contentType =
fromMaybe
"application/octet-stream"
(List.lookup hContentType (Wai.requestHeaders request))
bodyParser =
Network.HTTP.Media.mapAccept
[(mediaType, parser) | BodyParser mediaType parser <- parsers]
contentType
case bodyParser of
Just parseBody ->
parseBody withBody request respond
Nothing ->
respond (Wai.responseBuilder (toEnum 415) [] mempty)
{-# INLINE parseRequestBody #-}
parseRequestBodyJSON :: (Data.Aeson.FromJSON a) => (a -> Wai.Application) -> Wai.Application
parseRequestBodyJSON withBody = \request respond -> do
body <- Wai.lazyRequestBody request
case Data.Aeson.decode' body of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Just body ->
withBody body request respond
{-# INLINEABLE parseRequestBodyJSON #-}
parseRequestBodyForm :: (FromForm a) => (a -> Wai.Application) -> Wai.Application
parseRequestBodyForm withBody = \request respond -> do
body <- Wai.lazyRequestBody request
case urlDecodeAsForm body of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Right form ->
withBody form request respond
{-# INLINEABLE parseRequestBodyForm #-}