module Response where
import Control.Proxy (Pipe, ProxyFast, respond)
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Types (Response(..))
responseWriter :: Monad m =>
Response m
-> Pipe ProxyFast ByteString ByteString m ()
responseWriter Response{..} =
do respond $ B.concat [ statusLine rsCode
, renderHeaders rsHeaders
, "\r\n"
]
rsBody
statusLine :: Int -> ByteString
statusLine 200 = ok_status
statusLine 404 = not_found_status
ok_status :: ByteString
ok_status = "HTTP/1.1 200 OK\r\n"
not_found_status :: ByteString
not_found_status = "HTTP/1.1 404 Not Found\r\n"
renderHeaders :: [(ByteString, ByteString)] -> ByteString
renderHeaders = B.concat . map renderHeader
renderHeader :: (ByteString, ByteString) -> ByteString
renderHeader (fieldName, fieldValue) =
B.concat [fieldName, ": ", fieldValue, "\r\n"]