summaryrefslogtreecommitdiff
path: root/app/Web/Response.hs
blob: 11ab5f287806fb9bc3d7365f2a04287dfe6dba7c (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
module Web.Response where

import qualified Data.Text as T
import Data.Text (Text)

import Web.Utils

data Response
  = HtmlResponse Int Text -- Код возврата, содержимое HTML
  | TextResponse Int Text Text -- Код возврата, Content-Type, содержимое HTML
  deriving Show

notFoundResponse :: Response
notFoundResponse = HtmlResponse 404 (T.pack "<strong>404 Not Found</strong>")

getStatusCode :: Response -> Int
getStatusCode (HtmlResponse code _) = code
getStatusCode (TextResponse code _ _) = code

getContentType :: Response -> Text
getContentType (HtmlResponse _ _) = (T.pack "text/html")
getContentType (TextResponse _ contentType _) = contentType

getContent :: Response -> Text
getContent (HtmlResponse _ content) = content
getContent (TextResponse _ _ content) = content

formResponse :: Response -> Text
formResponse (HtmlResponse code html) =
  T.strip $ T.unlines
  $ map T.pack [ "HTTP/1.1 " ++ getStatus code
               , "Content-Type: text/html; charset=utf-8"
               , "Connection: close"
               , "" ] ++ [html]
-- TODO: Добавить обработчик
formResponse _ = undefined