module Acme.Response where
import Acme.Types (Response(..))
import Data.ByteString (ByteString, concat, append)
import Data.ByteString.Char8 ()
import Prelude hiding (concat)
pong :: (ByteString -> IO ()) -> IO ()
pong send =
do send "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 4\r\n\r\nPONG"
sendResponse :: (ByteString -> IO ()) -> Response -> IO ()
sendResponse send PongResponse = pong send
sendResponse send ByteStringResponse{..} =
send $ concat (statusLine rsCode : (formatHeaders rsHeaders) ++ [rsBody])
where
formatHeaders :: [(ByteString, ByteString)] -> [ByteString]
formatHeaders [] = ["\r\n"]
formatHeaders ((f,v):hs) = [ f, ": " , v , "\r\n"] ++ formatHeaders hs
statusLine :: Int -> ByteString
statusLine 200 = ok_status
ok_status :: ByteString
ok_status = "HTTP/1.1 200 OK\r\n"