This repository was archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathHttp.hs
More file actions
55 lines (43 loc) · 1.75 KB
/
Http.hs
File metadata and controls
55 lines (43 loc) · 1.75 KB
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
module NStack.Http (
Method,
getMethod,
Path,
httpOK,
http400,
http404,
http500,
httpData,
httpUnauthorized,
httpMethodNotAllowed,
contentPlaintext
) where
import Control.Monad.Except (MonadError, throwError)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as Lazy -- from: bytestring
import qualified Network.Wai as Web -- from: wai
import qualified Network.HTTP.Types as Http -- from: http-types
-- | HTTP Request Method ADT
type Method = Http.StdMethod
-- | HTTP Request Path
type Path = ByteString
getMethod :: MonadError String m => Web.Request -> m Method
getMethod = eitherToExcept . Http.parseMethod . Web.requestMethod
where eitherToExcept = either (const $ throwError "Bad Request: Invalid HTTP Method") return
contentPlaintext :: Http.ResponseHeaders
contentPlaintext = [("Content-Type", "text/plain")]
contentOctetStream :: Http.ResponseHeaders
contentOctetStream = [("Content-Type", "application/octet-stream")]
http400 :: Lazy.ByteString -> Web.Response
http400 = Web.responseLBS Http.status400 contentPlaintext
httpOK :: Web.Response
httpOK = Web.responseLBS Http.status200 contentPlaintext "Message Accepted"
http404 :: Web.Response
http404 = Web.responseLBS Http.status404 contentPlaintext "Not Found"
http500 :: Lazy.ByteString -> Web.Response
http500 = Web.responseLBS Http.status500 contentPlaintext
httpData :: ByteString -> Web.Response
httpData = Web.responseLBS Http.status200 contentOctetStream . Lazy.fromStrict
httpUnauthorized :: Web.Response
httpUnauthorized = Web.responseLBS Http.status401 contentPlaintext "Unauthorized"
httpMethodNotAllowed :: Web.Response
httpMethodNotAllowed = Web.responseLBS Http.status405 contentPlaintext "405 Method Not Allowed"