packages feed

hack2-handler-mongrel2-http-2011.6.22: src/Hack2/Handler/Mongrel2/Response.hs

{-# LANGUAGE OverloadedStrings #-}

module Hack2.Handler.Mongrel2.Response
       ( Response
       , mkResponse
       ) where

import Blaze.ByteString.Builder (Builder, toByteString, fromByteString)
import Blaze.Text.Int (integral)
import Data.ByteString (ByteString)
import Data.ByteString.Char8 ()
import Data.List (intersperse)
import Data.Monoid
import Hack2.Handler.Mongrel2.Types
import qualified Data.ByteString as S


-- | Build a single space.
buildSpace :: Builder
buildSpace = fromByteString " "

-- | Build a UUID.
buildUUID :: UUID -> Builder
buildUUID = fromByteString


  -- def send(self, uuid, conn_id, msg):
  --     """
  --     Raw send to the given connection ID at the given uuid, mostly used 
  --     internally.
  --     """
  --     header = "%s %d:%s," % (uuid, len(str(conn_id)), str(conn_id))
  --     self.resp.send(header + ' ' + msg)
  
  -- bcc1453e-9cc0-11e0-af11-6cf049b16ec3 1:0, HTTP/1.1 200 OK
      
-- | Build a response from a server UUID, a list of
-- client IDs and a response body builder.
mkResponse :: UUID -> ClientID -> ByteString -> ByteString
mkResponse uuid clientID body =
  toByteString $ mconcat [ buildUUID uuid
                         , buildSpace
                         , integral $ S.length $ toByteString $ integral clientID
                         , fromByteString ":"
                         , integral clientID
                         , fromByteString ","
                         , buildSpace
                         , fromByteString body
                         ]